rename open with vfs method, add tests for custom vfs

This commit is contained in:
Stefan Sechelmann 2015-06-10 16:12:11 +02:00
parent fac5f64ad9
commit ca28671462
5 changed files with 44 additions and 8 deletions

View File

@ -68,6 +68,25 @@
}
- (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
{
[self.db close];

View File

@ -194,7 +194,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
- (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:
@ -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.
If vfs is given the value is passed to the vfs parameter of sqlite3_open_v2.
@return `YES` if successful, `NO` on error.
@see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
@ -219,7 +221,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
#if SQLITE_VERSION_NUMBER >= 3005000
- (BOOL)openWithFlags:(int)flags;
- (BOOL)openWithFlags:(int)flags andVFS:(const char*)vfs;
- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName;
#endif
/** Closing a database connection

View File

@ -151,14 +151,14 @@
#if SQLITE_VERSION_NUMBER >= 3005000
- (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) {
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) {
NSLog(@"error opening!: %d", err);
return NO;

View File

@ -117,6 +117,17 @@
- (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.
Subclasses can override this method to return specified Class of 'FMDatabase' subclass.

View File

@ -51,7 +51,7 @@ static const void * const kDispatchQueueSpecificKey = &kDispatchQueueSpecificKey
return [FMDatabase class];
}
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName {
self = [super init];
@ -61,7 +61,7 @@ static const void * const kDispatchQueueSpecificKey = &kDispatchQueueSpecificKey
FMDBRetain(_db);
#if SQLITE_VERSION_NUMBER >= 3005000
BOOL success = [_db openWithFlags:openFlags];
BOOL success = [_db openWithFlags:openFlags vfs:vfsName];
#else
BOOL success = [_db open];
#endif
@ -81,10 +81,14 @@ static const void * const kDispatchQueueSpecificKey = &kDispatchQueueSpecificKey
return self;
}
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
return [self initWithPath:aPath flags:openFlags vfs:nil];
}
- (instancetype)initWithPath:(NSString*)aPath {
// 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 {