Exclude fields that are NULL from the result.

FossilOrigin-Name: 6d068f6ae7e9cc62599ef05dfea2ae44890d8f4a3a142724180b165ce59d06ff
This commit is contained in:
Jonathan Schleifer 2012-10-04 22:18:31 +00:00
parent f640a29849
commit f232e248cb
2 changed files with 25 additions and 4 deletions

View file

@ -46,7 +46,13 @@
- (size_t)count - (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 - (id)objectForKey: (id)key
@ -58,6 +64,9 @@
else else
col = PQfnumber(res, [key UTF8String]); col = PQfnumber(res, [key UTF8String]);
if (PQgetisnull(res, row, col))
return nil;
return [OFString stringWithUTF8String: PQgetvalue(res, row, col)]; return [OFString stringWithUTF8String: PQgetvalue(res, row, col)];
} }
@ -102,6 +111,12 @@
if (pos >= count) if (pos >= count)
return nil; return nil;
while (pos < count && PQgetisnull(res, row, pos))
pos++;
if (pos >= count)
return nil;
return [OFString stringWithUTF8String: PQfname(res, pos++)]; return [OFString stringWithUTF8String: PQfname(res, pos++)];
} }
@end @end
@ -112,6 +127,12 @@
if (pos >= count) if (pos >= count)
return nil; return nil;
while (pos < count && PQgetisnull(res, row, pos))
pos++;
if (pos >= count)
return nil;
return [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]; return [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)];
} }
@end @end

6
test.m
View file

@ -31,9 +31,9 @@ OF_APPLICATION_DELEGATE(Test)
[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!"]]; parameters: @[@"1", @"foo", @"Hallo Welt!"]];
[connection executeCommand: @"INSERT INTO test (id, name, content) " [connection executeCommand: @"INSERT INTO test (id, content) "
@"VALUES($1, $2, $3)" @"VALUES($1, $2)"
parameters: @[@"2", @"bla", @"Blup!!"]]; parameters: @[@"2", @"Blup!!"]];
result = [connection executeCommand: @"SELECT * FROM test"]; result = [connection executeCommand: @"SELECT * FROM test"];
of_log(@"%@", result); of_log(@"%@", result);