Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/sechel/fmdb into sechel-master
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Jun 17, 2015
2 parents 12ac459 + ca28671 commit d0c9daf
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 5 deletions.
19 changes: 19 additions & 0 deletions Tests/FMDatabaseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
[self.db close];
Expand Down
96 changes: 96 additions & 0 deletions fmdb.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BF5D041518416BB2008C5AA9"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BF5D041518416BB2008C5AA9"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BF5D041518416BB2008C5AA9"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BF5D041518416BB2008C5AA9"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BF5D041518416BB2008C5AA9"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:fmdb.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
5 changes: 4 additions & 1 deletion src/fmdb/FMDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -219,6 +221,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary

#if SQLITE_VERSION_NUMBER >= 3005000
- (BOOL)openWithFlags:(int)flags;
- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName;
#endif

/** Closing a database connection
Expand Down
5 changes: 4 additions & 1 deletion src/fmdb/FMDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,14 @@ - (BOOL)open {

#if SQLITE_VERSION_NUMBER >= 3005000
- (BOOL)openWithFlags:(int)flags {
return [self openWithFlags:flags vfs:nil];
}
- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName; {
if (_db) {
return YES;
}

int err = sqlite3_open_v2([self sqlitePath], &_db, flags, NULL /* Name of VFS module to use */);
int err = sqlite3_open_v2([self sqlitePath], &_db, flags, [vfsName UTF8String]);
if(err != SQLITE_OK) {
NSLog(@"error opening!: %d", err);
return NO;
Expand Down
11 changes: 11 additions & 0 deletions src/fmdb/FMDatabaseQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions src/fmdb/FMDatabaseQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ + (Class)databaseClass {
return [FMDatabase class];
}

- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName {

self = [super init];

Expand All @@ -61,7 +61,7 @@ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
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
Expand All @@ -81,10 +81,14 @@ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
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 {
Expand Down

0 comments on commit d0c9daf

Please sign in to comment.