From 9f22b628f21864d9910607374e190a460ff5c7ce Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 1 Oct 2020 23:53:00 +0000 Subject: [PATCH] SL3PreparedStatement: Add -[row{Array,Dictionary}] FossilOrigin-Name: 7a52167702278bdd7c5f3aac48771cba8ac5a77abfac4d1664b05a507fe8236e --- src/SL3PreparedStatement.h | 4 +++- src/SL3PreparedStatement.m | 28 ++++++++++++++++++++++++++++ tests/Tests.m | 36 ++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 15 deletions(-) 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];