Skip to content

Loading…

executeQuery & executeUpdate with va_list as parameter #107

Closed
wants to merge 1 commit into from

2 participants

@ibrahimennafaa

In general, if you're writing variadic functions (that is, functions which take a variable number of arguments) in C, you should write two versions of each function: one which takes an ellipsis (...), and one which takes a va_list. This will allow us to call executeQuery & executeUpdate methods from a function which takes an ellipsis (...).

Ibrahim Ennafaa executeQuery and executeUpdate with va_list as parameter
Allow to call executeQuery and executeUpdate passing a va_list
parameter. This will allow us to call thos two methods from a function
which takes an ellipsis (...).
58e1360
@ccgus
Owner

Can you write a simple function + test in main.m that would exercise this patch? I'd like to see it in use, and make sure it gets tested along with the other stuff before adding it to FMDB

@ibrahimennafaa

Hi,

I couldn't write a test function as I didn't understand your main test function...

In my project I have a wrapper of FMDB with two principal functions:

//**********************************************
// FETCH ALL FROM QUERY
//**********************************************
/!
@method - (NSArray
)fetchAllFromQuery: (NSString*) query, ...
@abstract Fetch all rows returned by the query
@result Return an array of dictionary result
*/

  • (NSArray)fetchAllFromQuery: (NSString) query, ... {
    va_list args;
    va_start(args, query);
    FMResultSet *resultSet = [database executeQuery:query withVAList:args];
    NSMutableArray *resultArray = [NSMutableArray array];

    while ([resultSet next]) {
    [resultArray addObject: [resultSet resultDictionary]];
    }

    va_end(args);

    return resultArray;
    }

//**********************************************
// UPDATE QUERY
//**********************************************
/!
@method - (BOOL)updateQuery: (NSString
) query, ...
@abstract execute any sort of SQL statement which is not a SELECT
@result YES if succeed
*/

  • (BOOL)updateQuery: (NSString*) query, ... {
    va_list args;
    va_start(args, query);
    BOOL executeSuccess = [database executeUpdate:query withVAList:args];
    va_end(args);

    return executeSuccess;
    }

They both use the functions I wanted to add in FMDB. You can see why it is useful and take this code to add unit tests into your main file. I hope it is clear enough. And thank you again for the amazing work on FMDB.

Cheers!

@ccgus
Owner

OK, I see how you are using it. At this time, I'm going to keep it out of the main distribution. However- you should be able to add it with no problem using a category on FMDB.

@ccgus
Owner

With a little push from Marco Arment, I've gotten this added to FMDB. Thanks!

@ccgus ccgus closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 19, 2012
  1. executeQuery and executeUpdate with va_list as parameter

    Ibrahim Ennafaa committed
    Allow to call executeQuery and executeUpdate passing a va_list
    parameter. This will allow us to call thos two methods from a function
    which takes an ellipsis (...).
Showing with 14 additions and 0 deletions.
  1. +2 −0 src/FMDatabase.h
  2. +12 −0 src/FMDatabase.m
View
2 src/FMDatabase.h
@@ -105,11 +105,13 @@
- (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ...;
- (BOOL)executeUpdate:(NSString*)sql, ...;
+- (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;
- (BOOL)executeUpdateWithFormat:(NSString *)format, ...;
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
- (FMResultSet *)executeQuery:(NSString*)sql, ...;
+- (FMResultSet *)executeQuery:(NSString*)sql withVAList: (va_list)args;
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...;
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;
- (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments;
View
12 src/FMDatabase.m
@@ -665,6 +665,12 @@ - (FMResultSet *)executeQuery:(NSString*)sql, ... {
return result;
}
+- (FMResultSet *)executeQuery:(NSString*)sql withVAList: (va_list)args {
+ id result = [self executeQuery:sql withArgumentsInArray:nil orDictionary:nil orVAList:args];
+
+ return result;
+}
+
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... {
va_list args;
va_start(args, format);
@@ -908,6 +914,12 @@ - (BOOL)executeUpdate:(NSString*)sql, ... {
return result;
}
+- (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args {
+ BOOL result = [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:nil orVAList:args];
+
+ return result;
+}
+
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments {
return [self executeUpdate:sql error:nil withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
}
Something went wrong with that request. Please try again.