Browse Source

Changed up the behavior of binding empty NSData objects ([NSData data]). It will now insert an empty value, rather than a null value- which is consistent with [NSMutableData data] and empty strings (see https://github.com/ccgus/fmdb/issues/73 for a discussion on this). Thanks to Jens Alfke for pointing this out!

ccgus 13 years ago
parent
commit
8666d8f95a
3 changed files with 22 additions and 1 deletions
  1. 3 0
      CHANGES_AND_TODO_LIST.txt
  2. 7 1
      src/FMDatabase.m
  3. 12 0
      src/fmdb.m

+ 3 - 0
CHANGES_AND_TODO_LIST.txt

@@ -3,6 +3,9 @@ Zip, nada, zilch.  Got any ideas?
 
 If you would like to contribute some code- awesome!  I just ask that you make it conform to the coding conventions already set in here, and to add a couple of tests for your new code to fmdb.m.  And of course, the code should be of general use to more than just a couple of folks.  Send your patches to gus@flyingmeat.com.
 
+2012.05.29:
+    Changed up the behavior of binding empty NSData objects ([NSData data]).  It will now insert an empty value, rather than a null value- which is consistent with [NSMutableData data] and empty strings (see https://github.com/ccgus/fmdb/issues/73 for a discussion on this).  Thanks to Jens Alfke for pointing this out!
+    
 2012.05.25:
     Deprecated columnExists:columnName: in favor of columnExists:inTableWithName:
     Remembered to update the changes notes.  I've been forgetting to do this recently.

+ 7 - 1
src/FMDatabase.m

@@ -338,7 +338,13 @@ - (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
     
     // FIXME - someday check the return codes on these binds.
     else if ([obj isKindOfClass:[NSData class]]) {
-        sqlite3_bind_blob(pStmt, idx, [obj bytes], (int)[obj length], SQLITE_STATIC);
+        const void *bytes = [obj bytes];
+        if (!bytes) {
+            // it's an empty NSData object, aka [NSData data].
+            // Don't pass a NULL pointer, or sqlite will bind a SQL null instead of a blob.
+            bytes = "";
+        }
+        sqlite3_bind_blob(pStmt, idx, bytes, (int)[obj length], SQLITE_STATIC);
     }
     else if ([obj isKindOfClass:[NSDate class]]) {
         sqlite3_bind_double(pStmt, idx, [obj timeIntervalSince1970]);

+ 12 - 0
src/fmdb.m

@@ -57,6 +57,18 @@ int main (int argc, const char * argv[]) {
     FMDBQuickCheck([err code] == SQLITE_ERROR);
     NSLog(@"err: '%@'", err);
     
+    
+    
+    // empty strings should still return a value.
+    FMDBQuickCheck(([db boolForQuery:@"SELECT ? not null", @""]));
+    
+    // same with empty bits o' mutable data
+    FMDBQuickCheck(([db boolForQuery:@"SELECT ? not null", [NSMutableData data]]));
+    
+    // same with empty bits o' data
+    FMDBQuickCheck(([db boolForQuery:@"SELECT ? not null", [NSData data]]));
+
+    
     // but of course, I don't bother checking the error codes below.
     // Bad programmer, no cookie.