ObjPgSQL  Diff

Differences From Artifact [fce9f9ad1f]:

To Artifact [bf3d3626a7]:


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
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
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
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
115
116
117
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#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]];

	case 700: /* FLOAT4OID */
		return [OFNumber numberWithFloat: [str floatValue]];
	case 701: /* FLOAT8OID */
		return [OFNumber numberWithDouble: [str doubleValue]];
	}

	return str;
}

@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
+ 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
{
	[result release];

	[super dealloc];
}

- (size_t)count
{
	int i, count, fields = PQnfields(res);

	for (i = count = 0; i < fields; i++)
		if (!PQgetisnull(res, row, i))
			count++;

	return count;
}

- (id)objectForKey: (id)key
{
	int col;

	if ([key isKindOfClass: [OFNumber class]])
		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];
}

- (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;
		state->extra[1] = PQnfields(res);
	}

	if (count > SIZE_MAX - state->state)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (state->state + count > state->extra[1])
		count = state->extra[1] - state->state;

	for (i = j = 0; i < count; i++) {
		if (PQgetisnull(res, row, state->state + i))
			continue;

		objects[j++] = [OFString stringWithUTF8String:
		    PQfname(res, state->state + i)];
	}

	state->state += count;
	state->itemsPtr = objects;
	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
{
	[result release];

	[super dealloc];
}

- (void)reset
{
	pos = 0;
}
@end

@implementation PGResultRowKeyEnumerator
- (id)nextObject
{
	if (pos >= count)
		return nil;

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

	if (pos >= count)
		return nil;

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

@implementation PGResultRowObjectEnumerator
- (id)nextObject
{
	if (pos >= count)
		return nil;

	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



|

|

|




|
>

|
>

|
>

|

|


|




|
|
|




















|
|



|
|
|






|






|


|







|


|

|

|


|
|





|
|





|
|










|









|



|











|
|



|
|
|
|






|






|






|


|
|

|


|






|


|
|

|


|
|


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
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
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
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
115
116
117
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#import "PGResultRow.h"

static id
convert_type(PGresult *res, int column, OFString *string)
{
	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
+ 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
{
	[_result release];

	[super dealloc];
}

- (size_t)count
{
	int i, count, fields = PQnfields(_res);

	for (i = count = 0; i < fields; i++)
		if (!PQgetisnull(_res, _row, i))
			count++;

	return count;
}

- (id)objectForKey: (id)key
{
	int column;

	if ([key isKindOfClass: [OFNumber class]])
		column = [key intValue];
	else
		column = PQfnumber(_res, [key UTF8String]);

	if (PQgetisnull(_res, _row, column))
		return nil;

	return convert_type(_res, column,
	    [OFString stringWithUTF8String: PQgetvalue(_res, _row, column)]);
}

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

- (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;
		state->extra[1] = PQnfields(_res);
	}

	if (count > SIZE_MAX - state->state)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (state->state + count > state->extra[1])
		count = state->extra[1] - state->state;

	for (i = j = 0; i < count; i++) {
		if (PQgetisnull(_res, _row, state->state + i))
			continue;

		objects[j++] = [OFString stringWithUTF8String:
		    PQfname(_res, state->state + i)];
	}

	state->state += count;
	state->itemsPtr = objects;
	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
{
	[_result release];

	[super dealloc];
}

- (void)reset
{
	_pos = 0;
}
@end

@implementation PGResultRowKeyEnumerator
- (id)nextObject
{
	if (_pos >= _count)
		return nil;

	while (_pos < _count && PQgetisnull(_res, _row, _pos))
		_pos++;

	if (_pos >= _count)
		return nil;

	return [OFString stringWithUTF8String: PQfname(_res, _pos++)];
}
@end

@implementation PGResultRowObjectEnumerator
- (id)nextObject
{
	if (_pos >= _count)
		return nil;

	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