postgres 0.7.1 → 0.7.9.2007.12.12

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