sqlite3backup.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. unit sqlite3backup;
  2. { SQLite3 backup class.
  3. Copyright (C) 2012 Ludo Brands
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Library General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at your
  7. option) any later version with the following modification:
  8. As a special exception, the copyright holders of this library give you
  9. permission to link this library with independent modules to produce an
  10. executable, regardless of the license terms of these independent modules,and
  11. to copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the terms
  13. and conditions of the license of that module. An independent module is a
  14. module which is not derived from or based on this library. If you modify
  15. this library, you may extend this exception to your version of the library,
  16. but you are not obligated to do so. If you do not wish to do so, delete this
  17. exception statement from your version.
  18. This program is distributed in the hope that it will be useful, but WITHOUT
  19. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  21. for more details.
  22. You should have received a copy of the GNU Library General Public License
  23. along with this library; if not, write to the Free Software Foundation,
  24. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. }
  26. {$mode objfpc}{$H+}
  27. interface
  28. uses
  29. Classes, SysUtils,sqlite3conn,sqlite3dyn;
  30. type
  31. TOnBackupProgress = procedure (Sender: TObject; Remaining, PageCount: integer) of object;
  32. { TSQLite3Backup }
  33. TSQLite3Backup=class
  34. private
  35. FErrorMessage: string;
  36. FLockReleaseTime: integer;
  37. FOnBackupProgress: TOnBackupProgress;
  38. FPageStep: integer;
  39. public
  40. constructor Create;
  41. //Backup one TSQLite3Connection to another TSQLite3Connection
  42. //SourceDBName and DestinationDBName is "main" for the main database, "temp" for the temporary
  43. //database, or the name specified after the AS keyword in an ATTACH statement for an
  44. //attached database
  45. //LockUntilFinished: Set to false when simultanuous access from other processes is required.
  46. //The backup process will restart when another process writes to the database.
  47. //Pro: the backup is a correct snapshot
  48. //Contra: the backup can take a very long time when a lot of writes to the database are made during backup
  49. //Warning: don't use the destination TSQLite3Connection (the handle) for anything else while doing a backup.
  50. //It can corrupt the database and even cause a mutex deadlock.
  51. function Backup(Source,Destination:TSQLite3Connection;LockUntilFinished:boolean=true;
  52. SourceDBName:string='main';DestinationDBName:string='main'):boolean;
  53. //Backup a database to file
  54. function Backup(Source:TSQLite3Connection;FileName:string;LockUntilFinished:boolean=true;
  55. SourceDBName:string='main'):boolean;
  56. //Restore a database from file
  57. function Restore(FileName:string;Destination:TSQLite3Connection;LockUntilFinished:boolean=true;
  58. DestinationDBName:string='main'):boolean;
  59. published
  60. //Delay between backup steps in ms. Default:100ms. Only used when LockUntilFinished=false
  61. property LockReleaseTime:integer read FLockReleaseTime write FLockReleaseTime;
  62. //Page size between backup steps. Default:10 Only used when LockUntilFinished=false
  63. property PageStep:integer read FPageStep write FPageStep;
  64. property ErrorMessage:string read FErrorMessage;
  65. property OnBackupProgress:TOnBackupProgress read FOnBackupProgress write FOnBackupProgress;
  66. end;
  67. implementation
  68. { TSQLite3Backup }
  69. constructor TSQLite3Backup.Create;
  70. begin
  71. FLockReleaseTime:=100;
  72. FPageStep:=10;
  73. end;
  74. function TSQLite3Backup.Backup(Source, Destination: TSQLite3Connection;
  75. LockUntilFinished: boolean; SourceDBName: string; DestinationDBName: string
  76. ): boolean;
  77. var
  78. pBackup:psqlite3backup;
  79. nPage:integer;
  80. res: integer;
  81. begin
  82. FErrorMessage:='';
  83. Source.Connected:=true;
  84. Destination.Connected:=true;
  85. pBackup := sqlite3_backup_init(Destination.Handle, pchar(DestinationDBName),
  86. Source.Handle, pchar(SourceDBName));
  87. if LockUntilFinished then
  88. nPage:=-1
  89. else
  90. nPage:=FPageStep;
  91. result:=false;
  92. if assigned(pBackup) then
  93. begin
  94. { Each iteration of this loop copies PageStep database pages from database
  95. pDb to the backup database. If the return value of backup_step()
  96. indicates that there are still further pages to copy, sleep for
  97. LockReleaseTime ms before repeating. }
  98. repeat
  99. res := sqlite3_backup_step(pBackup, nPage);
  100. if assigned(FOnBackupProgress) then
  101. FOnBackupProgress(self,sqlite3_backup_remaining(pBackup),
  102. sqlite3_backup_pagecount(pBackup));
  103. if not LockUntilFinished and (res in [SQLITE_OK,SQLITE_BUSY,SQLITE_LOCKED]) then
  104. sqlite3_sleep(LockReleaseTime);
  105. until LockUntilFinished or not (res in [SQLITE_OK,SQLITE_BUSY,SQLITE_LOCKED]);
  106. result:=sqlite3_backup_finish(pBackup)=SQLITE_OK;
  107. end;
  108. if not result then
  109. FErrorMessage:=strpas(sqlite3_errmsg(Destination.Handle));
  110. end;
  111. function TSQLite3Backup.Backup(Source: TSQLite3Connection; FileName: string;
  112. LockUntilFinished: boolean; SourceDBName: string): boolean;
  113. var conn:TSQLite3Connection;
  114. begin
  115. conn:=TSQLite3Connection.Create(nil);
  116. try
  117. conn.DatabaseName:=FileName;
  118. conn.Connected:=true;
  119. result:=Backup(Source,conn,LockUntilFinished,SourceDBName);
  120. finally
  121. conn.Destroy;
  122. end;
  123. end;
  124. function TSQLite3Backup.Restore(FileName: string;
  125. Destination: TSQLite3Connection; LockUntilFinished: boolean;
  126. DestinationDBName: string): boolean;
  127. var conn:TSQLite3Connection;
  128. begin
  129. conn:=TSQLite3Connection.Create(nil);
  130. try
  131. conn.DatabaseName:=FileName;
  132. conn.Connected:=true;
  133. result:=Backup(conn,Destination,LockUntilFinished,'main',DestinationDBName);
  134. finally
  135. conn.Destroy;
  136. end;
  137. end;
  138. end.