Эх сурвалжийг харах

Baked in support for the new application_id pragma in SQLite version 3.7.17:
- (uint32_t)applicationID;
- (void)setApplicationID:(uint32_t)appID;
- (NSString*)applicationIDString;
- (void)setApplicationIDString:(NSString*)s;

ccgus 12 жил өмнө
parent
commit
faa5f473f0

+ 7 - 1
CHANGES_AND_TODO_LIST.txt

@@ -6,7 +6,13 @@ If you would like to contribute some code- awesome!  I just ask that you make it
 2013.05.24
     Merged in Chris Wright's date format additions to FMDatabase.
     Fixed a problem where executeUpdateWithFormat: + %@ as a placeholder and the value was nil would cause a bad value to be inserted.  Thanks to rustybox on github for the fix.
-     Fixed a variable argument crash if an incorrect number of arguments are passed in - patch by Joshua Tessier.
+    Fixed a variable argument crash if an incorrect number of arguments are passed in - patch by Joshua Tessier.
+    Baked in support for the new application_id pragma in SQLite version 3.7.17:
+        - (uint32_t)applicationID;
+        - (void)setApplicationID:(uint32_t)appID;
+        - (NSString*)applicationIDString;
+        - (void)setApplicationIDString:(NSString*)s;
+
 
 2013.04.17
     Added two new methods to FMDatabase for setting crypto keys, which take NSData objects.  Thanks to Phillip Kast for the patch! <https://github.com/ccgus/fmdb/pull/135>

+ 10 - 0
src/FMDatabaseAdditions.h

@@ -31,6 +31,16 @@
 
 - (BOOL)validateSQL:(NSString*)sql error:(NSError**)error;
 
+
+#if SQLITE_VERSION_NUMBER >= 3007017
+- (uint32_t)applicationID;
+- (void)setApplicationID:(uint32_t)appID;
+
+- (NSString*)applicationIDString;
+- (void)setApplicationIDString:(NSString*)s;
+#endif
+
+
 // deprecated - use columnExists:inTableWithName: instead.
 - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName __attribute__ ((deprecated));
 

+ 46 - 0
src/FMDatabaseAdditions.m

@@ -117,6 +117,52 @@ - (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName
     return returnBool;
 }
 
+#if SQLITE_VERSION_NUMBER >= 3007017
+- (uint32_t)applicationID {
+    
+    uint32_t r = 0;
+    
+    FMResultSet *rs = [self executeQuery:@"pragma application_id"];
+    
+    if ([rs next]) {
+        r = (uint32_t)[rs longLongIntForColumnIndex:0];
+    }
+    
+    [rs close];
+    
+    return r;
+}
+
+- (NSString*)applicationIDString {
+    NSString *s = NSFileTypeForHFSTypeCode([self applicationID]);
+    
+    assert([s length] == 6);
+    
+    s = [s substringWithRange:NSMakeRange(1, 4)];
+    
+    
+    return s;
+    
+}
+
+- (void)setApplicationID:(uint32_t)appID {
+    NSString *query = [NSString stringWithFormat:@"PRAGMA application_id=%d", appID];
+    FMResultSet *rs = [self executeQuery:query];
+    [rs next];
+    [rs close];
+}
+
+- (void)setApplicationIDString:(NSString*)s {
+    
+    if ([s length] != 4) {
+        NSLog(@"setApplicationIDString: string passed is not exactly 4 chars long. (was %ld)", [s length]);
+    }
+    
+    [self setApplicationID:NSHFSTypeCodeFromFileType([NSString stringWithFormat:@"'%@'", s])];
+}
+
+#endif
+
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wdeprecated-implementations"
 

+ 24 - 1
src/fmdb.m

@@ -203,8 +203,31 @@ int main (int argc, const char * argv[]) {
     rs = [db getTableSchema:@"234 fds"];
     FMDBQuickCheck([rs next]);
     [rs close];
+
+
+#if SQLITE_VERSION_NUMBER >= 3007017
+    {
+        uint32_t appID = NSHFSTypeCodeFromFileType(NSFileTypeForHFSTypeCode('fmdb'));
+        
+        [db setApplicationID:appID];
+        
+        uint32_t rAppID = [db applicationID];
+        
+        NSLog(@"rAppID: %d", rAppID);
+        
+        FMDBQuickCheck(rAppID == appID);
+        
+        [db setApplicationIDString:@"acrn"];
+        
+        NSString *s = [db applicationIDString];
+        
+        NSLog(@"s: '%@'", s);
+        
+        FMDBQuickCheck([s isEqualToString:@"acrn"]);
+        
+    }
     
-    
+#endif