Add support for retrieving columns

FossilOrigin-Name: 727a6838a567ab678c28cb0dfc2b95aaa4fa9b5af7b29e9f9833ffd81cea8d9d
This commit is contained in:
Jonathan Schleifer 2020-10-01 23:43:39 +00:00
parent f7aa1209f7
commit 5967ebf3b7
3 changed files with 82 additions and 2 deletions

View file

@ -41,7 +41,10 @@ OF_ASSUME_NONNULL_BEGIN
- (void)bindWithDictionary: - (void)bindWithDictionary:
(OFDictionary OF_GENERIC(OFString *, id) *)dictionary; (OFDictionary OF_GENERIC(OFString *, id) *)dictionary;
- (void)clearBindings; - (void)clearBindings;
- (void)step; - (id)objectForColumn: (size_t)column;
- (size_t)columnCount;
- (OFString *)nameForColumn: (size_t)column;
- (bool)step;
- (void)reset; - (void)reset;
@end @end

View file

@ -161,7 +161,7 @@ bindObject(SL3PreparedStatement *statement, int column, id object)
errorCode: code]; errorCode: code];
} }
- (void)step - (bool)step
{ {
int code = sqlite3_step(_stmt); int code = sqlite3_step(_stmt);
@ -169,6 +169,52 @@ bindObject(SL3PreparedStatement *statement, int column, id object)
@throw [SL3ExecuteStatementFailedException @throw [SL3ExecuteStatementFailedException
exceptionWithStatement: self exceptionWithStatement: self
errorCode: code]; errorCode: code];
return (code == SQLITE_ROW);
}
- (id)objectForColumn: (size_t)column
{
if (column > INT_MAX)
@throw [OFOutOfRangeException exception];
switch (sqlite3_column_type(_stmt, column)) {
case SQLITE_INTEGER:
return [OFNumber numberWithLongLong:
sqlite3_column_int64(_stmt, column)];
case SQLITE_FLOAT:
return [OFNumber numberWithDouble:
sqlite3_column_double(_stmt, column)];
case SQLITE_TEXT:
return [OFString stringWithUTF8String:
(const char *)sqlite3_column_text(_stmt, column)];
case SQLITE_BLOB:
return [OFData
dataWithItems: sqlite3_column_blob(_stmt, column)
count: sqlite3_column_bytes(_stmt, column)];
case SQLITE_NULL:
return [OFNull null];
default:
OF_ENSURE(0);
}
}
- (size_t)columnCount
{
return sqlite3_column_count(_stmt);
}
- (OFString *)nameForColumn: (size_t)column
{
const char *name;
if (column > [self columnCount])
@throw [OFOutOfRangeException exception];
if ((name = sqlite3_column_name(_stmt, column)) == NULL)
@throw [OFOutOfMemoryException exception];
return [OFString stringWithUTF8String: name];
} }
- (void)reset - (void)reset

View file

@ -64,6 +64,37 @@ OF_APPLICATION_DELEGATE(Tests)
nil]]; nil]];
[stmt step]; [stmt step];
stmt = [conn prepareStatement: @"SELECT * FROM test"];
for (size_t i = 0; [stmt step]; i++) {
OF_ENSURE([stmt columnCount] == 3);
OF_ENSURE([[stmt nameForColumn: 0] isEqual: @"a"]);
OF_ENSURE([[stmt nameForColumn: 1] isEqual: @"b"]);
OF_ENSURE([[stmt nameForColumn: 2] isEqual: @"c"]);
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]]);
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]]);
break;
default:
OF_ENSURE(0);
}
}
[OFApplication terminate]; [OFApplication terminate];
} }
@end @end