Convert types using the result of PQftypes().

FossilOrigin-Name: 4a2b1fff7ecbdccaaff50842a27a4921f3e336546b07e9b6ebeb440adb29ef60
This commit is contained in:
Jonathan Schleifer 2012-10-07 22:45:36 +00:00
parent 728b3d1e2a
commit d22558fdcc
4 changed files with 46 additions and 9 deletions

View file

@ -34,6 +34,7 @@ all:
${LIBS} \ ${LIBS} \
${SRCS} ${SRCS}
.PHONY: test
test: test:
@objfw-compile \ @objfw-compile \
-o test \ -o test \

View file

@ -99,7 +99,20 @@
do { do {
if ([parameter isKindOfClass: [OFNull class]]) if ([parameter isKindOfClass: [OFNull class]])
values[i++] = NULL; values[i++] = NULL;
else if ([parameter isKindOfClass: [OFNumber class]]) {
switch ([parameter type]) {
case OF_NUMBER_BOOL:
if ([parameter boolValue])
values[i++] = "t";
else else
values[i++] = "f";
break;
default:
values[i++] = [[parameter description]
UTF8String];
break;
}
} else
values[i++] = [parameter UTF8String]; values[i++] = [parameter UTF8String];
} while ((parameter = va_arg(args, id)) != nil); } while ((parameter = va_arg(args, id)) != nil);

View file

@ -1,5 +1,25 @@
#import "PGResultRow.h" #import "PGResultRow.h"
static id
convert_type(PGresult *res, int col, OFString *str)
{
switch (PQftype(res, col)) {
case 16: /* BOOLOID */
if ([str isEqual: @"t"])
return [OFNumber numberWithBool: YES];
else
return [OFNumber numberWithBool: NO];
case 21: /* INT2OID */
return [OFNumber numberWithInt16: (int16_t)[str decimalValue]];
case 23: /* INT4OID */
return [OFNumber numberWithInt32: (int32_t)[str decimalValue]];
case 20: /* INT8OID */
return [OFNumber numberWithInt64: (int64_t)[str decimalValue]];
}
return str;
}
@interface PGResultRowEnumerator: OFEnumerator @interface PGResultRowEnumerator: OFEnumerator
{ {
PGResult *result; PGResult *result;
@ -67,7 +87,8 @@
if (PQgetisnull(res, row, col)) if (PQgetisnull(res, row, col))
return nil; return nil;
return [OFString stringWithUTF8String: PQgetvalue(res, row, col)]; return convert_type(res, col,
[OFString stringWithUTF8String: PQgetvalue(res, row, col)]);
} }
- (OFEnumerator*)keyEnumerator - (OFEnumerator*)keyEnumerator
@ -133,6 +154,7 @@
if (pos >= count) if (pos >= count)
return nil; return nil;
return [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]; return convert_type(res, pos,
[OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]);
} }
@end @end

13
test.m
View file

@ -26,14 +26,15 @@ OF_APPLICATION_DELEGATE(Test)
[connection executeCommand: @"CREATE TABLE test (" [connection executeCommand: @"CREATE TABLE test ("
@" id integer," @" id integer,"
@" name varchar(255)," @" name varchar(255),"
@" content text" @" content text,"
@" success boolean"
@")"]; @")"];
[connection executeCommand: @"INSERT INTO test (id, name, content) " [connection executeCommand: @"INSERT INTO test (id, name, content) "
@"VALUES($1, $2, $3)" @"VALUES ($1, $2, $3)"
parameters: @"1", @"foo", @"Hallo Welt!", nil]; parameters: @1, @"foo", @"Hallo Welt!", nil];
[connection executeCommand: @"INSERT INTO test (id, content) " [connection executeCommand: @"INSERT INTO test (id, content, success) "
@"VALUES($1, $2)" @"VALUES ($1, $2, $3)"
parameters: @"2", @"Blup!!", nil]; parameters: @2, @2, @YES];
[connection insertRow: @{ @"content": @"Hallo!", @"name": @"foo" } [connection insertRow: @{ @"content": @"Hallo!", @"name": @"foo" }
intoTable: @"test"]; intoTable: @"test"];