Browse Source

The format bits have been removed since it's a super bad code smell.

August Mueller 9 years ago
parent
commit
05780fb391
4 changed files with 0 additions and 374 deletions
  1. 0 107
      Tests/FMDatabaseTests.m
  2. 0 55
      src/fmdb/FMDatabase.h
  3. 0 147
      src/fmdb/FMDatabase.m
  4. 0 65
      src/sample/main.m

+ 0 - 107
Tests/FMDatabaseTests.m

@@ -556,70 +556,6 @@ - (void)testColumnNamesContainingPeriods
     [rs close];
     [rs close];
 }
 }
 
 
-- (void)testFormatStringParsing
-{
-    XCTAssertTrue([self.db executeUpdate:@"create table t5 (a text, b int, c blob, d text, e text)"]);
-    [self.db executeUpdateWithFormat:@"insert into t5 values (%s, %d, %@, %c, %lld)", "text", 42, @"BLOB", 'd', 12345678901234ll];
-    
-    FMResultSet *rs = [self.db executeQueryWithFormat:@"select * from t5 where a = %s and a = %@ and b = %d", "text", @"text", 42];
-    XCTAssertNotNil(rs);
-    
-    XCTAssertTrue([rs next]);
-    
-    XCTAssertEqualObjects([rs stringForColumn:@"a"], @"text");
-    XCTAssertEqual([rs intForColumn:@"b"], 42);
-    XCTAssertEqualObjects([rs stringForColumn:@"c"], @"BLOB");
-    XCTAssertEqualObjects([rs stringForColumn:@"d"], @"d");
-    XCTAssertEqual([rs longLongIntForColumn:@"e"], 12345678901234ll);
-    
-    [rs close];
-}
-
-- (void)testFormatStringParsingWithSizePrefixes
-{
-    XCTAssertTrue([self.db executeUpdate:@"create table t55 (a text, b int, c float)"]);
-    short testShort = -4;
-    float testFloat = 5.5;
-    [self.db executeUpdateWithFormat:@"insert into t55 values (%c, %hi, %g)", 'a', testShort, testFloat];
-    
-    unsigned short testUShort = 6;
-    [self.db executeUpdateWithFormat:@"insert into t55 values (%c, %hu, %g)", 'a', testUShort, testFloat];
-    
-    
-    FMResultSet *rs = [self.db executeQueryWithFormat:@"select * from t55 where a = %s order by 2", "a"];
-    XCTAssertNotNil(rs);
-    
-    XCTAssertTrue([rs next]);
-    
-    XCTAssertEqualObjects([rs stringForColumn:@"a"], @"a");
-    XCTAssertEqual([rs intForColumn:@"b"], -4);
-    XCTAssertEqualObjects([rs stringForColumn:@"c"], @"5.5");
-    
-    
-    XCTAssertTrue([rs next]);
-    
-    XCTAssertEqualObjects([rs stringForColumn:@"a"], @"a");
-    XCTAssertEqual([rs intForColumn:@"b"], 6);
-    XCTAssertEqualObjects([rs stringForColumn:@"c"], @"5.5");
-    
-    [rs close];
-}
-
-- (void)testFormatStringParsingWithNilValue
-{
-    XCTAssertTrue([self.db executeUpdate:@"create table tatwhat (a text)"]);
-    
-    BOOL worked = [self.db executeUpdateWithFormat:@"insert into tatwhat values(%@)", nil];
-    
-    XCTAssertTrue(worked);
-    
-    FMResultSet *rs = [self.db executeQueryWithFormat:@"select * from tatwhat"];
-    XCTAssertNotNil(rs);
-    XCTAssertTrue([rs next]);
-    XCTAssertTrue([rs columnIndexIsNull:0]);
-    
-    XCTAssertFalse([rs next]);
-}
 
 
 - (void)testUpdateWithErrorAndBindings
 - (void)testUpdateWithErrorAndBindings
 {
 {
@@ -1123,49 +1059,6 @@ - (void)testBind {
     XCTAssertEqualObjects(insertedValue, retrievedValue, @"values don't match");
     XCTAssertEqualObjects(insertedValue, retrievedValue, @"values don't match");
 }
 }
 
 
-- (void)testFormatStrings {
-    FMDatabase *db = [[FMDatabase alloc] init];
-    XCTAssert([db open], @"open failed");
-    XCTAssert([db executeUpdate:@"create table foo (id integer primary key autoincrement, a numeric)"], @"create failed");
-    
-    BOOL success;
-    
-    char insertedChar = 'A';
-    success = [db executeUpdateWithFormat:@"insert into foo (a) values (%c)", insertedChar];
-    XCTAssert(success, @"insert failed");
-    const char *retrievedChar = [[db stringForQuery:@"select a from foo where id = ?", @([db lastInsertRowId])] UTF8String];
-    XCTAssertEqual(insertedChar, retrievedChar[0], @"values don't match");
-    
-    const char *insertedString = "baz";
-    success = [db executeUpdateWithFormat:@"insert into foo (a) values (%s)", insertedString];
-    XCTAssert(success, @"insert failed");
-    const char *retrievedString = [[db stringForQuery:@"select a from foo where id = ?", @([db lastInsertRowId])] UTF8String];
-    XCTAssert(strcmp(insertedString, retrievedString) == 0, @"values don't match");
-    
-    int insertedInt = 42;
-    success = [db executeUpdateWithFormat:@"insert into foo (a) values (%d)", insertedInt];
-    XCTAssert(success, @"insert failed");
-    int retrievedInt = [db intForQuery:@"select a from foo where id = ?", @([db lastInsertRowId])];
-    XCTAssertEqual(insertedInt, retrievedInt, @"values don't match");
-
-    char insertedUnsignedInt = 43;
-    success = [db executeUpdateWithFormat:@"insert into foo (a) values (%u)", insertedUnsignedInt];
-    XCTAssert(success, @"insert failed");
-    char retrievedUnsignedInt = [db intForQuery:@"select a from foo where id = ?", @([db lastInsertRowId])];
-    XCTAssertEqual(insertedUnsignedInt, retrievedUnsignedInt, @"values don't match");
-    
-    float insertedFloat = 44;
-    success = [db executeUpdateWithFormat:@"insert into foo (a) values (%f)", insertedFloat];
-    XCTAssert(success, @"insert failed");
-    float retrievedFloat = [db doubleForQuery:@"select a from foo where id = ?", @([db lastInsertRowId])];
-    XCTAssertEqual(insertedFloat, retrievedFloat, @"values don't match");
-    
-    unsigned long long insertedUnsignedLongLong = 45;
-    success = [db executeUpdateWithFormat:@"insert into foo (a) values (%llu)", insertedUnsignedLongLong];
-    XCTAssert(success, @"insert failed");
-    unsigned long long retrievedUnsignedLongLong = [db longForQuery:@"select a from foo where id = ?", @([db lastInsertRowId])];
-    XCTAssertEqual(insertedUnsignedLongLong, retrievedUnsignedLongLong, @"values don't match");
-}
 
 
 - (void)testStepError {
 - (void)testStepError {
     FMDatabase *db = [[FMDatabase alloc] init];
     FMDatabase *db = [[FMDatabase alloc] init];

+ 0 - 55
src/fmdb/FMDatabase.h

@@ -328,33 +328,7 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
 
 - (BOOL)executeUpdate:(NSString*)sql, ...;
 - (BOOL)executeUpdate:(NSString*)sql, ...;
 
 
-/** Execute single update statement
-
- This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL. Do not use `?` placeholders in the SQL if you use this method.
-
- @param format The SQL to be performed, with `printf`-style escape sequences.
-
- @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
-
- @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
-
- @see executeUpdate:
- @see lastError
- @see lastErrorCode
- @see lastErrorMessage
- 
- @note This method does not technically perform a traditional printf-style replacement. What this method actually does is replace the printf-style percent sequences with a SQLite `?` placeholder, and then bind values to that placeholder. Thus the following command
-
-    [db executeUpdateWithFormat:@"INSERT INTO test (name) VALUES (%@)", @"Gus"];
 
 
- is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeUpdate:>`
-
-    [db executeUpdate:@"INSERT INTO test (name) VALUES (?)", @"Gus"];
-
- There are two reasons why this distinction is important. First, the printf-style escape sequences can only be used where it is permissible to use a SQLite `?` placeholder. You can use it only for values in SQL statements, but not for table names or column names or any other non-value context. This method also cannot be used in conjunction with `pragma` statements and the like. Second, note the lack of quotation marks in the SQL. The `VALUES` clause was _not_ `VALUES ('%@')` (like you might have to do if you built a SQL statement using `NSString` method `stringWithFormat`), but rather simply `VALUES (%@)`.
- */
-
-- (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
 
 
 /** Execute single update statement
 /** Execute single update statement
  
  
@@ -532,35 +506,6 @@ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary
 
 
 - (FMResultSet *)executeQuery:(NSString*)sql, ...;
 - (FMResultSet *)executeQuery:(NSString*)sql, ...;
 
 
-/** Execute select statement
-
- Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure.  Like executing updates, there is a variant that accepts an `NSError **` parameter.  Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
- 
- In order to iterate through the results of your query, you use a `while()` loop.  You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
- 
- @param format The SQL to be performed, with `printf`-style escape sequences.
-
- @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
-
- @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
-
- @see executeQuery:
- @see FMResultSet
- @see [`FMResultSet next`](<[FMResultSet next]>)
-
- @note This method does not technically perform a traditional printf-style replacement. What this method actually does is replace the printf-style percent sequences with a SQLite `?` placeholder, and then bind values to that placeholder. Thus the following command
- 
-    [db executeQueryWithFormat:@"SELECT * FROM test WHERE name=%@", @"Gus"];
- 
- is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeQuery:>`
- 
-    [db executeQuery:@"SELECT * FROM test WHERE name=?", @"Gus"];
- 
- There are two reasons why this distinction is important. First, the printf-style escape sequences can only be used where it is permissible to use a SQLite `?` placeholder. You can use it only for values in SQL statements, but not for table names or column names or any other non-value context. This method also cannot be used in conjunction with `pragma` statements and the like. Second, note the lack of quotation marks in the SQL. The `WHERE` clause was _not_ `WHERE name='%@'` (like you might have to do if you built a SQL statement using `NSString` method `stringWithFormat`), but rather simply `WHERE name=%@`.
- 
- */
-
-- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
 
 
 /** Execute select statement
 /** Execute select statement
 
 

+ 0 - 147
src/fmdb/FMDatabase.m

@@ -618,126 +618,6 @@ - (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
     }
     }
 }
 }
 
 
-- (void)extractSQL:(NSString *)sql argumentsList:(va_list)args intoString:(NSMutableString *)cleanedSQL arguments:(NSMutableArray *)arguments {
-    
-    NSUInteger length = [sql length];
-    unichar last = '\0';
-    for (NSUInteger i = 0; i < length; ++i) {
-        id arg = nil;
-        unichar current = [sql characterAtIndex:i];
-        unichar add = current;
-        if (last == '%') {
-            switch (current) {
-                case '@':
-                    arg = va_arg(args, id);
-                    break;
-                case 'c':
-                    // warning: second argument to 'va_arg' is of promotable type 'char'; this va_arg has undefined behavior because arguments will be promoted to 'int'
-                    arg = [NSString stringWithFormat:@"%c", va_arg(args, int)];
-                    break;
-                case 's':
-                    arg = [NSString stringWithUTF8String:va_arg(args, char*)];
-                    break;
-                case 'd':
-                case 'D':
-                case 'i':
-                    arg = [NSNumber numberWithInt:va_arg(args, int)];
-                    break;
-                case 'u':
-                case 'U':
-                    arg = [NSNumber numberWithUnsignedInt:va_arg(args, unsigned int)];
-                    break;
-                case 'h':
-                    i++;
-                    if (i < length && [sql characterAtIndex:i] == 'i') {
-                        //  warning: second argument to 'va_arg' is of promotable type 'short'; this va_arg has undefined behavior because arguments will be promoted to 'int'
-                        arg = [NSNumber numberWithShort:(short)(va_arg(args, int))];
-                    }
-                    else if (i < length && [sql characterAtIndex:i] == 'u') {
-                        // warning: second argument to 'va_arg' is of promotable type 'unsigned short'; this va_arg has undefined behavior because arguments will be promoted to 'int'
-                        arg = [NSNumber numberWithUnsignedShort:(unsigned short)(va_arg(args, uint))];
-                    }
-                    else {
-                        i--;
-                    }
-                    break;
-                case 'q':
-                    i++;
-                    if (i < length && [sql characterAtIndex:i] == 'i') {
-                        arg = [NSNumber numberWithLongLong:va_arg(args, long long)];
-                    }
-                    else if (i < length && [sql characterAtIndex:i] == 'u') {
-                        arg = [NSNumber numberWithUnsignedLongLong:va_arg(args, unsigned long long)];
-                    }
-                    else {
-                        i--;
-                    }
-                    break;
-                case 'f':
-                    arg = [NSNumber numberWithDouble:va_arg(args, double)];
-                    break;
-                case 'g':
-                    // warning: second argument to 'va_arg' is of promotable type 'float'; this va_arg has undefined behavior because arguments will be promoted to 'double'
-                    arg = [NSNumber numberWithFloat:(float)(va_arg(args, double))];
-                    break;
-                case 'l':
-                    i++;
-                    if (i < length) {
-                        unichar next = [sql characterAtIndex:i];
-                        if (next == 'l') {
-                            i++;
-                            if (i < length && [sql characterAtIndex:i] == 'd') {
-                                //%lld
-                                arg = [NSNumber numberWithLongLong:va_arg(args, long long)];
-                            }
-                            else if (i < length && [sql characterAtIndex:i] == 'u') {
-                                //%llu
-                                arg = [NSNumber numberWithUnsignedLongLong:va_arg(args, unsigned long long)];
-                            }
-                            else {
-                                i--;
-                            }
-                        }
-                        else if (next == 'd') {
-                            //%ld
-                            arg = [NSNumber numberWithLong:va_arg(args, long)];
-                        }
-                        else if (next == 'u') {
-                            //%lu
-                            arg = [NSNumber numberWithUnsignedLong:va_arg(args, unsigned long)];
-                        }
-                        else {
-                            i--;
-                        }
-                    }
-                    else {
-                        i--;
-                    }
-                    break;
-                default:
-                    // something else that we can't interpret. just pass it on through like normal
-                    break;
-            }
-        }
-        else if (current == '%') {
-            // percent sign; skip this character
-            add = '\0';
-        }
-        
-        if (arg != nil) {
-            [cleanedSQL appendString:@"?"];
-            [arguments addObject:arg];
-        }
-        else if (add == (unichar)'@' && last == (unichar) '%') {
-            [cleanedSQL appendFormat:@"NULL"];
-        }
-        else if (add != '\0') {
-            [cleanedSQL appendFormat:@"%C", add];
-        }
-        last = current;
-    }
-}
-
 #pragma mark Execute queries
 #pragma mark Execute queries
 
 
 - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments {
 - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments {
@@ -900,19 +780,6 @@ - (FMResultSet *)executeQuery:(NSString*)sql, ... {
     return result;
     return result;
 }
 }
 
 
-- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... {
-    va_list args;
-    va_start(args, format);
-    
-    NSMutableString *sql = [NSMutableString stringWithCapacity:[format length]];
-    NSMutableArray *arguments = [NSMutableArray array];
-    [self extractSQL:format argumentsList:args intoString:sql arguments:arguments];
-    
-    va_end(args);
-    
-    return [self executeQuery:sql withArgumentsInArray:arguments];
-}
-
 - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments {
 - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments {
     return [self executeQuery:sql withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
     return [self executeQuery:sql withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
 }
 }
@@ -1175,20 +1042,6 @@ - (BOOL)executeUpdate:(NSString*)sql withVAList:(va_list)args {
     return [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:nil orVAList:args];
     return [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:nil orVAList:args];
 }
 }
 
 
-- (BOOL)executeUpdateWithFormat:(NSString*)format, ... {
-    va_list args;
-    va_start(args, format);
-    
-    NSMutableString *sql      = [NSMutableString stringWithCapacity:[format length]];
-    NSMutableArray *arguments = [NSMutableArray array];
-    
-    [self extractSQL:format argumentsList:args intoString:sql arguments:arguments];
-    
-    va_end(args);
-    
-    return [self executeUpdate:sql withArgumentsInArray:arguments];
-}
-
 
 
 int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names); // shhh clang.
 int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names); // shhh clang.
 int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names) {
 int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names) {

+ 0 - 65
src/sample/main.m

@@ -685,71 +685,6 @@ int main (int argc, const char * argv[]) {
     }
     }
     
     
     
     
-    {
-        FMDBQuickCheck([db executeUpdate:@"create table t5 (a text, b int, c blob, d text, e text)"]);
-        FMDBQuickCheck(([db executeUpdateWithFormat:@"insert into t5 values (%s, %d, %@, %c, %lld)", "text", 42, @"BLOB", 'd', 12345678901234ll]));
-        
-        rs = [db executeQueryWithFormat:@"select * from t5 where a = %s and a = %@ and b = %d", "text", @"text", 42];
-        FMDBQuickCheck((rs != nil));
-        
-        [rs next];
-        
-        FMDBQuickCheck([[rs stringForColumn:@"a"] isEqualToString:@"text"]);
-        FMDBQuickCheck(([rs intForColumn:@"b"] == 42));
-        FMDBQuickCheck([[rs stringForColumn:@"c"] isEqualToString:@"BLOB"]);
-        FMDBQuickCheck([[rs stringForColumn:@"d"] isEqualToString:@"d"]);
-        FMDBQuickCheck(([rs longLongIntForColumn:@"e"] == 12345678901234));
-        
-        [rs close];
-    }
-    
-    
-    
-    {
-        FMDBQuickCheck([db executeUpdate:@"create table t55 (a text, b int, c float)"]);
-        short testShort = -4;
-        float testFloat = 5.5;
-        FMDBQuickCheck(([db executeUpdateWithFormat:@"insert into t55 values (%c, %hi, %g)", 'a', testShort, testFloat]));
-        
-        unsigned short testUShort = 6;
-        FMDBQuickCheck(([db executeUpdateWithFormat:@"insert into t55 values (%c, %hu, %g)", 'a', testUShort, testFloat]));
-        
-        
-        rs = [db executeQueryWithFormat:@"select * from t55 where a = %s order by 2", "a"];
-        FMDBQuickCheck((rs != nil));
-        
-        [rs next];
-        
-        FMDBQuickCheck([[rs stringForColumn:@"a"] isEqualToString:@"a"]);
-        FMDBQuickCheck(([rs intForColumn:@"b"] == -4));
-        FMDBQuickCheck([[rs stringForColumn:@"c"] isEqualToString:@"5.5"]);
-        
-        
-        [rs next];
-        
-        FMDBQuickCheck([[rs stringForColumn:@"a"] isEqualToString:@"a"]);
-        FMDBQuickCheck(([rs intForColumn:@"b"] == 6));
-        FMDBQuickCheck([[rs stringForColumn:@"c"] isEqualToString:@"5.5"]);
-        
-        [rs close];
-        
-    }
-    
-    {
-        FMDBQuickCheck([db executeUpdate:@"create table tatwhat (a text)"]);
-        
-        BOOL worked = [db executeUpdateWithFormat:@"insert into tatwhat values(%@)", nil];
-        
-        FMDBQuickCheck(worked);
-        
-        rs = [db executeQueryWithFormat:@"select * from tatwhat"];
-        FMDBQuickCheck((rs != nil));
-        FMDBQuickCheck(([rs next]));
-        FMDBQuickCheck([rs columnIndexIsNull:0]);
-        
-        FMDBQuickCheck((![rs next]));
-        
-    }
     
     
     
     
     {
     {