FMDatabase+InMemoryOnDiskIO.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #import "FMDatabase+InMemoryOnDiskIO.h"
  2. // http://www.sqlite.org/backup.html
  3. static
  4. int loadOrSaveDb(sqlite3 *pInMemory, const char *zFilename, int isSave)
  5. {
  6. int rc; /* Function return code */
  7. sqlite3 *pFile; /* Database connection opened on zFilename */
  8. sqlite3_backup *pBackup; /* Backup object used to copy data */
  9. sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */
  10. sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */
  11. /* Open the database file identified by zFilename. Exit early if this fails
  12. ** for any reason. */
  13. rc = sqlite3_open(zFilename, &pFile);
  14. if( rc==SQLITE_OK ){
  15. /* If this is a 'load' operation (isSave==0), then data is copied
  16. ** from the database file just opened to database pInMemory.
  17. ** Otherwise, if this is a 'save' operation (isSave==1), then data
  18. ** is copied from pInMemory to pFile. Set the variables pFrom and
  19. ** pTo accordingly. */
  20. pFrom = (isSave ? pInMemory : pFile);
  21. pTo = (isSave ? pFile : pInMemory);
  22. /* Set up the backup procedure to copy from the "main" database of
  23. ** connection pFile to the main database of connection pInMemory.
  24. ** If something goes wrong, pBackup will be set to NULL and an error
  25. ** code and message left in connection pTo.
  26. **
  27. ** If the backup object is successfully created, call backup_step()
  28. ** to copy data from pFile to pInMemory. Then call backup_finish()
  29. ** to release resources associated with the pBackup object. If an
  30. ** error occurred, then an error code and message will be left in
  31. ** connection pTo. If no error occurred, then the error code belonging
  32. ** to pTo is set to SQLITE_OK.
  33. */
  34. pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
  35. if( pBackup ){
  36. (void)sqlite3_backup_step(pBackup, -1);
  37. (void)sqlite3_backup_finish(pBackup);
  38. }
  39. rc = sqlite3_errcode(pTo);
  40. }
  41. /* Close the database connection opened on database file zFilename
  42. ** and return the result of this function. */
  43. (void)sqlite3_close(pFile);
  44. return rc;
  45. }
  46. @implementation FMDatabase (InMemoryOnDiskIO)
  47. - (BOOL)readFromFile:(NSString*)filePath
  48. {
  49. // only attempt to load an on-disk representation for an in-memory database
  50. if ( self->_databasePath != nil )
  51. {
  52. NSLog(@"Database is not an in-memory representation." );
  53. return NO;
  54. }
  55. // and only if the database is open
  56. if ( self->_db == nil )
  57. {
  58. NSLog(@"Invalid database connection." );
  59. return NO;
  60. }
  61. return ( SQLITE_OK == loadOrSaveDb( self->_db, [filePath fileSystemRepresentation], false ) );
  62. }
  63. - (BOOL)writeToFile:(NSString *)filePath
  64. {
  65. // only attempt to save an on-disk representation for an in-memory database
  66. if ( self->_databasePath != nil )
  67. {
  68. NSLog(@"Database is not an in-memory representation." );
  69. return NO;
  70. }
  71. // and only if the database is open
  72. if ( self->_db == nil )
  73. {
  74. NSLog(@"Invalid database connection." );
  75. return NO;
  76. }
  77. // save the in-memory representation
  78. return ( SQLITE_OK == loadOrSaveDb( self->_db, [filePath fileSystemRepresentation], true ) );
  79. }
  80. @end