Overview
Comment: | Add support for retrieving columns |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
727a6838a567ab678c28cb0dfc2b95aa |
User & Date: | js 2020-10-01 23:43:39 |
Context
2020-10-01
| ||
23:53 | SL3PreparedStatement: Add -[row{Array,Dictionary}] check-in: 7a52167702 user: js tags: trunk | |
23:43 | Add support for retrieving columns check-in: 727a6838a5 user: js tags: trunk | |
23:20 | Add initial tests check-in: bda6d49813 user: js tags: trunk | |
Changes
Changes to src/SL3PreparedStatement.h.
︙ | ︙ | |||
37 38 39 40 41 42 43 | sqlite3_stmt *_stmt; } - (void)bindWithArray: (OFArray *)array; - (void)bindWithDictionary: (OFDictionary OF_GENERIC(OFString *, id) *)dictionary; - (void)clearBindings; | > > > | | 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | sqlite3_stmt *_stmt; } - (void)bindWithArray: (OFArray *)array; - (void)bindWithDictionary: (OFDictionary OF_GENERIC(OFString *, id) *)dictionary; - (void)clearBindings; - (id)objectForColumn: (size_t)column; - (size_t)columnCount; - (OFString *)nameForColumn: (size_t)column; - (bool)step; - (void)reset; @end OF_ASSUME_NONNULL_END |
Changes to src/SL3PreparedStatement.m.
︙ | ︙ | |||
157 158 159 160 161 162 163 | if (code != SQLITE_OK) @throw [SL3ClearBindingsFailedException exceptionWithStatement: self errorCode: code]; } | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | if (code != SQLITE_OK) @throw [SL3ClearBindingsFailedException exceptionWithStatement: self errorCode: code]; } - (bool)step { int code = sqlite3_step(_stmt); if (code != SQLITE_DONE && code != SQLITE_ROW) @throw [SL3ExecuteStatementFailedException exceptionWithStatement: self 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 { int code = sqlite3_reset(_stmt); if (code != SQLITE_OK) |
︙ | ︙ |
Changes to tests/Tests.m.
︙ | ︙ | |||
59 60 61 62 63 64 65 66 67 68 69 | [stmt bindWithDictionary: [OFDictionary dictionaryWithKeysAndObjects: @"$a", [OFNumber numberWithInt: 7], @"$b", @"Test", @"$c", [OFData dataWithItems: "xyz" count: 3], nil]]; [stmt step]; [OFApplication terminate]; } @end | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | [stmt bindWithDictionary: [OFDictionary dictionaryWithKeysAndObjects: @"$a", [OFNumber numberWithInt: 7], @"$b", @"Test", @"$c", [OFData dataWithItems: "xyz" count: 3], nil]]; [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]; } @end |