From 6e1eda2f24a5e9b90a0579d70283119f5e16eed0 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Fri, 5 Oct 2012 20:17:44 +0000 Subject: [PATCH] Nicer API. -[executeCommand:parameters:] is now a variadic function instead of taking an array as argument. FossilOrigin-Name: d2fe40f16065b5ccf5ba135af1ec95f09ed2f8ee96e661a7a7d085da833e972e --- PGConnection.h | 2 +- PGConnection.m | 19 ++++++++++++------- test.m | 7 +++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/PGConnection.h b/PGConnection.h index 3026b5c..039a646 100644 --- a/PGConnection.h +++ b/PGConnection.h @@ -20,6 +20,6 @@ - (void)reset; - (PGResult*)executeCommand: (OFString*)command; - (PGResult*)executeCommand: (OFString*)command - parameters: (OFArray*)parameters; + parameters: (id)firstParameter, ...; - (PGconn*)PG_connection; @end diff --git a/PGConnection.m b/PGConnection.m index 31bb386..050d89c 100644 --- a/PGConnection.m +++ b/PGConnection.m @@ -78,28 +78,33 @@ } - (PGResult*)executeCommand: (OFString*)command - parameters: (OFArray*)parameters_ + parameters: (id)parameter, ... { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; PGresult *result; const char **values; + va_list args, args2; + size_t argsCount; + + va_start(args, parameter); + va_copy(args2, args); + + for (argsCount = 1; va_arg(args2, id) != nil; argsCount++); values = [self allocMemoryWithSize: sizeof(*values) - count: [parameters_ count]]; + count: argsCount]; @try { - OFEnumerator *enumerator = [parameters_ objectEnumerator]; size_t i = 0; - id parameter; - while ((parameter = [enumerator nextObject]) != nil) { + do { if ([parameter isKindOfClass: [OFNull class]]) values[i++] = NULL; else values[i++] = [parameter UTF8String]; - } + } while ((parameter = va_arg(args, id)) != nil); result = PQexecParams(conn, [command UTF8String], - [parameters_ count], NULL, values, NULL, NULL, 0); + argsCount, NULL, values, NULL, NULL, 0); } @finally { [self freeMemory: values]; } diff --git a/test.m b/test.m index 8b17b35..5fb262d 100644 --- a/test.m +++ b/test.m @@ -30,15 +30,18 @@ OF_APPLICATION_DELEGATE(Test) @")"]; [connection executeCommand: @"INSERT INTO test (id, name, content) " @"VALUES($1, $2, $3)" - parameters: @[@"1", @"foo", @"Hallo Welt!"]]; + parameters: @"1", @"foo", @"Hallo Welt!", nil]; [connection executeCommand: @"INSERT INTO test (id, content) " @"VALUES($1, $2)" - parameters: @[@"2", @"Blup!!"]]; + parameters: @"2", @"Blup!!", nil]; result = [connection executeCommand: @"SELECT * FROM test"]; of_log(@"%@", result); of_log(@"JSON: %@", [result JSONRepresentation]); + result = [connection executeCommand: @"SELECT COUNT(*) FROM test"]; + of_log(@"%@", result); + [OFApplication terminate]; } @end