From 74aa01c7cd114761d2e8a3f7484486361ff803f5 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Tue, 30 Oct 2012 22:33:40 +0000 Subject: [PATCH] Implement fast enumeration. FossilOrigin-Name: 2da674855b3d179b7524f02132638c404a62952fce04cc7a97907d133939c826 --- PGResultRow.m | 32 ++++++++++++++++++++++++++++++++ test.m | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/PGResultRow.m b/PGResultRow.m index 15da69b..7540707 100644 --- a/PGResultRow.m +++ b/PGResultRow.m @@ -108,6 +108,38 @@ convert_type(PGresult *res, int col, OFString *str) initWithResult: result row: row] autorelease]; } + +- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state + objects: (id*)objects + count: (int)count +{ + int i, j; + + if (state->extra[0] == 0) { + state->extra[0] = 1; + state->extra[1] = PQnfields(res); + } + + if (count > SIZE_MAX - state->state) + @throw [OFOutOfRangeException exceptionWithClass: [self class]]; + + if (state->state + count > state->extra[1]) + count = state->extra[1] - state->state; + + for (i = j = 0; i < count; i++) { + if (PQgetisnull(res, row, state->state + i)) + continue; + + objects[j++] = [OFString stringWithUTF8String: + PQfname(res, state->state + i)]; + } + + state->state += count; + state->itemsPtr = objects; + state->mutationsPtr = (unsigned long*)self; + + return j; +} @end @implementation PGResultRowEnumerator diff --git a/test.m b/test.m index 18cfa02..d3bbe89 100644 --- a/test.m +++ b/test.m @@ -42,6 +42,10 @@ OF_APPLICATION_DELEGATE(Test) of_log(@"%@", result); of_log(@"JSON: %@", [result JSONRepresentation]); + for (id row in result) + for (id col in row) + of_log(@"%@", col); + result = [connection executeCommand: @"SELECT COUNT(*) FROM test"]; of_log(@"%@", result);