Add -[insertRows:intoTable:].

FossilOrigin-Name: 7b335b3af81d4506f2842748671541073f4c455b4a0ce8a108eab0d43280f3ad
This commit is contained in:
Jonathan Schleifer 2012-10-06 21:02:01 +00:00
parent f9be9ea547
commit 728b3d1e2a
4 changed files with 97 additions and 4 deletions

View file

@ -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 = "<absolute>"; };
/* 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 = "<group>";
@ -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;
};

View file

@ -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

View file

@ -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;

2
test.m
View file

@ -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);