Overview
Comment: | Adjust to ObjFW changes |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
0af9225d647a95b3135fc711065584dc |
User & Date: | js 2021-04-29 22:36:08 |
Context
2021-04-29
| ||
22:46 | Fix method name mismatch check-in: 770a096b55 user: js tags: trunk | |
22:36 | Adjust to ObjFW changes check-in: 0af9225d64 user: js tags: trunk | |
2020-05-31
| ||
20:11 | Update URL and e-mail check-in: 6417eb405d user: js tags: trunk | |
Changes
Changes to src/PGConnection.h.
︙ | ︙ | |||
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #import <ObjFW/ObjFW.h> #import "PGResult.h" OF_ASSUME_NONNULL_BEGIN @interface PGConnection: OFObject { PGconn *_connection; OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters; } @property (nonatomic, copy) OFDictionary OF_GENERIC(OFString *, OFString *) *parameters; - (void)connect; - (void)reset; - (void)close; - (nullable PGResult *)executeCommand: (OFConstantString *)command; - (nullable PGResult *)executeCommand: (OFConstantString *)command | > > | < | | | 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #import <ObjFW/ObjFW.h> #import "PGResult.h" OF_ASSUME_NONNULL_BEGIN typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow; @interface PGConnection: OFObject { PGconn *_connection; OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters; } @property (nonatomic, copy) OFDictionary OF_GENERIC(OFString *, OFString *) *parameters; - (void)connect; - (void)reset; - (void)close; - (nullable PGResult *)executeCommand: (OFConstantString *)command; - (nullable PGResult *)executeCommand: (OFConstantString *)command parameters: (id)firstParameter, ... OF_SENTINEL; - (void)insertRow: (PGRow)row intoTable: (OFString *)table; - (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows intoTable: (OFString *)table; @end OF_ASSUME_NONNULL_END |
Changes to src/PGConnection.m.
︙ | ︙ | |||
26 27 28 29 30 31 32 | #import "PGResult.h" #import "PGResult+Private.h" #import "PGConnectionFailedException.h" #import "PGCommandFailedException.h" @implementation PGConnection | | < | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #import "PGResult.h" #import "PGResult+Private.h" #import "PGConnectionFailedException.h" #import "PGCommandFailedException.h" @implementation PGConnection @synthesize pg_connection = _connection, parameters = _parameters; - (void)dealloc { [_parameters release]; [self close]; |
︙ | ︙ | |||
119 120 121 122 123 124 125 | int argsCount; va_start(args, parameter); va_copy(args2, args); for (argsCount = 1; va_arg(args2, id) != nil; argsCount++); | | < | | | < < < | < | | 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | int argsCount; va_start(args, parameter); va_copy(args2, args); for (argsCount = 1; va_arg(args2, id) != nil; argsCount++); values = OFAllocMemory(argsCount, sizeof(*values)); @try { size_t i = 0; do { if ([parameter isKindOfClass: [OFString class]]) values[i++] = [parameter UTF8String]; else if ([parameter isKindOfClass: [OFNumber class]]) { OFNumber *number = parameter; if (strcmp(number.objCType, @encode(bool)) == 0) { if (number.boolValue) values[i++] = "t"; else values[i++] = "f"; } else values[i++] = number.description.UTF8String; } else if ([parameter isKindOfClass: [OFNull class]]) values[i++] = NULL; else values[i++] = [parameter description].UTF8String; } while ((parameter = va_arg(args, id)) != nil); result = PQexecParams(_connection, command.UTF8String, argsCount, NULL, values, NULL, NULL, 0); } @finally { OFFreeMemory(values); } objc_autoreleasePoolPop(pool); switch (PQresultStatus(result)) { case PGRES_TUPLES_OK: return [PGResult pg_resultWithResult: result]; case PGRES_COMMAND_OK: PQclear(result); return nil; default: PQclear(result); @throw [PGCommandFailedException exceptionWithConnection: self command: command]; } } - (void)insertRow: (PGRow)row intoTable: (OFString *)table { void *pool = objc_autoreleasePoolPush(); OFMutableString *command; OFEnumerator *enumerator; const char **values; PGresult *result; OFString *key, *value; |
︙ | ︙ | |||
201 202 203 204 205 206 207 | [command appendString: key]; i++; } [command appendString: @") VALUES ("]; | | < | | | < | 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | [command appendString: key]; i++; } [command appendString: @") VALUES ("]; values = OFAllocMemory(count, sizeof(*values)); @try { i = 0; enumerator = [row objectEnumerator]; while ((value = [enumerator nextObject]) != nil) { if (i > 0) [command appendString: @", "]; values[i] = value.UTF8String; [command appendFormat: @"$%zd", ++i]; } [command appendString: @")"]; result = PQexecParams(_connection, command.UTF8String, (int)count, NULL, values, NULL, NULL, 0); } @finally { OFFreeMemory(values); } objc_autoreleasePoolPop(pool); if (PQresultStatus(result) != PGRES_COMMAND_OK) { PQclear(result); @throw [PGCommandFailedException exceptionWithConnection: self command: command]; } PQclear(result); } - (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows intoTable: (OFString *)table { for (OFDictionary *row in rows) [self insertRow: row intoTable: table]; } @end |
Changes to src/PGResult.m.
︙ | ︙ | |||
57 58 59 60 61 62 63 | } - (id)objectAtIndex: (size_t)index { if (index > PQntuples(_result)) @throw [OFOutOfRangeException exception]; | | < | 57 58 59 60 61 62 63 64 65 66 | } - (id)objectAtIndex: (size_t)index { if (index > PQntuples(_result)) @throw [OFOutOfRangeException exception]; return [PGResultRow pg_rowWithResult: self row: (int)index]; } @end |
Changes to src/PGResultRow+Private.h.
︙ | ︙ | |||
22 23 24 25 26 27 28 | */ #import "PGResultRow.h" OF_ASSUME_NONNULL_BEGIN @interface PGResultRow () | | < | < | 22 23 24 25 26 27 28 29 30 31 32 33 | */ #import "PGResultRow.h" OF_ASSUME_NONNULL_BEGIN @interface PGResultRow () + (instancetype)pg_rowWithResult: (PGResult *)result row: (int)row; - (instancetype)pg_initWithResult: (PGResult *)result row: (int)row; @end OF_ASSUME_NONNULL_END |
Changes to src/PGResultRow.m.
︙ | ︙ | |||
30 31 32 33 34 35 36 | switch (PQftype(res, column)) { case 16: /* BOOLOID */ if ([string isEqual: @"t"]) return [OFNumber numberWithBool: YES]; else return [OFNumber numberWithBool: NO]; case 21: /* INT2OID */ | | | | | | | | < | < | < | < | 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 | switch (PQftype(res, column)) { case 16: /* BOOLOID */ if ([string isEqual: @"t"]) return [OFNumber numberWithBool: YES]; else return [OFNumber numberWithBool: NO]; case 21: /* INT2OID */ return [OFNumber numberWithShort: (short)[string longLongValueWithBase: 10]]; case 23: /* INT4OID */ return [OFNumber numberWithLong: (long)[string longLongValueWithBase: 10]]; case 20: /* INT8OID */ return [OFNumber numberWithLongLong: [string longLongValueWithBase: 10]]; case 700: /* FLOAT4OID */ return [OFNumber numberWithFloat: string.floatValue]; case 701: /* FLOAT8OID */ return [OFNumber numberWithDouble: string.doubleValue]; } return string; } @interface PGResultRowEnumerator: OFEnumerator { PGResult *_result; PGresult *_res; int _row, _pos, _count; } - initWithResult: (PGResult*)result row: (int)row; @end @interface PGResultRowKeyEnumerator: PGResultRowEnumerator @end @interface PGResultRowObjectEnumerator: PGResultRowEnumerator @end @implementation PGResultRow + (instancetype)rowWithResult: (PGResult *)result row: (int)row { return [[[self alloc] initWithResult: result row: row] autorelease]; } - (instancetype)initWithResult: (PGResult *)result row: (int)row { self = [super init]; _result = [result retain]; _res = result.pg_result; _row = row; |
︙ | ︙ | |||
132 133 134 135 136 137 138 | - (OFEnumerator *)objectEnumerator { return [[[PGResultRowObjectEnumerator alloc] initWithResult: _result row: _row] autorelease]; } | | | 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | - (OFEnumerator *)objectEnumerator { return [[[PGResultRowObjectEnumerator alloc] initWithResult: _result row: _row] autorelease]; } - (int)countByEnumeratingWithState: (OFFastEnumerationState *)state objects: (id *)objects count: (int)count { int i, j; if (state->extra[0] == 0) { state->extra[0] = 1; |
︙ | ︙ | |||
166 167 168 169 170 171 172 | state->mutationsPtr = (unsigned long *)self; return j; } @end @implementation PGResultRowEnumerator | | < | 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | state->mutationsPtr = (unsigned long *)self; return j; } @end @implementation PGResultRowEnumerator - (instancetype)initWithResult: (PGResult *)result row: (int)row { self = [super init]; _result = [result retain]; _res = result.pg_result; _row = row; _count = PQnfields(_res); |
︙ | ︙ |
Changes to tests/tests.m.
︙ | ︙ | |||
65 66 67 68 69 70 71 | [OFNumber numberWithInt: 2], [OFNumber numberWithBool: true], nil]; [_connection insertRow: [OFDictionary dictionaryWithKeysAndObjects: @"content", @"Hallo!", @"name", @"foo", nil] intoTable: @"test"]; result = [_connection executeCommand: @"SELECT * FROM test"]; | | | | | | 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | [OFNumber numberWithInt: 2], [OFNumber numberWithBool: true], nil]; [_connection insertRow: [OFDictionary dictionaryWithKeysAndObjects: @"content", @"Hallo!", @"name", @"foo", nil] intoTable: @"test"]; result = [_connection executeCommand: @"SELECT * FROM test"]; OFLog(@"%@", result); OFLog(@"JSON: %@", [result JSONRepresentation]); for (id row in result) for (id col in row) OFLog(@"%@", col); result = [_connection executeCommand: @"SELECT COUNT(*) FROM test"]; OFLog(@"%@", result); [OFApplication terminate]; } @end |