Bläddra i källkod

Merge pull request #225 from DanOranges/master

Fixed [FMDatabase openWithFlags:] so it does not reopen the db unnecessarily
August "Gus" Mueller 12 år sedan
förälder
incheckning
f7e1a9298a
3 ändrade filer med 45 tillägg och 0 borttagningar
  1. 4 0
      src/FMDatabase.m
  2. 21 0
      src/FMDatabaseAdditions.h
  3. 20 0
      src/FMDatabaseAdditions.m

+ 4 - 0
src/FMDatabase.m

@@ -112,6 +112,10 @@ - (BOOL)open {
 
 #if SQLITE_VERSION_NUMBER >= 3005000
 - (BOOL)openWithFlags:(int)flags {
+    if (_db) {
+        return YES;
+    }
+
     int err = sqlite3_open_v2([self sqlitePath], &_db, flags, NULL /* Name of VFS module to use */);
     if(err != SQLITE_OK) {
         NSLog(@"error opening!: %d", err);

+ 21 - 0
src/FMDatabaseAdditions.h

@@ -242,5 +242,26 @@
 
 #endif
 
+///-----------------------------------
+/// @name user version identifier tasks
+///-----------------------------------
+
+/** Retrieve user version
+ 
+ @return The `uint32_t` numeric value of the user version.
+ 
+ @see setUserVersion:
+ */
+
+- (uint32_t)userVersion;
+
+/** Set the user-version
+ 
+ @param appID The `uint32_t` numeric value of the user version.
+ 
+ @see userVersion
+ */
+
+- (void)setUserVersion:(uint32_t)version;
 
 @end

+ 20 - 0
src/FMDatabaseAdditions.m

@@ -171,6 +171,26 @@ - (void)setApplicationIDString:(NSString*)s {
 
 #endif
 
+- (uint32_t)userVersion {
+    uint32_t r = 0;
+    
+    FMResultSet *rs = [self executeQuery:@"pragma user_version"];
+    
+    if ([rs next]) {
+        r = (uint32_t)[rs longLongIntForColumnIndex:0];
+    }
+    
+    [rs close];
+    return r;
+}
+
+- (void)setUserVersion:(uint32_t)version {
+    NSString *query = [NSString stringWithFormat:@"PRAGMA user_version=%d", version];
+    FMResultSet *rs = [self executeQuery:query];
+    [rs next];
+    [rs close];
+}
+
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wdeprecated-implementations"