Procházet zdrojové kódy

Adjust encoding of BOOL and char values

Because `@encode(BOOL)` is the same as `@encode(char)`, the code was applying `BOOL` logic to char values. I’ve moved the `BOOL` logic to the end of the if statements. As it turns out, it will probably never reach that line because the `char` logic will take care of it fine. But it seemed like good defensive programming to keep the `BOOL` logic in there, nonetheless, in case Objective-C ever introduces a different `objCType` encoding for `BOOL`.

I also added a unit test for `BOOL` and `char` values.
robertmryan před 11 roky
rodič
revize
f67190bfe7
2 změnil soubory, kde provedl 30 přidání a 4 odebrání
  1. 26 0
      Tests/FMDatabaseTests.m
  2. 4 4
      src/fmdb/FMDatabase.m

+ 26 - 0
Tests/FMDatabaseTests.m

@@ -872,4 +872,30 @@ - (void)testExecuteStatements
     XCTAssertTrue(success, @"bulk drop");
 }
 
+- (void)testCharAndBoolTypes
+{
+    XCTAssertTrue([self.db executeUpdate:@"create table charBoolTest (a, b, c)"]);
+
+    BOOL success = [self.db executeUpdate:@"insert into charBoolTest values (?, ?, ?)", @YES, @NO, @('x')];
+    XCTAssertTrue(success, @"Unable to insert values");
+
+    FMResultSet *rs = [self.db executeQuery:@"select * from charBoolTest"];
+    XCTAssertNotNil(rs);
+
+    XCTAssertTrue([rs next], @"Did not return row");
+
+    XCTAssertEqual([rs boolForColumn:@"a"], true, @"Column a was not true");
+    XCTAssertEqualObjects([rs objectForColumnName:@"a"], @YES, @"Column a was not true");
+
+    XCTAssertEqual([rs boolForColumn:@"b"], false, @"Column b was not false");
+    XCTAssertEqualObjects([rs objectForColumnName:@"b"], @NO, @"Column b was not true");
+
+    XCTAssertEqual([rs intForColumn:@"c"], 'x', @"Column c was not 'x'");
+
+    [rs close];
+
+    XCTAssertTrue([self.db executeUpdate:@"drop table charBoolTest"], @"Did not drop table");
+
+}
+
 @end

+ 4 - 4
src/fmdb/FMDatabase.m

@@ -550,10 +550,7 @@ - (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
     }
     else if ([obj isKindOfClass:[NSNumber class]]) {
         
-        if (strcmp([obj objCType], @encode(BOOL)) == 0) {
-            sqlite3_bind_int(pStmt, idx, ([obj boolValue] ? 1 : 0));
-        }
-        else if (strcmp([obj objCType], @encode(char)) == 0) {
+        if (strcmp([obj objCType], @encode(char)) == 0) {
             sqlite3_bind_int(pStmt, idx, [obj charValue]);
         }
         else if (strcmp([obj objCType], @encode(unsigned char)) == 0) {
@@ -589,6 +586,9 @@ - (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
         else if (strcmp([obj objCType], @encode(double)) == 0) {
             sqlite3_bind_double(pStmt, idx, [obj doubleValue]);
         }
+        else if (strcmp([obj objCType], @encode(BOOL)) == 0) {
+            sqlite3_bind_int(pStmt, idx, ([obj boolValue] ? 1 : 0));
+        }
         else {
             sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);
         }