ObjPgSQL  Check-in [dfb0336763]

Overview
Comment:Use dot syntax
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dfb0336763997b766d56a302c7534656f518a37c06178af7f15edbed5790dabf
User & Date: js on 2019-03-16 22:58:16
Other Links: manifest | tags
Context
2020-05-31
20:11
Update URL and e-mail check-in: 6417eb405d user: js tags: trunk
2019-03-16
22:58
Use dot syntax check-in: dfb0336763 user: js tags: trunk
2018-11-06
21:45
Modernize coding style check-in: 491b090606 user: js tags: trunk
Changes

Modified src/PGConnection.m from [1ce8286c7a] to [87cb0e8477].

1
2
3
4
5
6
7
8
9
/*
 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017
 *   Jonathan Schleifer <js@heap.zone>
 *
 * https://heap.zone/git/objpgsql.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.

|







1
2
3
4
5
6
7
8
9
/*
 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
 *   Jonathan Schleifer <js@heap.zone>
 *
 * https://heap.zone/git/objpgsql.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		if (connectionInfo != nil)
			[connectionInfo appendFormat: @" %@=%@", key, object];
		else
			connectionInfo = [OFMutableString stringWithFormat:
			    @"%@=%@", key, object];
	}

	if ((_connection = PQconnectdb([connectionInfo UTF8String])) == NULL)
		@throw [OFOutOfMemoryException exception];

	if (PQstatus(_connection) == CONNECTION_BAD)
		@throw [PGConnectionFailedException
		    exceptionWithConnection: self];

	objc_autoreleasePoolPop(pool);







|







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		if (connectionInfo != nil)
			[connectionInfo appendFormat: @" %@=%@", key, object];
		else
			connectionInfo = [OFMutableString stringWithFormat:
			    @"%@=%@", key, object];
	}

	if ((_connection = PQconnectdb(connectionInfo.UTF8String)) == NULL)
		@throw [OFOutOfMemoryException exception];

	if (PQstatus(_connection) == CONNECTION_BAD)
		@throw [PGConnectionFailedException
		    exceptionWithConnection: self];

	objc_autoreleasePoolPop(pool);
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
		PQfinish(_connection);

	_connection = NULL;
}

- (PGResult *)executeCommand: (OFConstantString *)command
{
	PGresult *result = PQexec(_connection, [command UTF8String]);

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
				    command: command];
	}







|







82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
		PQfinish(_connection);

	_connection = NULL;
}

- (PGResult *)executeCommand: (OFConstantString *)command
{
	PGresult *result = PQexec(_connection, command.UTF8String);

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
				    command: command];
	}
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

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








|

|





|
|





|
|


|







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

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

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	OFString *key, *value;
	size_t i, count;

	command = [OFMutableString stringWithString: @"INSERT INTO "];
	[command appendString: table];
	[command appendString: @" ("];

	count = [row count];

	i = 0;
	enumerator = [row keyEnumerator];
	while ((key = [enumerator nextObject]) != nil) {
		if (i > 0)
			[command appendString: @", "];








|







186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	OFString *key, *value;
	size_t i, count;

	command = [OFMutableString stringWithString: @"INSERT INTO "];
	[command appendString: table];
	[command appendString: @" ("];

	count = row.count;

	i = 0;
	enumerator = [row keyEnumerator];
	while ((key = [enumerator nextObject]) != nil) {
		if (i > 0)
			[command appendString: @", "];

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	@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);








|






|







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	@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);

Modified src/PGResult+Private.h from [159fb60c68] to [edbe188f70].

25
26
27
28
29
30
31
32
33
34
35
36

OF_ASSUME_NONNULL_BEGIN

@interface PGResult ()
@property (readonly, nonatomic) PGresult *pg_result;

+ (instancetype)pg_resultWithResult: (PGresult *)result;
- (instancetype)pg_initWithResult: (PGresult *)result OF_METHOD_FAMILY(init)
    OF_DESIGNATED_INITIALIZER;
@end

OF_ASSUME_NONNULL_END







|
<



25
26
27
28
29
30
31
32

33
34
35

OF_ASSUME_NONNULL_BEGIN

@interface PGResult ()
@property (readonly, nonatomic) PGresult *pg_result;

+ (instancetype)pg_resultWithResult: (PGresult *)result;
- (instancetype)pg_initWithResult: (PGresult *)result;

@end

OF_ASSUME_NONNULL_END

Modified src/PGResultRow+Private.h from [54a71cebbe] to [8d209e7216].

25
26
27
28
29
30
31
32
33
34
35
36

OF_ASSUME_NONNULL_BEGIN

@interface PGResultRow ()
+ (instancetype)pg_rowWithResult: (PGResult *)result
			     row: (int)row;
- (instancetype)pg_initWithResult: (PGResult *)result
			      row: (int)row OF_METHOD_FAMILY(init)
    OF_DESIGNATED_INITIALIZER;
@end

OF_ASSUME_NONNULL_END







|
<



25
26
27
28
29
30
31
32

33
34
35

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 [80d17b4c5e] to [3db1de9ebe].

1
2
3
4
5
6
7
8
9
/*
 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017
 *   Jonathan Schleifer <js@heap.zone>
 *
 * https://heap.zone/git/objpgsql.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.

|







1
2
3
4
5
6
7
8
9
/*
 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
 *   Jonathan Schleifer <js@heap.zone>
 *
 * https://heap.zone/git/objpgsql.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
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
	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
{







|


|


|

|

|







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
	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
{
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
+ (instancetype)rowWithResult: (PGResult *)result
			  row: (int)row
{
	return [[[self alloc] initWithResult: result
					 row: row] autorelease];
}

- initWithResult: (PGResult *)result
	     row: (int)row
{
	self = [super init];

	_result = [result retain];
	_res = [result pg_result];
	_row = row;

	return self;
}

- (void)dealloc
{







|
|




|







72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
+ (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;

	return self;
}

- (void)dealloc
{
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	state->mutationsPtr = (unsigned long *)self;

	return j;
}
@end

@implementation PGResultRowEnumerator
- initWithResult: (PGResult *)result
	     row: (int)row
{
	self = [super init];

	_result = [result retain];
	_res = [result pg_result];
	_row = row;
	_count = PQnfields(_res);

	return self;
}

- (void)dealloc







|
|




|







166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	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);

	return self;
}

- (void)dealloc