postgres 0.7.9.2008.01.28 → 0.8.0

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.
@@ -1,2772 +0,0 @@
1
- /************************************************
2
-
3
- pg.c -
4
-
5
- Author: matz
6
- created at: Tue May 13 20:07:35 JST 1997
7
-
8
- Author: ematsu
9
- modified at: Wed Jan 20 16:41:51 1999
10
-
11
- $Author: jdavis $
12
- $Date: 2007-12-07 09:51:23 -0800 (Fri, 07 Dec 2007) $
13
- ************************************************/
14
-
15
- #include "ruby.h"
16
- #include "rubyio.h"
17
-
18
- #if RUBY_VM != 1
19
- #define RUBY_18_COMPAT
20
- #endif
21
-
22
- #ifdef RUBY_18_COMPAT
23
- #include "st.h"
24
- #include "intern.h"
25
- #else
26
- #include "ruby/st.h"
27
- #include "ruby/intern.h"
28
- #endif
29
-
30
- /* grep '^#define' $(pg_config --includedir)/server/catalog/pg_type.h | grep OID */
31
- #include "type-oids.h"
32
- #include <libpq-fe.h>
33
- #include <libpq/libpq-fs.h> /* large-object interface */
34
- #include <stdio.h>
35
- #include <stdlib.h>
36
- #include <sys/types.h>
37
-
38
- #ifndef HAVE_PQSERVERVERSION
39
- static int
40
- PQserverVersion(const PGconn *conn)
41
- {
42
- rb_raise(rb_eArgError,"this version of libpq doesn't support PQserverVersion");
43
- }
44
- #endif /* HAVE_PQSERVERVERSION */
45
-
46
- #ifndef RHASH_SIZE
47
- #define RHASH_SIZE(x) RHASH((x))->tbl->num_entries
48
- #endif /* RHASH_SIZE */
49
-
50
- #ifndef RSTRING_LEN
51
- #define RSTRING_LEN(x) RSTRING((x))->len
52
- #endif /* RSTRING_LEN */
53
-
54
- #ifndef RSTRING_PTR
55
- #define RSTRING_PTR(x) RSTRING((x))->ptr
56
- #endif /* RSTRING_PTR */
57
-
58
- #ifndef HAVE_PG_ENCODING_TO_CHAR
59
- #define pg_encoding_to_char(x) "SQL_ASCII"
60
- #endif
61
-
62
- #ifndef HAVE_PQFREEMEM
63
- #define PQfreemem(ptr) free(ptr)
64
- #endif
65
-
66
- #ifndef StringValuePtr
67
- #define StringValuePtr(x) STR2CSTR(x)
68
- #endif
69
-
70
- #define AssignCheckedStringValue(cstring, rstring) do { \
71
- if (!NIL_P(temp = rstring)) { \
72
- Check_Type(temp, T_STRING); \
73
- cstring = StringValuePtr(temp); \
74
- } \
75
- } while (0)
76
-
77
- #if RUBY_VERSION_CODE < 180
78
- #define rb_check_string_type(x) rb_check_convert_type(x, T_STRING, "String", "to_str")
79
- #endif
80
-
81
- #define rb_check_hash_type(x) rb_check_convert_type(x, T_HASH, "Hash", "to_hash")
82
-
83
- #define rb_define_singleton_alias(klass,new,old) rb_define_alias(rb_singleton_class(klass),new,old)
84
-
85
- #define Data_Set_Struct(obj,ptr) do { \
86
- Check_Type(obj, T_DATA); \
87
- DATA_PTR(obj) = ptr; \
88
- } while (0)
89
-
90
- #define RUBY_CLASS(name) rb_const_get(rb_cObject, rb_intern(name))
91
-
92
- #define SINGLE_QUOTE '\''
93
-
94
- EXTERN VALUE rb_mEnumerable;
95
- EXTERN VALUE rb_mKernel;
96
- EXTERN VALUE rb_cTime;
97
-
98
- static VALUE rb_cDate;
99
- static VALUE rb_cDateTime;
100
- static VALUE rb_cBigDecimal;
101
-
102
- static VALUE rb_cPGconn;
103
- static VALUE rb_cPGresult;
104
- static VALUE rb_ePGError;
105
- static VALUE rb_cPGlarge;
106
- static VALUE rb_cPGrow;
107
-
108
- static VALUE pgconn_lastval _((VALUE));
109
- static VALUE pgconn_close _((VALUE));
110
- static VALUE pgresult_fields _((VALUE));
111
- static VALUE pgresult_clear _((VALUE));
112
- static VALUE pgresult_result_with_clear _((VALUE));
113
- static VALUE pgresult_new _((PGresult*));
114
-
115
- static int translate_results = 0;
116
-
117
- /* Large Object support */
118
- typedef struct pglarge_object
119
- {
120
- PGconn *pgconn;
121
- Oid lo_oid;
122
- int lo_fd;
123
- } PGlarge;
124
-
125
- static VALUE pglarge_new _((PGconn*, Oid, int));
126
- /* Large Object support */
127
-
128
- static void free_pgconn(PGconn *);
129
-
130
- static VALUE
131
- pgconn_alloc(klass)
132
- VALUE klass;
133
- {
134
- return Data_Wrap_Struct(klass, 0, free_pgconn, NULL);
135
- }
136
-
137
- static int build_key_value_string_i(VALUE key, VALUE value, VALUE result);
138
- static PGconn *get_pgconn(VALUE obj);
139
-
140
- static PGconn *
141
- try_connectdb(arg)
142
- VALUE arg;
143
- {
144
- VALUE conninfo;
145
-
146
- if (!NIL_P(conninfo = rb_check_string_type(arg))) {
147
- /* do nothing */
148
- }
149
- else if (!NIL_P(conninfo = rb_check_hash_type(arg))) {
150
- VALUE key_values = rb_ary_new2(RHASH_SIZE(conninfo));
151
- rb_hash_foreach(conninfo, build_key_value_string_i, key_values);
152
- conninfo = rb_ary_join(key_values, rb_str_new2(" "));
153
- }
154
- else {
155
- return NULL;
156
- }
157
-
158
- return PQconnectdb(StringValuePtr(conninfo));
159
- }
160
-
161
- static PGconn *
162
- try_setdbLogin(args)
163
- VALUE args;
164
- {
165
- VALUE temp;
166
- char *host, *port, *opt, *tty, *dbname, *login, *pwd;
167
- host=port=opt=tty=dbname=login=pwd=NULL;
168
-
169
- rb_funcall(args, rb_intern("flatten!"), 0);
170
-
171
- AssignCheckedStringValue(host, rb_ary_entry(args, 0));
172
- if (!NIL_P(temp = rb_ary_entry(args, 1)) && NUM2INT(temp) != -1) {
173
- temp = rb_obj_as_string(temp);
174
- port = StringValuePtr(temp);
175
- }
176
- AssignCheckedStringValue(opt, rb_ary_entry(args, 2));
177
- AssignCheckedStringValue(tty, rb_ary_entry(args, 3));
178
- AssignCheckedStringValue(dbname, rb_ary_entry(args, 4));
179
- AssignCheckedStringValue(login, rb_ary_entry(args, 5));
180
- AssignCheckedStringValue(pwd, rb_ary_entry(args, 6));
181
-
182
- return PQsetdbLogin(host, port, opt, tty, dbname, login, pwd);
183
- }
184
-
185
- static VALUE
186
- pgconn_connect(argc, argv, self)
187
- int argc;
188
- VALUE *argv;
189
- VALUE self;
190
- {
191
- VALUE args;
192
- PGconn *conn = NULL;
193
-
194
- rb_scan_args(argc, argv, "0*", &args);
195
- if (RARRAY(args)->len == 1) {
196
- conn = try_connectdb(rb_ary_entry(args, 0));
197
- }
198
- if (conn == NULL) {
199
- conn = try_setdbLogin(args);
200
- }
201
-
202
- if (PQstatus(conn) == CONNECTION_BAD) {
203
- VALUE message = rb_str_new2(PQerrorMessage(conn));
204
- PQfinish(conn);
205
- rb_raise(rb_ePGError, StringValuePtr(message));
206
- }
207
-
208
- #ifdef HAVE_PQSERVERVERSION
209
- if (PQserverVersion(conn) >= 80100) {
210
- rb_define_singleton_method(self, "lastval", pgconn_lastval, 0);
211
- }
212
- #endif /* HAVE_PQSERVERVERSION */
213
-
214
- Data_Set_Struct(self, conn);
215
- return self;
216
- }
217
-
218
- /*
219
- * call-seq:
220
- * PGconn.translate_results = boolean
221
- *
222
- * When true (default), results are translated to appropriate ruby class.
223
- * When false, results are returned as +Strings+.
224
- */
225
- static VALUE
226
- pgconn_s_translate_results_set(self, fact)
227
- VALUE self, fact;
228
- {
229
- translate_results = (fact == Qfalse || fact == Qnil) ? 0 : 1;
230
- return fact;
231
- }
232
-
233
- static VALUE
234
- pgconn_s_format(self, obj)
235
- VALUE self;
236
- VALUE obj;
237
- {
238
-
239
- switch(TYPE(obj)) {
240
- case T_STRING:
241
- return obj;
242
-
243
- case T_TRUE:
244
- case T_FALSE:
245
- case T_FIXNUM:
246
- case T_BIGNUM:
247
- case T_FLOAT:
248
- return rb_obj_as_string(obj);
249
-
250
- case T_NIL:
251
- return rb_str_new2("NULL");
252
-
253
- default:
254
- if (CLASS_OF(obj) == rb_cBigDecimal) {
255
- return rb_funcall(obj, rb_intern("to_s"), 1, rb_str_new2("F"));
256
- }
257
- else if (rb_block_given_p()) {
258
- return rb_yield(obj);
259
- } else {
260
- rb_raise(rb_ePGError, "can't format");
261
- }
262
- }
263
- }
264
-
265
-
266
- /*
267
- * call-seq:
268
- * PGconn.quote( obj )
269
- * PGconn.quote( obj ) { |obj| ... }
270
- * PGconn.format( obj )
271
- * PGconn.format( obj ) { |obj| ... }
272
- *
273
- * If _obj_ is a Number, String, Array, +nil+, +true+, or +false+ then
274
- * #quote returns a String representation of that object safe for use in PostgreSQL.
275
- *
276
- * If _obj_ is not one of the above classes and a block is supplied to #quote,
277
- * the block is invoked, passing along the object. The return value from the
278
- * block is returned as a string.
279
- *
280
- * If _obj_ is not one of the recognized classes andno block is supplied,
281
- * a PGError is raised.
282
- */
283
- static VALUE
284
- pgconn_s_quote(self, obj)
285
- VALUE self, obj;
286
- {
287
- char* quoted;
288
- int size;
289
- VALUE result;
290
-
291
- if (TYPE(obj) == T_STRING) {
292
- /* length * 2 because every char could require escaping */
293
- /* + 2 for the quotes, + 1 for the null terminator */
294
- quoted = ALLOCA_N(char, RSTRING_LEN(obj) * 2 + 2 + 1);
295
- size = PQescapeString(quoted + 1, RSTRING_PTR(obj), RSTRING_LEN(obj));
296
- *quoted = *(quoted + size + 1) = SINGLE_QUOTE;
297
- result = rb_str_new(quoted, size + 2);
298
- OBJ_INFECT(result, obj);
299
- return result;
300
- }
301
- else {
302
- return pgconn_s_format(self, obj);
303
- }
304
- }
305
-
306
- /*
307
- * call-seq:
308
- * PGconn.quote( obj )
309
- * PGconn.quote( obj ) { |obj| ... }
310
- * PGconn.format( obj )
311
- * PGconn.format( obj ) { |obj| ... }
312
- *
313
- * If _obj_ is a Number, String, Array, +nil+, +true+, or +false+ then
314
- * #quote returns a String representation of that object safe for use in PostgreSQL.
315
- *
316
- * If _obj_ is not one of the above classes and a block is supplied to #quote,
317
- * the block is invoked, passing along the object. The return value from the
318
- * block is returned as a string.
319
- *
320
- * If _obj_ is not one of the recognized classes andno block is supplied,
321
- * a PGError is raised.
322
- */
323
- static VALUE
324
- pgconn_quote(self, obj)
325
- VALUE self, obj;
326
- {
327
- char* quoted;
328
- int size,error;
329
- VALUE result;
330
-
331
- if (TYPE(obj) == T_STRING) {
332
- /* length * 2 because every char could require escaping */
333
- /* + 2 for the quotes, + 1 for the null terminator */
334
- quoted = ALLOCA_N(char, RSTRING_LEN(obj) * 2 + 2 + 1);
335
- size = PQescapeStringConn(get_pgconn(self),quoted + 1,
336
- RSTRING_PTR(obj), RSTRING_LEN(obj), &error);
337
- *quoted = *(quoted + size + 1) = SINGLE_QUOTE;
338
- result = rb_str_new(quoted, size + 2);
339
- OBJ_INFECT(result, obj);
340
- return result;
341
- }
342
- else {
343
- return pgconn_s_format(self, obj);
344
- }
345
- }
346
-
347
- static VALUE
348
- pgconn_s_quote_connstr(string)
349
- VALUE string;
350
- {
351
- char *str,*ptr;
352
- int i,j=0,len;
353
- VALUE result;
354
-
355
- Check_Type(string, T_STRING);
356
-
357
- ptr = RSTRING_PTR(string);
358
- len = RSTRING_LEN(string);
359
- str = ALLOCA_N(char, len * 2 + 2 + 1);
360
- str[j++] = '\'';
361
- for(i = 0; i < len; i++) {
362
- if(ptr[i] == '\'' || ptr[i] == '\\')
363
- str[j++] = '\\';
364
- str[j++] = ptr[i];
365
- }
366
- str[j++] = '\'';
367
- result = rb_str_new(str, j);
368
- OBJ_INFECT(result, string);
369
- return result;
370
- }
371
-
372
- static int
373
- build_key_value_string_i(key, value, result)
374
- VALUE key, value, result;
375
- {
376
- VALUE key_value;
377
- if (key == Qundef) return ST_CONTINUE;
378
- key_value = (TYPE(key) == T_STRING ? rb_str_dup(key) : rb_obj_as_string(key));
379
- rb_str_cat(key_value, "=", 1);
380
- rb_str_concat(key_value, pgconn_s_quote_connstr(value));
381
- rb_ary_push(result, key_value);
382
- return ST_CONTINUE;
383
- }
384
-
385
- /*
386
- * call-seq:
387
- * PGconn.quote_ident( str )
388
- *
389
- * Returns a SQL-safe identifier.
390
- */
391
- static VALUE
392
- pgconn_s_quote_ident(self, string)
393
- VALUE self;
394
- VALUE string;
395
- {
396
- char *str,*ptr;
397
- int i,j=0,len;
398
- VALUE result;
399
-
400
- Check_Type(string, T_STRING);
401
-
402
- ptr = RSTRING_PTR(string);
403
- len = RSTRING_LEN(string);
404
- str = ALLOCA_N(char, len * 2 + 2 + 1);
405
- str[j++] = '"';
406
- for(i = 0; i < len; i++) {
407
- if(ptr[i] == '"')
408
- str[j++] = '"';
409
- else if(ptr[i] == '\0')
410
- rb_raise(rb_ePGError, "Identifier cannot contain NULL bytes");
411
- str[j++] = ptr[i];
412
- }
413
- str[j++] = '"';
414
- result = rb_str_new(str, j);
415
- OBJ_INFECT(result, string);
416
- return result;
417
- }
418
-
419
- /*
420
- * Returns a SQL-safe version of the String _str_. Unlike #quote, does not wrap the String in '...'.
421
- */
422
- static VALUE
423
- pgconn_s_escape(self, string)
424
- VALUE self;
425
- VALUE string;
426
- {
427
- char *escaped;
428
- int size;
429
- VALUE result;
430
-
431
- Check_Type(string, T_STRING);
432
-
433
- escaped = ALLOCA_N(char, RSTRING_LEN(string) * 2 + 1);
434
- size = PQescapeString(escaped, RSTRING_PTR(string), RSTRING_LEN(string));
435
- result = rb_str_new(escaped, size);
436
- OBJ_INFECT(result, string);
437
- return result;
438
- }
439
-
440
- /*
441
- * Returns a SQL-safe version of the String _str_. Unlike #quote, does not wrap the String in '...'.
442
- */
443
- static VALUE
444
- pgconn_escape(self, string)
445
- VALUE self;
446
- VALUE string;
447
- {
448
- char *escaped;
449
- int size,error;
450
- VALUE result;
451
-
452
- Check_Type(string, T_STRING);
453
-
454
- escaped = ALLOCA_N(char, RSTRING_LEN(string) * 2 + 1);
455
- size = PQescapeStringConn(get_pgconn(self),escaped, RSTRING_PTR(string),
456
- RSTRING_LEN(string), &error);
457
- result = rb_str_new(escaped, size);
458
- OBJ_INFECT(result, string);
459
- return result;
460
- }
461
-
462
- /*
463
- * call-seq:
464
- * PGconn.escape_bytea( obj )
465
- *
466
- * Escapes binary data for use within an SQL command with the type +bytea+.
467
- *
468
- * Certain byte values must be escaped (but all byte values may be escaped)
469
- * when used as part of a +bytea+ literal in an SQL statement. In general, to
470
- * escape a byte, it is converted into the three digit octal number equal to
471
- * the octet value, and preceded by two backslashes. The single quote (') and
472
- * backslash (\) characters have special alternative escape sequences.
473
- * #escape_bytea performs this operation, escaping only the minimally required bytes.
474
- *
475
- * See the PostgreSQL documentation on PQescapeBytea[http://www.postgresql.org/docs/current/interactive/libpq-exec.html#LIBPQ-EXEC-ESCAPE-BYTEA] for more information.
476
- */
477
- static VALUE
478
- pgconn_s_escape_bytea(self, obj)
479
- VALUE self;
480
- VALUE obj;
481
- {
482
- unsigned char *from, *to;
483
- size_t from_len, to_len;
484
- VALUE ret;
485
-
486
- Check_Type(obj, T_STRING);
487
- from = (unsigned char*)RSTRING_PTR(obj);
488
- from_len = RSTRING_LEN(obj);
489
-
490
- to = PQescapeBytea(from, from_len, &to_len);
491
-
492
- ret = rb_str_new((char*)to, to_len - 1);
493
- OBJ_INFECT(ret, obj);
494
- PQfreemem(to);
495
- return ret;
496
- }
497
-
498
- /*
499
- * call-seq:
500
- * PGconn.escape_bytea( obj )
501
- *
502
- * Escapes binary data for use within an SQL command with the type +bytea+.
503
- *
504
- * Certain byte values must be escaped (but all byte values may be escaped)
505
- * when used as part of a +bytea+ literal in an SQL statement. In general, to
506
- * escape a byte, it is converted into the three digit octal number equal to
507
- * the octet value, and preceded by two backslashes. The single quote (') and
508
- * backslash (\) characters have special alternative escape sequences.
509
- * #escape_bytea performs this operation, escaping only the minimally required bytes.
510
- *
511
- * See the PostgreSQL documentation on PQescapeBytea[http://www.postgresql.org/docs/current/interactive/libpq-exec.html#LIBPQ-EXEC-ESCAPE-BYTEA] for more information.
512
- */
513
- static VALUE
514
- pgconn_escape_bytea(self, obj)
515
- VALUE self;
516
- VALUE obj;
517
- {
518
- unsigned char *from, *to;
519
- size_t from_len, to_len;
520
- VALUE ret;
521
-
522
- Check_Type(obj, T_STRING);
523
- from = (unsigned char*)RSTRING_PTR(obj);
524
- from_len = RSTRING_LEN(obj);
525
-
526
- to = PQescapeByteaConn(get_pgconn(self),from, from_len, &to_len);
527
-
528
- ret = rb_str_new((char*)to, to_len - 1);
529
- OBJ_INFECT(ret, obj);
530
- PQfreemem(to);
531
- return ret;
532
- }
533
-
534
- /*
535
- * call-seq:
536
- * PGconn.unescape_bytea( obj )
537
- *
538
- * Converts an escaped string representation of binary data into binary data --- the
539
- * reverse of #escape_bytea. This is needed when retrieving +bytea+ data in text format,
540
- * but not when retrieving it in binary format.
541
- *
542
- * See the PostgreSQL documentation on PQunescapeBytea[http://www.postgresql.org/docs/current/interactive/libpq-exec.html#LIBPQ-EXEC-ESCAPE-BYTEA] for more information.
543
- */
544
- static VALUE
545
- pgconn_s_unescape_bytea(self, obj)
546
- VALUE self, obj;
547
- {
548
- unsigned char *from, *to;
549
- size_t to_len;
550
- VALUE ret;
551
-
552
- Check_Type(obj, T_STRING);
553
- from = (unsigned char*)StringValuePtr(obj);
554
-
555
- to = PQunescapeBytea(from, &to_len);
556
-
557
- ret = rb_str_new((char*)to, to_len);
558
- OBJ_INFECT(ret, obj);
559
- PQfreemem(to);
560
-
561
- return ret;
562
- }
563
-
564
- /*
565
- * Document-method: new
566
- *
567
- * call-seq:
568
- * PGconn.open(connection_hash) -> PGconn
569
- * PGconn.open(connection_string) -> PGconn
570
- * PGconn.open(host, port, options, tty, dbname, login, passwd) -> PGconn
571
- *
572
- * _host_:: server hostname
573
- * _port_:: server port number
574
- * _options_:: backend options (String)
575
- * _tty_:: tty to print backend debug message <i>(ignored in newer versions of PostgreSQL)</i> (String)
576
- * _dbname_:: connecting database name
577
- * _login_:: login user name
578
- * _passwd_:: login password
579
- *
580
- * On failure, it raises a PGError exception.
581
- */
582
- #ifndef HAVE_RB_DEFINE_ALLOC_FUNC
583
- static VALUE
584
- pgconn_s_new(argc, argv, klass)
585
- int argc;
586
- VALUE *argv;
587
- VALUE klass;
588
- {
589
- VALUE obj = rb_obj_alloc(klass);
590
- rb_obj_call_init(obj, argc, argv);
591
- return obj;
592
- }
593
- #endif
594
-
595
- static VALUE
596
- pgconn_init(argc, argv, self)
597
- int argc;
598
- VALUE *argv;
599
- VALUE self;
600
- {
601
- pgconn_connect(argc, argv, self);
602
- if (rb_block_given_p()) {
603
- return rb_ensure(rb_yield, self, pgconn_close, self);
604
- }
605
- return self;
606
- }
607
-
608
- static PGconn*
609
- get_pgconn(obj)
610
- VALUE obj;
611
- {
612
- PGconn *conn;
613
-
614
- Data_Get_Struct(obj, PGconn, conn);
615
- if (conn == NULL) rb_raise(rb_ePGError, "closed connection");
616
- return conn;
617
- }
618
-
619
- /*
620
- * call-seq:
621
- * conn.close
622
- *
623
- * Closes the backend connection.
624
- */
625
- static VALUE
626
- pgconn_close(self)
627
- VALUE self;
628
- {
629
- PQfinish(get_pgconn(self));
630
- DATA_PTR(self) = NULL;
631
- return Qnil;
632
- }
633
-
634
- /*
635
- * call-seq:
636
- * conn.reset()
637
- *
638
- * Resets the backend connection. This method closes the backend connection and tries to re-connect.
639
- */
640
- static VALUE
641
- pgconn_reset(obj)
642
- VALUE obj;
643
- {
644
- PQreset(get_pgconn(obj));
645
- return obj;
646
- }
647
-
648
- static PGresult*
649
- get_pgresult(obj)
650
- VALUE obj;
651
- {
652
- PGresult *result;
653
- Data_Get_Struct(obj, PGresult, result);
654
- if (result == NULL) rb_raise(rb_ePGError, "query not performed");
655
- return result;
656
- }
657
-
658
- #ifndef HAVE_PQEXECPARAMS
659
- PGresult *PQexecParams_compat(PGconn *conn, VALUE command, VALUE values);
660
- #endif
661
-
662
- #define TEXT_FORMAT 0
663
- #define BINARY_FORMAT 1
664
-
665
- void
666
- translate_to_pg(VALUE value, char const** result, int* length, int* format)
667
- {
668
- switch (TYPE(value)) {
669
- case T_NIL:
670
- *result = NULL;
671
- *length = 0;
672
- *format = BINARY_FORMAT;
673
- return;
674
- case T_TRUE:
675
- *result = "\1";
676
- *length = 1;
677
- *format = BINARY_FORMAT;
678
- return;
679
- case T_FALSE:
680
- *result = "\0";
681
- *length = 1;
682
- *format = BINARY_FORMAT;
683
- return;
684
- case T_STRING:
685
- *result = StringValuePtr(value);
686
- *length = RSTRING_LEN(value);
687
- *format = BINARY_FORMAT;
688
- return;
689
- default: {
690
- VALUE formatted = pgconn_s_format(rb_cPGconn, value);
691
- *result = StringValuePtr(formatted);
692
- *length = RSTRING_LEN(formatted);
693
- *format = TEXT_FORMAT;
694
- }
695
- }
696
- }
697
-
698
- /*
699
- * call-seq:
700
- * conn.exec(sql, *bind_values)
701
- *
702
- * Sends SQL query request specified by _sql_ to the PostgreSQL.
703
- * Returns a PGresult instance on success.
704
- * On failure, it raises a PGError exception.
705
- *
706
- * +bind_values+ represents values for the PostgreSQL bind parameters found in the +sql+. PostgreSQL bind parameters are presented as $1, $1, $2, etc.
707
- */
708
- static VALUE
709
- pgconn_exec(argc, argv, obj)
710
- int argc;
711
- VALUE *argv;
712
- VALUE obj;
713
- {
714
- PGconn *conn = get_pgconn(obj);
715
- PGresult *result = NULL;
716
- VALUE command, params;
717
- char *msg;
718
-
719
- rb_scan_args(argc, argv, "1*", &command, &params);
720
-
721
- Check_Type(command, T_STRING);
722
-
723
- if (RARRAY(params)->len <= 0) {
724
- result = PQexec(conn, StringValuePtr(command));
725
- }
726
- else {
727
- int len = RARRAY(params)->len;
728
- int i;
729
- #ifdef HAVE_PQEXECPARAMS
730
- VALUE* ptr = RARRAY(params)->ptr;
731
- char const** values = ALLOCA_N(char const*, len);
732
- int* lengths = ALLOCA_N(int, len);
733
- int* formats = ALLOCA_N(int, len);
734
- for (i = 0; i < len; i++, ptr++) {
735
- translate_to_pg(*ptr, values+i, lengths+i, formats+i);
736
- }
737
- result = PQexecParams(conn, StringValuePtr(command), len, NULL, values, lengths, formats, 0);
738
- #else
739
- for (i = 0; i < len; i++) {
740
- rb_ary_store(params, i, pgconn_s_quote(rb_cPGconn, rb_ary_entry(params, i)));
741
- }
742
- result = PQexecParams_compat(conn, command, params);
743
- #endif
744
- }
745
-
746
- if (!result) {
747
- rb_raise(rb_ePGError, PQerrorMessage(conn));
748
- }
749
-
750
- switch (PQresultStatus(result)) {
751
- case PGRES_TUPLES_OK:
752
- case PGRES_COPY_OUT:
753
- case PGRES_COPY_IN:
754
- case PGRES_EMPTY_QUERY:
755
- case PGRES_COMMAND_OK: {
756
- VALUE pg_result = pgresult_new(result);
757
- if (rb_block_given_p()) {
758
- return rb_ensure(rb_yield, pg_result, pgresult_clear, pg_result);
759
- }
760
- else {
761
- return pg_result;
762
- }
763
- }
764
-
765
- case PGRES_BAD_RESPONSE:
766
- case PGRES_FATAL_ERROR:
767
- case PGRES_NONFATAL_ERROR:
768
- msg = RSTRING_PTR(rb_str_new2(PQresultErrorMessage(result)));
769
- break;
770
- default:
771
- msg = "internal error : unknown result status.";
772
- break;
773
- }
774
- PQclear(result);
775
- rb_raise(rb_ePGError, msg);
776
- }
777
-
778
- /*
779
- * call-seq:
780
- * conn.async_exec( sql )
781
- *
782
- * Sends an asyncrhonous SQL query request specified by _sql_ to the PostgreSQL.
783
- * Returns a PGresult instance on success.
784
- * On failure, it raises a PGError exception.
785
- */
786
- static VALUE
787
- pgconn_async_exec(obj, str)
788
- VALUE obj, str;
789
- {
790
- PGconn *conn = get_pgconn(obj);
791
- PGresult *result;
792
- char *msg;
793
-
794
- int cs;
795
- int ret;
796
- fd_set rset;
797
-
798
- Check_Type(str, T_STRING);
799
-
800
- while ((result = PQgetResult(conn)) != NULL) {
801
- PQclear(result);
802
- }
803
-
804
- if (!PQsendQuery(conn, RSTRING_PTR(str))) {
805
- rb_raise(rb_ePGError, PQerrorMessage(conn));
806
- }
807
-
808
- cs = PQsocket(conn);
809
- for(;;) {
810
- FD_ZERO(&rset);
811
- FD_SET(cs, &rset);
812
- ret = rb_thread_select(cs + 1, &rset, NULL, NULL, NULL);
813
- if (ret < 0) {
814
- rb_sys_fail(0);
815
- }
816
-
817
- if (ret == 0) {
818
- continue;
819
- }
820
-
821
- if (PQconsumeInput(conn) == 0) {
822
- rb_raise(rb_ePGError, PQerrorMessage(conn));
823
- }
824
-
825
- if (PQisBusy(conn) == 0) {
826
- break;
827
- }
828
- }
829
-
830
- result = PQgetResult(conn);
831
-
832
- if (!result) {
833
- rb_raise(rb_ePGError, PQerrorMessage(conn));
834
- }
835
-
836
- switch (PQresultStatus(result)) {
837
- case PGRES_TUPLES_OK:
838
- case PGRES_COPY_OUT:
839
- case PGRES_COPY_IN:
840
- case PGRES_EMPTY_QUERY:
841
- case PGRES_COMMAND_OK: /* no data will be received */
842
- return pgresult_new(result);
843
-
844
- case PGRES_BAD_RESPONSE:
845
- case PGRES_FATAL_ERROR:
846
- case PGRES_NONFATAL_ERROR:
847
- msg = RSTRING_PTR(rb_str_new2(PQresultErrorMessage(result)));
848
- break;
849
- default:
850
- msg = "internal error : unknown result status.";
851
- break;
852
- }
853
- PQclear(result);
854
- rb_raise(rb_ePGError, msg);
855
- }
856
-
857
- /*
858
- * call-seq:
859
- * conn.query(sql, *bind_values)
860
- *
861
- * Sends SQL query request specified by _sql_ to the PostgreSQL.
862
- * Returns an Array as the resulting tuple on success.
863
- * On failure, it returns +nil+, and the error details can be obtained by #error.
864
- *
865
- * +bind_values+ represents values for the PostgreSQL bind parameters found in the +sql+. PostgreSQL bind parameters are presented as $1, $1, $2, etc.
866
- */
867
- static VALUE
868
- pgconn_query(argc, argv, obj)
869
- int argc;
870
- VALUE *argv;
871
- VALUE obj;
872
- {
873
- return pgresult_result_with_clear(pgconn_exec(argc, argv, obj));
874
- }
875
-
876
- /*
877
- * call-seq:
878
- * conn.async_query(sql)
879
- *
880
- * Sends an asynchronous SQL query request specified by _sql_ to the PostgreSQL.
881
- * Returns an Array as the resulting tuple on success.
882
- * On failure, it returns +nil+, and the error details can be obtained by #error.
883
- */
884
- static VALUE
885
- pgconn_async_query(obj, str)
886
- VALUE obj, str;
887
- {
888
- return pgresult_result_with_clear(pgconn_async_exec(obj, str));
889
- }
890
-
891
- /*
892
- * call-seq:
893
- * conn.get_notify()
894
- *
895
- * Returns an array of the unprocessed notifiers.
896
- * If there is no unprocessed notifier, it returns +nil+.
897
- */
898
- static VALUE
899
- pgconn_get_notify(obj)
900
- VALUE obj;
901
- {
902
- PGconn* conn = get_pgconn(obj);
903
- PGnotify *notify;
904
- VALUE ary;
905
-
906
- if (PQconsumeInput(conn) == 0) {
907
- rb_raise(rb_ePGError, PQerrorMessage(conn));
908
- }
909
- /* gets notify and builds result */
910
- notify = PQnotifies(conn);
911
- if (notify == NULL) {
912
- /* there are no unhandled notifications */
913
- return Qnil;
914
- }
915
- ary = rb_ary_new3(2, rb_tainted_str_new2(notify->relname), INT2NUM(notify->be_pid));
916
- PQfreemem(notify);
917
-
918
- /* returns result */
919
- return ary;
920
- }
921
-
922
- static VALUE pg_escape_str;
923
- static ID pg_gsub_bang_id;
924
-
925
- static void
926
- free_pgconn(ptr)
927
- PGconn *ptr;
928
- {
929
- PQfinish(ptr);
930
- }
931
-
932
- /*
933
- * call-seq:
934
- * conn.insert_table( table, values )
935
- *
936
- * Inserts contents of the _values_ Array into the _table_.
937
- */
938
- static VALUE
939
- pgconn_insert_table(obj, table, values)
940
- VALUE obj, table, values;
941
- {
942
- PGconn *conn = get_pgconn(obj);
943
- PGresult *result;
944
- VALUE s, buffer;
945
- int i, j;
946
- int res = 0;
947
-
948
- Check_Type(table, T_STRING);
949
- Check_Type(values, T_ARRAY);
950
- i = RARRAY(values)->len;
951
- while (i--) {
952
- if (TYPE(RARRAY(RARRAY(values)->ptr[i])) != T_ARRAY) {
953
- rb_raise(rb_ePGError, "second arg must contain some kind of arrays.");
954
- }
955
- }
956
-
957
- buffer = rb_str_new(0, RSTRING_LEN(table) + 17 + 1);
958
- /* starts query */
959
- snprintf(RSTRING_PTR(buffer), RSTRING_LEN(buffer), "copy %s from stdin ", StringValuePtr(table));
960
-
961
- result = PQexec(conn, StringValuePtr(buffer));
962
- if (!result){
963
- rb_raise(rb_ePGError, PQerrorMessage(conn));
964
- }
965
- PQclear(result);
966
-
967
- for (i = 0; i < RARRAY(values)->len; i++) {
968
- struct RArray *row = RARRAY(RARRAY(values)->ptr[i]);
969
- buffer = rb_tainted_str_new(0,0);
970
- for (j = 0; j < row->len; j++) {
971
- if (j > 0) rb_str_cat(buffer, "\t", 1);
972
- if (NIL_P(row->ptr[j])) {
973
- rb_str_cat(buffer, "\\N",2);
974
- } else {
975
- s = rb_obj_as_string(row->ptr[j]);
976
- rb_funcall(s,pg_gsub_bang_id,2,
977
- rb_str_new("([\\t\\n\\\\])", 10),pg_escape_str);
978
- rb_str_cat(buffer, StringValuePtr(s), RSTRING_LEN(s));
979
- }
980
- }
981
- rb_str_cat(buffer, "\n\0", 2);
982
- /* sends data */
983
- PQputline(conn, StringValuePtr(buffer));
984
- }
985
- PQputline(conn, "\\.\n");
986
- res = PQendcopy(conn);
987
-
988
- return obj;
989
- }
990
-
991
- /*
992
- * call-seq:
993
- * conn.putline()
994
- *
995
- * Sends the string to the backend server.
996
- * Users must send a single "." to denote the end of data transmission.
997
- */
998
- static VALUE
999
- pgconn_putline(obj, str)
1000
- VALUE obj, str;
1001
- {
1002
- Check_Type(str, T_STRING);
1003
- PQputline(get_pgconn(obj), StringValuePtr(str));
1004
- return obj;
1005
- }
1006
-
1007
- /*
1008
- * call-seq:
1009
- * conn.getline()
1010
- *
1011
- * Reads a line from the backend server into internal buffer.
1012
- * Returns +nil+ for EOF, +0+ for success, +1+ for buffer overflowed.
1013
- * You need to ensure single "." from backend to confirm transmission completion.
1014
- * The sample program <tt>psql.rb</tt> (see source for postgres) treats this copy protocol right.
1015
- */
1016
- static VALUE
1017
- pgconn_getline(obj)
1018
- VALUE obj;
1019
- {
1020
- PGconn *conn = get_pgconn(obj);
1021
- VALUE str;
1022
- long size = BUFSIZ;
1023
- long bytes = 0;
1024
- int ret;
1025
-
1026
- str = rb_tainted_str_new(0, size);
1027
-
1028
- for (;;) {
1029
- ret = PQgetline(conn, RSTRING_PTR(str) + bytes, size - bytes);
1030
- switch (ret) {
1031
- case EOF:
1032
- return Qnil;
1033
- case 0:
1034
- rb_str_resize(str, strlen(StringValuePtr(str)));
1035
- return str;
1036
- }
1037
- bytes += BUFSIZ;
1038
- size += BUFSIZ;
1039
- rb_str_resize(str, size);
1040
- }
1041
- return Qnil;
1042
- }
1043
-
1044
- /*
1045
- * call-seq:
1046
- * conn.endcopy()
1047
- *
1048
- * Waits until the backend completes the copying.
1049
- * You should call this method after #putline or #getline.
1050
- * Returns +nil+ on success; raises an exception otherwise.
1051
- */
1052
- static VALUE
1053
- pgconn_endcopy(obj)
1054
- VALUE obj;
1055
- {
1056
- if (PQendcopy(get_pgconn(obj)) == 1) {
1057
- rb_raise(rb_ePGError, "cannot complete copying");
1058
- }
1059
- return Qnil;
1060
- }
1061
-
1062
- static void
1063
- notice_proxy(self, message)
1064
- VALUE self;
1065
- const char *message;
1066
- {
1067
- VALUE block;
1068
- if ((block = rb_iv_get(self, "@on_notice")) != Qnil) {
1069
- rb_funcall(block, rb_intern("call"), 1, rb_str_new2(message));
1070
- }
1071
- }
1072
-
1073
- /*
1074
- * call-seq:
1075
- * conn.on_notice {|message| ... }
1076
- *
1077
- * Notice and warning messages generated by the server are not returned
1078
- * by the query execution functions, since they do not imply failure of
1079
- * the query. Instead they are passed to a notice handling function, and
1080
- * execution continues normally after the handler returns. The default
1081
- * notice handling function prints the message on <tt>stderr</tt>, but the
1082
- * application can override this behavior by supplying its own handling
1083
- * function.
1084
- */
1085
- static VALUE
1086
- pgconn_on_notice(self)
1087
- VALUE self;
1088
- {
1089
- VALUE block = rb_block_proc();
1090
- PGconn *conn = get_pgconn(self);
1091
- if (PQsetNoticeProcessor(conn, NULL, NULL) != notice_proxy) {
1092
- PQsetNoticeProcessor(conn, notice_proxy, (void *) self);
1093
- }
1094
- rb_iv_set(self, "@on_notice", block);
1095
- return self;
1096
- }
1097
-
1098
- /*
1099
- * call-seq:
1100
- * conn.host()
1101
- *
1102
- * Returns the connected server name.
1103
- */
1104
- static VALUE
1105
- pgconn_host(obj)
1106
- VALUE obj;
1107
- {
1108
- char *host = PQhost(get_pgconn(obj));
1109
- if (!host) return Qnil;
1110
- return rb_tainted_str_new2(host);
1111
- }
1112
-
1113
- /*
1114
- * call-seq:
1115
- * conn.port()
1116
- *
1117
- * Returns the connected server port number.
1118
- */
1119
- static VALUE
1120
- pgconn_port(obj)
1121
- VALUE obj;
1122
- {
1123
- char* port = PQport(get_pgconn(obj));
1124
- return INT2NUM(atol(port));
1125
- }
1126
-
1127
- /*
1128
- * call-seq:
1129
- * conn.db()
1130
- *
1131
- * Returns the connected database name.
1132
- */
1133
- static VALUE
1134
- pgconn_db(obj)
1135
- VALUE obj;
1136
- {
1137
- char *db = PQdb(get_pgconn(obj));
1138
- if (!db) return Qnil;
1139
- return rb_tainted_str_new2(db);
1140
- }
1141
-
1142
- /*
1143
- * call-seq:
1144
- * conn.options()
1145
- *
1146
- * Returns backend option string.
1147
- */
1148
- static VALUE
1149
- pgconn_options(obj)
1150
- VALUE obj;
1151
- {
1152
- char *options = PQoptions(get_pgconn(obj));
1153
- if (!options) return Qnil;
1154
- return rb_tainted_str_new2(options);
1155
- }
1156
-
1157
- /*
1158
- * call-seq:
1159
- * conn.tty()
1160
- *
1161
- * Returns the connected pgtty.
1162
- */
1163
- static VALUE
1164
- pgconn_tty(obj)
1165
- VALUE obj;
1166
- {
1167
- char *tty = PQtty(get_pgconn(obj));
1168
- if (!tty) return Qnil;
1169
- return rb_tainted_str_new2(tty);
1170
- }
1171
-
1172
- /*
1173
- * call-seq:
1174
- * conn.user()
1175
- *
1176
- * Returns the authenticated user name.
1177
- */
1178
- static VALUE
1179
- pgconn_user(obj)
1180
- VALUE obj;
1181
- {
1182
- char *user = PQuser(get_pgconn(obj));
1183
- if (!user) return Qnil;
1184
- return rb_tainted_str_new2(user);
1185
- }
1186
-
1187
- /*
1188
- * call-seq:
1189
- * conn.status()
1190
- *
1191
- * MISSING: documentation
1192
- */
1193
- static VALUE
1194
- pgconn_status(obj)
1195
- VALUE obj;
1196
- {
1197
- return INT2NUM(PQstatus(get_pgconn(obj)));
1198
- }
1199
-
1200
- /*
1201
- * call-seq:
1202
- * conn.error()
1203
- *
1204
- * Returns the error message about connection.
1205
- */
1206
- static VALUE
1207
- pgconn_error(obj)
1208
- VALUE obj;
1209
- {
1210
- char *error = PQerrorMessage(get_pgconn(obj));
1211
- if (!error) return Qnil;
1212
- return rb_tainted_str_new2(error);
1213
- }
1214
-
1215
- /*TODO broken for ruby 1.9
1216
- * call-seq:
1217
- * conn.trace( port )
1218
- *
1219
- * Enables tracing message passing between backend.
1220
- * The trace message will be written to the _port_ object,
1221
- * which is an instance of the class +File+.
1222
- */
1223
- static VALUE
1224
- pgconn_trace(obj, port)
1225
- VALUE obj, port;
1226
- {
1227
- //OpenFile* fp;
1228
-
1229
- Check_Type(port, T_FILE);
1230
- //GetOpenFile(port, fp);
1231
-
1232
- //PQtrace(get_pgconn(obj), fp->f2?fp->f2:fp->f);
1233
-
1234
- return obj;
1235
- }
1236
-
1237
- /*
1238
- * call-seq:
1239
- * conn.untrace()
1240
- *
1241
- * Disables the message tracing.
1242
- */
1243
- static VALUE
1244
- pgconn_untrace(obj)
1245
- VALUE obj;
1246
- {
1247
- PQuntrace(get_pgconn(obj));
1248
- return obj;
1249
- }
1250
-
1251
- /*
1252
- * call-seq:
1253
- * conn.transaction_status()
1254
- *
1255
- * returns one of the following statuses:
1256
- * PQTRANS_IDLE = 0 (connection idle)
1257
- * PQTRANS_ACTIVE = 1 (command in progress)
1258
- * PQTRANS_INTRANS = 2 (idle, within transaction block)
1259
- * PQTRANS_INERROR = 3 (idle, within failed transaction)
1260
- * PQTRANS_UNKNOWN = 4 (cannot determine status)
1261
- *
1262
- * See the PostgreSQL documentation on PQtransactionStatus[http://www.postgresql.org/docs/current/interactive/libpq-status.html#AEN24919] for more information.
1263
- */
1264
- static VALUE
1265
- pgconn_transaction_status(obj)
1266
- VALUE obj;
1267
- {
1268
- return INT2NUM(PQtransactionStatus(get_pgconn(obj)));
1269
- }
1270
-
1271
- #ifdef HAVE_PQSETCLIENTENCODING
1272
-
1273
- /*
1274
- * call-seq:
1275
- * conn.protocol_version -> Integer
1276
- *
1277
- * The 3.0 protocol will normally be used when communicating with PostgreSQL 7.4 or later servers; pre-7.4 servers support only protocol 2.0. (Protocol 1.0 is obsolete and not supported by libpq.)
1278
- */
1279
- static VALUE
1280
- pgconn_protocol_version(obj)
1281
- VALUE obj;
1282
- {
1283
- return INT2NUM(PQprotocolVersion(get_pgconn(obj)));
1284
- }
1285
-
1286
- /*
1287
- * call-seq:
1288
- * conn.server_version -> Integer
1289
- *
1290
- * The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. For example, version 7.4.2 will be returned as 70402, and version 8.1 will be returned as 80100 (leading zeroes are not shown). Zero is returned if the connection is bad.
1291
- */
1292
- static VALUE
1293
- pgconn_server_version(obj)
1294
- VALUE obj;
1295
- {
1296
- return INT2NUM(PQserverVersion(get_pgconn(obj)));
1297
- }
1298
-
1299
- /*
1300
- * call-seq:
1301
- * conn.lastval -> Integer
1302
- *
1303
- * Returns the sequence value returned by the last call to the PostgreSQL function <tt>nextval(sequence_name)</tt>. Equivalent to <tt>conn.query('select lastval()').first.first</tt>.
1304
- *
1305
- * This functionality is only available with PostgreSQL 8.1 and newer.
1306
- * See the PostgreSQL documentation on lastval[http://www.postgresql.org/docs/current/interactive/functions-sequence.html] for more information.
1307
- */
1308
- static VALUE
1309
- pgconn_lastval(obj)
1310
- VALUE obj;
1311
- {
1312
- PGconn *conn = get_pgconn(obj);
1313
- PGresult *result;
1314
- VALUE lastval, error;
1315
-
1316
- result = PQexec(conn, "select lastval()");
1317
- if (!result) rb_raise(rb_ePGError, PQerrorMessage(conn));
1318
-
1319
- switch (PQresultStatus(result)) {
1320
- case PGRES_TUPLES_OK:
1321
- lastval = rb_cstr2inum(PQgetvalue(result, 0, 0), 10);
1322
- PQclear(result);
1323
- return lastval;
1324
-
1325
- case PGRES_BAD_RESPONSE:
1326
- case PGRES_FATAL_ERROR:
1327
- case PGRES_NONFATAL_ERROR:
1328
- error = rb_str_new2(PQresultErrorMessage(result));
1329
- PQclear(result);
1330
- rb_raise(rb_ePGError, StringValuePtr(error));
1331
-
1332
- default:
1333
- PQclear(result);
1334
- rb_raise(rb_ePGError, "unknown lastval");
1335
- }
1336
- }
1337
-
1338
- /*
1339
- * call-seq:
1340
- * conn.client_encoding() -> String
1341
- *
1342
- * Returns the client encoding as a String.
1343
- */
1344
- static VALUE
1345
- pgconn_client_encoding(obj)
1346
- VALUE obj;
1347
- {
1348
- char *encoding = (char *)pg_encoding_to_char(PQclientEncoding(get_pgconn(obj)));
1349
- return rb_tainted_str_new2(encoding);
1350
- }
1351
-
1352
- /*
1353
- * call-seq:
1354
- * conn.set_client_encoding( encoding )
1355
- *
1356
- * Sets the client encoding to the _encoding_ String.
1357
- */
1358
- static VALUE
1359
- pgconn_set_client_encoding(obj, str)
1360
- VALUE obj, str;
1361
- {
1362
- Check_Type(str, T_STRING);
1363
- if ((PQsetClientEncoding(get_pgconn(obj), StringValuePtr(str))) == -1){
1364
- rb_raise(rb_ePGError, "invalid encoding name: %s",StringValuePtr(str));
1365
- }
1366
- return Qnil;
1367
- }
1368
- #endif
1369
-
1370
- static void
1371
- free_pgresult(ptr)
1372
- PGresult *ptr;
1373
- {
1374
- PQclear(ptr);
1375
- }
1376
-
1377
- #define VARHDRSZ 4
1378
- #define SCALE_MASK 0xffff
1379
-
1380
- static int
1381
- has_numeric_scale(typmod)
1382
- int typmod;
1383
- {
1384
- if (typmod == -1) return 1;
1385
- return (typmod - VARHDRSZ) & SCALE_MASK;
1386
- }
1387
-
1388
- #define PARSE(klass, string) rb_funcall(klass, rb_intern("parse"), 1, rb_tainted_str_new2(string));
1389
-
1390
- static VALUE
1391
- fetch_pgresult(result, row, column)
1392
- PGresult *result;
1393
- int row;
1394
- int column;
1395
- {
1396
- char* string;
1397
-
1398
- if (PQgetisnull(result, row, column)) {
1399
- return Qnil;
1400
- }
1401
-
1402
- string = PQgetvalue(result, row, column);
1403
-
1404
- if (!translate_results) {
1405
- return rb_tainted_str_new2(string);
1406
- }
1407
-
1408
- switch (PQftype(result, column)) {
1409
-
1410
- case BOOLOID:
1411
- return *string == 't' ? Qtrue : Qfalse;
1412
-
1413
- case BYTEAOID:
1414
- return pgconn_s_unescape_bytea(rb_cPGconn, rb_tainted_str_new2(string));
1415
-
1416
- case NUMERICOID:
1417
- if (has_numeric_scale(PQfmod(result, column))) {
1418
- return rb_funcall(rb_cBigDecimal, rb_intern("new"), 1, rb_tainted_str_new2(string));
1419
- }
1420
- /* when scale == 0 return inum */
1421
-
1422
- case INT8OID:
1423
- case INT4OID:
1424
- case INT2OID:
1425
- return rb_cstr2inum(string, 10);
1426
-
1427
- case FLOAT8OID:
1428
- case FLOAT4OID:
1429
- return rb_float_new(rb_cstr_to_dbl(string, Qfalse));
1430
-
1431
- case DATEOID:
1432
- return PARSE(rb_cDate, string);
1433
- case TIMEOID:
1434
- case TIMETZOID:
1435
- case TIMESTAMPOID:
1436
- case TIMESTAMPTZOID:
1437
- return PARSE(rb_cTime, string);
1438
-
1439
- default:
1440
- return rb_tainted_str_new2(string);
1441
- }
1442
- }
1443
-
1444
-
1445
- static VALUE
1446
- pgresult_new(ptr)
1447
- PGresult *ptr;
1448
- {
1449
- return Data_Wrap_Struct(rb_cPGresult, 0, free_pgresult, ptr);
1450
- }
1451
-
1452
- /*
1453
- * call-seq:
1454
- * res.status()
1455
- *
1456
- * Returns the status of the query. The status value is one of:
1457
- * * +EMPTY_QUERY+
1458
- * * +COMMAND_OK+
1459
- * * +TUPLES_OK+
1460
- * * +COPY_OUT+
1461
- * * +COPY_IN+
1462
- */
1463
- static VALUE
1464
- pgresult_status(obj)
1465
- VALUE obj;
1466
- {
1467
- return INT2NUM(PQresultStatus(get_pgresult(obj)));
1468
- }
1469
-
1470
- /*
1471
- * call-seq:
1472
- * res.result()
1473
- *
1474
- * Returns an array of tuples (rows, which are themselves arrays) that represent the query result.
1475
- */
1476
-
1477
- static VALUE
1478
- fetch_pgrow(self, fields, row_num)
1479
- VALUE self, fields;
1480
- int row_num;
1481
- {
1482
- PGresult *result = get_pgresult(self);
1483
- VALUE row = rb_funcall(rb_cPGrow, rb_intern("new"), 1, fields);
1484
- int field_num;
1485
- for (field_num = 0; field_num < RARRAY(fields)->len; field_num++) {
1486
- /* don't use push, PGrow is sized with nils in #new */
1487
- rb_ary_store(row, field_num, fetch_pgresult(result, row_num, field_num));
1488
- }
1489
- return row;
1490
- }
1491
-
1492
- /*
1493
- * call-seq:
1494
- * conn.select_one(query, *bind_values)
1495
- *
1496
- * Return the first row of the query results.
1497
- * Equivalent to conn.query(query, *bind_values).first
1498
- */
1499
- static VALUE
1500
- pgconn_select_one(argc, argv, self)
1501
- int argc;
1502
- VALUE *argv;
1503
- VALUE self;
1504
- {
1505
- VALUE result = pgconn_exec(argc, argv, self);
1506
- VALUE row = fetch_pgrow(result, pgresult_fields(result), 0);
1507
- pgresult_clear(result);
1508
- return row;
1509
- }
1510
-
1511
- /*
1512
- * call-seq:
1513
- * conn.select_value(query, *bind_values)
1514
- *
1515
- * Return the first value of the first row of the query results.
1516
- * Equivalent to conn.query(query, *bind_values).first.first
1517
- */
1518
- static VALUE
1519
- pgconn_select_value(argc, argv, self)
1520
- int argc;
1521
- VALUE *argv;
1522
- VALUE self;
1523
- {
1524
- VALUE result = pgconn_exec(argc, argv, self);
1525
- VALUE value = fetch_pgresult(get_pgresult(result), 0, 0);
1526
- pgresult_clear(result);
1527
- return value;
1528
- }
1529
-
1530
- /*
1531
- * call-seq:
1532
- * conn.select_values(query, *bind_values)
1533
- *
1534
- * Equivalent to conn.query(query, *bind_values).flatten
1535
- */
1536
- static VALUE
1537
- pgconn_select_values(argc, argv, self)
1538
- int argc;
1539
- VALUE *argv;
1540
- VALUE self;
1541
- {
1542
- VALUE pg_result = pgconn_exec(argc, argv, self);
1543
- PGresult * result = get_pgresult(pg_result);
1544
- int ntuples = PQntuples(result);
1545
- int nfields = PQnfields(result);
1546
-
1547
- VALUE values = rb_ary_new2(ntuples * nfields);
1548
- int row_num, field_num;
1549
- for (row_num = 0; row_num < ntuples; row_num++) {
1550
- for (field_num = 0; field_num < nfields; field_num++) {
1551
- rb_ary_push(values, fetch_pgresult(result, row_num, field_num));
1552
- }
1553
- }
1554
-
1555
- pgresult_clear(pg_result);
1556
- return values;
1557
- }
1558
-
1559
- /*
1560
- * call-seq:
1561
- * res.each{ |tuple| ... }
1562
- *
1563
- * Invokes the block for each tuple (row) in the result.
1564
- *
1565
- * Equivalent to <tt>res.result.each{ |tuple| ... }</tt>.
1566
- */
1567
- static VALUE
1568
- pgresult_each(self)
1569
- VALUE self;
1570
- {
1571
- PGresult *result = get_pgresult(self);
1572
- int row_count = PQntuples(result);
1573
- VALUE fields = pgresult_fields(self);
1574
-
1575
- int row_num;
1576
- for (row_num = 0; row_num < row_count; row_num++) {
1577
- VALUE row = fetch_pgrow(self, fields, row_num);
1578
- rb_yield(row);
1579
- }
1580
-
1581
- return self;
1582
- }
1583
-
1584
- /*
1585
- * call-seq:
1586
- * res[ n ]
1587
- *
1588
- * Returns the tuple (row) corresponding to _n_. Returns +nil+ if <tt>_n_ >= res.num_tuples</tt>.
1589
- *
1590
- * Equivalent to <tt>res.result[n]</tt>.
1591
- */
1592
- static VALUE
1593
- pgresult_aref(argc, argv, obj)
1594
- int argc;
1595
- VALUE *argv;
1596
- VALUE obj;
1597
- {
1598
- PGresult *result;
1599
- VALUE a1, a2, val;
1600
- int i, j, nf, nt;
1601
-
1602
- result = get_pgresult(obj);
1603
- nt = PQntuples(result);
1604
- nf = PQnfields(result);
1605
- switch (rb_scan_args(argc, argv, "11", &a1, &a2)) {
1606
- case 1:
1607
- i = NUM2INT(a1);
1608
- if( i >= nt ) return Qnil;
1609
-
1610
- val = rb_ary_new();
1611
- for (j=0; j<nf; j++) {
1612
- VALUE value = fetch_pgresult(result, i, j);
1613
- rb_ary_push(val, value);
1614
- }
1615
- return val;
1616
-
1617
- case 2:
1618
- i = NUM2INT(a1);
1619
- if( i >= nt ) return Qnil;
1620
- j = NUM2INT(a2);
1621
- if( j >= nf ) return Qnil;
1622
- return fetch_pgresult(result, i, j);
1623
-
1624
- default:
1625
- return Qnil; /* not reached */
1626
- }
1627
- }
1628
-
1629
- /*
1630
- * call-seq:
1631
- * res.fields()
1632
- *
1633
- * Returns an array of Strings representing the names of the fields in the result.
1634
- *
1635
- * res=conn.exec("SELECT foo,bar AS biggles,jim,jam FROM mytable")
1636
- * res.fields => [ 'foo' , 'biggles' , 'jim' , 'jam' ]
1637
- */
1638
- static VALUE
1639
- pgresult_fields(obj)
1640
- VALUE obj;
1641
- {
1642
- PGresult *result;
1643
- VALUE ary;
1644
- int n, i;
1645
-
1646
- result = get_pgresult(obj);
1647
- n = PQnfields(result);
1648
- ary = rb_ary_new2(n);
1649
- for (i=0;i<n;i++) {
1650
- rb_ary_push(ary, rb_tainted_str_new2(PQfname(result, i)));
1651
- }
1652
- return ary;
1653
- }
1654
-
1655
- /*
1656
- * call-seq:
1657
- * res.num_tuples()
1658
- *
1659
- * Returns the number of tuples (rows) in the query result.
1660
- *
1661
- * Similar to <tt>res.result.length</tt> (but faster).
1662
- */
1663
- static VALUE
1664
- pgresult_num_tuples(obj)
1665
- VALUE obj;
1666
- {
1667
- int n;
1668
-
1669
- n = PQntuples(get_pgresult(obj));
1670
- return INT2NUM(n);
1671
- }
1672
-
1673
- /*
1674
- * call-seq:
1675
- * res.num_fields()
1676
- *
1677
- * Returns the number of fields (columns) in the query result.
1678
- *
1679
- * Similar to <tt>res.result[0].length</tt> (but faster).
1680
- */
1681
- static VALUE
1682
- pgresult_num_fields(obj)
1683
- VALUE obj;
1684
- {
1685
- int n;
1686
-
1687
- n = PQnfields(get_pgresult(obj));
1688
- return INT2NUM(n);
1689
- }
1690
-
1691
- /*
1692
- * call-seq:
1693
- * res.fieldname( index )
1694
- *
1695
- * Returns the name of the field (column) corresponding to the index.
1696
- *
1697
- * res=conn.exec("SELECT foo,bar AS biggles,jim,jam FROM mytable")
1698
- * puts res.fieldname(2) => 'jim'
1699
- * puts res.fieldname(1) => 'biggles'
1700
- *
1701
- * Equivalent to <tt>res.fields[_index_]</tt>.
1702
- */
1703
- static VALUE
1704
- pgresult_fieldname(obj, index)
1705
- VALUE obj, index;
1706
- {
1707
- PGresult *result;
1708
- int i = NUM2INT(index);
1709
- char *name;
1710
-
1711
- result = get_pgresult(obj);
1712
- if (i < 0 || i >= PQnfields(result)) {
1713
- rb_raise(rb_eArgError,"invalid field number %d", i);
1714
- }
1715
- name = PQfname(result, i);
1716
- return rb_tainted_str_new2(name);
1717
- }
1718
-
1719
- /*
1720
- * call-seq:
1721
- * res.fieldnum( name )
1722
- *
1723
- * Returns the index of the field specified by the string _name_.
1724
- *
1725
- * res=conn.exec("SELECT foo,bar AS biggles,jim,jam FROM mytable")
1726
- * puts res.fieldnum('foo') => 0
1727
- *
1728
- * Raises an ArgumentError if the specified _name_ isn't one of the field names;
1729
- * raises a TypeError if _name_ is not a String.
1730
- */
1731
- static VALUE
1732
- pgresult_fieldnum(obj, name)
1733
- VALUE obj, name;
1734
- {
1735
- int n;
1736
-
1737
- Check_Type(name, T_STRING);
1738
-
1739
- n = PQfnumber(get_pgresult(obj), StringValuePtr(name));
1740
- if (n == -1) {
1741
- rb_raise(rb_eArgError,"Unknown field: %s", StringValuePtr(name));
1742
- }
1743
- return INT2NUM(n);
1744
- }
1745
-
1746
- /*
1747
- * call-seq:
1748
- * res.type( index )
1749
- *
1750
- * Returns the data type associated with the given column number.
1751
- *
1752
- * The integer returned is the internal +OID+ number (in PostgreSQL) of the type.
1753
- * If you have the PostgreSQL source available, you can see the OIDs for every column type in the file <tt>src/include/catalog/pg_type.h</tt>.
1754
- */
1755
- static VALUE
1756
- pgresult_type(obj, index)
1757
- VALUE obj, index;
1758
- {
1759
- PGresult* result = get_pgresult(obj);
1760
- int i = NUM2INT(index);
1761
- if (i < 0 || i >= PQnfields(result)) {
1762
- rb_raise(rb_eArgError, "invalid field number %d", i);
1763
- }
1764
- return INT2NUM(PQftype(result, i));
1765
- }
1766
-
1767
- /*
1768
- * call-seq:
1769
- * res.size( index )
1770
- *
1771
- * Returns the size of the field type in bytes. Returns <tt>-1</tt> if the field is variable sized.
1772
- *
1773
- * res = conn.exec("SELECT myInt, myVarChar50 FROM foo")
1774
- * res.size(0) => 4
1775
- * res.size(1) => -1
1776
- */
1777
- static VALUE
1778
- pgresult_size(obj, index)
1779
- VALUE obj, index;
1780
- {
1781
- PGresult *result;
1782
- int i = NUM2INT(index);
1783
- int size;
1784
-
1785
- result = get_pgresult(obj);
1786
- if (i < 0 || i >= PQnfields(result)) {
1787
- rb_raise(rb_eArgError,"invalid field number %d", i);
1788
- }
1789
- size = PQfsize(result, i);
1790
- return INT2NUM(size);
1791
- }
1792
-
1793
- /*
1794
- * call-seq:
1795
- * res.value( tup_num, field_num )
1796
- *
1797
- * Returns the value in tuple number <i>tup_num</i>, field number <i>field_num</i>. (Row <i>tup_num</i>, column <i>field_num</i>.)
1798
- *
1799
- * Equivalent to <tt>res.result[<i>tup_num</i>][<i>field_num</i>]</tt> (but faster).
1800
- */
1801
- static VALUE
1802
- pgresult_getvalue(obj, tup_num, field_num)
1803
- VALUE obj, tup_num, field_num;
1804
- {
1805
- PGresult *result;
1806
- int i = NUM2INT(tup_num);
1807
- int j = NUM2INT(field_num);
1808
-
1809
- result = get_pgresult(obj);
1810
- if (i < 0 || i >= PQntuples(result)) {
1811
- rb_raise(rb_eArgError,"invalid tuple number %d", i);
1812
- }
1813
- if (j < 0 || j >= PQnfields(result)) {
1814
- rb_raise(rb_eArgError,"invalid field number %d", j);
1815
- }
1816
-
1817
- return fetch_pgresult(result, i, j);
1818
- }
1819
-
1820
-
1821
- /*
1822
- * call-seq:
1823
- * res.value_byname( tup_num, field_name )
1824
- *
1825
- * Returns the value in tuple number <i>tup_num</i>, for the field named <i>field_name</i>.
1826
- *
1827
- * Equivalent to (but faster than) either of:
1828
- * res.result[<i>tup_num</i>][ res.fieldnum(<i>field_name</i>) ]
1829
- * res.value( <i>tup_num</i>, res.fieldnum(<i>field_name</i>) )
1830
- *
1831
- * <i>(This method internally calls #value as like the second example above; it is slower than using the field index directly.)</i>
1832
- */
1833
- static VALUE
1834
- pgresult_getvalue_byname(obj, tup_num, field_name)
1835
- VALUE obj, tup_num, field_name;
1836
- {
1837
- return pgresult_getvalue(obj, tup_num, pgresult_fieldnum(obj, field_name));
1838
- }
1839
-
1840
-
1841
- /*
1842
- * call-seq:
1843
- * res.getlength( tup_num, field_num )
1844
- *
1845
- * Returns the (String) length of the field in bytes.
1846
- *
1847
- * Equivalent to <tt>res.value(<i>tup_num</i>,<i>field_num</i>).length</tt>.
1848
- */
1849
- static VALUE
1850
- pgresult_getlength(obj, tup_num, field_num)
1851
- VALUE obj, tup_num, field_num;
1852
- {
1853
- PGresult *result;
1854
- int i = NUM2INT(tup_num);
1855
- int j = NUM2INT(field_num);
1856
-
1857
- result = get_pgresult(obj);
1858
- if (i < 0 || i >= PQntuples(result)) {
1859
- rb_raise(rb_eArgError,"invalid tuple number %d", i);
1860
- }
1861
- if (j < 0 || j >= PQnfields(result)) {
1862
- rb_raise(rb_eArgError,"invalid field number %d", j);
1863
- }
1864
- return INT2FIX(PQgetlength(result, i, j));
1865
- }
1866
-
1867
- /*
1868
- * call-seq:
1869
- * res.getisnull(tuple_position, field_position) -> boolean
1870
- *
1871
- * Returns +true+ if the specified value is +nil+; +false+ otherwise.
1872
- *
1873
- * Equivalent to <tt>res.value(<i>tup_num</i>,<i>field_num</i>)==+nil+</tt>.
1874
- */
1875
- static VALUE
1876
- pgresult_getisnull(obj, tup_num, field_num)
1877
- VALUE obj, tup_num, field_num;
1878
- {
1879
- PGresult *result;
1880
- int i = NUM2INT(tup_num);
1881
- int j = NUM2INT(field_num);
1882
-
1883
- result = get_pgresult(obj);
1884
- if (i < 0 || i >= PQntuples(result)) {
1885
- rb_raise(rb_eArgError,"invalid tuple number %d", i);
1886
- }
1887
- if (j < 0 || j >= PQnfields(result)) {
1888
- rb_raise(rb_eArgError,"invalid field number %d", j);
1889
- }
1890
- return PQgetisnull(result, i, j) ? Qtrue : Qfalse;
1891
- }
1892
-
1893
- /*
1894
- * call-seq:
1895
- * res.cmdtuples()
1896
- *
1897
- * Returns the number of tuples (rows) affected by the SQL command.
1898
- *
1899
- * If the SQL command that generated the PGresult was not one of +INSERT+, +UPDATE+, +DELETE+, +MOVE+, or +FETCH+, or if no tuples (rows) were affected, <tt>0</tt> is returned.
1900
- */
1901
- static VALUE
1902
- pgresult_cmdtuples(obj)
1903
- VALUE obj;
1904
- {
1905
- long n;
1906
- n = strtol(PQcmdTuples(get_pgresult(obj)),NULL, 10);
1907
- return INT2NUM(n);
1908
- }
1909
- /*
1910
- * call-seq:
1911
- * res.cmdstatus()
1912
- *
1913
- * Returns the status string of the last query command.
1914
- */
1915
- static VALUE
1916
- pgresult_cmdstatus(obj)
1917
- VALUE obj;
1918
- {
1919
- return rb_tainted_str_new2(PQcmdStatus(get_pgresult(obj)));
1920
- }
1921
-
1922
- /*
1923
- * call-seq:
1924
- * res.oid()
1925
- *
1926
- * Returns the +oid+.
1927
- */
1928
- static VALUE
1929
- pgresult_oid(obj)
1930
- VALUE obj;
1931
- {
1932
- Oid n = PQoidValue(get_pgresult(obj));
1933
- if (n == InvalidOid)
1934
- return Qnil;
1935
- else
1936
- return INT2NUM(n);
1937
- }
1938
-
1939
- /*
1940
- * call-seq:
1941
- * res.clear()
1942
- *
1943
- * Clears the PGresult object as the result of the query.
1944
- */
1945
- static VALUE
1946
- pgresult_clear(obj)
1947
- VALUE obj;
1948
- {
1949
- PQclear(get_pgresult(obj));
1950
- DATA_PTR(obj) = 0;
1951
-
1952
- return Qnil;
1953
- }
1954
-
1955
- static VALUE
1956
- pgresult_result_with_clear(self)
1957
- VALUE self;
1958
- {
1959
- VALUE rows = rb_funcall(self, rb_intern("rows"), 0);
1960
- pgresult_clear(self);
1961
- return rows;
1962
- }
1963
-
1964
- /* Large Object support */
1965
- static PGlarge*
1966
- get_pglarge(obj)
1967
- VALUE obj;
1968
- {
1969
- PGlarge *pglarge;
1970
- Data_Get_Struct(obj, PGlarge, pglarge);
1971
- if (pglarge == NULL) rb_raise(rb_ePGError, "invalid large object");
1972
- return pglarge;
1973
- }
1974
-
1975
- /*
1976
- * call-seq:
1977
- * conn.lo_import(file) -> PGlarge
1978
- *
1979
- * Import a file to a large object. Returns a PGlarge instance on success. On failure, it raises a PGError exception.
1980
- */
1981
- static VALUE
1982
- pgconn_loimport(obj, filename)
1983
- VALUE obj, filename;
1984
- {
1985
- Oid lo_oid;
1986
-
1987
- PGconn *conn = get_pgconn(obj);
1988
-
1989
- Check_Type(filename, T_STRING);
1990
-
1991
- lo_oid = lo_import(conn, StringValuePtr(filename));
1992
- if (lo_oid == 0) {
1993
- rb_raise(rb_ePGError, PQerrorMessage(conn));
1994
- }
1995
- return pglarge_new(conn, lo_oid, -1);
1996
- }
1997
-
1998
- /*
1999
- * call-seq:
2000
- * conn.lo_export( oid, file )
2001
- *
2002
- * Saves a large object of _oid_ to a _file_.
2003
- */
2004
- static VALUE
2005
- pgconn_loexport(obj, lo_oid,filename)
2006
- VALUE obj, lo_oid, filename;
2007
- {
2008
- PGconn *conn = get_pgconn(obj);
2009
- int oid;
2010
- Check_Type(filename, T_STRING);
2011
-
2012
- oid = NUM2INT(lo_oid);
2013
- if (oid < 0) {
2014
- rb_raise(rb_ePGError, "invalid large object oid %d",oid);
2015
- }
2016
-
2017
- if (!lo_export(conn, oid, StringValuePtr(filename))) {
2018
- rb_raise(rb_ePGError, PQerrorMessage(conn));
2019
- }
2020
- return Qnil;
2021
- }
2022
-
2023
- /*
2024
- * call-seq:
2025
- * conn.lo_create( [mode] ) -> PGlarge
2026
- *
2027
- * Returns a PGlarge instance on success. On failure, it raises PGError exception.
2028
- * <i>(See #lo_open for information on _mode_.)</i>
2029
- */
2030
- static VALUE
2031
- pgconn_locreate(argc, argv, obj)
2032
- int argc;
2033
- VALUE *argv;
2034
- VALUE obj;
2035
- {
2036
- Oid lo_oid;
2037
- int mode;
2038
- VALUE nmode;
2039
- PGconn *conn;
2040
-
2041
- if (rb_scan_args(argc, argv, "01", &nmode) == 0) {
2042
- mode = INV_READ;
2043
- }
2044
- else {
2045
- mode = FIX2INT(nmode);
2046
- }
2047
-
2048
- conn = get_pgconn(obj);
2049
- lo_oid = lo_creat(conn, mode);
2050
- if (lo_oid == 0){
2051
- rb_raise(rb_ePGError, "can't creat large object");
2052
- }
2053
-
2054
- return pglarge_new(conn, lo_oid, -1);
2055
- }
2056
-
2057
- /*
2058
- * call-seq:
2059
- * conn.lo_open( oid, [mode] ) -> PGlarge
2060
- *
2061
- * Open a large object of _oid_. Returns a PGlarge instance on success.
2062
- * The _mode_ argument specifies the mode for the opened large object,
2063
- * which is either +INV_READ+, or +INV_WRITE+.
2064
- * * If _mode_ On failure, it raises a PGError exception.
2065
- * * If _mode_ is omitted, the default is +INV_READ+.
2066
- */
2067
- static VALUE
2068
- pgconn_loopen(argc, argv, obj)
2069
- int argc;
2070
- VALUE *argv;
2071
- VALUE obj;
2072
- {
2073
- Oid lo_oid;
2074
- int fd, mode;
2075
- VALUE nmode, objid;
2076
- PGconn *conn = get_pgconn(obj);
2077
-
2078
- switch (rb_scan_args(argc, argv, "02", &objid, &nmode)) {
2079
- case 1:
2080
- lo_oid = NUM2INT(objid);
2081
- mode = INV_READ;
2082
- break;
2083
- case 2:
2084
- lo_oid = NUM2INT(objid);
2085
- mode = FIX2INT(nmode);
2086
- break;
2087
- default:
2088
- mode = INV_READ;
2089
- lo_oid = lo_creat(conn, mode);
2090
- if (lo_oid == 0){
2091
- rb_raise(rb_ePGError, "can't creat large object");
2092
- }
2093
- }
2094
- if((fd = lo_open(conn, lo_oid, mode)) < 0) {
2095
- rb_raise(rb_ePGError, "can't open large object");
2096
- }
2097
- return pglarge_new(conn, lo_oid, fd);
2098
- }
2099
-
2100
- /*
2101
- * call-seq:
2102
- * conn.lo_unlink( oid )
2103
- *
2104
- * Unlinks (deletes) the postgres large object of _oid_.
2105
- */
2106
- static VALUE
2107
- pgconn_lounlink(obj, lo_oid)
2108
- VALUE obj, lo_oid;
2109
- {
2110
- PGconn *conn;
2111
- int oid = NUM2INT(lo_oid);
2112
- int result;
2113
-
2114
- if (oid < 0){
2115
- rb_raise(rb_ePGError, "invalid oid %d",oid);
2116
- }
2117
- conn = get_pgconn(obj);
2118
- result = lo_unlink(conn,oid);
2119
-
2120
- return Qnil;
2121
- }
2122
-
2123
- static void
2124
- free_pglarge(ptr)
2125
- PGlarge *ptr;
2126
- {
2127
- if ((ptr->lo_fd) > 0) {
2128
- lo_close(ptr->pgconn,ptr->lo_fd);
2129
- }
2130
- free(ptr);
2131
- }
2132
-
2133
- static VALUE
2134
- pglarge_new(conn, lo_oid ,lo_fd)
2135
- PGconn *conn;
2136
- Oid lo_oid;
2137
- int lo_fd;
2138
- {
2139
- VALUE obj;
2140
- PGlarge *pglarge;
2141
-
2142
- obj = Data_Make_Struct(rb_cPGlarge, PGlarge, 0, free_pglarge, pglarge);
2143
- pglarge->pgconn = conn;
2144
- pglarge->lo_oid = lo_oid;
2145
- pglarge->lo_fd = lo_fd;
2146
-
2147
- return obj;
2148
- }
2149
-
2150
- /*
2151
- * call-seq:
2152
- * lrg.oid()
2153
- *
2154
- * Returns the large object's +oid+.
2155
- */
2156
- static VALUE
2157
- pglarge_oid(obj)
2158
- VALUE obj;
2159
- {
2160
- PGlarge *pglarge = get_pglarge(obj);
2161
-
2162
- return INT2NUM(pglarge->lo_oid);
2163
- }
2164
-
2165
- /*
2166
- * call-seq:
2167
- * lrg.open( [mode] )
2168
- *
2169
- * Opens a large object.
2170
- * The _mode_ argument specifies the mode for the opened large object,
2171
- * which is either +INV_READ+ or +INV_WRITE+.
2172
- */
2173
- static VALUE
2174
- pglarge_open(argc, argv, obj)
2175
- int argc;
2176
- VALUE *argv;
2177
- VALUE obj;
2178
- {
2179
- PGlarge *pglarge = get_pglarge(obj);
2180
- VALUE nmode;
2181
- int fd;
2182
- int mode;
2183
-
2184
- if (rb_scan_args(argc, argv, "01", &nmode) == 0) {
2185
- mode = INV_READ;
2186
- }
2187
- else {
2188
- mode = FIX2INT(nmode);
2189
- }
2190
-
2191
- if((fd = lo_open(pglarge->pgconn, pglarge->lo_oid, mode)) < 0) {
2192
- rb_raise(rb_ePGError, "can't open large object");
2193
- }
2194
- pglarge->lo_fd = fd;
2195
-
2196
- return INT2FIX(pglarge->lo_fd);
2197
- }
2198
-
2199
- /*
2200
- * call-seq:
2201
- * lrg.close()
2202
- *
2203
- * Closes a large object. Closed when they are garbage-collected.
2204
- */
2205
- static VALUE
2206
- pglarge_close(obj)
2207
- VALUE obj;
2208
- {
2209
- PGlarge *pglarge = get_pglarge(obj);
2210
-
2211
- if((lo_close(pglarge->pgconn, pglarge->lo_fd)) < 0) {
2212
- rb_raise(rb_ePGError, "can't closed large object");
2213
- }
2214
- DATA_PTR(obj) = 0;
2215
-
2216
- return Qnil;
2217
- }
2218
-
2219
- /*
2220
- * call-seq:
2221
- * lrg.tell()
2222
- *
2223
- * Returns the current position of the large object pointer.
2224
- */
2225
- static VALUE
2226
- pglarge_tell(obj)
2227
- VALUE obj;
2228
- {
2229
- int start;
2230
- PGlarge *pglarge = get_pglarge(obj);
2231
-
2232
- if ((start = lo_tell(pglarge->pgconn,pglarge->lo_fd)) == -1) {
2233
- rb_raise(rb_ePGError, "error while getting position");
2234
- }
2235
- return INT2NUM(start);
2236
- }
2237
-
2238
- static VALUE
2239
- loread_all(obj)
2240
- VALUE obj;
2241
- {
2242
- PGlarge *pglarge = get_pglarge(obj);
2243
- VALUE str;
2244
- long siz = BUFSIZ;
2245
- long bytes = 0;
2246
- int n;
2247
-
2248
- str = rb_tainted_str_new(0,siz);
2249
- for (;;) {
2250
- n = lo_read(pglarge->pgconn, pglarge->lo_fd, RSTRING_PTR(str) + bytes,siz - bytes);
2251
- if (n == 0 && bytes == 0) return Qnil;
2252
- bytes += n;
2253
- if (bytes < siz ) break;
2254
- siz += BUFSIZ;
2255
- rb_str_resize(str,siz);
2256
- }
2257
- if (bytes == 0) return rb_tainted_str_new(0,0);
2258
- if (bytes != siz) rb_str_resize(str, bytes);
2259
- return str;
2260
- }
2261
-
2262
- /*
2263
- * call-seq:
2264
- * lrg.read( [length] )
2265
- *
2266
- * Attempts to read _length_ bytes from large object.
2267
- * If no _length_ is given, reads all data.
2268
- */
2269
- static VALUE
2270
- pglarge_read(argc, argv, obj)
2271
- int argc;
2272
- VALUE *argv;
2273
- VALUE obj;
2274
- {
2275
- int len;
2276
- PGlarge *pglarge = get_pglarge(obj);
2277
- VALUE length;
2278
- char *buffer;
2279
-
2280
- rb_scan_args(argc, argv, "01", &length);
2281
- if (NIL_P(length)) {
2282
- return loread_all(obj);
2283
- }
2284
-
2285
- len = NUM2INT(length);
2286
- if (len < 0){
2287
- rb_raise(rb_ePGError,"nagative length %d given", len);
2288
- }
2289
- buffer = ALLOCA_N(char, len);
2290
-
2291
- if((len = lo_read(pglarge->pgconn, pglarge->lo_fd, buffer, len)) < 0) {
2292
- rb_raise(rb_ePGError, "error while reading");
2293
- }
2294
- if (len == 0) return Qnil;
2295
- return rb_str_new(buffer,len);
2296
- }
2297
-
2298
- /*
2299
- * call-seq:
2300
- * lrg.write( str )
2301
- *
2302
- * Writes the string _str_ to the large object.
2303
- * Returns the number of bytes written.
2304
- */
2305
- static VALUE
2306
- pglarge_write(obj, buffer)
2307
- VALUE obj, buffer;
2308
- {
2309
- int n;
2310
- PGlarge *pglarge = get_pglarge(obj);
2311
-
2312
- Check_Type(buffer, T_STRING);
2313
-
2314
- if( RSTRING_LEN(buffer) < 0) {
2315
- rb_raise(rb_ePGError, "write buffer zero string");
2316
- }
2317
- if((n = lo_write(pglarge->pgconn, pglarge->lo_fd, StringValuePtr(buffer),
2318
- RSTRING_LEN(buffer))) == -1) {
2319
- rb_raise(rb_ePGError, "buffer truncated during write");
2320
- }
2321
-
2322
- return INT2FIX(n);
2323
- }
2324
-
2325
- /*
2326
- * call-seq:
2327
- * lrg.seek( offset, whence )
2328
- *
2329
- * Move the large object pointer to the _offset_.
2330
- * Valid values for _whence_ are +SEEK_SET+, +SEEK_CUR+, and +SEEK_END+.
2331
- * (Or 0, 1, or 2.)
2332
- */
2333
- static VALUE
2334
- pglarge_seek(obj, offset, whence)
2335
- VALUE obj, offset, whence;
2336
- {
2337
- PGlarge *pglarge = get_pglarge(obj);
2338
- int ret;
2339
-
2340
- if((ret = lo_lseek(pglarge->pgconn, pglarge->lo_fd, NUM2INT(offset), NUM2INT(whence))) == -1) {
2341
- rb_raise(rb_ePGError, "error while moving cursor");
2342
- }
2343
-
2344
- return INT2NUM(ret);
2345
- }
2346
-
2347
- /*
2348
- * call-seq:
2349
- * lrg.size()
2350
- *
2351
- * Returns the size of the large object.
2352
- */
2353
- static VALUE
2354
- pglarge_size(obj)
2355
- VALUE obj;
2356
- {
2357
- PGlarge *pglarge = get_pglarge(obj);
2358
- int start, end;
2359
-
2360
- if ((start = lo_tell(pglarge->pgconn,pglarge->lo_fd)) == -1) {
2361
- rb_raise(rb_ePGError, "error while getting position");
2362
- }
2363
-
2364
- if ((end = lo_lseek(pglarge->pgconn, pglarge->lo_fd, 0, SEEK_END)) == -1) {
2365
- rb_raise(rb_ePGError, "error while moving cursor");
2366
- }
2367
-
2368
- if ((start = lo_lseek(pglarge->pgconn, pglarge->lo_fd,start, SEEK_SET)) == -1) {
2369
- rb_raise(rb_ePGError, "error while moving back to posiion");
2370
- }
2371
-
2372
- return INT2NUM(end);
2373
- }
2374
-
2375
- /*
2376
- * call-seq:
2377
- * lrg.export( file )
2378
- *
2379
- * Saves the large object to a file.
2380
- */
2381
- static VALUE
2382
- pglarge_export(obj, filename)
2383
- VALUE obj, filename;
2384
- {
2385
- PGlarge *pglarge = get_pglarge(obj);
2386
-
2387
- Check_Type(filename, T_STRING);
2388
-
2389
- if (!lo_export(pglarge->pgconn, pglarge->lo_oid, StringValuePtr(filename))){
2390
- rb_raise(rb_ePGError, PQerrorMessage(pglarge->pgconn));
2391
- }
2392
-
2393
- return Qnil;
2394
- }
2395
-
2396
- /*
2397
- * call-seq:
2398
- * lrg.unlink()
2399
- *
2400
- * Deletes the large object.
2401
- */
2402
- static VALUE
2403
- pglarge_unlink(obj)
2404
- VALUE obj;
2405
- {
2406
- PGlarge *pglarge = get_pglarge(obj);
2407
-
2408
- if (!lo_unlink(pglarge->pgconn,pglarge->lo_oid)) {
2409
- rb_raise(rb_ePGError, PQerrorMessage(pglarge->pgconn));
2410
- }
2411
- DATA_PTR(obj) = 0;
2412
-
2413
- return Qnil;
2414
- }
2415
-
2416
- static VALUE
2417
- pgrow_init(self, keys)
2418
- VALUE self, keys;
2419
- {
2420
- VALUE args[1] = { LONG2NUM(RARRAY(keys)->len) };
2421
- rb_call_super(1, args);
2422
- rb_iv_set(self, "@keys", keys);
2423
- return self;
2424
- }
2425
-
2426
- /*
2427
- * call-seq:
2428
- * row.keys -> Array
2429
- *
2430
- * Column names.
2431
- */
2432
- static VALUE
2433
- pgrow_keys(self)
2434
- VALUE self;
2435
- {
2436
- return rb_iv_get(self, "@keys");
2437
- }
2438
-
2439
- /*
2440
- * call-seq:
2441
- * row.values -> row
2442
- */
2443
- static VALUE
2444
- pgrow_values(self)
2445
- VALUE self;
2446
- {
2447
- return self;
2448
- }
2449
-
2450
- /*
2451
- * call-seq:
2452
- * row[position] -> value
2453
- * row[name] -> value
2454
- *
2455
- * Access elements of this row by column position or name.
2456
- */
2457
- static VALUE
2458
- pgrow_aref(argc, argv, self)
2459
- int argc;
2460
- VALUE * argv;
2461
- VALUE self;
2462
- {
2463
- if (TYPE(argv[0]) == T_STRING) {
2464
- VALUE keys = pgrow_keys(self);
2465
- VALUE index = rb_funcall(keys, rb_intern("index"), 1, argv[0]);
2466
- if (index == Qnil) {
2467
- rb_raise(rb_ePGError, "%s: field not found", StringValuePtr(argv[0]));
2468
- }
2469
- else {
2470
- return rb_ary_entry(self, NUM2INT(index));
2471
- }
2472
- }
2473
- else {
2474
- return rb_call_super(argc, argv);
2475
- }
2476
- }
2477
-
2478
- /*
2479
- * call-seq:
2480
- * row.each_value { |value| block } -> row
2481
- *
2482
- * Iterate with values.
2483
- */
2484
- static VALUE
2485
- pgrow_each_value(self)
2486
- VALUE self;
2487
- {
2488
- rb_ary_each(self);
2489
- return self;
2490
- }
2491
-
2492
- /*
2493
- * call-seq:
2494
- * row.each_pair { |column_value_array| block } -> row
2495
- *
2496
- * Iterate with column,value pairs.
2497
- */
2498
- static VALUE
2499
- pgrow_each_pair(self)
2500
- VALUE self;
2501
- {
2502
- VALUE keys = pgrow_keys(self);
2503
- int i;
2504
- for (i = 0; i < RARRAY(keys)->len; ++i) {
2505
- rb_yield(rb_assoc_new(rb_ary_entry(keys, i), rb_ary_entry(self, i)));
2506
- }
2507
- return self;
2508
- }
2509
-
2510
- /*
2511
- * call-seq:
2512
- * row.each { |column, value| block } -> row
2513
- * row.each { |value| block } -> row
2514
- *
2515
- * Iterate with values or column,value pairs.
2516
- */
2517
- static VALUE
2518
- pgrow_each(self)
2519
- VALUE self;
2520
- {
2521
- int arity = NUM2INT(rb_funcall(rb_block_proc(), rb_intern("arity"), 0));
2522
- if (arity == 2) {
2523
- pgrow_each_pair(self);
2524
- }
2525
- else {
2526
- pgrow_each_value(self);
2527
- }
2528
- return self;
2529
- }
2530
-
2531
- /*
2532
- * call-seq:
2533
- * row.each_key { |column| block } -> row
2534
- *
2535
- * Iterate with column names.
2536
- */
2537
- static VALUE
2538
- pgrow_each_key(self)
2539
- VALUE self;
2540
- {
2541
- rb_ary_each(pgrow_keys(self));
2542
- return self;
2543
- }
2544
-
2545
- /*
2546
- * call-seq:
2547
- * row.to_hash -> Hash
2548
- *
2549
- * Returns a +Hash+ of the row's values indexed by column name.
2550
- * Equivalent to <tt>Hash [*row.keys.zip(row).flatten]</tt>
2551
- */
2552
- static VALUE
2553
- pgrow_to_hash(self)
2554
- VALUE self;
2555
- {
2556
- VALUE result = rb_hash_new();
2557
- VALUE keys = pgrow_keys(self);
2558
- int i;
2559
- for (i = 0; i < RARRAY(self)->len; ++i) {
2560
- rb_hash_aset(result, rb_ary_entry(keys, i), rb_ary_entry(self, i));
2561
- }
2562
- return result;
2563
- }
2564
-
2565
- /* Large Object support */
2566
-
2567
- /********************************************************************
2568
- *
2569
- * Document-class: PGconn
2570
- *
2571
- * The class to access PostgreSQL database.
2572
- *
2573
- * For example, to send query to the database on the localhost:
2574
- * require 'pg'
2575
- * conn = PGconn.open('dbname' => 'test1')
2576
- * res = conn.exec('select * from a')
2577
- *
2578
- * See the PGresult class for information on working with the results of a query.
2579
- */
2580
-
2581
-
2582
- /********************************************************************
2583
- *
2584
- * Document-class: PGresult
2585
- *
2586
- * The class to represent the query result tuples (rows).
2587
- * An instance of this class is created as the result of every query.
2588
- * You may need to invoke the #clear method of the instance when finished with
2589
- * the result for better memory performance.
2590
- */
2591
-
2592
-
2593
- /********************************************************************
2594
- *
2595
- * Document-class: PGrow
2596
- *
2597
- * Array subclass that provides hash-like behavior.
2598
- */
2599
-
2600
-
2601
- /********************************************************************
2602
- *
2603
- * Document-class: PGlarge
2604
- *
2605
- * The class to access large objects.
2606
- * An instance of this class is created as the result of
2607
- * PGconn#lo_import, PGconn#lo_create, and PGconn#lo_open.
2608
- */
2609
-
2610
- void
2611
- Init_postgres()
2612
- {
2613
- pg_gsub_bang_id = rb_intern("gsub!");
2614
- pg_escape_str = rb_str_new("\\\\\\1", 4);
2615
- rb_global_variable(&pg_escape_str);
2616
-
2617
- rb_require("bigdecimal");
2618
- rb_require("date");
2619
- rb_require("time");
2620
- rb_cBigDecimal = RUBY_CLASS("BigDecimal");
2621
- rb_cDate = RUBY_CLASS("Date");
2622
- rb_cDateTime = RUBY_CLASS("DateTime");
2623
-
2624
- rb_ePGError = rb_define_class("PGError", rb_eStandardError);
2625
- rb_define_alias(rb_ePGError, "error", "message");
2626
-
2627
- rb_cPGconn = rb_define_class("PGconn", rb_cObject);
2628
- #ifdef HAVE_RB_DEFINE_ALLOC_FUNC
2629
- rb_define_alloc_func(rb_cPGconn, pgconn_alloc);
2630
- #else
2631
- rb_define_singleton_method(rb_cPGconn, "new", pgconn_s_new, -1);
2632
- #endif
2633
- rb_define_singleton_alias(rb_cPGconn, "connect", "new");
2634
- rb_define_singleton_alias(rb_cPGconn, "open", "connect");
2635
- rb_define_singleton_alias(rb_cPGconn, "setdb", "connect");
2636
- rb_define_singleton_alias(rb_cPGconn, "setdblogin", "connect");
2637
- rb_define_singleton_method(rb_cPGconn, "escape", pgconn_s_escape, 1);
2638
- rb_define_singleton_method(rb_cPGconn, "quote", pgconn_s_quote, 1);
2639
- rb_define_singleton_alias(rb_cPGconn, "format", "quote");
2640
- rb_define_singleton_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
2641
- rb_define_singleton_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
2642
- rb_define_singleton_method(rb_cPGconn, "translate_results=", pgconn_s_translate_results_set, 1);
2643
- rb_define_singleton_method(rb_cPGconn, "quote_ident", pgconn_s_quote_ident, 1);
2644
-
2645
- rb_define_const(rb_cPGconn, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
2646
- rb_define_const(rb_cPGconn, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
2647
-
2648
- rb_define_method(rb_cPGconn, "initialize", pgconn_init, -1);
2649
- rb_define_method(rb_cPGconn, "db", pgconn_db, 0);
2650
- rb_define_method(rb_cPGconn, "host", pgconn_host, 0);
2651
- rb_define_method(rb_cPGconn, "options", pgconn_options, 0);
2652
- rb_define_method(rb_cPGconn, "port", pgconn_port, 0);
2653
- rb_define_method(rb_cPGconn, "tty", pgconn_tty, 0);
2654
- rb_define_method(rb_cPGconn, "status", pgconn_status, 0);
2655
- rb_define_method(rb_cPGconn, "error", pgconn_error, 0);
2656
- rb_define_method(rb_cPGconn, "close", pgconn_close, 0);
2657
- rb_define_alias(rb_cPGconn, "finish", "close");
2658
- rb_define_method(rb_cPGconn, "reset", pgconn_reset, 0);
2659
- rb_define_method(rb_cPGconn, "user", pgconn_user, 0);
2660
- rb_define_method(rb_cPGconn, "trace", pgconn_trace, 1);
2661
- rb_define_method(rb_cPGconn, "untrace", pgconn_untrace, 0);
2662
- rb_define_method(rb_cPGconn, "exec", pgconn_exec, -1);
2663
- rb_define_method(rb_cPGconn, "query", pgconn_query, -1);
2664
- rb_define_method(rb_cPGconn, "select_one", pgconn_select_one, -1);
2665
- rb_define_method(rb_cPGconn, "select_value", pgconn_select_value, -1);
2666
- rb_define_method(rb_cPGconn, "select_values", pgconn_select_values, -1);
2667
- rb_define_method(rb_cPGconn, "async_exec", pgconn_async_exec, 1);
2668
- rb_define_method(rb_cPGconn, "async_query", pgconn_async_query, 1);
2669
- rb_define_method(rb_cPGconn, "get_notify", pgconn_get_notify, 0);
2670
- rb_define_method(rb_cPGconn, "insert_table", pgconn_insert_table, 2);
2671
- rb_define_method(rb_cPGconn, "putline", pgconn_putline, 1);
2672
- rb_define_method(rb_cPGconn, "getline", pgconn_getline, 0);
2673
- rb_define_method(rb_cPGconn, "endcopy", pgconn_endcopy, 0);
2674
- rb_define_method(rb_cPGconn, "on_notice", pgconn_on_notice, 0);
2675
- rb_define_method(rb_cPGconn, "transaction_status", pgconn_transaction_status, 0);
2676
- rb_define_method(rb_cPGconn, "protocol_version", pgconn_protocol_version, 0);
2677
- rb_define_method(rb_cPGconn, "server_version", pgconn_server_version, 0);
2678
- rb_define_method(rb_cPGconn, "escape", pgconn_escape, 1);
2679
- rb_define_method(rb_cPGconn, "escape_bytea", pgconn_escape_bytea, 1);
2680
- rb_define_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
2681
- rb_define_method(rb_cPGconn, "quote", pgconn_quote, 1);
2682
- rb_define_method(rb_cPGconn, "quote_ident", pgconn_s_quote_ident, 1);
2683
- rb_define_alias(rb_cPGconn, "format", "quote");
2684
-
2685
- /* following line is for rdoc */
2686
- /* rb_define_method(rb_cPGconn, "lastval", pgconn_lastval, 0); */
2687
-
2688
- #ifdef HAVE_PQSETCLIENTENCODING
2689
- rb_define_method(rb_cPGconn, "client_encoding", pgconn_client_encoding, 0);
2690
- rb_define_method(rb_cPGconn, "set_client_encoding", pgconn_set_client_encoding, 1);
2691
- #endif
2692
-
2693
- /* Large Object support */
2694
- rb_define_method(rb_cPGconn, "lo_import", pgconn_loimport, 1);
2695
- rb_define_alias(rb_cPGconn, "loimport", "lo_import");
2696
- rb_define_method(rb_cPGconn, "lo_create", pgconn_locreate, -1);
2697
- rb_define_alias(rb_cPGconn, "locreate", "lo_create");
2698
- rb_define_method(rb_cPGconn, "lo_open", pgconn_loopen, -1);
2699
- rb_define_alias(rb_cPGconn, "loopen", "lo_open");
2700
- rb_define_method(rb_cPGconn, "lo_export", pgconn_loexport, 2);
2701
- rb_define_alias(rb_cPGconn, "loexport", "lo_export");
2702
- rb_define_method(rb_cPGconn, "lo_unlink", pgconn_lounlink, 1);
2703
- rb_define_alias(rb_cPGconn, "lounlink", "lo_unlink");
2704
-
2705
- rb_cPGlarge = rb_define_class("PGlarge", rb_cObject);
2706
- rb_define_method(rb_cPGlarge, "oid",pglarge_oid, 0);
2707
- rb_define_method(rb_cPGlarge, "open",pglarge_open, -1);
2708
- rb_define_method(rb_cPGlarge, "close",pglarge_close, 0);
2709
- rb_define_method(rb_cPGlarge, "read",pglarge_read, -1);
2710
- rb_define_method(rb_cPGlarge, "write",pglarge_write, 1);
2711
- rb_define_method(rb_cPGlarge, "seek",pglarge_seek, 2);
2712
- rb_define_method(rb_cPGlarge, "tell",pglarge_tell, 0);
2713
- rb_define_method(rb_cPGlarge, "size",pglarge_size, 0);
2714
- rb_define_method(rb_cPGlarge, "export",pglarge_export, 1);
2715
- rb_define_method(rb_cPGlarge, "unlink",pglarge_unlink, 0);
2716
-
2717
- rb_define_const(rb_cPGlarge, "INV_WRITE", INT2FIX(INV_WRITE));
2718
- rb_define_const(rb_cPGlarge, "INV_READ", INT2FIX(INV_READ));
2719
- rb_define_const(rb_cPGlarge, "SEEK_SET", INT2FIX(SEEK_SET));
2720
- rb_define_const(rb_cPGlarge, "SEEK_CUR", INT2FIX(SEEK_CUR));
2721
- rb_define_const(rb_cPGlarge, "SEEK_END", INT2FIX(SEEK_END));
2722
- /* Large Object support */
2723
-
2724
- rb_cPGresult = rb_define_class("PGresult", rb_cObject);
2725
- rb_include_module(rb_cPGresult, rb_mEnumerable);
2726
-
2727
- rb_define_const(rb_cPGresult, "EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
2728
- rb_define_const(rb_cPGresult, "COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
2729
- rb_define_const(rb_cPGresult, "TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
2730
- rb_define_const(rb_cPGresult, "COPY_OUT", INT2FIX(PGRES_COPY_OUT));
2731
- rb_define_const(rb_cPGresult, "COPY_IN", INT2FIX(PGRES_COPY_IN));
2732
- rb_define_const(rb_cPGresult, "BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
2733
- rb_define_const(rb_cPGresult, "NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
2734
- rb_define_const(rb_cPGresult, "FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
2735
-
2736
- rb_define_method(rb_cPGresult, "status", pgresult_status, 0);
2737
- rb_define_alias(rb_cPGresult, "result", "entries");
2738
- rb_define_alias(rb_cPGresult, "rows", "entries");
2739
- rb_define_method(rb_cPGresult, "each", pgresult_each, 0);
2740
- rb_define_method(rb_cPGresult, "[]", pgresult_aref, -1);
2741
- rb_define_method(rb_cPGresult, "fields", pgresult_fields, 0);
2742
- rb_define_method(rb_cPGresult, "ntuples", pgresult_num_tuples, 0);
2743
- rb_define_alias(rb_cPGresult, "num_tuples", "ntuples");
2744
- rb_define_method(rb_cPGresult, "nfields", pgresult_num_fields, 0);
2745
- rb_define_alias(rb_cPGresult, "num_fields", "nfields");
2746
- rb_define_method(rb_cPGresult, "fieldname", pgresult_fieldname, 1);
2747
- rb_define_method(rb_cPGresult, "fieldnum", pgresult_fieldnum, 1);
2748
- rb_define_method(rb_cPGresult, "ftype", pgresult_type, 1);
2749
- rb_define_alias(rb_cPGresult, "type", "ftype");
2750
- rb_define_method(rb_cPGresult, "size", pgresult_size, 1);
2751
- rb_define_method(rb_cPGresult, "getvalue", pgresult_getvalue, 2);
2752
- rb_define_method(rb_cPGresult, "getvalue_byname", pgresult_getvalue_byname, 2);
2753
- rb_define_method(rb_cPGresult, "getlength", pgresult_getlength, 2);
2754
- rb_define_method(rb_cPGresult, "getisnull", pgresult_getisnull, 2);
2755
- rb_define_method(rb_cPGresult, "cmd_tuples", pgresult_cmdtuples, 0);
2756
- rb_define_alias(rb_cPGresult, "cmdtuples", "cmd_tuples");
2757
- rb_define_method(rb_cPGresult, "cmdstatus", pgresult_cmdstatus, 0);
2758
- rb_define_method(rb_cPGresult, "oid", pgresult_oid, 0);
2759
- rb_define_method(rb_cPGresult, "clear", pgresult_clear, 0);
2760
- rb_define_alias(rb_cPGresult, "close", "clear");
2761
-
2762
- rb_cPGrow = rb_define_class("PGrow", rb_cArray);
2763
- rb_define_method(rb_cPGrow, "initialize", pgrow_init, 1);
2764
- rb_define_method(rb_cPGrow, "[]", pgrow_aref, -1);
2765
- rb_define_method(rb_cPGrow, "keys", pgrow_keys, 0);
2766
- rb_define_method(rb_cPGrow, "values", pgrow_values, 0);
2767
- rb_define_method(rb_cPGrow, "each", pgrow_each, 0);
2768
- rb_define_method(rb_cPGrow, "each_pair", pgrow_each_pair, 0);
2769
- rb_define_method(rb_cPGrow, "each_key", pgrow_each_key, 0);
2770
- rb_define_method(rb_cPGrow, "each_value", pgrow_each_value, 0);
2771
- rb_define_method(rb_cPGrow, "to_hash", pgrow_to_hash, 0);
2772
- }