Browse Source

Merge branch 'sechel-master'

August Mueller 10 years ago
parent
commit
1a30bddb8b

+ 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];

+ 96 - 0
fmdb.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

@@ -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>

+ 4 - 1
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,6 +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 vfs:(NSString *)vfsName;
 #endif
 #endif
 
 
 /** Closing a database connection
 /** Closing a database connection

+ 4 - 1
src/fmdb/FMDatabase.m

@@ -151,11 +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 vfs:nil];
+}
+- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName; {
     if (_db) {
     if (_db) {
         return YES;
         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) {
     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 {