diff --git a/src/SL3PreparedStatement.h b/src/SL3PreparedStatement.h index 73480fa..69d11e4 100644 --- a/src/SL3PreparedStatement.h +++ b/src/SL3PreparedStatement.h @@ -41,10 +41,12 @@ OF_ASSUME_NONNULL_BEGIN - (void)bindWithDictionary: (OFDictionary OF_GENERIC(OFString *, id) *)dictionary; - (void)clearBindings; +- (bool)step; - (id)objectForColumn: (size_t)column; - (size_t)columnCount; - (OFString *)nameForColumn: (size_t)column; -- (bool)step; +- (OFArray *)rowArray; +- (OFDictionary OF_GENERIC(OFString *, id) *)rowDictionary; - (void)reset; @end diff --git a/src/SL3PreparedStatement.m b/src/SL3PreparedStatement.m index e979092..0c85f43 100644 --- a/src/SL3PreparedStatement.m +++ b/src/SL3PreparedStatement.m @@ -217,6 +217,34 @@ bindObject(SL3PreparedStatement *statement, int column, id object) return [OFString stringWithUTF8String: name]; } +- (OFArray *)rowArray +{ + size_t count = [self columnCount]; + OFMutableArray *array = [OFMutableArray arrayWithCapacity: count]; + + for (size_t i = 0; i < count; i++) + [array addObject: [self objectForColumn: i]]; + + [array makeImmutable]; + + return array; +} + +- (OFDictionary OF_GENERIC(OFString *, id) *)rowDictionary +{ + size_t count = [self columnCount]; + OFMutableDictionary *dictionary = + [OFMutableDictionary dictionaryWithCapacity: count]; + + for (size_t i = 0; i < count; i++) + [dictionary setObject: [self objectForColumn: i] + forKey: [self nameForColumn: i]]; + + [dictionary makeImmutable]; + + return dictionary; +} + - (void)reset { int code = sqlite3_reset(_stmt); diff --git a/tests/Tests.m b/tests/Tests.m index 6c224ac..cbd725a 100644 --- a/tests/Tests.m +++ b/tests/Tests.m @@ -66,6 +66,10 @@ OF_APPLICATION_DELEGATE(Tests) stmt = [conn prepareStatement: @"SELECT * FROM test"]; for (size_t i = 0; [stmt step]; i++) { + OFNumber *a; + OFString *b; + OFData *c; + OF_ENSURE([stmt columnCount] == 3); OF_ENSURE([[stmt nameForColumn: 0] isEqual: @"a"]); OF_ENSURE([[stmt nameForColumn: 1] isEqual: @"b"]); @@ -73,26 +77,30 @@ OF_APPLICATION_DELEGATE(Tests) switch (i) { case 0: - OF_ENSURE([[stmt objectForColumn: 0] - isEqual: [OFNumber numberWithInt: 5]]); - OF_ENSURE([[stmt objectForColumn: 1] - isEqual: @"String"]); - OF_ENSURE([[stmt objectForColumn: 2] - isEqual: [OFData dataWithItems: "abc" - count: 3]]); + a = [OFNumber numberWithInt: 5]; + b = @"String"; + c = [OFData dataWithItems: "abc" + count: 3]; break; case 1: - OF_ENSURE([[stmt objectForColumn: 0] - isEqual: [OFNumber numberWithInt: 7]]); - OF_ENSURE([[stmt objectForColumn: 1] - isEqual: @"Test"]); - OF_ENSURE([[stmt objectForColumn: 2] - isEqual: [OFData dataWithItems: "xyz" - count: 3]]); + a = [OFNumber numberWithInt: 7]; + b = @"Test"; + c = [OFData dataWithItems: "xyz" + count: 3]; break; default: OF_ENSURE(0); } + + OF_ENSURE([[stmt objectForColumn: 0] isEqual: a]); + OF_ENSURE([[stmt objectForColumn: 1] isEqual: b]); + OF_ENSURE([[stmt objectForColumn: 2] isEqual: c]); + + OF_ENSURE([[stmt rowArray] isEqual: ([OFArray arrayWithObjects: + a, b, c, nil])]); + OF_ENSURE([[stmt rowDictionary] isEqual: + ([OFDictionary dictionaryWithKeysAndObjects: + @"a", a, @"b", b, @"c", c, nil])]); } [OFApplication terminate];