ObjPgSQL  Check-in [d2fe40f160]

Overview
Comment:Nicer API.

-[executeCommand:parameters:] is now a variadic function instead of
taking an array as argument.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d2fe40f16065b5ccf5ba135af1ec95f09ed2f8ee96e661a7a7d085da833e972e
User & Date: js on 2012-10-05 20:17:45
Other Links: manifest | tags
Context
2012-10-05
21:19
Add Xcode project. check-in: cf2fe18597 user: js tags: trunk
20:17
Nicer API. check-in: d2fe40f160 user: js tags: trunk
2012-10-04
22:18
Exclude fields that are NULL from the result. check-in: 6d068f6ae7 user: js tags: trunk
Changes

Modified PGConnection.h from [6e2d36c890] to [260fd28f22].

16
17
18
19
20
21
22
23
24
25

- (void)setParameters: (OFDictionary*)parameters;
- (OFDictionary*)parameters;
- (void)connect;
- (void)reset;
- (PGResult*)executeCommand: (OFString*)command;
- (PGResult*)executeCommand: (OFString*)command
		 parameters: (OFArray*)parameters;
- (PGconn*)PG_connection;
@end







|


16
17
18
19
20
21
22
23
24
25

- (void)setParameters: (OFDictionary*)parameters;
- (OFDictionary*)parameters;
- (void)connect;
- (void)reset;
- (PGResult*)executeCommand: (OFString*)command;
- (PGResult*)executeCommand: (OFString*)command
		 parameters: (id)firstParameter, ...;
- (PGconn*)PG_connection;
@end

Modified PGConnection.m from [8d29eddb59] to [d345fef3f0].

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
101
102
103
104
105
106
107
108
109
		return [PGResult PG_resultWithResult: result];

	PQclear(result);
	return nil;
}

- (PGResult*)executeCommand: (OFString*)command
		 parameters: (OFArray*)parameters_
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	PGresult *result;
	const char **values;








	values = [self allocMemoryWithSize: sizeof(*values)
				     count: [parameters_ count]];
	@try {
		OFEnumerator *enumerator = [parameters_ objectEnumerator];
		size_t i = 0;
		id parameter;

		while ((parameter = [enumerator nextObject]) != nil) {

			if ([parameter isKindOfClass: [OFNull class]])
				values[i++] = NULL;
			else
				values[i++] = [parameter UTF8String];
		}

		result = PQexecParams(conn, [command UTF8String],
		    [parameters_ count], NULL, values, NULL, NULL, 0);
	} @finally {
		[self freeMemory: values];
	}

	[pool release];

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {







|




>
>

>
>
>
>
>

|

<

<

<
>




|


|







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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
		return [PGResult PG_resultWithResult: result];

	PQclear(result);
	return nil;
}

- (PGResult*)executeCommand: (OFString*)command
		 parameters: (id)parameter, ...
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	PGresult *result;
	const char **values;
	va_list args, args2;
	size_t 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: [OFNull class]])
				values[i++] = NULL;
			else
				values[i++] = [parameter UTF8String];
		} while ((parameter = va_arg(args, id)) != nil);

		result = PQexecParams(conn, [command UTF8String],
		    argsCount, NULL, values, NULL, NULL, 0);
	} @finally {
		[self freeMemory: values];
	}

	[pool release];

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {

Modified test.m from [e494e1ab6f] to [f9e8525602].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40



41
42
43
44
	[connection executeCommand: @"CREATE TABLE test ("
				    @"    id integer,"
				    @"    name varchar(255),"
				    @"    content text"
				    @")"];
	[connection executeCommand: @"INSERT INTO test (id, name, content) "
				    @"VALUES($1, $2, $3)"
			parameters: @[@"1", @"foo", @"Hallo Welt!"]];
	[connection executeCommand: @"INSERT INTO test (id, content) "
				    @"VALUES($1, $2)"
			parameters: @[@"2", @"Blup!!"]];

	result = [connection executeCommand: @"SELECT * FROM test"];
	of_log(@"%@", result);
	of_log(@"JSON: %@", [result JSONRepresentation]);




	[OFApplication terminate];
}
@end







|


|




>
>
>




26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	[connection executeCommand: @"CREATE TABLE test ("
				    @"    id integer,"
				    @"    name varchar(255),"
				    @"    content text"
				    @")"];
	[connection executeCommand: @"INSERT INTO test (id, name, content) "
				    @"VALUES($1, $2, $3)"
			parameters: @"1", @"foo", @"Hallo Welt!", nil];
	[connection executeCommand: @"INSERT INTO test (id, content) "
				    @"VALUES($1, $2)"
			parameters: @"2", @"Blup!!", nil];

	result = [connection executeCommand: @"SELECT * FROM test"];
	of_log(@"%@", result);
	of_log(@"JSON: %@", [result JSONRepresentation]);

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

	[OFApplication terminate];
}
@end