From 728b3d1e2af66d1093d8154d8d5c702e3bda3396 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Sat, 6 Oct 2012 21:02:01 +0000 Subject: [PATCH] Add -[insertRows:intoTable:]. FossilOrigin-Name: 7b335b3af81d4506f2842748671541073f4c455b4a0ce8a108eab0d43280f3ad --- ObjPgSQL.xcodeproj/project.pbxproj | 16 ++++-- PGConnection.h | 4 ++ PGConnection.m | 79 ++++++++++++++++++++++++++++++ test.m | 2 + 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/ObjPgSQL.xcodeproj/project.pbxproj b/ObjPgSQL.xcodeproj/project.pbxproj index b273eda..72c18ca 100644 --- a/ObjPgSQL.xcodeproj/project.pbxproj +++ b/ObjPgSQL.xcodeproj/project.pbxproj @@ -21,7 +21,7 @@ 4BCC7456161F82820074ED30 /* PGConnectionFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BCC7450161F82820074ED30 /* PGConnectionFailedException.m */; }; 4BCC7457161F82820074ED30 /* PGException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BCC7451161F82820074ED30 /* PGException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BCC7458161F82820074ED30 /* PGException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BCC7452161F82820074ED30 /* PGException.m */; }; - 4BCC7460161F842F0074ED30 /* libpq.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4BCC745F161F842F0074ED30 /* libpq.dylib */; }; + 4BCC74B0162036A70074ED30 /* libpq.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4BCC74AF162036A70074ED30 /* libpq.dylib */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -40,8 +40,8 @@ 4BCC7450161F82820074ED30 /* PGConnectionFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PGConnectionFailedException.m; path = exceptions/PGConnectionFailedException.m; sourceTree = SOURCE_ROOT; }; 4BCC7451161F82820074ED30 /* PGException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PGException.h; path = exceptions/PGException.h; sourceTree = SOURCE_ROOT; }; 4BCC7452161F82820074ED30 /* PGException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PGException.m; path = exceptions/PGException.m; sourceTree = SOURCE_ROOT; }; - 4BCC745F161F842F0074ED30 /* libpq.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libpq.dylib; path = usr/lib/libpq.dylib; sourceTree = SDKROOT; }; 4BCC7464161F85DF0074ED30 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = SOURCE_ROOT; }; + 4BCC74AF162036A70074ED30 /* libpq.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libpq.dylib; path = /usr/local/lib/libpq.dylib; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -49,8 +49,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 4BCC74B0162036A70074ED30 /* libpq.dylib in Frameworks */, 4BCC743D161F82000074ED30 /* ObjFW.framework in Frameworks */, - 4BCC7460161F842F0074ED30 /* libpq.dylib in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -115,7 +115,7 @@ 4BCC7461161F843F0074ED30 /* Libraries */ = { isa = PBXGroup; children = ( - 4BCC745F161F842F0074ED30 /* libpq.dylib */, + 4BCC74AF162036A70074ED30 /* libpq.dylib */, ); name = Libraries; sourceTree = ""; @@ -281,6 +281,10 @@ FRAMEWORK_VERSION = A; GCC_PRECOMPILE_PREFIX_HEADER = NO; INFOPLIST_FILE = Info.plist; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + /usr/local/lib, + ); PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = framework; }; @@ -295,6 +299,10 @@ FRAMEWORK_VERSION = A; GCC_PRECOMPILE_PREFIX_HEADER = NO; INFOPLIST_FILE = Info.plist; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + /usr/local/lib, + ); PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = framework; }; diff --git a/PGConnection.h b/PGConnection.h index 039a646..a4b7f65 100644 --- a/PGConnection.h +++ b/PGConnection.h @@ -22,4 +22,8 @@ - (PGResult*)executeCommand: (OFString*)command parameters: (id)firstParameter, ...; - (PGconn*)PG_connection; +- (void)insertRow: (OFDictionary*)row + intoTable: (OFString*)table; +- (void)insertRows: (OFArray*)rows + intoTable: (OFString*)table; @end diff --git a/PGConnection.m b/PGConnection.m index 3e7bc53..82177ac 100644 --- a/PGConnection.m +++ b/PGConnection.m @@ -126,6 +126,85 @@ return nil; } +- (void)insertRow: (OFDictionary*)row + intoTable: (OFString*)table +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFMutableString *command; + OFEnumerator *enumerator; + const char **values; + PGresult *result; + OFString *key, *value; + size_t i, count; + + command = [OFMutableString stringWithString: @"INSERT INTO "]; + [command appendString: table]; + [command appendString: @" ("]; + + count = [row count]; + + i = 0; + enumerator = [row keyEnumerator]; + while ((key = [enumerator nextObject]) != nil) { + if (i > 0) + [command appendString: @", "]; + + [command appendString: key]; + + i++; + } + + [command appendString: @") VALUES ("]; + + values = [self allocMemoryWithSize: sizeof(*values) + count: count]; + @try { + i = 0; + enumerator = [row objectEnumerator]; + while ((value = [enumerator nextObject]) != nil) { + if (i > 0) + [command appendString: @", "]; + + values[i] = [value UTF8String]; + + [command appendFormat: @"$%zd", ++i]; + } + + [command appendString: @")"]; + + result = PQexecParams(conn, [command UTF8String], (int)count, + NULL, values, NULL, NULL, 0); + } @finally { + [self freeMemory: values]; + } + + [pool release]; + + if (PQresultStatus(result) != PGRES_COMMAND_OK) { + PQclear(result); + @throw [PGCommandFailedException + exceptionWithClass: [self class] + connection: self + command: command]; + } + + PQclear(result); +} + +- (void)insertRows: (OFArray*)rows + intoTable: (OFString*)table +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFEnumerator *enumerator = [rows objectEnumerator]; + OFDictionary *row; + + while ((row = [enumerator nextObject]) != nil) + [self insertRow: row + intoTable: table]; + + [pool release]; +} + - (PGconn*)PG_connection { return conn; diff --git a/test.m b/test.m index 5fb262d..4b77047 100644 --- a/test.m +++ b/test.m @@ -34,6 +34,8 @@ OF_APPLICATION_DELEGATE(Test) [connection executeCommand: @"INSERT INTO test (id, content) " @"VALUES($1, $2)" parameters: @"2", @"Blup!!", nil]; + [connection insertRow: @{ @"content": @"Hallo!", @"name": @"foo" } + intoTable: @"test"]; result = [connection executeCommand: @"SELECT * FROM test"]; of_log(@"%@", result);