SL3PreparedStatement: Add -[row{Array,Dictionary}]

FossilOrigin-Name: 7a52167702278bdd7c5f3aac48771cba8ac5a77abfac4d1664b05a507fe8236e
This commit is contained in:
Jonathan Schleifer 2020-10-01 23:53:00 +00:00
parent 5967ebf3b7
commit 9f22b628f2
3 changed files with 53 additions and 15 deletions

View file

@ -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

View file

@ -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);

View file

@ -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];