Browse Source

rename open with vfs method, add tests for custom vfs

Stefan Sechelmann 10 years ago
parent
commit
ca28671462

+ 19 - 0
Tests/FMDatabaseTests.m

@@ -68,6 +68,25 @@ - (void)tearDown
     
     
 }
 }
 
 
+- (void)testOpenWithVFS
+{
+    // create custom vfs
+    sqlite3_vfs vfs = *sqlite3_vfs_find(NULL);
+    vfs.zName = "MyCustomVFS";
+    XCTAssertEqual(SQLITE_OK, sqlite3_vfs_register(&vfs, 0));
+    // use custom vfs to open a in memory database
+    FMDatabase *db = [[FMDatabase alloc] initWithPath:@":memory:"];
+    [db openWithFlags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE vfs:@"MyCustomVFS"];
+    XCTAssertFalse([db hadError], @"Open with a custom VFS should have succeeded");
+}
+
+- (void)testFailOnOpenWithUnknownVFS
+{
+    FMDatabase *db = [[FMDatabase alloc] initWithPath:@":memory:"];
+    [db openWithFlags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE vfs:@"UnknownVFS"];
+    XCTAssertTrue([db hadError], @"Should have failed");    
+}
+
 - (void)testFailOnUnopenedDatabase
 - (void)testFailOnUnopenedDatabase
 {
 {
     [self.db close];
     [self.db close];

+ 4 - 2
src/fmdb/FMDatabase.h

@@ -194,7 +194,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
 
 - (BOOL)open;
 - (BOOL)open;
 
 
-/** Opening a new database connection with flags
+/** Opening a new database connection with flags and an optional virtual file system (VFS)
 
 
  @param flags one of the following three values, optionally combined with the `SQLITE_OPEN_NOMUTEX`, `SQLITE_OPEN_FULLMUTEX`, `SQLITE_OPEN_SHAREDCACHE`, `SQLITE_OPEN_PRIVATECACHE`, and/or `SQLITE_OPEN_URI` flags:
  @param flags one of the following three values, optionally combined with the `SQLITE_OPEN_NOMUTEX`, `SQLITE_OPEN_FULLMUTEX`, `SQLITE_OPEN_SHAREDCACHE`, `SQLITE_OPEN_PRIVATECACHE`, and/or `SQLITE_OPEN_URI` flags:
 
 
@@ -210,6 +210,8 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
  
  
  The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for `open` method.
  The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for `open` method.
  
  
+ If vfs is given the value is passed to the vfs parameter of sqlite3_open_v2.
+ 
  @return `YES` if successful, `NO` on error.
  @return `YES` if successful, `NO` on error.
 
 
  @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
  @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
@@ -219,7 +221,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
 
 #if SQLITE_VERSION_NUMBER >= 3005000
 #if SQLITE_VERSION_NUMBER >= 3005000
 - (BOOL)openWithFlags:(int)flags;
 - (BOOL)openWithFlags:(int)flags;
-- (BOOL)openWithFlags:(int)flags andVFS:(const char*)vfs;
+- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName;
 #endif
 #endif
 
 
 /** Closing a database connection
 /** Closing a database connection

+ 3 - 3
src/fmdb/FMDatabase.m

@@ -151,14 +151,14 @@ - (BOOL)open {
 
 
 #if SQLITE_VERSION_NUMBER >= 3005000
 #if SQLITE_VERSION_NUMBER >= 3005000
 - (BOOL)openWithFlags:(int)flags {
 - (BOOL)openWithFlags:(int)flags {
-    return [self openWithFlags:flags andVFS:NULL];
+    return [self openWithFlags:flags vfs:nil];
 }
 }
-- (BOOL)openWithFlags:(int)flags andVFS:(const char*)vfs {
+- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName; {
     if (_db) {
     if (_db) {
         return YES;
         return YES;
     }
     }
 
 
-    int err = sqlite3_open_v2([self sqlitePath], &_db, flags, vfs);
+    int err = sqlite3_open_v2([self sqlitePath], &_db, flags, [vfsName UTF8String]);
     if(err != SQLITE_OK) {
     if(err != SQLITE_OK) {
         NSLog(@"error opening!: %d", err);
         NSLog(@"error opening!: %d", err);
         return NO;
         return NO;

+ 11 - 0
src/fmdb/FMDatabaseQueue.h

@@ -117,6 +117,17 @@
 
 
 - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
 - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
 
 
+/** Create queue using path and specified flags.
+ 
+ @param aPath The file path of the database.
+ @param openFlags Flags passed to the openWithFlags method of the database
+ @param vfsName The name of a custom virtual file system
+ 
+ @return The `FMDatabaseQueue` object. `nil` on error.
+ */
+
+- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName;
+
 /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
 /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  
  
  Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
  Subclasses can override this method to return specified Class of 'FMDatabase' subclass.

+ 7 - 3
src/fmdb/FMDatabaseQueue.m

@@ -51,7 +51,7 @@ + (Class)databaseClass {
     return [FMDatabase class];
     return [FMDatabase class];
 }
 }
 
 
-- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
+- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName {
     
     
     self = [super init];
     self = [super init];
     
     
@@ -61,7 +61,7 @@ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
         FMDBRetain(_db);
         FMDBRetain(_db);
         
         
 #if SQLITE_VERSION_NUMBER >= 3005000
 #if SQLITE_VERSION_NUMBER >= 3005000
-        BOOL success = [_db openWithFlags:openFlags];
+        BOOL success = [_db openWithFlags:openFlags vfs:vfsName];
 #else
 #else
         BOOL success = [_db open];
         BOOL success = [_db open];
 #endif
 #endif
@@ -81,10 +81,14 @@ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
     return self;
     return self;
 }
 }
 
 
+- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
+    return [self initWithPath:aPath flags:openFlags vfs:nil];
+}
+
 - (instancetype)initWithPath:(NSString*)aPath {
 - (instancetype)initWithPath:(NSString*)aPath {
     
     
     // default flags for sqlite3_open
     // default flags for sqlite3_open
-    return [self initWithPath:aPath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE];
+    return [self initWithPath:aPath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE vfs:nil];
 }
 }
 
 
 - (instancetype)init {
 - (instancetype)init {