ObjPgSQL  Check-in [4a2b1fff7e]

Overview
Comment:Convert types using the result of PQftypes().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4a2b1fff7ecbdccaaff50842a27a4921f3e336546b07e9b6ebeb440adb29ef60
User & Date: js on 2012-10-07 22:45:37
Other Links: manifest | tags
Context
2012-10-08
18:52
Enforce queries to be constant. check-in: 53f46b10cd user: js tags: trunk
2012-10-07
22:45
Convert types using the result of PQftypes(). check-in: 4a2b1fff7e user: js tags: trunk
2012-10-06
21:02
Add -[insertRows:intoTable:]. check-in: 7b335b3af8 user: js tags: trunk
Changes

Modified Makefile from [534205b75f] to [84367df8f0].

30
31
32
33
34
35
36

37
38
39
40
41
42
43
		--lib 0.0		\
		-o objpgsql		\
		--builddir build	\
		${CPPFLAGS}		\
		${LIBS}			\
		${SRCS}


test:
	@objfw-compile			\
		-o test			\
		--builddir build	\
		-L.			\
		-lobjpgsql		\
		${CPPFLAGS}		\







>







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
		--lib 0.0		\
		-o objpgsql		\
		--builddir build	\
		${CPPFLAGS}		\
		${LIBS}			\
		${SRCS}

.PHONY: test
test:
	@objfw-compile			\
		-o test			\
		--builddir build	\
		-L.			\
		-lobjpgsql		\
		${CPPFLAGS}		\

Modified PGConnection.m from [e8b20f208f] to [ef78860558].

95
96
97
98
99
100
101





102








103
104
105
106
107
108
109
				     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];







>
>
>
>
>
|
>
>
>
>
>
>
>
>







95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
				     count: argsCount];
	@try {
		size_t i = 0;

		do {
			if ([parameter isKindOfClass: [OFNull class]])
				values[i++] = NULL;
			else if ([parameter isKindOfClass: [OFNumber class]]) {
				switch ([parameter type]) {
				case OF_NUMBER_BOOL:
					if ([parameter boolValue])
						values[i++] = "t";
					else
						values[i++] = "f";
					break;
				default:
					values[i++] = [[parameter description]
					    UTF8String];
					break;
				}
			} 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];

Modified PGResultRow.m from [28af410d0c] to [77d330bc63].

1




















2
3
4
5
6
7
8
#import "PGResultRow.h"





















@interface PGResultRowEnumerator: OFEnumerator
{
	PGResult *result;
	PGresult *res;
	int row, pos, count;
}

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#import "PGResultRow.h"

static id
convert_type(PGresult *res, int col, OFString *str)
{
	switch (PQftype(res, col)) {
	case 16: /* BOOLOID */
		if ([str isEqual: @"t"])
			return [OFNumber numberWithBool: YES];
		else
			return [OFNumber numberWithBool: NO];
	case 21: /* INT2OID */
		return [OFNumber numberWithInt16: (int16_t)[str decimalValue]];
	case 23: /* INT4OID */
		return [OFNumber numberWithInt32: (int32_t)[str decimalValue]];
	case 20: /* INT8OID */
		return [OFNumber numberWithInt64: (int64_t)[str decimalValue]];
	}

	return str;
}

@interface PGResultRowEnumerator: OFEnumerator
{
	PGResult *result;
	PGresult *res;
	int row, pos, count;
}
63
64
65
66
67
68
69

70
71
72
73
74
75
76
77
		col = [key intValue];
	else
		col = PQfnumber(res, [key UTF8String]);

	if (PQgetisnull(res, row, col))
		return nil;


	return [OFString stringWithUTF8String: PQgetvalue(res, row, col)];
}

- (OFEnumerator*)keyEnumerator
{
	return [[[PGResultRowKeyEnumerator alloc]
	    initWithResult: result
		       row: row] autorelease];







>
|







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		col = [key intValue];
	else
		col = PQfnumber(res, [key UTF8String]);

	if (PQgetisnull(res, row, col))
		return nil;

	return convert_type(res, col,
	    [OFString stringWithUTF8String: PQgetvalue(res, row, col)]);
}

- (OFEnumerator*)keyEnumerator
{
	return [[[PGResultRowKeyEnumerator alloc]
	    initWithResult: result
		       row: row] autorelease];
129
130
131
132
133
134
135

136
137
138

	while (pos < count && PQgetisnull(res, row, pos))
		pos++;

	if (pos >= count)
		return nil;


	return [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)];
}
@end







>
|


150
151
152
153
154
155
156
157
158
159
160

	while (pos < count && PQgetisnull(res, row, pos))
		pos++;

	if (pos >= count)
		return nil;

	return convert_type(res, pos,
	    [OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]);
}
@end

Modified test.m from [5b6ed458d7] to [d0a83f4346].

22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
							@"dbname", @"js", nil]];
	[connection connect];

	[connection executeCommand: @"DROP TABLE IF EXISTS test"];
	[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];
	[connection insertRow: @{ @"content": @"Hallo!", @"name": @"foo" }
		    intoTable: @"test"];

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








|
>


|
|
|
|
|







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
							@"dbname", @"js", nil]];
	[connection connect];

	[connection executeCommand: @"DROP TABLE IF EXISTS test"];
	[connection executeCommand: @"CREATE TABLE test ("
				    @"    id integer,"
				    @"    name varchar(255),"
				    @"    content text,"
				    @"    success boolean"
				    @")"];
	[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, success) "
				    @"VALUES ($1, $2, $3)"
			parameters: @2, @2, @YES];
	[connection insertRow: @{ @"content": @"Hallo!", @"name": @"foo" }
		    intoTable: @"test"];

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