postgres 0.7.1 → 0.7.9.2007.12.12

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