diff --git a/PGResultRow.m b/PGResultRow.m index 2c37264..476ef5a 100644 --- a/PGResultRow.m +++ b/PGResultRow.m @@ -46,7 +46,13 @@ - (size_t)count { - return PQnfields(res); + size_t i, count, fields = PQnfields(res); + + for (i = count = 0; i < fields; i++) + if (!PQgetisnull(res, row, i)) + count++; + + return count; } - (id)objectForKey: (id)key @@ -58,6 +64,9 @@ else col = PQfnumber(res, [key UTF8String]); + if (PQgetisnull(res, row, col)) + return nil; + return [OFString stringWithUTF8String: PQgetvalue(res, row, col)]; } @@ -102,6 +111,12 @@ if (pos >= count) return nil; + while (pos < count && PQgetisnull(res, row, pos)) + pos++; + + if (pos >= count) + return nil; + return [OFString stringWithUTF8String: PQfname(res, pos++)]; } @end @@ -112,6 +127,12 @@ if (pos >= count) return nil; + while (pos < count && PQgetisnull(res, row, pos)) + pos++; + + if (pos >= count) + return nil; + return [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]; } @end diff --git a/test.m b/test.m index 2fab746..8b17b35 100644 --- a/test.m +++ b/test.m @@ -31,9 +31,9 @@ OF_APPLICATION_DELEGATE(Test) [connection executeCommand: @"INSERT INTO test (id, name, content) " @"VALUES($1, $2, $3)" parameters: @[@"1", @"foo", @"Hallo Welt!"]]; - [connection executeCommand: @"INSERT INTO test (id, name, content) " - @"VALUES($1, $2, $3)" - parameters: @[@"2", @"bla", @"Blup!!"]]; + [connection executeCommand: @"INSERT INTO test (id, content) " + @"VALUES($1, $2)" + parameters: @[@"2", @"Blup!!"]]; result = [connection executeCommand: @"SELECT * FROM test"]; of_log(@"%@", result);