ObjPgSQL  Check-in [0af9225d64]

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: 0af9225d647a95b3135fc711065584dcff39b523377528c734edc0ecb2b379f4
User & Date: js on 2021-04-29 22:36:08
Other Links: manifest | tags
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

Modified src/PGConnection.h from [959beb709d] to [ae7a76644d].

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

#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
		  parameters: (id)firstParameter, ... OF_SENTINEL;
- (void)insertRow: (OFDictionary *)row
	intoTable: (OFString *)table;
- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows
	 intoTable: (OFString *)table;
@end

OF_ASSUME_NONNULL_END







>
>














|
<
|
|




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

Modified src/PGConnection.m from [14ce011f15] to [44eb09f94d].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#import "PGResult.h"
#import "PGResult+Private.h"

#import "PGConnectionFailedException.h"
#import "PGCommandFailedException.h"

@implementation PGConnection
@synthesize pg_connection = _connection;
@synthesize parameters = _parameters;

- (void)dealloc
{
	[_parameters release];

	[self close];








|
<







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
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
181
182
183
184
185
186
	int argsCount;

	va_start(args, parameter);
	va_copy(args2, args);

	for (argsCount = 1; va_arg(args2, id) != nil; argsCount++);

	values = [self allocMemoryWithSize: sizeof(*values)
				     count: argsCount];
	@try {
		size_t i = 0;

		do {
			if ([parameter isKindOfClass: [OFString class]])
				values[i++] = [parameter UTF8String];
			else if ([parameter isKindOfClass: [OFNumber class]]) {
				OFNumber *number = parameter;

				switch (number.type) {
				case OF_NUMBER_TYPE_BOOL:
					if (number.boolValue)
						values[i++] = "t";
					else
						values[i++] = "f";
					break;
				default:
					values[i++] =
					    number.description.UTF8String;
					break;
				}
			} 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 {
		[self freeMemory: 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: (OFDictionary *)row
	intoTable: (OFString *)table
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableString *command;
	OFEnumerator *enumerator;
	const char **values;
	PGresult *result;
	OFString *key, *value;







|
<









|
|




|
<


<
<










|


















<
|







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
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
242
243
244
245
246
247
248
249
		[command appendString: key];

		i++;
	}

	[command appendString: @") VALUES ("];

	values = [self allocMemoryWithSize: sizeof(*values)
				     count: count];
	@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 {
		[self freeMemory: 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(OFDictionary *) *)rows
	 intoTable: (OFString *)table
{
	for (OFDictionary *row in rows)
		[self insertRow: row
		      intoTable: table];
}
@end







|
<

















|














|



|
<


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

Modified src/PGResult.m from [95d2097d1f] to [0b8c7e69a5].

57
58
59
60
61
62
63
64
65
66
67
}

- (id)objectAtIndex: (size_t)index
{
	if (index > PQntuples(_result))
		@throw [OFOutOfRangeException exception];

	return [PGResultRow pg_rowWithResult: self
					 row: (int)index];
}
@end







|
<


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

Modified src/PGResultRow+Private.h from [11616d090c] to [7e2807226e].

22
23
24
25
26
27
28
29
30
31
32
33
34
35
 */

#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







|
<
|
<



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

Modified src/PGResultRow.m from [d8c72a2db5] to [007cd2722f].

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
84
85
86
87
	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 numberWithInt16:
		    (int16_t)string.decimalValue];
	case 23:  /* INT4OID */
		return [OFNumber numberWithInt32:
		    (int32_t)string.decimalValue];
	case 20:  /* INT8OID */
		return [OFNumber numberWithInt64:
		    (int64_t)string.decimalValue];
	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;








|
|

|
|

|
|
















|
<









|
<

|
<


|
<







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
139
140
141
142
143
144
145
146
- (OFEnumerator *)objectEnumerator
{
	return [[[PGResultRowObjectEnumerator alloc]
	    initWithResult: _result
		       row: _row] autorelease];
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id *)objects
			     count: (int)count
{
	int i, j;

	if (state->extra[0] == 0) {
		state->extra[0] = 1;







|







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
173
174
175
176
177
178
179
180
181
	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);







|
<







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

Modified tests/tests.m from [68a20488bf] to [7a8b6cc45d].

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"];
	of_log(@"%@", result);
	of_log(@"JSON: %@", [result JSONRepresentation]);

	for (id row in result)
		for (id col in row)
			of_log(@"%@", col);

	result = [_connection executeCommand: @"SELECT COUNT(*) FROM test"];
	of_log(@"%@", result);

	[OFApplication terminate];
}
@end







|
|



|


|




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