diff --git a/Makefile b/Makefile index a86c213..63beed8 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ all: ${LIBS} \ ${SRCS} +.PHONY: test test: @objfw-compile \ -o test \ diff --git a/PGConnection.m b/PGConnection.m index 82177ac..d2cb292 100644 --- a/PGConnection.m +++ b/PGConnection.m @@ -99,7 +99,20 @@ do { if ([parameter isKindOfClass: [OFNull class]]) values[i++] = NULL; - else + else if ([parameter isKindOfClass: [OFNumber class]]) { + switch ([parameter type]) { + case OF_NUMBER_BOOL: + if ([parameter boolValue]) + values[i++] = "t"; + else + values[i++] = "f"; + break; + default: + values[i++] = [[parameter description] + UTF8String]; + break; + } + } else values[i++] = [parameter UTF8String]; } while ((parameter = va_arg(args, id)) != nil); diff --git a/PGResultRow.m b/PGResultRow.m index 94d9b84..880595c 100644 --- a/PGResultRow.m +++ b/PGResultRow.m @@ -1,5 +1,25 @@ #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 { PGResult *result; @@ -67,7 +87,8 @@ if (PQgetisnull(res, row, col)) return nil; - return [OFString stringWithUTF8String: PQgetvalue(res, row, col)]; + return convert_type(res, col, + [OFString stringWithUTF8String: PQgetvalue(res, row, col)]); } - (OFEnumerator*)keyEnumerator @@ -133,6 +154,7 @@ if (pos >= count) return nil; - return [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]; + return convert_type(res, pos, + [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]); } @end diff --git a/test.m b/test.m index 4b77047..a3e354d 100644 --- a/test.m +++ b/test.m @@ -26,14 +26,15 @@ OF_APPLICATION_DELEGATE(Test) [connection executeCommand: @"CREATE TABLE test (" @" id integer," @" name varchar(255)," - @" content text" + @" content text," + @" success boolean" @")"]; [connection executeCommand: @"INSERT INTO test (id, name, content) " - @"VALUES($1, $2, $3)" - parameters: @"1", @"foo", @"Hallo Welt!", nil]; - [connection executeCommand: @"INSERT INTO test (id, content) " - @"VALUES($1, $2)" - parameters: @"2", @"Blup!!", nil]; + @"VALUES ($1, $2, $3)" + parameters: @1, @"foo", @"Hallo Welt!", nil]; + [connection executeCommand: @"INSERT INTO test (id, content, success) " + @"VALUES ($1, $2, $3)" + parameters: @2, @2, @YES]; [connection insertRow: @{ @"content": @"Hallo!", @"name": @"foo" } intoTable: @"test"];