sequel_pg_extended_columns 1.6.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG +149 -0
- data/MIT-LICENSE +47 -0
- data/README.rdoc +190 -0
- data/Rakefile +21 -0
- data/ext/sequel_pg/extconf.rb +21 -0
- data/ext/sequel_pg/sequel_pg.c +1129 -0
- data/lib/sequel/extensions/pg_streaming.rb +149 -0
- data/lib/sequel_pg/sequel_pg.rb +103 -0
- metadata +108 -0
@@ -0,0 +1,1129 @@
|
|
1
|
+
#define SEQUEL_PG_VERSION_INTEGER 10619
|
2
|
+
|
3
|
+
#include <string.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
#include <math.h>
|
6
|
+
#include <libpq-fe.h>
|
7
|
+
#include <ruby.h>
|
8
|
+
|
9
|
+
#if defined(HAVE_RUBY_ENCODING_H) && HAVE_RUBY_ENCODING_H
|
10
|
+
#define SPG_ENCODING 1
|
11
|
+
#include <ruby/encoding.h>
|
12
|
+
#define ENC_INDEX ,enc_index
|
13
|
+
#else
|
14
|
+
#define ENC_INDEX
|
15
|
+
#endif
|
16
|
+
|
17
|
+
#ifndef SPG_MAX_FIELDS
|
18
|
+
#define SPG_MAX_FIELDS 1600
|
19
|
+
#endif
|
20
|
+
#define SPG_MICROSECONDS_PER_DAY_LL 86400000000ULL
|
21
|
+
#define SPG_MICROSECONDS_PER_DAY 86400000000.0
|
22
|
+
#define SPG_MINUTES_PER_DAY 1440.0
|
23
|
+
#define SPG_SECONDS_PER_DAY 86400.0
|
24
|
+
|
25
|
+
#define SPG_DT_ADD_USEC if (usec != 0) { dt = rb_funcall(dt, spg_id_op_plus, 1, spg_id_Rational ? rb_funcall(rb_cObject, spg_id_Rational, 2, INT2NUM(usec), ULL2NUM(SPG_MICROSECONDS_PER_DAY_LL)) : rb_float_new(usec/SPG_MICROSECONDS_PER_DAY)); }
|
26
|
+
|
27
|
+
#define SPG_NO_TZ 0
|
28
|
+
#define SPG_DB_LOCAL 1
|
29
|
+
#define SPG_DB_UTC 2
|
30
|
+
#define SPG_APP_LOCAL 4
|
31
|
+
#define SPG_APP_UTC 8
|
32
|
+
|
33
|
+
#define SPG_YIELD_NORMAL 0
|
34
|
+
#define SPG_YIELD_COLUMN 1
|
35
|
+
#define SPG_YIELD_COLUMNS 2
|
36
|
+
#define SPG_YIELD_FIRST 3
|
37
|
+
#define SPG_YIELD_ARRAY 4
|
38
|
+
#define SPG_YIELD_KV_HASH 5
|
39
|
+
#define SPG_YIELD_MKV_HASH 6
|
40
|
+
#define SPG_YIELD_KMV_HASH 7
|
41
|
+
#define SPG_YIELD_MKMV_HASH 8
|
42
|
+
#define SPG_YIELD_MODEL 9
|
43
|
+
#define SPG_YIELD_KV_HASH_GROUPS 10
|
44
|
+
#define SPG_YIELD_MKV_HASH_GROUPS 11
|
45
|
+
#define SPG_YIELD_KMV_HASH_GROUPS 12
|
46
|
+
#define SPG_YIELD_MKMV_HASH_GROUPS 13
|
47
|
+
|
48
|
+
/* Whether the data objects are structs instead of just pointers */
|
49
|
+
static int unwrap_structs;
|
50
|
+
static int use_columns_method;
|
51
|
+
|
52
|
+
/* External functions defined by ruby-pg when data objects are structs */
|
53
|
+
PGconn* pg_get_pgconn(VALUE);
|
54
|
+
PGresult* pgresult_get(VALUE);
|
55
|
+
|
56
|
+
/* Normalize access to data objects for both old and new versions of pg gem */
|
57
|
+
#define GetPGconn(_val, _var) if (unwrap_structs) {Check_Type(_val, T_DATA); _var = pg_get_pgconn(_val);} else {Data_Get_Struct(_val, PGconn, _var);}
|
58
|
+
#define GetPGresult(_val, _var) if (unwrap_structs) {Check_Type(_val, T_DATA); _var = pgresult_get(_val);} else {Data_Get_Struct(_val, PGresult, _var);}
|
59
|
+
|
60
|
+
static VALUE spg_Sequel;
|
61
|
+
static VALUE spg_Blob;
|
62
|
+
static VALUE spg_BigDecimal;
|
63
|
+
static VALUE spg_Date;
|
64
|
+
static VALUE spg_SQLTime;
|
65
|
+
static VALUE spg_PGError;
|
66
|
+
|
67
|
+
static VALUE spg_sym_utc;
|
68
|
+
static VALUE spg_sym_local;
|
69
|
+
static VALUE spg_sym_map;
|
70
|
+
static VALUE spg_sym_first;
|
71
|
+
static VALUE spg_sym_array;
|
72
|
+
static VALUE spg_sym_hash;
|
73
|
+
static VALUE spg_sym_hash_groups;
|
74
|
+
static VALUE spg_sym_model;
|
75
|
+
static VALUE spg_sym__sequel_pg_type;
|
76
|
+
static VALUE spg_sym__sequel_pg_value;
|
77
|
+
|
78
|
+
static VALUE spg_nan;
|
79
|
+
static VALUE spg_pos_inf;
|
80
|
+
static VALUE spg_neg_inf;
|
81
|
+
|
82
|
+
static ID spg_id_Rational;
|
83
|
+
static ID spg_id_new;
|
84
|
+
static ID spg_id_local;
|
85
|
+
static ID spg_id_year;
|
86
|
+
static ID spg_id_month;
|
87
|
+
static ID spg_id_day;
|
88
|
+
static ID spg_id_output_identifier;
|
89
|
+
static ID spg_id_datetime_class;
|
90
|
+
static ID spg_id_application_timezone;
|
91
|
+
static ID spg_id_to_application_timestamp;
|
92
|
+
static ID spg_id_timezone;
|
93
|
+
static ID spg_id_op_plus;
|
94
|
+
static ID spg_id_utc;
|
95
|
+
static ID spg_id_utc_offset;
|
96
|
+
static ID spg_id_localtime;
|
97
|
+
static ID spg_id_new_offset;
|
98
|
+
static ID spg_id_convert_infinite_timestamps;
|
99
|
+
static ID spg_id_infinite_timestamp_value;
|
100
|
+
|
101
|
+
static ID spg_id_call;
|
102
|
+
static ID spg_id_get;
|
103
|
+
static ID spg_id_opts;
|
104
|
+
|
105
|
+
static ID spg_id_db;
|
106
|
+
static ID spg_id_conversion_procs;
|
107
|
+
|
108
|
+
static ID spg_id_columns_equal;
|
109
|
+
static ID spg_id_columns;
|
110
|
+
static ID spg_id_encoding;
|
111
|
+
static ID spg_id_values;
|
112
|
+
|
113
|
+
#if HAVE_PQSETSINGLEROWMODE
|
114
|
+
static ID spg_id_get_result;
|
115
|
+
static ID spg_id_clear;
|
116
|
+
static ID spg_id_check;
|
117
|
+
#endif
|
118
|
+
|
119
|
+
#if SPG_ENCODING
|
120
|
+
static int enc_get_index(VALUE val)
|
121
|
+
{
|
122
|
+
int i = ENCODING_GET_INLINED(val);
|
123
|
+
if (i == ENCODING_INLINE_MAX) {
|
124
|
+
i = NUM2INT(rb_ivar_get(val, spg_id_encoding));
|
125
|
+
}
|
126
|
+
return i;
|
127
|
+
}
|
128
|
+
#endif
|
129
|
+
|
130
|
+
static VALUE read_array(int *index, char *c_pg_array_string, int array_string_length, char *word, VALUE converter
|
131
|
+
#ifdef SPG_ENCODING
|
132
|
+
, int enc_index
|
133
|
+
#endif
|
134
|
+
)
|
135
|
+
{
|
136
|
+
int word_index = 0;
|
137
|
+
|
138
|
+
/* The current character in the input string. */
|
139
|
+
char c;
|
140
|
+
|
141
|
+
/* 0: Currently outside a quoted string, current word never quoted
|
142
|
+
* 1: Currently inside a quoted string
|
143
|
+
* -1: Currently outside a quoted string, current word previously quoted */
|
144
|
+
int openQuote = 0;
|
145
|
+
|
146
|
+
/* Inside quoted input means the next character should be treated literally,
|
147
|
+
* instead of being treated as a metacharacter.
|
148
|
+
* Outside of quoted input, means that the word shouldn't be pushed to the array,
|
149
|
+
* used when the last entry was a subarray (which adds to the array itself). */
|
150
|
+
int escapeNext = 0;
|
151
|
+
|
152
|
+
VALUE array = rb_ary_new();
|
153
|
+
RB_GC_GUARD(array);
|
154
|
+
|
155
|
+
/* Special case the empty array, so it doesn't need to be handled manually inside
|
156
|
+
* the loop. */
|
157
|
+
if(((*index) < array_string_length) && c_pg_array_string[(*index)] == '}')
|
158
|
+
{
|
159
|
+
return array;
|
160
|
+
}
|
161
|
+
|
162
|
+
for(;(*index) < array_string_length; ++(*index))
|
163
|
+
{
|
164
|
+
c = c_pg_array_string[*index];
|
165
|
+
if(openQuote < 1)
|
166
|
+
{
|
167
|
+
if(c == ',' || c == '}')
|
168
|
+
{
|
169
|
+
if(!escapeNext)
|
170
|
+
{
|
171
|
+
if(openQuote == 0 && word_index == 4 && !strncmp(word, "NULL", word_index))
|
172
|
+
{
|
173
|
+
rb_ary_push(array, Qnil);
|
174
|
+
}
|
175
|
+
else
|
176
|
+
{
|
177
|
+
VALUE rword = rb_tainted_str_new(word, word_index);
|
178
|
+
RB_GC_GUARD(rword);
|
179
|
+
|
180
|
+
#ifdef SPG_ENCODING
|
181
|
+
rb_enc_associate_index(rword, enc_index);
|
182
|
+
#endif
|
183
|
+
|
184
|
+
if (RTEST(converter)) {
|
185
|
+
rword = rb_funcall(converter, spg_id_call, 1, rword);
|
186
|
+
}
|
187
|
+
|
188
|
+
rb_ary_push(array, rword);
|
189
|
+
}
|
190
|
+
}
|
191
|
+
if(c == '}')
|
192
|
+
{
|
193
|
+
return array;
|
194
|
+
}
|
195
|
+
escapeNext = 0;
|
196
|
+
openQuote = 0;
|
197
|
+
word_index = 0;
|
198
|
+
}
|
199
|
+
else if(c == '"')
|
200
|
+
{
|
201
|
+
openQuote = 1;
|
202
|
+
}
|
203
|
+
else if(c == '{')
|
204
|
+
{
|
205
|
+
(*index)++;
|
206
|
+
rb_ary_push(array, read_array(index, c_pg_array_string, array_string_length, word, converter
|
207
|
+
#ifdef SPG_ENCODING
|
208
|
+
, enc_index
|
209
|
+
#endif
|
210
|
+
));
|
211
|
+
escapeNext = 1;
|
212
|
+
}
|
213
|
+
else
|
214
|
+
{
|
215
|
+
word[word_index] = c;
|
216
|
+
word_index++;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
else if (escapeNext) {
|
220
|
+
word[word_index] = c;
|
221
|
+
word_index++;
|
222
|
+
escapeNext = 0;
|
223
|
+
}
|
224
|
+
else if (c == '\\')
|
225
|
+
{
|
226
|
+
escapeNext = 1;
|
227
|
+
}
|
228
|
+
else if (c == '"')
|
229
|
+
{
|
230
|
+
openQuote = -1;
|
231
|
+
}
|
232
|
+
else
|
233
|
+
{
|
234
|
+
word[word_index] = c;
|
235
|
+
word_index++;
|
236
|
+
}
|
237
|
+
}
|
238
|
+
|
239
|
+
return array;
|
240
|
+
}
|
241
|
+
|
242
|
+
static VALUE parse_pg_array(VALUE self, VALUE pg_array_string, VALUE converter) {
|
243
|
+
|
244
|
+
/* convert to c-string, create additional ruby string buffer of
|
245
|
+
* the same length, as that will be the worst case. */
|
246
|
+
char *c_pg_array_string = StringValueCStr(pg_array_string);
|
247
|
+
int array_string_length = RSTRING_LEN(pg_array_string);
|
248
|
+
VALUE buf = rb_str_buf_new(array_string_length);
|
249
|
+
RB_GC_GUARD(buf);
|
250
|
+
char *word = RSTRING_PTR(buf);
|
251
|
+
int index = 1;
|
252
|
+
|
253
|
+
if (array_string_length == 0) {
|
254
|
+
rb_raise(rb_eArgError, "unexpected PostgreSQL array format, empty");
|
255
|
+
}
|
256
|
+
|
257
|
+
switch (c_pg_array_string[0]) {
|
258
|
+
case '[':
|
259
|
+
/* Skip explicit subscripts, scanning until opening array */
|
260
|
+
for(;index < array_string_length && c_pg_array_string[index] != '{'; ++index)
|
261
|
+
/* nothing */;
|
262
|
+
|
263
|
+
if (index >= array_string_length) {
|
264
|
+
rb_raise(rb_eArgError, "unexpected PostgreSQL array format, no {");
|
265
|
+
} else {
|
266
|
+
++index;
|
267
|
+
}
|
268
|
+
case '{':
|
269
|
+
break;
|
270
|
+
default:
|
271
|
+
rb_raise(rb_eArgError, "unexpected PostgreSQL array format, doesn't start with { or [");
|
272
|
+
}
|
273
|
+
|
274
|
+
return read_array(&index, c_pg_array_string, array_string_length, word, converter
|
275
|
+
#ifdef SPG_ENCODING
|
276
|
+
, enc_get_index(pg_array_string)
|
277
|
+
#endif
|
278
|
+
);
|
279
|
+
}
|
280
|
+
|
281
|
+
static VALUE spg_time(const char *s) {
|
282
|
+
VALUE now;
|
283
|
+
int hour, minute, second, tokens, i;
|
284
|
+
char subsec[7];
|
285
|
+
int usec = 0;
|
286
|
+
|
287
|
+
tokens = sscanf(s, "%2d:%2d:%2d.%6s", &hour, &minute, &second, subsec);
|
288
|
+
if(tokens == 4) {
|
289
|
+
for(i=0; i<6; i++) {
|
290
|
+
if(subsec[i] == '-') {
|
291
|
+
subsec[i] = '\0';
|
292
|
+
}
|
293
|
+
}
|
294
|
+
usec = atoi(subsec);
|
295
|
+
usec *= (int) pow(10, (6 - strlen(subsec)));
|
296
|
+
} else if(tokens < 3) {
|
297
|
+
rb_raise(rb_eArgError, "unexpected time format");
|
298
|
+
}
|
299
|
+
|
300
|
+
now = rb_funcall(spg_SQLTime, spg_id_new, 0);
|
301
|
+
return rb_funcall(spg_SQLTime, spg_id_local, 7, rb_funcall(now, spg_id_year, 0), rb_funcall(now, spg_id_month, 0), rb_funcall(now, spg_id_day, 0), INT2NUM(hour), INT2NUM(minute), INT2NUM(second), INT2NUM(usec));
|
302
|
+
}
|
303
|
+
|
304
|
+
static VALUE spg_timestamp_error(const char *s, VALUE self, const char *error_msg) {
|
305
|
+
VALUE db;
|
306
|
+
db = rb_funcall(self, spg_id_db, 0);
|
307
|
+
if(RTEST(rb_funcall(db, spg_id_convert_infinite_timestamps, 0))) {
|
308
|
+
if((strcmp(s, "infinity") == 0) || (strcmp(s, "-infinity") == 0)) {
|
309
|
+
return rb_funcall(db, spg_id_infinite_timestamp_value, 1, rb_tainted_str_new2(s));
|
310
|
+
}
|
311
|
+
}
|
312
|
+
rb_raise(rb_eArgError, "%s", error_msg);
|
313
|
+
}
|
314
|
+
|
315
|
+
static VALUE spg_date(const char *s, VALUE self) {
|
316
|
+
int year, month, day;
|
317
|
+
|
318
|
+
if(3 != sscanf(s, "%d-%2d-%2d", &year, &month, &day)) {
|
319
|
+
return spg_timestamp_error(s, self, "unexpected date format");
|
320
|
+
}
|
321
|
+
|
322
|
+
return rb_funcall(spg_Date, spg_id_new, 3, INT2NUM(year), INT2NUM(month), INT2NUM(day));
|
323
|
+
}
|
324
|
+
|
325
|
+
static VALUE spg_timestamp(const char *s, VALUE self) {
|
326
|
+
VALUE dtc, dt, rtz, db;
|
327
|
+
int tz = SPG_NO_TZ;
|
328
|
+
int year, month, day, hour, min, sec, usec, tokens, utc_offset;
|
329
|
+
int usec_start, usec_stop;
|
330
|
+
char offset_sign = 0;
|
331
|
+
int offset_hour = 0;
|
332
|
+
int offset_minute = 0;
|
333
|
+
int offset_seconds = 0;
|
334
|
+
double offset_fraction = 0.0;
|
335
|
+
|
336
|
+
db = rb_funcall(self, spg_id_db, 0);
|
337
|
+
rtz = rb_funcall(db, spg_id_timezone, 0);
|
338
|
+
if (rtz != Qnil) {
|
339
|
+
if (rtz == spg_sym_local) {
|
340
|
+
tz += SPG_DB_LOCAL;
|
341
|
+
} else if (rtz == spg_sym_utc) {
|
342
|
+
tz += SPG_DB_UTC;
|
343
|
+
} else {
|
344
|
+
return rb_funcall(db, spg_id_to_application_timestamp, 1, rb_str_new2(s));
|
345
|
+
}
|
346
|
+
}
|
347
|
+
|
348
|
+
rtz = rb_funcall(spg_Sequel, spg_id_application_timezone, 0);
|
349
|
+
if (rtz != Qnil) {
|
350
|
+
if (rtz == spg_sym_local) {
|
351
|
+
tz += SPG_APP_LOCAL;
|
352
|
+
} else if (rtz == spg_sym_utc) {
|
353
|
+
tz += SPG_APP_UTC;
|
354
|
+
} else {
|
355
|
+
return rb_funcall(db, spg_id_to_application_timestamp, 1, rb_str_new2(s));
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
if (0 != strchr(s, '.')) {
|
360
|
+
tokens = sscanf(s, "%d-%2d-%2d %2d:%2d:%2d.%n%d%n%c%02d:%02d",
|
361
|
+
&year, &month, &day, &hour, &min, &sec,
|
362
|
+
&usec_start, &usec, &usec_stop,
|
363
|
+
&offset_sign, &offset_hour, &offset_minute);
|
364
|
+
if(tokens < 7) {
|
365
|
+
return spg_timestamp_error(s, self, "unexpected datetime format");
|
366
|
+
}
|
367
|
+
usec *= (int) pow(10, (6 - (usec_stop - usec_start)));
|
368
|
+
} else {
|
369
|
+
tokens = sscanf(s, "%d-%2d-%2d %2d:%2d:%2d%c%02d:%02d",
|
370
|
+
&year, &month, &day, &hour, &min, &sec,
|
371
|
+
&offset_sign, &offset_hour, &offset_minute);
|
372
|
+
if (tokens == 3) {
|
373
|
+
hour = 0;
|
374
|
+
min = 0;
|
375
|
+
sec = 0;
|
376
|
+
} else if (tokens < 6) {
|
377
|
+
return spg_timestamp_error(s, self, "unexpected datetime format");
|
378
|
+
}
|
379
|
+
usec = 0;
|
380
|
+
}
|
381
|
+
|
382
|
+
if (offset_sign == '-') {
|
383
|
+
offset_hour *= -1;
|
384
|
+
offset_minute *= -1;
|
385
|
+
}
|
386
|
+
|
387
|
+
dtc = rb_funcall(spg_Sequel, spg_id_datetime_class, 0);
|
388
|
+
|
389
|
+
if (dtc == rb_cTime) {
|
390
|
+
if (offset_sign) {
|
391
|
+
/* Offset given, convert to local time if not already in local time.
|
392
|
+
* While PostgreSQL generally returns timestamps in local time, it's unwise to rely on this.
|
393
|
+
*/
|
394
|
+
dt = rb_funcall(rb_cTime, spg_id_local, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), INT2NUM(usec));
|
395
|
+
utc_offset = NUM2INT(rb_funcall(dt, spg_id_utc_offset, 0));
|
396
|
+
offset_seconds = offset_hour * 3600 + offset_minute * 60;
|
397
|
+
if (utc_offset != offset_seconds) {
|
398
|
+
dt = rb_funcall(dt, spg_id_op_plus, 1, INT2NUM(utc_offset - offset_seconds));
|
399
|
+
}
|
400
|
+
|
401
|
+
if (tz & SPG_APP_UTC) {
|
402
|
+
dt = rb_funcall(dt, spg_id_utc, 0);
|
403
|
+
}
|
404
|
+
return dt;
|
405
|
+
} else if (tz == SPG_NO_TZ) {
|
406
|
+
return rb_funcall(rb_cTime, spg_id_local, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), INT2NUM(usec));
|
407
|
+
}
|
408
|
+
|
409
|
+
/* No offset given, and some timezone combination given */
|
410
|
+
if (tz & SPG_DB_UTC) {
|
411
|
+
dt = rb_funcall(rb_cTime, spg_id_utc, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), INT2NUM(usec));
|
412
|
+
if (tz & SPG_APP_LOCAL) {
|
413
|
+
return rb_funcall(dt, spg_id_localtime, 0);
|
414
|
+
} else {
|
415
|
+
return dt;
|
416
|
+
}
|
417
|
+
} else {
|
418
|
+
dt = rb_funcall(rb_cTime, spg_id_local, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), INT2NUM(usec));
|
419
|
+
if (tz & SPG_APP_UTC) {
|
420
|
+
return rb_funcall(dt, spg_id_utc, 0);
|
421
|
+
} else {
|
422
|
+
return dt;
|
423
|
+
}
|
424
|
+
}
|
425
|
+
} else {
|
426
|
+
/* datetime.class == DateTime */
|
427
|
+
if (offset_sign) {
|
428
|
+
/* Offset given, handle correct local time.
|
429
|
+
* While PostgreSQL generally returns timestamps in local time, it's unwise to rely on this.
|
430
|
+
*/
|
431
|
+
offset_fraction = offset_hour/24.0 + offset_minute/SPG_MINUTES_PER_DAY;
|
432
|
+
dt = rb_funcall(dtc, spg_id_new, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), rb_float_new(offset_fraction));
|
433
|
+
SPG_DT_ADD_USEC
|
434
|
+
|
435
|
+
if (tz & SPG_APP_LOCAL) {
|
436
|
+
utc_offset = NUM2INT(rb_funcall(rb_funcall(rb_cTime, spg_id_new, 0), spg_id_utc_offset, 0))/SPG_SECONDS_PER_DAY;
|
437
|
+
dt = rb_funcall(dt, spg_id_new_offset, 1, rb_float_new(utc_offset));
|
438
|
+
} else if (tz & SPG_APP_UTC) {
|
439
|
+
dt = rb_funcall(dt, spg_id_new_offset, 1, INT2NUM(0));
|
440
|
+
}
|
441
|
+
return dt;
|
442
|
+
} else if (tz == SPG_NO_TZ) {
|
443
|
+
dt = rb_funcall(dtc, spg_id_new, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
|
444
|
+
SPG_DT_ADD_USEC
|
445
|
+
return dt;
|
446
|
+
}
|
447
|
+
|
448
|
+
/* No offset given, and some timezone combination given */
|
449
|
+
if (tz & SPG_DB_LOCAL) {
|
450
|
+
offset_fraction = NUM2INT(rb_funcall(rb_funcall(rb_cTime, spg_id_local, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec)), spg_id_utc_offset, 0))/SPG_SECONDS_PER_DAY;
|
451
|
+
dt = rb_funcall(dtc, spg_id_new, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), rb_float_new(offset_fraction));
|
452
|
+
SPG_DT_ADD_USEC
|
453
|
+
if (tz & SPG_APP_UTC) {
|
454
|
+
return rb_funcall(dt, spg_id_new_offset, 1, INT2NUM(0));
|
455
|
+
} else {
|
456
|
+
return dt;
|
457
|
+
}
|
458
|
+
} else {
|
459
|
+
dt = rb_funcall(dtc, spg_id_new, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
|
460
|
+
SPG_DT_ADD_USEC
|
461
|
+
if (tz & SPG_APP_LOCAL) {
|
462
|
+
offset_fraction = NUM2INT(rb_funcall(rb_funcall(rb_cTime, spg_id_local, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec)), spg_id_utc_offset, 0))/SPG_SECONDS_PER_DAY;
|
463
|
+
return rb_funcall(dt, spg_id_new_offset, 1, rb_float_new(offset_fraction));
|
464
|
+
} else {
|
465
|
+
return dt;
|
466
|
+
}
|
467
|
+
}
|
468
|
+
}
|
469
|
+
}
|
470
|
+
|
471
|
+
static VALUE spg_fetch_rows_set_cols(VALUE self, VALUE ignore) {
|
472
|
+
return Qnil;
|
473
|
+
}
|
474
|
+
|
475
|
+
static VALUE spg__col_value(VALUE self, PGresult *res, long i, long j, VALUE* colconvert
|
476
|
+
#ifdef SPG_ENCODING
|
477
|
+
, int enc_index
|
478
|
+
#endif
|
479
|
+
) {
|
480
|
+
char *v;
|
481
|
+
VALUE rv;
|
482
|
+
size_t l;
|
483
|
+
|
484
|
+
if(PQgetisnull(res, i, j)) {
|
485
|
+
rv = Qnil;
|
486
|
+
} else {
|
487
|
+
v = PQgetvalue(res, i, j);
|
488
|
+
|
489
|
+
switch(PQftype(res, j)) {
|
490
|
+
case 16: /* boolean */
|
491
|
+
rv = *v == 't' ? Qtrue : Qfalse;
|
492
|
+
break;
|
493
|
+
case 17: /* bytea */
|
494
|
+
v = (char *)PQunescapeBytea((unsigned char*)v, &l);
|
495
|
+
rv = rb_funcall(spg_Blob, spg_id_new, 1, rb_str_new(v, l));
|
496
|
+
PQfreemem(v);
|
497
|
+
break;
|
498
|
+
case 20: /* integer */
|
499
|
+
case 21:
|
500
|
+
case 23:
|
501
|
+
case 26:
|
502
|
+
rv = rb_cstr2inum(v, 10);
|
503
|
+
break;
|
504
|
+
case 700: /* float */
|
505
|
+
case 701:
|
506
|
+
if (strcmp("NaN", v) == 0) {
|
507
|
+
rv = spg_nan;
|
508
|
+
} else if (strcmp("Infinity", v) == 0) {
|
509
|
+
rv = spg_pos_inf;
|
510
|
+
} else if (strcmp("-Infinity", v) == 0) {
|
511
|
+
rv = spg_neg_inf;
|
512
|
+
} else {
|
513
|
+
rv = rb_float_new(rb_cstr_to_dbl(v, Qfalse));
|
514
|
+
}
|
515
|
+
break;
|
516
|
+
case 1700: /* numeric */
|
517
|
+
rv = rb_funcall(spg_BigDecimal, spg_id_new, 1, rb_str_new(v, PQgetlength(res, i, j)));
|
518
|
+
break;
|
519
|
+
case 1082: /* date */
|
520
|
+
rv = spg_date(v, self);
|
521
|
+
break;
|
522
|
+
case 1083: /* time */
|
523
|
+
case 1266:
|
524
|
+
rv = spg_time(v);
|
525
|
+
break;
|
526
|
+
case 1114: /* timestamp */
|
527
|
+
case 1184:
|
528
|
+
rv = spg_timestamp(v, self);
|
529
|
+
break;
|
530
|
+
case 18: /* char */
|
531
|
+
case 25: /* text */
|
532
|
+
case 1043: /* varchar*/
|
533
|
+
rv = rb_tainted_str_new(v, PQgetlength(res, i, j));
|
534
|
+
#ifdef SPG_ENCODING
|
535
|
+
rb_enc_associate_index(rv, enc_index);
|
536
|
+
#endif
|
537
|
+
break;
|
538
|
+
default:
|
539
|
+
rv = rb_tainted_str_new(v, PQgetlength(res, i, j));
|
540
|
+
#ifdef SPG_ENCODING
|
541
|
+
rb_enc_associate_index(rv, enc_index);
|
542
|
+
#endif
|
543
|
+
if (colconvert[j] != Qnil) {
|
544
|
+
rv = rb_funcall(colconvert[j], spg_id_call, 1, rv);
|
545
|
+
}
|
546
|
+
}
|
547
|
+
}
|
548
|
+
return rv;
|
549
|
+
}
|
550
|
+
|
551
|
+
static VALUE spg__col_values(VALUE self, VALUE v, VALUE *colsyms, long nfields, PGresult *res, long i, VALUE *colconvert
|
552
|
+
#ifdef SPG_ENCODING
|
553
|
+
, int enc_index
|
554
|
+
#endif
|
555
|
+
) {
|
556
|
+
long j;
|
557
|
+
VALUE cur;
|
558
|
+
long len = RARRAY_LEN(v);
|
559
|
+
VALUE a = rb_ary_new2(len);
|
560
|
+
for (j=0; j<len; j++) {
|
561
|
+
cur = rb_ary_entry(v, j);
|
562
|
+
rb_ary_store(a, j, cur == Qnil ? Qnil : spg__col_value(self, res, i, NUM2LONG(cur), colconvert ENC_INDEX));
|
563
|
+
}
|
564
|
+
return a;
|
565
|
+
}
|
566
|
+
|
567
|
+
static long spg__field_id(VALUE v, VALUE *colsyms, long nfields) {
|
568
|
+
long j;
|
569
|
+
for (j=0; j<nfields; j++) {
|
570
|
+
if (colsyms[j] == v) {
|
571
|
+
return j;
|
572
|
+
}
|
573
|
+
}
|
574
|
+
return -1;
|
575
|
+
}
|
576
|
+
|
577
|
+
static VALUE spg__field_ids(VALUE v, VALUE *colsyms, long nfields) {
|
578
|
+
long i;
|
579
|
+
long j;
|
580
|
+
VALUE cur;
|
581
|
+
long len = RARRAY_LEN(v);
|
582
|
+
VALUE pg_columns = rb_ary_new2(len);
|
583
|
+
for (i=0; i<len; i++) {
|
584
|
+
cur = rb_ary_entry(v, i);
|
585
|
+
j = spg__field_id(cur, colsyms, nfields);
|
586
|
+
rb_ary_store(pg_columns, i, j == -1 ? Qnil : LONG2NUM(j));
|
587
|
+
}
|
588
|
+
return pg_columns;
|
589
|
+
}
|
590
|
+
|
591
|
+
static void spg_set_columns(VALUE self, VALUE cols) {
|
592
|
+
if (use_columns_method) {
|
593
|
+
rb_funcall(self, spg_id_columns_equal, 1, cols);
|
594
|
+
} else {
|
595
|
+
rb_ivar_set(self, spg_id_columns, cols);
|
596
|
+
}
|
597
|
+
}
|
598
|
+
|
599
|
+
static void spg_set_column_info(VALUE self, PGresult *res, VALUE *colsyms, VALUE *colconvert) {
|
600
|
+
long i;
|
601
|
+
long j;
|
602
|
+
long nfields;
|
603
|
+
VALUE conv_procs = 0;
|
604
|
+
|
605
|
+
nfields = PQnfields(res);
|
606
|
+
|
607
|
+
for(j=0; j<nfields; j++) {
|
608
|
+
colsyms[j] = rb_funcall(self, spg_id_output_identifier, 1, rb_str_new2(PQfname(res, j)));
|
609
|
+
i = PQftype(res, j);
|
610
|
+
switch (i) {
|
611
|
+
case 16:
|
612
|
+
case 17:
|
613
|
+
case 20:
|
614
|
+
case 21:
|
615
|
+
case 23:
|
616
|
+
case 26:
|
617
|
+
case 700:
|
618
|
+
case 701:
|
619
|
+
case 790:
|
620
|
+
case 1700:
|
621
|
+
case 1082:
|
622
|
+
case 1083:
|
623
|
+
case 1266:
|
624
|
+
case 1114:
|
625
|
+
case 1184:
|
626
|
+
case 18:
|
627
|
+
case 25:
|
628
|
+
case 1043:
|
629
|
+
colconvert[j] = Qnil;
|
630
|
+
break;
|
631
|
+
default:
|
632
|
+
if (conv_procs == 0) {
|
633
|
+
conv_procs = rb_funcall(rb_funcall(self, spg_id_db, 0), spg_id_conversion_procs, 0);
|
634
|
+
}
|
635
|
+
colconvert[j] = rb_funcall(conv_procs, spg_id_get, 1, INT2NUM(i));
|
636
|
+
break;
|
637
|
+
}
|
638
|
+
}
|
639
|
+
spg_set_columns(self, rb_ary_new4(nfields, colsyms));
|
640
|
+
}
|
641
|
+
|
642
|
+
static VALUE spg_yield_hash_rows(VALUE self, VALUE rres, VALUE ignore) {
|
643
|
+
PGresult *res;
|
644
|
+
VALUE colsyms[SPG_MAX_FIELDS];
|
645
|
+
VALUE colconvert[SPG_MAX_FIELDS];
|
646
|
+
long ntuples;
|
647
|
+
long nfields;
|
648
|
+
long i;
|
649
|
+
long j;
|
650
|
+
VALUE h;
|
651
|
+
VALUE opts;
|
652
|
+
VALUE pg_type;
|
653
|
+
VALUE pg_value;
|
654
|
+
char type = SPG_YIELD_NORMAL;
|
655
|
+
|
656
|
+
if (!RTEST(rres)) {
|
657
|
+
return self;
|
658
|
+
}
|
659
|
+
GetPGresult(rres, res);
|
660
|
+
|
661
|
+
#ifdef SPG_ENCODING
|
662
|
+
int enc_index;
|
663
|
+
enc_index = enc_get_index(rres);
|
664
|
+
#endif
|
665
|
+
|
666
|
+
ntuples = PQntuples(res);
|
667
|
+
nfields = PQnfields(res);
|
668
|
+
if (nfields > SPG_MAX_FIELDS) {
|
669
|
+
rb_raise(rb_eRangeError, "more than %d columns in query (%ld columns detected)", SPG_MAX_FIELDS, nfields);
|
670
|
+
}
|
671
|
+
|
672
|
+
spg_set_column_info(self, res, colsyms, colconvert);
|
673
|
+
|
674
|
+
opts = rb_funcall(self, spg_id_opts, 0);
|
675
|
+
if (rb_type(opts) == T_HASH) {
|
676
|
+
pg_type = rb_hash_aref(opts, spg_sym__sequel_pg_type);
|
677
|
+
pg_value = rb_hash_aref(opts, spg_sym__sequel_pg_value);
|
678
|
+
if (SYMBOL_P(pg_type)) {
|
679
|
+
if (pg_type == spg_sym_map) {
|
680
|
+
if (SYMBOL_P(pg_value)) {
|
681
|
+
type = SPG_YIELD_COLUMN;
|
682
|
+
} else if (rb_type(pg_value) == T_ARRAY) {
|
683
|
+
type = SPG_YIELD_COLUMNS;
|
684
|
+
}
|
685
|
+
} else if (pg_type == spg_sym_first) {
|
686
|
+
type = SPG_YIELD_FIRST;
|
687
|
+
} else if (pg_type == spg_sym_array) {
|
688
|
+
type = SPG_YIELD_ARRAY;
|
689
|
+
} else if ((pg_type == spg_sym_hash || pg_type == spg_sym_hash_groups) && rb_type(pg_value) == T_ARRAY) {
|
690
|
+
VALUE pg_value_key, pg_value_value;
|
691
|
+
pg_value_key = rb_ary_entry(pg_value, 0);
|
692
|
+
pg_value_value = rb_ary_entry(pg_value, 1);
|
693
|
+
if (SYMBOL_P(pg_value_key)) {
|
694
|
+
if (SYMBOL_P(pg_value_value)) {
|
695
|
+
type = pg_type == spg_sym_hash_groups ? SPG_YIELD_KV_HASH_GROUPS : SPG_YIELD_KV_HASH;
|
696
|
+
} else if (rb_type(pg_value_value) == T_ARRAY) {
|
697
|
+
type = pg_type == spg_sym_hash_groups ? SPG_YIELD_KMV_HASH_GROUPS : SPG_YIELD_KMV_HASH;
|
698
|
+
}
|
699
|
+
} else if (rb_type(pg_value_key) == T_ARRAY) {
|
700
|
+
if (SYMBOL_P(pg_value_value)) {
|
701
|
+
type = pg_type == spg_sym_hash_groups ? SPG_YIELD_MKV_HASH_GROUPS : SPG_YIELD_MKV_HASH;
|
702
|
+
} else if (rb_type(pg_value_value) == T_ARRAY) {
|
703
|
+
type = pg_type == spg_sym_hash_groups ? SPG_YIELD_MKMV_HASH_GROUPS : SPG_YIELD_MKMV_HASH;
|
704
|
+
}
|
705
|
+
}
|
706
|
+
} else if (pg_type == spg_sym_model && rb_type(pg_value) == T_CLASS) {
|
707
|
+
type = SPG_YIELD_MODEL;
|
708
|
+
}
|
709
|
+
}
|
710
|
+
}
|
711
|
+
|
712
|
+
switch(type) {
|
713
|
+
case SPG_YIELD_NORMAL:
|
714
|
+
/* Normal, hash for entire row */
|
715
|
+
for(i=0; i<ntuples; i++) {
|
716
|
+
h = rb_hash_new();
|
717
|
+
for(j=0; j<nfields; j++) {
|
718
|
+
rb_hash_aset(h, colsyms[j], spg__col_value(self, res, i, j, colconvert ENC_INDEX));
|
719
|
+
}
|
720
|
+
rb_yield(h);
|
721
|
+
}
|
722
|
+
break;
|
723
|
+
case SPG_YIELD_COLUMN:
|
724
|
+
/* Single column */
|
725
|
+
j = spg__field_id(pg_value, colsyms, nfields);
|
726
|
+
if (j == -1) {
|
727
|
+
for(i=0; i<ntuples; i++) {
|
728
|
+
rb_yield(Qnil);
|
729
|
+
}
|
730
|
+
} else {
|
731
|
+
for(i=0; i<ntuples; i++) {
|
732
|
+
rb_yield(spg__col_value(self, res, i, j, colconvert ENC_INDEX));
|
733
|
+
}
|
734
|
+
}
|
735
|
+
break;
|
736
|
+
case SPG_YIELD_COLUMNS:
|
737
|
+
/* Multiple columns as an array */
|
738
|
+
h = spg__field_ids(pg_value, colsyms, nfields);
|
739
|
+
for(i=0; i<ntuples; i++) {
|
740
|
+
rb_yield(spg__col_values(self, h, colsyms, nfields, res, i, colconvert ENC_INDEX));
|
741
|
+
}
|
742
|
+
break;
|
743
|
+
case SPG_YIELD_FIRST:
|
744
|
+
/* First column */
|
745
|
+
for(i=0; i<ntuples; i++) {
|
746
|
+
rb_yield(spg__col_value(self, res, i, 0, colconvert ENC_INDEX));
|
747
|
+
}
|
748
|
+
break;
|
749
|
+
case SPG_YIELD_ARRAY:
|
750
|
+
/* Array of all columns */
|
751
|
+
for(i=0; i<ntuples; i++) {
|
752
|
+
h = rb_ary_new2(nfields);
|
753
|
+
for(j=0; j<nfields; j++) {
|
754
|
+
rb_ary_store(h, j, spg__col_value(self, res, i, j, colconvert ENC_INDEX));
|
755
|
+
}
|
756
|
+
rb_yield(h);
|
757
|
+
}
|
758
|
+
break;
|
759
|
+
case SPG_YIELD_KV_HASH:
|
760
|
+
case SPG_YIELD_KV_HASH_GROUPS:
|
761
|
+
/* Hash with single key and single value */
|
762
|
+
{
|
763
|
+
VALUE k, v;
|
764
|
+
h = rb_hash_new();
|
765
|
+
k = spg__field_id(rb_ary_entry(pg_value, 0), colsyms, nfields);
|
766
|
+
v = spg__field_id(rb_ary_entry(pg_value, 1), colsyms, nfields);
|
767
|
+
if(type == SPG_YIELD_KV_HASH) {
|
768
|
+
for(i=0; i<ntuples; i++) {
|
769
|
+
rb_hash_aset(h, spg__col_value(self, res, i, k, colconvert ENC_INDEX), spg__col_value(self, res, i, v, colconvert ENC_INDEX));
|
770
|
+
}
|
771
|
+
} else {
|
772
|
+
VALUE kv, vv, a;
|
773
|
+
for(i=0; i<ntuples; i++) {
|
774
|
+
kv = spg__col_value(self, res, i, k, colconvert ENC_INDEX);
|
775
|
+
vv = spg__col_value(self, res, i, v, colconvert ENC_INDEX);
|
776
|
+
a = rb_hash_lookup(h, kv);
|
777
|
+
if(!RTEST(a)) {
|
778
|
+
rb_hash_aset(h, kv, rb_ary_new3(1, vv));
|
779
|
+
} else {
|
780
|
+
rb_ary_push(a, vv);
|
781
|
+
}
|
782
|
+
}
|
783
|
+
}
|
784
|
+
rb_yield(h);
|
785
|
+
}
|
786
|
+
break;
|
787
|
+
case SPG_YIELD_MKV_HASH:
|
788
|
+
case SPG_YIELD_MKV_HASH_GROUPS:
|
789
|
+
/* Hash with array of keys and single value */
|
790
|
+
{
|
791
|
+
VALUE k, v;
|
792
|
+
h = rb_hash_new();
|
793
|
+
k = spg__field_ids(rb_ary_entry(pg_value, 0), colsyms, nfields);
|
794
|
+
v = spg__field_id(rb_ary_entry(pg_value, 1), colsyms, nfields);
|
795
|
+
if(type == SPG_YIELD_MKV_HASH) {
|
796
|
+
for(i=0; i<ntuples; i++) {
|
797
|
+
rb_hash_aset(h, spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX), spg__col_value(self, res, i, v, colconvert ENC_INDEX));
|
798
|
+
}
|
799
|
+
} else {
|
800
|
+
VALUE kv, vv, a;
|
801
|
+
for(i=0; i<ntuples; i++) {
|
802
|
+
kv = spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX);
|
803
|
+
vv = spg__col_value(self, res, i, v, colconvert ENC_INDEX);
|
804
|
+
a = rb_hash_lookup(h, kv);
|
805
|
+
if(!RTEST(a)) {
|
806
|
+
rb_hash_aset(h, kv, rb_ary_new3(1, vv));
|
807
|
+
} else {
|
808
|
+
rb_ary_push(a, vv);
|
809
|
+
}
|
810
|
+
}
|
811
|
+
}
|
812
|
+
rb_yield(h);
|
813
|
+
}
|
814
|
+
break;
|
815
|
+
case SPG_YIELD_KMV_HASH:
|
816
|
+
case SPG_YIELD_KMV_HASH_GROUPS:
|
817
|
+
/* Hash with single keys and array of values */
|
818
|
+
{
|
819
|
+
VALUE k, v;
|
820
|
+
h = rb_hash_new();
|
821
|
+
k = spg__field_id(rb_ary_entry(pg_value, 0), colsyms, nfields);
|
822
|
+
v = spg__field_ids(rb_ary_entry(pg_value, 1), colsyms, nfields);
|
823
|
+
if(type == SPG_YIELD_KMV_HASH) {
|
824
|
+
for(i=0; i<ntuples; i++) {
|
825
|
+
rb_hash_aset(h, spg__col_value(self, res, i, k, colconvert ENC_INDEX), spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX));
|
826
|
+
}
|
827
|
+
} else {
|
828
|
+
VALUE kv, vv, a;
|
829
|
+
for(i=0; i<ntuples; i++) {
|
830
|
+
kv = spg__col_value(self, res, i, k, colconvert ENC_INDEX);
|
831
|
+
vv = spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX);
|
832
|
+
a = rb_hash_lookup(h, kv);
|
833
|
+
if(!RTEST(a)) {
|
834
|
+
rb_hash_aset(h, kv, rb_ary_new3(1, vv));
|
835
|
+
} else {
|
836
|
+
rb_ary_push(a, vv);
|
837
|
+
}
|
838
|
+
}
|
839
|
+
}
|
840
|
+
rb_yield(h);
|
841
|
+
}
|
842
|
+
break;
|
843
|
+
case SPG_YIELD_MKMV_HASH:
|
844
|
+
case SPG_YIELD_MKMV_HASH_GROUPS:
|
845
|
+
/* Hash with array of keys and array of values */
|
846
|
+
{
|
847
|
+
VALUE k, v;
|
848
|
+
h = rb_hash_new();
|
849
|
+
k = spg__field_ids(rb_ary_entry(pg_value, 0), colsyms, nfields);
|
850
|
+
v = spg__field_ids(rb_ary_entry(pg_value, 1), colsyms, nfields);
|
851
|
+
if(type == SPG_YIELD_MKMV_HASH) {
|
852
|
+
for(i=0; i<ntuples; i++) {
|
853
|
+
rb_hash_aset(h, spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX), spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX));
|
854
|
+
}
|
855
|
+
} else {
|
856
|
+
VALUE kv, vv, a;
|
857
|
+
for(i=0; i<ntuples; i++) {
|
858
|
+
kv = spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX);
|
859
|
+
vv = spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX);
|
860
|
+
a = rb_hash_lookup(h, kv);
|
861
|
+
if(!RTEST(a)) {
|
862
|
+
rb_hash_aset(h, kv, rb_ary_new3(1, vv));
|
863
|
+
} else {
|
864
|
+
rb_ary_push(a, vv);
|
865
|
+
}
|
866
|
+
}
|
867
|
+
}
|
868
|
+
rb_yield(h);
|
869
|
+
}
|
870
|
+
break;
|
871
|
+
case SPG_YIELD_MODEL:
|
872
|
+
/* Model object for entire row */
|
873
|
+
for(i=0; i<ntuples; i++) {
|
874
|
+
h = rb_hash_new();
|
875
|
+
for(j=0; j<nfields; j++) {
|
876
|
+
rb_hash_aset(h, colsyms[j], spg__col_value(self, res, i, j, colconvert ENC_INDEX));
|
877
|
+
}
|
878
|
+
/* Abuse local variable */
|
879
|
+
pg_type = rb_obj_alloc(pg_value);
|
880
|
+
rb_ivar_set(pg_type, spg_id_values, h);
|
881
|
+
rb_yield(pg_type);
|
882
|
+
}
|
883
|
+
break;
|
884
|
+
}
|
885
|
+
return self;
|
886
|
+
}
|
887
|
+
|
888
|
+
static VALUE spg_supports_streaming_p(VALUE self) {
|
889
|
+
return
|
890
|
+
#if HAVE_PQSETSINGLEROWMODE
|
891
|
+
Qtrue;
|
892
|
+
#else
|
893
|
+
Qfalse;
|
894
|
+
#endif
|
895
|
+
}
|
896
|
+
|
897
|
+
#if HAVE_PQSETSINGLEROWMODE
|
898
|
+
static VALUE spg_set_single_row_mode(VALUE self) {
|
899
|
+
PGconn *conn;
|
900
|
+
GetPGconn(self, conn);
|
901
|
+
if (PQsetSingleRowMode(conn) != 1) {
|
902
|
+
rb_raise(spg_PGError, "cannot set single row mode");
|
903
|
+
}
|
904
|
+
return Qnil;
|
905
|
+
}
|
906
|
+
|
907
|
+
static VALUE spg__yield_each_row(VALUE self) {
|
908
|
+
PGconn *conn;
|
909
|
+
PGresult *res;
|
910
|
+
VALUE rres;
|
911
|
+
VALUE rconn;
|
912
|
+
VALUE colsyms[SPG_MAX_FIELDS];
|
913
|
+
VALUE colconvert[SPG_MAX_FIELDS];
|
914
|
+
long nfields;
|
915
|
+
long j;
|
916
|
+
VALUE h;
|
917
|
+
VALUE opts;
|
918
|
+
VALUE pg_type;
|
919
|
+
VALUE pg_value = Qnil;
|
920
|
+
char type = SPG_YIELD_NORMAL;
|
921
|
+
|
922
|
+
rconn = rb_ary_entry(self, 1);
|
923
|
+
self = rb_ary_entry(self, 0);
|
924
|
+
GetPGconn(rconn, conn);
|
925
|
+
|
926
|
+
rres = rb_funcall(rconn, spg_id_get_result, 0);
|
927
|
+
rb_funcall(rres, spg_id_check, 0);
|
928
|
+
GetPGresult(rres, res);
|
929
|
+
|
930
|
+
#ifdef SPG_ENCODING
|
931
|
+
int enc_index;
|
932
|
+
enc_index = enc_get_index(rres);
|
933
|
+
#endif
|
934
|
+
|
935
|
+
/* Only handle regular and model types. All other types require compiling all
|
936
|
+
* of the results at once, which is not a use case for streaming. The streaming
|
937
|
+
* code does not call this function for the other types. */
|
938
|
+
opts = rb_funcall(self, spg_id_opts, 0);
|
939
|
+
if (rb_type(opts) == T_HASH) {
|
940
|
+
pg_type = rb_hash_aref(opts, spg_sym__sequel_pg_type);
|
941
|
+
pg_value = rb_hash_aref(opts, spg_sym__sequel_pg_value);
|
942
|
+
if (SYMBOL_P(pg_type) && pg_type == spg_sym_model && rb_type(pg_value) == T_CLASS) {
|
943
|
+
type = SPG_YIELD_MODEL;
|
944
|
+
}
|
945
|
+
}
|
946
|
+
|
947
|
+
nfields = PQnfields(res);
|
948
|
+
if (nfields > SPG_MAX_FIELDS) {
|
949
|
+
rb_funcall(rres, spg_id_clear, 0);
|
950
|
+
rb_raise(rb_eRangeError, "more than %d columns in query", SPG_MAX_FIELDS);
|
951
|
+
}
|
952
|
+
|
953
|
+
spg_set_column_info(self, res, colsyms, colconvert);
|
954
|
+
|
955
|
+
while (PQntuples(res) != 0) {
|
956
|
+
h = rb_hash_new();
|
957
|
+
for(j=0; j<nfields; j++) {
|
958
|
+
rb_hash_aset(h, colsyms[j], spg__col_value(self, res, 0, j, colconvert ENC_INDEX));
|
959
|
+
}
|
960
|
+
|
961
|
+
rb_funcall(rres, spg_id_clear, 0);
|
962
|
+
|
963
|
+
if(type == SPG_YIELD_MODEL) {
|
964
|
+
/* Abuse local variable */
|
965
|
+
pg_type = rb_obj_alloc(pg_value);
|
966
|
+
rb_ivar_set(pg_type, spg_id_values, h);
|
967
|
+
rb_yield(pg_type);
|
968
|
+
} else {
|
969
|
+
rb_yield(h);
|
970
|
+
}
|
971
|
+
|
972
|
+
rres = rb_funcall(rconn, spg_id_get_result, 0);
|
973
|
+
rb_funcall(rres, spg_id_check, 0);
|
974
|
+
GetPGresult(rres, res);
|
975
|
+
}
|
976
|
+
rb_funcall(rres, spg_id_clear, 0);
|
977
|
+
|
978
|
+
return self;
|
979
|
+
}
|
980
|
+
|
981
|
+
static VALUE spg__flush_results(VALUE rconn) {
|
982
|
+
PGconn *conn;
|
983
|
+
PGresult *res;
|
984
|
+
VALUE error = 0;
|
985
|
+
GetPGconn(rconn, conn);
|
986
|
+
|
987
|
+
while ((res = PQgetResult(conn)) != NULL) {
|
988
|
+
if (!error) {
|
989
|
+
switch (PQresultStatus(res))
|
990
|
+
{
|
991
|
+
case PGRES_BAD_RESPONSE:
|
992
|
+
case PGRES_FATAL_ERROR:
|
993
|
+
case PGRES_NONFATAL_ERROR:
|
994
|
+
error = rb_str_new2(PQresultErrorMessage(res));
|
995
|
+
break;
|
996
|
+
default:
|
997
|
+
break;
|
998
|
+
}
|
999
|
+
}
|
1000
|
+
PQclear(res);
|
1001
|
+
}
|
1002
|
+
|
1003
|
+
if (error) {
|
1004
|
+
VALUE exception = rb_exc_new3(spg_PGError, error);
|
1005
|
+
rb_iv_set(exception, "@connection", rconn);
|
1006
|
+
rb_exc_raise(exception);
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
return rconn;
|
1010
|
+
}
|
1011
|
+
|
1012
|
+
static VALUE spg_yield_each_row(VALUE self, VALUE rconn) {
|
1013
|
+
VALUE v;
|
1014
|
+
v = rb_ary_new3(2, self, rconn);
|
1015
|
+
return rb_ensure(spg__yield_each_row, v, spg__flush_results, rconn);
|
1016
|
+
}
|
1017
|
+
#endif
|
1018
|
+
|
1019
|
+
void Init_sequel_pg(void) {
|
1020
|
+
VALUE c, spg_Postgres;
|
1021
|
+
ID cg;
|
1022
|
+
cg = rb_intern("const_get");
|
1023
|
+
|
1024
|
+
spg_Sequel = rb_funcall(rb_cObject, cg, 1, rb_str_new2("Sequel"));
|
1025
|
+
spg_Postgres = rb_funcall(spg_Sequel, cg, 1, rb_str_new2("Postgres"));
|
1026
|
+
|
1027
|
+
if(rb_obj_respond_to(spg_Postgres, rb_intern("sequel_pg_version_supported?"), 0)) {
|
1028
|
+
if(!RTEST(rb_funcall(spg_Postgres, rb_intern("sequel_pg_version_supported?"), 1, INT2FIX(SEQUEL_PG_VERSION_INTEGER)))) {
|
1029
|
+
rb_warn("sequel_pg not loaded as it is not compatible with the Sequel version in use; install the latest version of sequel_pg or uninstall sequel_pg");
|
1030
|
+
return;
|
1031
|
+
}
|
1032
|
+
}
|
1033
|
+
|
1034
|
+
spg_id_new = rb_intern("new");
|
1035
|
+
spg_id_local = rb_intern("local");
|
1036
|
+
spg_id_year = rb_intern("year");
|
1037
|
+
spg_id_month = rb_intern("month");
|
1038
|
+
spg_id_day = rb_intern("day");
|
1039
|
+
spg_id_output_identifier = rb_intern("output_identifier");
|
1040
|
+
spg_id_datetime_class = rb_intern("datetime_class");
|
1041
|
+
spg_id_application_timezone = rb_intern("application_timezone");
|
1042
|
+
spg_id_to_application_timestamp = rb_intern("to_application_timestamp");
|
1043
|
+
spg_id_timezone = rb_intern("timezone");
|
1044
|
+
spg_id_op_plus = rb_intern("+");
|
1045
|
+
spg_id_utc = rb_intern("utc");
|
1046
|
+
spg_id_utc_offset = rb_intern("utc_offset");
|
1047
|
+
spg_id_localtime = rb_intern("localtime");
|
1048
|
+
spg_id_new_offset = rb_intern("new_offset");
|
1049
|
+
spg_id_convert_infinite_timestamps = rb_intern("convert_infinite_timestamps");
|
1050
|
+
spg_id_infinite_timestamp_value = rb_intern("infinite_timestamp_value");
|
1051
|
+
|
1052
|
+
spg_id_call = rb_intern("call");
|
1053
|
+
spg_id_get = rb_intern("[]");
|
1054
|
+
|
1055
|
+
spg_id_opts = rb_intern("opts");
|
1056
|
+
|
1057
|
+
spg_id_db = rb_intern("db");
|
1058
|
+
spg_id_conversion_procs = rb_intern("conversion_procs");
|
1059
|
+
spg_id_columns_equal = rb_intern("columns=");
|
1060
|
+
|
1061
|
+
spg_id_columns = rb_intern("@columns");
|
1062
|
+
spg_id_encoding = rb_intern("@encoding");
|
1063
|
+
spg_id_values = rb_intern("@values");
|
1064
|
+
|
1065
|
+
spg_sym_utc = ID2SYM(rb_intern("utc"));
|
1066
|
+
spg_sym_local = ID2SYM(rb_intern("local"));
|
1067
|
+
spg_sym_map = ID2SYM(rb_intern("map"));
|
1068
|
+
spg_sym_first = ID2SYM(rb_intern("first"));
|
1069
|
+
spg_sym_array = ID2SYM(rb_intern("array"));
|
1070
|
+
spg_sym_hash = ID2SYM(rb_intern("hash"));
|
1071
|
+
spg_sym_hash_groups = ID2SYM(rb_intern("hash_groups"));
|
1072
|
+
spg_sym_model = ID2SYM(rb_intern("model"));
|
1073
|
+
spg_sym__sequel_pg_type = ID2SYM(rb_intern("_sequel_pg_type"));
|
1074
|
+
spg_sym__sequel_pg_value = ID2SYM(rb_intern("_sequel_pg_value"));
|
1075
|
+
|
1076
|
+
spg_Blob = rb_funcall(rb_funcall(spg_Sequel, cg, 1, rb_str_new2("SQL")), cg, 1, rb_str_new2("Blob"));
|
1077
|
+
spg_SQLTime= rb_funcall(spg_Sequel, cg, 1, rb_str_new2("SQLTime"));
|
1078
|
+
spg_BigDecimal = rb_funcall(rb_cObject, cg, 1, rb_str_new2("BigDecimal"));
|
1079
|
+
spg_Date = rb_funcall(rb_cObject, cg, 1, rb_str_new2("Date"));
|
1080
|
+
spg_PGError = rb_eval_string("defined?(PG::Error) ? PG::Error : PGError");
|
1081
|
+
|
1082
|
+
spg_nan = rb_eval_string("0.0/0.0");
|
1083
|
+
spg_pos_inf = rb_eval_string("1.0/0.0");
|
1084
|
+
spg_neg_inf = rb_eval_string("-1.0/0.0");
|
1085
|
+
|
1086
|
+
rb_global_variable(&spg_Sequel);
|
1087
|
+
rb_global_variable(&spg_Blob);
|
1088
|
+
rb_global_variable(&spg_BigDecimal);
|
1089
|
+
rb_global_variable(&spg_Date);
|
1090
|
+
rb_global_variable(&spg_SQLTime);
|
1091
|
+
rb_global_variable(&spg_PGError);
|
1092
|
+
rb_global_variable(&spg_nan);
|
1093
|
+
rb_global_variable(&spg_pos_inf);
|
1094
|
+
rb_global_variable(&spg_neg_inf);
|
1095
|
+
|
1096
|
+
/* Check for 1.8-1.9.2 stdlib date that needs Rational for usec accuracy */
|
1097
|
+
if (rb_attr_get(rb_eval_string("Date.today"), rb_intern("@ajd")) != Qnil) {
|
1098
|
+
spg_id_Rational = rb_intern("Rational");
|
1099
|
+
}
|
1100
|
+
if (rb_eval_string("defined?(PG::TypeMapAllStrings)") != Qnil) {
|
1101
|
+
unwrap_structs = 1;
|
1102
|
+
}
|
1103
|
+
|
1104
|
+
c = rb_funcall(spg_Postgres, cg, 1, rb_str_new2("Dataset"));
|
1105
|
+
rb_undef_method(c, "yield_hash_rows");
|
1106
|
+
rb_define_private_method(c, "yield_hash_rows", spg_yield_hash_rows, 2);
|
1107
|
+
rb_undef_method(c, "fetch_rows_set_cols");
|
1108
|
+
rb_define_private_method(c, "fetch_rows_set_cols", spg_fetch_rows_set_cols, 1);
|
1109
|
+
|
1110
|
+
if (rb_eval_string("Sequel::Dataset.private_method_defined?(:columns=)") == Qtrue) {
|
1111
|
+
use_columns_method = 1;
|
1112
|
+
}
|
1113
|
+
|
1114
|
+
rb_define_singleton_method(spg_Postgres, "supports_streaming?", spg_supports_streaming_p, 0);
|
1115
|
+
|
1116
|
+
#if HAVE_PQSETSINGLEROWMODE
|
1117
|
+
spg_id_get_result = rb_intern("get_result");
|
1118
|
+
spg_id_clear = rb_intern("clear");
|
1119
|
+
spg_id_check = rb_intern("check");
|
1120
|
+
|
1121
|
+
rb_define_private_method(c, "yield_each_row", spg_yield_each_row, 1);
|
1122
|
+
c = rb_funcall(spg_Postgres, cg, 1, rb_str_new2("Adapter"));
|
1123
|
+
rb_define_private_method(c, "set_single_row_mode", spg_set_single_row_mode, 0);
|
1124
|
+
#endif
|
1125
|
+
|
1126
|
+
rb_define_singleton_method(spg_Postgres, "parse_pg_array", parse_pg_array, 2);
|
1127
|
+
|
1128
|
+
rb_require("sequel_pg/sequel_pg");
|
1129
|
+
}
|