123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #import "FMDatabase+InMemoryOnDiskIO.h"
- static
- int loadOrSaveDb(sqlite3 *pInMemory, const char *zFilename, int isSave)
- {
- int rc;
- sqlite3 *pFile;
- sqlite3_backup *pBackup;
- sqlite3 *pTo;
- sqlite3 *pFrom;
-
-
- rc = sqlite3_open(zFilename, &pFile);
- if( rc==SQLITE_OK ){
-
-
- pFrom = (isSave ? pInMemory : pFile);
- pTo = (isSave ? pFile : pInMemory);
-
-
- pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
- if( pBackup ){
- (void)sqlite3_backup_step(pBackup, -1);
- (void)sqlite3_backup_finish(pBackup);
- }
- rc = sqlite3_errcode(pTo);
- }
-
-
- (void)sqlite3_close(pFile);
- return rc;
- }
- @implementation FMDatabase (InMemoryOnDiskIO)
- - (BOOL)readFromFile:(NSString*)filePath
- {
-
- if ( self->_databasePath != nil )
- {
- NSLog(@"Database is not an in-memory representation." );
- return NO;
- }
-
-
- if ( self->_db == nil )
- {
- NSLog(@"Invalid database connection." );
- return NO;
- }
-
- return ( SQLITE_OK == loadOrSaveDb( self->_db, [filePath fileSystemRepresentation], false ) );
- }
- - (BOOL)writeToFile:(NSString *)filePath
- {
-
- if ( self->_databasePath != nil )
- {
- NSLog(@"Database is not an in-memory representation." );
- return NO;
- }
-
-
- if ( self->_db == nil )
- {
- NSLog(@"Invalid database connection." );
- return NO;
- }
-
-
- return ( SQLITE_OK == loadOrSaveDb( self->_db, [filePath fileSystemRepresentation], true ) );
- }
- @end
|