kamk-pg 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/ext/extconf.rb +87 -0
  2. data/ext/pg.c +3877 -0
  3. metadata +57 -0
data/ext/pg.c ADDED
@@ -0,0 +1,3877 @@
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: 2009-03-28 17:49:43 +0100 (so, 28 bře 2009) $
13
+ ************************************************/
14
+
15
+ #include "pg.h"
16
+ #if defined(HAVE_RUBY_ENCODING_H) && HAVE_RUBY_ENCODING_H
17
+ # define M17N_SUPPORTED
18
+ #endif
19
+
20
+ #define rb_define_singleton_alias(klass,new,old) rb_define_alias(rb_singleton_class(klass),new,old)
21
+
22
+ static VALUE rb_cPGconn;
23
+ static VALUE rb_cPGresult;
24
+ static VALUE rb_ePGError;
25
+
26
+ /* The following functions are part of libpq, but not
27
+ * available from ruby-pg, because they are deprecated,
28
+ * obsolete, or generally not useful:
29
+ *
30
+ * * PQfreemem -- unnecessary: copied to ruby object, then
31
+ * freed. Ruby object's memory is freed when
32
+ * it is garbage collected.
33
+ * * PQbinaryTuples -- better to use PQfformat
34
+ * * PQprint -- not very useful
35
+ * * PQsetdb -- not very useful
36
+ * * PQoidStatus -- deprecated, use PQoidValue
37
+ * * PQrequestCancel -- deprecated, use PQcancel
38
+ * * PQfn -- use a prepared statement instead
39
+ * * PQgetline -- deprecated, use PQgetCopyData
40
+ * * PQgetlineAsync -- deprecated, use PQgetCopyData
41
+ * * PQputline -- deprecated, use PQputCopyData
42
+ * * PQputnbytes -- deprecated, use PQputCopyData
43
+ * * PQendcopy -- deprecated, use PQputCopyEnd
44
+ */
45
+
46
+ /***************************************************************************
47
+ * UTILITY FUNCTIONS
48
+ **************************************************************************/
49
+
50
+ static void free_pgconn(PGconn *);
51
+ static void pgresult_check(VALUE, VALUE);
52
+
53
+ static PGconn *get_pgconn(VALUE self);
54
+ static VALUE pgconn_finish(VALUE self);
55
+ static VALUE pgresult_clear(VALUE self);
56
+ static VALUE pgresult_aref(VALUE self, VALUE index);
57
+
58
+ #ifdef M17N_SUPPORTED
59
+ # define ASSOCIATE_INDEX(obj, index_holder) rb_enc_associate_index((obj), enc_get_index((index_holder)))
60
+ static rb_encoding * pgconn_get_client_encoding_as_rb_encoding(PGconn* conn);
61
+ static int enc_get_index(VALUE val);
62
+ #else
63
+ # define ASSOCIATE_INDEX(obj, index_holder) /* nothing */
64
+ #endif
65
+
66
+ static PQnoticeReceiver default_notice_receiver = NULL;
67
+ static PQnoticeProcessor default_notice_processor = NULL;
68
+
69
+
70
+ /*
71
+ * Used to quote the values passed in a Hash to PGconn.init
72
+ * when building the connection string.
73
+ */
74
+ static VALUE
75
+ pgconn_s_quote_connstr(VALUE string)
76
+ {
77
+ char *str,*ptr;
78
+ int i,j=0,len;
79
+ VALUE result;
80
+
81
+ Check_Type(string, T_STRING);
82
+
83
+ ptr = RSTRING_PTR(string);
84
+ len = RSTRING_LEN(string);
85
+ str = ALLOC_N(char, len * 2 + 2 + 1);
86
+ str[j++] = '\'';
87
+ for(i = 0; i < len; i++) {
88
+ if(ptr[i] == '\'' || ptr[i] == '\\')
89
+ str[j++] = '\\';
90
+ str[j++] = ptr[i];
91
+ }
92
+ str[j++] = '\'';
93
+ result = rb_str_new(str, j);
94
+ xfree(str);
95
+ return result;
96
+ }
97
+
98
+ static void
99
+ free_pgconn(PGconn *conn)
100
+ {
101
+ if(conn != NULL)
102
+ PQfinish(conn);
103
+ }
104
+
105
+ static void
106
+ free_pgresult(PGresult *result)
107
+ {
108
+ if(result != NULL)
109
+ PQclear(result);
110
+ }
111
+
112
+ static PGconn*
113
+ get_pgconn(VALUE self)
114
+ {
115
+ PGconn *conn;
116
+ Data_Get_Struct(self, PGconn, conn);
117
+ if (conn == NULL) rb_raise(rb_ePGError, "not connected");
118
+ return conn;
119
+ }
120
+
121
+ static PGresult*
122
+ get_pgresult(VALUE self)
123
+ {
124
+ PGresult *result;
125
+ Data_Get_Struct(self, PGresult, result);
126
+ if (result == NULL) rb_raise(rb_ePGError, "result has been cleared");
127
+ return result;
128
+ }
129
+
130
+ #ifdef M17N_SUPPORTED
131
+ static VALUE
132
+ new_pgresult(PGresult *result, PGconn *conn)
133
+ {
134
+ VALUE val = Data_Wrap_Struct(rb_cPGresult, NULL, free_pgresult, result);
135
+ rb_encoding *enc = pgconn_get_client_encoding_as_rb_encoding(conn);
136
+ rb_enc_set_index(val, rb_enc_to_index(enc));
137
+ return val;
138
+ }
139
+ #else
140
+ static VALUE
141
+ new_pgresult(PGresult *result)
142
+ {
143
+ return Data_Wrap_Struct(rb_cPGresult, NULL, free_pgresult, result);
144
+ }
145
+ # define new_pgresult(result, conn) new_pgresult((result))
146
+ #endif
147
+
148
+ /*
149
+ * Raises appropriate exception if PGresult is
150
+ * in a bad state.
151
+ */
152
+ static void
153
+ pgresult_check(VALUE rb_pgconn, VALUE rb_pgresult)
154
+ {
155
+ VALUE error;
156
+ PGconn *conn = get_pgconn(rb_pgconn);
157
+ PGresult *result;
158
+ Data_Get_Struct(rb_pgresult, PGresult, result);
159
+
160
+ if(result == NULL)
161
+ {
162
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
163
+ }
164
+ else
165
+ {
166
+ switch (PQresultStatus(result))
167
+ {
168
+ case PGRES_TUPLES_OK:
169
+ case PGRES_COPY_OUT:
170
+ case PGRES_COPY_IN:
171
+ case PGRES_EMPTY_QUERY:
172
+ case PGRES_COMMAND_OK:
173
+ return;
174
+ case PGRES_BAD_RESPONSE:
175
+ case PGRES_FATAL_ERROR:
176
+ case PGRES_NONFATAL_ERROR:
177
+ error = rb_exc_new2(rb_ePGError, PQresultErrorMessage(result));
178
+ break;
179
+ default:
180
+ error = rb_exc_new2(rb_ePGError,
181
+ "internal error : unknown result status.");
182
+ }
183
+ }
184
+
185
+ rb_iv_set(error, "@connection", rb_pgconn);
186
+ rb_iv_set(error, "@result", rb_pgresult);
187
+ rb_exc_raise(error);
188
+ return;
189
+ }
190
+
191
+ static VALUE
192
+ yield_pgresult(VALUE rb_pgresult)
193
+ {
194
+ int i;
195
+ PGresult *result = get_pgresult(rb_pgresult);
196
+ for(i = 0; i < PQntuples(result); i++) {
197
+ return rb_yield(pgresult_aref(rb_pgresult, INT2NUM(i)));
198
+ }
199
+ return Qnil;
200
+ }
201
+
202
+ static void
203
+ notice_receiver_proxy(void *arg, const PGresult *result)
204
+ {
205
+ VALUE proc;
206
+ VALUE self = (VALUE)arg;
207
+
208
+ if ((proc = rb_iv_get(self, "@notice_receiver")) != Qnil) {
209
+ rb_funcall(proc, rb_intern("call"), 1,
210
+ Data_Wrap_Struct(rb_cPGresult, NULL, NULL, (PGresult*)result));
211
+ }
212
+ return;
213
+ }
214
+
215
+ static void
216
+ notice_processor_proxy(void *arg, const char *message)
217
+ {
218
+ VALUE proc;
219
+ VALUE self = (VALUE)arg;
220
+
221
+ if ((proc = rb_iv_get(self, "@notice_processor")) != Qnil) {
222
+ rb_funcall(proc, rb_intern("call"), 1, rb_tainted_str_new2(message));
223
+ }
224
+ return;
225
+ }
226
+
227
+ /*
228
+ * Appends key='val' to conninfo_rstr
229
+ */
230
+ static void
231
+ build_key_value_string(VALUE conninfo_rstr, char *key, VALUE val)
232
+ {
233
+ if(val != Qnil) {
234
+ if(RSTRING_LEN(conninfo_rstr) > 0)
235
+ rb_str_cat2(conninfo_rstr, " ");
236
+ rb_str_cat2(conninfo_rstr, key);
237
+ rb_str_cat2(conninfo_rstr, "=");
238
+ rb_str_concat(conninfo_rstr,
239
+ pgconn_s_quote_connstr(rb_obj_as_string(val)));
240
+ }
241
+ return;
242
+ }
243
+
244
+ static VALUE
245
+ parse_connect_args(int argc, VALUE *argv, VALUE self)
246
+ {
247
+ VALUE args,arg;
248
+ VALUE conninfo_rstr = rb_str_new("",0);
249
+ char *host, *port, *opt, *tty, *dbname, *login, *pwd;
250
+ host=port=opt=tty=dbname=login=pwd=NULL;
251
+
252
+ rb_scan_args(argc, argv, "0*", &args);
253
+ if (RARRAY_LEN(args) == 1) {
254
+ arg = rb_ary_entry(args,0);
255
+ if(TYPE(arg) == T_HASH) {
256
+ build_key_value_string(conninfo_rstr, "host",
257
+ rb_hash_aref(arg, ID2SYM(rb_intern("host"))));
258
+ build_key_value_string(conninfo_rstr, "hostaddr",
259
+ rb_hash_aref(arg, ID2SYM(rb_intern("hostaddr"))));
260
+ build_key_value_string(conninfo_rstr, "port",
261
+ rb_hash_aref(arg, ID2SYM(rb_intern("port"))));
262
+ build_key_value_string(conninfo_rstr, "dbname",
263
+ rb_hash_aref(arg, ID2SYM(rb_intern("dbname"))));
264
+ build_key_value_string(conninfo_rstr, "user",
265
+ rb_hash_aref(arg, ID2SYM(rb_intern("user"))));
266
+ build_key_value_string(conninfo_rstr, "password",
267
+ rb_hash_aref(arg, ID2SYM(rb_intern("password"))));
268
+ build_key_value_string(conninfo_rstr, "connect_timeout",
269
+ rb_hash_aref(arg, ID2SYM(rb_intern("connect_timeout"))));
270
+ build_key_value_string(conninfo_rstr, "options",
271
+ rb_hash_aref(arg, ID2SYM(rb_intern("options"))));
272
+ build_key_value_string(conninfo_rstr, "tty",
273
+ rb_hash_aref(arg, ID2SYM(rb_intern("tty"))));
274
+ build_key_value_string(conninfo_rstr, "sslmode",
275
+ rb_hash_aref(arg, ID2SYM(rb_intern("sslmode"))));
276
+ build_key_value_string(conninfo_rstr, "krbsrvname",
277
+ rb_hash_aref(arg, ID2SYM(rb_intern("krbsrvname"))));
278
+ build_key_value_string(conninfo_rstr, "gsslib",
279
+ rb_hash_aref(arg, ID2SYM(rb_intern("gsslib"))));
280
+ build_key_value_string(conninfo_rstr, "service",
281
+ rb_hash_aref(arg, ID2SYM(rb_intern("service"))));
282
+ }
283
+ else if(TYPE(arg) == T_STRING) {
284
+ conninfo_rstr = arg;
285
+ }
286
+ else {
287
+ rb_raise(rb_eArgError,
288
+ "Expecting String or Hash as single argument");
289
+ }
290
+ }
291
+ else if (RARRAY_LEN(args) == 7) {
292
+ build_key_value_string(conninfo_rstr, "host", rb_ary_entry(args,0));
293
+ build_key_value_string(conninfo_rstr, "port", rb_ary_entry(args,1));
294
+ build_key_value_string(conninfo_rstr, "options", rb_ary_entry(args,2));
295
+ build_key_value_string(conninfo_rstr, "tty", rb_ary_entry(args,3));
296
+ build_key_value_string(conninfo_rstr, "dbname", rb_ary_entry(args,4));
297
+ build_key_value_string(conninfo_rstr, "user", rb_ary_entry(args,5));
298
+ build_key_value_string(conninfo_rstr, "password", rb_ary_entry(args,6));
299
+ }
300
+ else {
301
+ rb_raise(rb_eArgError,
302
+ "Expected connection info string, hash, or 7 separate arguments.");
303
+ }
304
+
305
+ return conninfo_rstr;
306
+ }
307
+
308
+ /********************************************************************
309
+ *
310
+ * Document-class: PGError
311
+ *
312
+ * This is the exception class raised when an error is returned from
313
+ * a libpq API call.
314
+ *
315
+ * The attributes +connection+ and +result+ are set to the connection
316
+ * object and result set object, respectively.
317
+ *
318
+ * If the connection object or result set object is not available from
319
+ * the context in which the error was encountered, it is +nil+.
320
+ */
321
+
322
+ /********************************************************************
323
+ *
324
+ * Document-class: PGconn
325
+ *
326
+ * The class to access PostgreSQL RDBMS, based on the libpq interface,
327
+ * provides convenient OO methods to interact with PostgreSQL.
328
+ *
329
+ * For example, to send query to the database on the localhost:
330
+ * require 'pg'
331
+ * conn = PGconn.open(:dbname => 'test')
332
+ * res = conn.exec('SELECT $1 AS a, $2 AS b, $3 AS c',[1, 2, nil])
333
+ * # Equivalent to:
334
+ * # res = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
335
+ *
336
+ * See the PGresult class for information on working with the results of a query.
337
+ *
338
+ */
339
+
340
+ static VALUE
341
+ pgconn_alloc(VALUE klass)
342
+ {
343
+ return Data_Wrap_Struct(klass, NULL, free_pgconn, NULL);
344
+ }
345
+
346
+ /**************************************************************************
347
+ * PGconn SINGLETON METHODS
348
+ **************************************************************************/
349
+
350
+ /*
351
+ * Document-method: new
352
+ *
353
+ * call-seq:
354
+ * PGconn.new(connection_hash) -> PGconn
355
+ * PGconn.new(connection_string) -> PGconn
356
+ * PGconn.new(host, port, options, tty, dbname, login, password) -> PGconn
357
+ *
358
+ * * +host+ - server hostname
359
+ * * +hostaddr+ - server address (avoids hostname lookup, overrides +host+)
360
+ * * +port+ - server port number
361
+ * * +dbname+ - connecting database name
362
+ * * +user+ - login user name
363
+ * * +password+ - login password
364
+ * * +connect_timeout+ - maximum time to wait for connection to succeed
365
+ * * +options+ - backend options
366
+ * * +tty+ - (ignored in newer versions of PostgreSQL)
367
+ * * +sslmode+ - (disable|allow|prefer|require)
368
+ * * +krbsrvname+ - kerberos service name
369
+ * * +gsslib+ - GSS library to use for GSSAPI authentication
370
+ * * +service+ - service name to use for additional parameters
371
+ *
372
+ * _connection_hash_ example: +PGconn.connect(:dbname=>'test', :port=>5432)
373
+ * _connection_string_ example: +PGconn.connect("dbname=test port=5432")
374
+ * _connection_hash_ example: +PGconn.connect(nil,5432,nil,nil,'test',nil,nil)
375
+ *
376
+ * On failure, it raises a PGError exception.
377
+ */
378
+ static VALUE
379
+ pgconn_init(int argc, VALUE *argv, VALUE self)
380
+ {
381
+ PGconn *conn = NULL;
382
+ VALUE conninfo;
383
+ VALUE error;
384
+
385
+ conninfo = parse_connect_args(argc, argv, self);
386
+ conn = PQconnectdb(StringValuePtr(conninfo));
387
+
388
+ if(conn == NULL)
389
+ rb_raise(rb_ePGError, "PQconnectStart() unable to allocate structure");
390
+
391
+ Check_Type(self, T_DATA);
392
+ DATA_PTR(self) = conn;
393
+
394
+ if (PQstatus(conn) == CONNECTION_BAD) {
395
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
396
+ rb_iv_set(error, "@connection", self);
397
+ rb_exc_raise(error);
398
+ }
399
+
400
+ if (rb_block_given_p()) {
401
+ return rb_ensure(rb_yield, self, pgconn_finish, self);
402
+ }
403
+ return self;
404
+ }
405
+
406
+ /*
407
+ * call-seq:
408
+ * PGconn.connect_start(connection_hash) -> PGconn
409
+ * PGconn.connect_start(connection_string) -> PGconn
410
+ * PGconn.connect_start(host, port, options, tty, dbname, login, password) -> PGconn
411
+ *
412
+ * This is an asynchronous version of PGconn.connect().
413
+ *
414
+ * Use PGconn#connect_poll to poll the status of the connection.
415
+ */
416
+ static VALUE
417
+ pgconn_s_connect_start(int argc, VALUE *argv, VALUE self)
418
+ {
419
+ PGconn *conn = NULL;
420
+ VALUE rb_conn;
421
+ VALUE conninfo;
422
+ VALUE error;
423
+
424
+ /*
425
+ * PGconn.connect_start must act as both alloc() and initialize()
426
+ * because it is not invoked by calling new().
427
+ */
428
+ rb_conn = pgconn_alloc(rb_cPGconn);
429
+
430
+ conninfo = parse_connect_args(argc, argv, self);
431
+ conn = PQconnectdb(StringValuePtr(conninfo));
432
+
433
+ if(conn == NULL)
434
+ rb_raise(rb_ePGError, "PQconnectStart() unable to allocate structure");
435
+ if (PQstatus(conn) == CONNECTION_BAD) {
436
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
437
+ rb_iv_set(error, "@connection", self);
438
+ rb_exc_raise(error);
439
+ }
440
+
441
+ Check_Type(rb_conn, T_DATA);
442
+ DATA_PTR(rb_conn) = conn;
443
+
444
+ if (rb_block_given_p()) {
445
+ return rb_ensure(rb_yield, self, pgconn_finish, self);
446
+ }
447
+ return rb_conn;
448
+ }
449
+
450
+ /*
451
+ * call-seq:
452
+ * PGconn.conndefaults() -> Array
453
+ *
454
+ * Returns an array of hashes. Each hash has the keys:
455
+ * * +:keyword+ - the name of the option
456
+ * * +:envvar+ - the environment variable to fall back to
457
+ * * +:compiled+ - the compiled in option as a secondary fallback
458
+ * * +:val+ - the option's current value, or +nil+ if not known
459
+ * * +:label+ - the label for the field
460
+ * * +:dispchar+ - "" for normal, "D" for debug, and "*" for password
461
+ * * +:dispsize+ - field size
462
+ */
463
+ static VALUE
464
+ pgconn_s_conndefaults(VALUE self)
465
+ {
466
+ PQconninfoOption *options = PQconndefaults();
467
+ VALUE ary = rb_ary_new();
468
+ VALUE hash;
469
+ int i = 0;
470
+
471
+ for(i = 0; options[i].keyword != NULL; i++) {
472
+ hash = rb_hash_new();
473
+ if(options[i].keyword)
474
+ rb_hash_aset(hash, ID2SYM(rb_intern("keyword")),
475
+ rb_str_new2(options[i].keyword));
476
+ if(options[i].envvar)
477
+ rb_hash_aset(hash, ID2SYM(rb_intern("envvar")),
478
+ rb_str_new2(options[i].envvar));
479
+ if(options[i].compiled)
480
+ rb_hash_aset(hash, ID2SYM(rb_intern("compiled")),
481
+ rb_str_new2(options[i].compiled));
482
+ if(options[i].val)
483
+ rb_hash_aset(hash, ID2SYM(rb_intern("val")),
484
+ rb_str_new2(options[i].val));
485
+ if(options[i].label)
486
+ rb_hash_aset(hash, ID2SYM(rb_intern("label")),
487
+ rb_str_new2(options[i].label));
488
+ if(options[i].dispchar)
489
+ rb_hash_aset(hash, ID2SYM(rb_intern("dispchar")),
490
+ rb_str_new2(options[i].dispchar));
491
+ rb_hash_aset(hash, ID2SYM(rb_intern("dispsize")),
492
+ INT2NUM(options[i].dispsize));
493
+ rb_ary_push(ary, hash);
494
+ }
495
+ PQconninfoFree(options);
496
+ return ary;
497
+ }
498
+
499
+
500
+ /*
501
+ * call-seq:
502
+ * PGconn.encrypt_password( password, username ) -> String
503
+ *
504
+ * This function is intended to be used by client applications that
505
+ * send commands like: +ALTER USER joe PASSWORD 'pwd'+.
506
+ * The arguments are the cleartext password, and the SQL name
507
+ * of the user it is for.
508
+ *
509
+ * Return value is the encrypted password.
510
+ */
511
+ static VALUE
512
+ pgconn_s_encrypt_password(VALUE self, VALUE password, VALUE username)
513
+ {
514
+ char *ret;
515
+ Check_Type(password, T_STRING);
516
+ Check_Type(username, T_STRING);
517
+ ret = PQencryptPassword(StringValuePtr(password),
518
+ StringValuePtr(username));
519
+ return rb_tainted_str_new2(ret);
520
+ }
521
+
522
+ /*
523
+ * call-seq:
524
+ * PGconn.isthreadsafe() -> Boolean
525
+ *
526
+ * Returns +true+ if libpq is thread safe, +false+ otherwise.
527
+ */
528
+ static VALUE
529
+ pgconn_s_isthreadsafe(VALUE self)
530
+ {
531
+ return PQisthreadsafe() ? Qtrue : Qfalse;
532
+ }
533
+
534
+ /**************************************************************************
535
+ * PGconn INSTANCE METHODS
536
+ **************************************************************************/
537
+
538
+ /*
539
+ * call-seq:
540
+ * conn.connect_poll() -> Fixnum
541
+ *
542
+ * Returns one of:
543
+ * * +PGRES_POLLING_READING+ - wait until the socket is ready to read
544
+ * * +PGRES_POLLING_WRITING+ - wait until the socket is ready to write
545
+ * * +PGRES_POLLING_FAILED+ - the asynchronous connection has failed
546
+ * * +PGRES_POLLING_OK+ - the asynchronous connection is ready
547
+ *
548
+ * Example:
549
+ * conn = PGconn.connect_start("dbname=mydatabase")
550
+ * socket = IO.for_fd(conn.socket)
551
+ * status = conn.connect_poll
552
+ * while(status != PGconn::PGRES_POLLING_OK) do
553
+ * # do some work while waiting for the connection to complete
554
+ * if(status == PGconn::PGRES_POLLING_READING)
555
+ * if(not select([socket], [], [], 10.0))
556
+ * raise "Asynchronous connection timed out!"
557
+ * end
558
+ * elsif(status == PGconn::PGRES_POLLING_WRITING)
559
+ * if(not select([], [socket], [], 10.0))
560
+ * raise "Asynchronous connection timed out!"
561
+ * end
562
+ * end
563
+ * status = conn.connect_poll
564
+ * end
565
+ * # now conn.status == CONNECTION_OK, and connection
566
+ * # is ready.
567
+ */
568
+ static VALUE
569
+ pgconn_connect_poll(VALUE self)
570
+ {
571
+ PostgresPollingStatusType status;
572
+ status = PQconnectPoll(get_pgconn(self));
573
+ return INT2FIX((int)status);
574
+ }
575
+
576
+ /*
577
+ * call-seq:
578
+ * conn.finish()
579
+ *
580
+ * Closes the backend connection.
581
+ */
582
+ static VALUE
583
+ pgconn_finish(VALUE self)
584
+ {
585
+ PQfinish(get_pgconn(self));
586
+ DATA_PTR(self) = NULL;
587
+ return Qnil;
588
+ }
589
+
590
+ /*
591
+ * call-seq:
592
+ * conn.reset()
593
+ *
594
+ * Resets the backend connection. This method closes the
595
+ * backend connection and tries to re-connect.
596
+ */
597
+ static VALUE
598
+ pgconn_reset(VALUE self)
599
+ {
600
+ PQreset(get_pgconn(self));
601
+ return self;
602
+ }
603
+
604
+ /*
605
+ * call-seq:
606
+ * conn.reset_start() -> nil
607
+ *
608
+ * Initiate a connection reset in a nonblocking manner.
609
+ * This will close the current connection and attempt to
610
+ * reconnect using the same connection parameters.
611
+ * Use PGconn#reset_poll to check the status of the
612
+ * connection reset.
613
+ */
614
+ static VALUE
615
+ pgconn_reset_start(VALUE self)
616
+ {
617
+ if(PQresetStart(get_pgconn(self)) == 0)
618
+ rb_raise(rb_ePGError, "reset has failed");
619
+ return Qnil;
620
+ }
621
+
622
+ /*
623
+ * call-seq:
624
+ * conn.reset_poll -> Fixnum
625
+ *
626
+ * Checks the status of a connection reset operation.
627
+ * See PGconn#connect_start and PGconn#connect_poll for
628
+ * usage information and return values.
629
+ */
630
+ static VALUE
631
+ pgconn_reset_poll(VALUE self)
632
+ {
633
+ PostgresPollingStatusType status;
634
+ status = PQresetPoll(get_pgconn(self));
635
+ return INT2FIX((int)status);
636
+ }
637
+
638
+ /*
639
+ * call-seq:
640
+ * conn.db()
641
+ *
642
+ * Returns the connected database name.
643
+ */
644
+ static VALUE
645
+ pgconn_db(VALUE self)
646
+ {
647
+ char *db = PQdb(get_pgconn(self));
648
+ if (!db) return Qnil;
649
+ return rb_tainted_str_new2(db);
650
+ }
651
+
652
+ /*
653
+ * call-seq:
654
+ * conn.user()
655
+ *
656
+ * Returns the authenticated user name.
657
+ */
658
+ static VALUE
659
+ pgconn_user(VALUE self)
660
+ {
661
+ char *user = PQuser(get_pgconn(self));
662
+ if (!user) return Qnil;
663
+ return rb_tainted_str_new2(user);
664
+ }
665
+
666
+ /*
667
+ * call-seq:
668
+ * conn.pass()
669
+ *
670
+ * Returns the authenticated user name.
671
+ */
672
+ static VALUE
673
+ pgconn_pass(VALUE self)
674
+ {
675
+ char *user = PQpass(get_pgconn(self));
676
+ if (!user) return Qnil;
677
+ return rb_tainted_str_new2(user);
678
+ }
679
+
680
+ /*
681
+ * call-seq:
682
+ * conn.host()
683
+ *
684
+ * Returns the connected server name.
685
+ */
686
+ static VALUE
687
+ pgconn_host(VALUE self)
688
+ {
689
+ char *host = PQhost(get_pgconn(self));
690
+ if (!host) return Qnil;
691
+ return rb_tainted_str_new2(host);
692
+ }
693
+
694
+ /*
695
+ * call-seq:
696
+ * conn.port()
697
+ *
698
+ * Returns the connected server port number.
699
+ */
700
+ static VALUE
701
+ pgconn_port(VALUE self)
702
+ {
703
+ char* port = PQport(get_pgconn(self));
704
+ return INT2NUM(atol(port));
705
+ }
706
+
707
+ /*
708
+ * call-seq:
709
+ * conn.tty()
710
+ *
711
+ * Returns the connected pgtty. (Obsolete)
712
+ */
713
+ static VALUE
714
+ pgconn_tty(VALUE self)
715
+ {
716
+ char *tty = PQtty(get_pgconn(self));
717
+ if (!tty) return Qnil;
718
+ return rb_tainted_str_new2(tty);
719
+ }
720
+
721
+ /*
722
+ * call-seq:
723
+ * conn.options()
724
+ *
725
+ * Returns backend option string.
726
+ */
727
+ static VALUE
728
+ pgconn_options(VALUE self)
729
+ {
730
+ char *options = PQoptions(get_pgconn(self));
731
+ if (!options) return Qnil;
732
+ return rb_tainted_str_new2(options);
733
+ }
734
+
735
+ /*
736
+ * call-seq:
737
+ * conn.status()
738
+ *
739
+ * Returns status of connection : CONNECTION_OK or CONNECTION_BAD
740
+ */
741
+ static VALUE
742
+ pgconn_status(VALUE self)
743
+ {
744
+ return INT2NUM(PQstatus(get_pgconn(self)));
745
+ }
746
+
747
+ /*
748
+ * call-seq:
749
+ * conn.transaction_status()
750
+ *
751
+ * returns one of the following statuses:
752
+ * PQTRANS_IDLE = 0 (connection idle)
753
+ * PQTRANS_ACTIVE = 1 (command in progress)
754
+ * PQTRANS_INTRANS = 2 (idle, within transaction block)
755
+ * PQTRANS_INERROR = 3 (idle, within failed transaction)
756
+ * PQTRANS_UNKNOWN = 4 (cannot determine status)
757
+ */
758
+ static VALUE
759
+ pgconn_transaction_status(VALUE self)
760
+ {
761
+ return INT2NUM(PQtransactionStatus(get_pgconn(self)));
762
+ }
763
+
764
+ /*
765
+ * call-seq:
766
+ * conn.parameter_status( param_name ) -> String
767
+ *
768
+ * Returns the setting of parameter _param_name_, where
769
+ * _param_name_ is one of
770
+ * * +server_version+
771
+ * * +server_encoding+
772
+ * * +client_encoding+
773
+ * * +is_superuser+
774
+ * * +session_authorization+
775
+ * * +DateStyle+
776
+ * * +TimeZone+
777
+ * * +integer_datetimes+
778
+ * * +standard_conforming_strings+
779
+ *
780
+ * Returns nil if the value of the parameter is not known.
781
+ */
782
+ static VALUE
783
+ pgconn_parameter_status(VALUE self, VALUE param_name)
784
+ {
785
+ const char *ret = PQparameterStatus(get_pgconn(self),
786
+ StringValuePtr(param_name));
787
+ if(ret == NULL)
788
+ return Qnil;
789
+ else
790
+ return rb_tainted_str_new2(ret);
791
+ }
792
+
793
+ /*
794
+ * call-seq:
795
+ * conn.protocol_version -> Integer
796
+ *
797
+ * The 3.0 protocol will normally be used when communicating with PostgreSQL 7.4
798
+ * or later servers; pre-7.4 servers support only protocol 2.0. (Protocol 1.0 is
799
+ * obsolete and not supported by libpq.)
800
+ */
801
+ static VALUE
802
+ pgconn_protocol_version(VALUE self)
803
+ {
804
+ return INT2NUM(PQprotocolVersion(get_pgconn(self)));
805
+ }
806
+
807
+ /*
808
+ * call-seq:
809
+ * conn.server_version -> Integer
810
+ *
811
+ * 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.
812
+ */
813
+ static VALUE
814
+ pgconn_server_version(VALUE self)
815
+ {
816
+ return INT2NUM(PQserverVersion(get_pgconn(self)));
817
+ }
818
+
819
+ /*
820
+ * call-seq:
821
+ * conn.error() -> String
822
+ *
823
+ * Returns the error message about connection.
824
+ */
825
+ static VALUE
826
+ pgconn_error_message(VALUE self)
827
+ {
828
+ char *error = PQerrorMessage(get_pgconn(self));
829
+ if (!error) return Qnil;
830
+ return rb_tainted_str_new2(error);
831
+ }
832
+
833
+ /*
834
+ * call-seq:
835
+ * conn.socket() -> Fixnum
836
+ *
837
+ * Returns the socket's file descriptor for this connection.
838
+ */
839
+ static VALUE
840
+ pgconn_socket(VALUE self)
841
+ {
842
+ int sd;
843
+ if( (sd = PQsocket(get_pgconn(self))) < 0)
844
+ rb_raise(rb_ePGError, "Can't get socket descriptor");
845
+ return INT2NUM(sd);
846
+ }
847
+
848
+
849
+ /*
850
+ * call-seq:
851
+ * conn.backend_pid() -> Fixnum
852
+ *
853
+ * Returns the process ID of the backend server
854
+ * process for this connection.
855
+ * Note that this is a PID on database server host.
856
+ */
857
+ static VALUE
858
+ pgconn_backend_pid(VALUE self)
859
+ {
860
+ return INT2NUM(PQbackendPID(get_pgconn(self)));
861
+ }
862
+
863
+ /*
864
+ * call-seq:
865
+ * conn.connection_needs_password() -> Boolean
866
+ *
867
+ * Returns +true+ if the authentication method required a
868
+ * password, but none was available. +false+ otherwise.
869
+ */
870
+ static VALUE
871
+ pgconn_connection_needs_password(VALUE self)
872
+ {
873
+ return PQconnectionNeedsPassword(get_pgconn(self)) ? Qtrue : Qfalse;
874
+ }
875
+
876
+ /*
877
+ * call-seq:
878
+ * conn.connection_used_password() -> Boolean
879
+ *
880
+ * Returns +true+ if the authentication method used
881
+ * a caller-supplied password, +false+ otherwise.
882
+ */
883
+ static VALUE
884
+ pgconn_connection_used_password(VALUE self)
885
+ {
886
+ return PQconnectionUsedPassword(get_pgconn(self)) ? Qtrue : Qfalse;
887
+ }
888
+
889
+
890
+ //TODO get_ssl
891
+
892
+
893
+ /*
894
+ * call-seq:
895
+ * conn.exec(sql [, params, result_format ] ) -> PGresult
896
+ *
897
+ * Sends SQL query request specified by _sql_ to PostgreSQL.
898
+ * Returns a PGresult instance on success.
899
+ * On failure, it raises a PGError exception.
900
+ *
901
+ * +params+ is an optional array of the bind parameters for the SQL query.
902
+ * Each element of the +params+ array may be either:
903
+ * a hash of the form:
904
+ * {:value => String (value of bind parameter)
905
+ * :type => Fixnum (oid of type of bind parameter)
906
+ * :format => Fixnum (0 for text, 1 for binary)
907
+ * }
908
+ * or, it may be a String. If it is a string, that is equivalent to the hash:
909
+ * { :value => <string value>, :type => 0, :format => 0 }
910
+ *
911
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
912
+ * inside the SQL query. The 0th element of the +params+ array is bound
913
+ * to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
914
+ *
915
+ * If the types are not specified, they will be inferred by PostgreSQL.
916
+ * Instead of specifying type oids, it's recommended to simply add
917
+ * explicit casts in the query to ensure that the right type is used.
918
+ *
919
+ * For example: "SELECT $1::int"
920
+ *
921
+ * The optional +result_format+ should be 0 for text results, 1
922
+ * for binary.
923
+ */
924
+ static VALUE
925
+ pgconn_exec(int argc, VALUE *argv, VALUE self)
926
+ {
927
+ PGconn *conn = get_pgconn(self);
928
+ PGresult *result = NULL;
929
+ VALUE rb_pgresult;
930
+ VALUE command, params, in_res_fmt;
931
+ VALUE param, param_type, param_value, param_format;
932
+ VALUE param_value_tmp;
933
+ VALUE sym_type, sym_value, sym_format;
934
+ VALUE gc_array;
935
+ int i=0;
936
+ int nParams;
937
+ Oid *paramTypes;
938
+ char ** paramValues;
939
+ int *paramLengths;
940
+ int *paramFormats;
941
+ int resultFormat;
942
+
943
+ rb_scan_args(argc, argv, "12", &command, &params, &in_res_fmt);
944
+
945
+ Check_Type(command, T_STRING);
946
+
947
+ /* If called with no parameters, use PQexec */
948
+ if(NIL_P(params)) {
949
+ result = PQexec(conn, StringValuePtr(command));
950
+ rb_pgresult = new_pgresult(result, conn);
951
+ pgresult_check(self, rb_pgresult);
952
+ if (rb_block_given_p()) {
953
+ return rb_ensure(yield_pgresult, rb_pgresult,
954
+ pgresult_clear, rb_pgresult);
955
+ }
956
+ return rb_pgresult;
957
+ }
958
+
959
+ /* If called with parameters, and optionally result_format,
960
+ * use PQexecParams
961
+ */
962
+ Check_Type(params, T_ARRAY);
963
+
964
+ if(NIL_P(in_res_fmt)) {
965
+ resultFormat = 0;
966
+ }
967
+ else {
968
+ resultFormat = NUM2INT(in_res_fmt);
969
+ }
970
+
971
+ gc_array = rb_ary_new();
972
+ rb_gc_register_address(&gc_array);
973
+ sym_type = ID2SYM(rb_intern("type"));
974
+ sym_value = ID2SYM(rb_intern("value"));
975
+ sym_format = ID2SYM(rb_intern("format"));
976
+ nParams = RARRAY_LEN(params);
977
+ paramTypes = ALLOC_N(Oid, nParams);
978
+ paramValues = ALLOC_N(char *, nParams);
979
+ paramLengths = ALLOC_N(int, nParams);
980
+ paramFormats = ALLOC_N(int, nParams);
981
+ for(i = 0; i < nParams; i++) {
982
+ param = rb_ary_entry(params, i);
983
+ if (TYPE(param) == T_HASH) {
984
+ param_type = rb_hash_aref(param, sym_type);
985
+ param_value_tmp = rb_hash_aref(param, sym_value);
986
+ if(param_value_tmp == Qnil)
987
+ param_value = param_value_tmp;
988
+ else
989
+ param_value = rb_obj_as_string(param_value_tmp);
990
+ param_format = rb_hash_aref(param, sym_format);
991
+ }
992
+ else {
993
+ param_type = Qnil;
994
+ if(param == Qnil)
995
+ param_value = param;
996
+ else
997
+ param_value = rb_obj_as_string(param);
998
+ param_format = Qnil;
999
+ }
1000
+
1001
+ if(param_type == Qnil)
1002
+ paramTypes[i] = 0;
1003
+ else
1004
+ paramTypes[i] = NUM2INT(param_type);
1005
+
1006
+ if(param_value == Qnil) {
1007
+ paramValues[i] = NULL;
1008
+ paramLengths[i] = 0;
1009
+ }
1010
+ else {
1011
+ Check_Type(param_value, T_STRING);
1012
+ /* make sure param_value doesn't get freed by the GC */
1013
+ rb_ary_push(gc_array, param_value);
1014
+ paramValues[i] = StringValuePtr(param_value);
1015
+ paramLengths[i] = RSTRING_LEN(param_value);
1016
+ }
1017
+
1018
+ if(param_format == Qnil)
1019
+ paramFormats[i] = 0;
1020
+ else
1021
+ paramFormats[i] = NUM2INT(param_format);
1022
+ }
1023
+
1024
+ result = PQexecParams(conn, StringValuePtr(command), nParams, paramTypes,
1025
+ (const char * const *)paramValues, paramLengths, paramFormats, resultFormat);
1026
+
1027
+ rb_gc_unregister_address(&gc_array);
1028
+
1029
+ xfree(paramTypes);
1030
+ xfree(paramValues);
1031
+ xfree(paramLengths);
1032
+ xfree(paramFormats);
1033
+
1034
+ rb_pgresult = new_pgresult(result, conn);
1035
+ pgresult_check(self, rb_pgresult);
1036
+ if (rb_block_given_p()) {
1037
+ return rb_ensure(yield_pgresult, rb_pgresult,
1038
+ pgresult_clear, rb_pgresult);
1039
+ }
1040
+ return rb_pgresult;
1041
+ }
1042
+
1043
+ /*
1044
+ * call-seq:
1045
+ * conn.prepare(stmt_name, sql [, param_types ] ) -> PGresult
1046
+ *
1047
+ * Prepares statement _sql_ with name _name_ to be executed later.
1048
+ * Returns a PGresult instance on success.
1049
+ * On failure, it raises a PGError exception.
1050
+ *
1051
+ * +param_types+ is an optional parameter to specify the Oids of the
1052
+ * types of the parameters.
1053
+ *
1054
+ * If the types are not specified, they will be inferred by PostgreSQL.
1055
+ * Instead of specifying type oids, it's recommended to simply add
1056
+ * explicit casts in the query to ensure that the right type is used.
1057
+ *
1058
+ * For example: "SELECT $1::int"
1059
+ *
1060
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1061
+ * inside the SQL query.
1062
+ */
1063
+ static VALUE
1064
+ pgconn_prepare(int argc, VALUE *argv, VALUE self)
1065
+ {
1066
+ PGconn *conn = get_pgconn(self);
1067
+ PGresult *result = NULL;
1068
+ VALUE rb_pgresult;
1069
+ VALUE name, command, in_paramtypes;
1070
+ VALUE param;
1071
+ int i = 0;
1072
+ int nParams = 0;
1073
+ Oid *paramTypes = NULL;
1074
+
1075
+ rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
1076
+ Check_Type(name, T_STRING);
1077
+ Check_Type(command, T_STRING);
1078
+
1079
+ if(! NIL_P(in_paramtypes)) {
1080
+ Check_Type(in_paramtypes, T_ARRAY);
1081
+ nParams = RARRAY_LEN(in_paramtypes);
1082
+ paramTypes = ALLOC_N(Oid, nParams);
1083
+ for(i = 0; i < nParams; i++) {
1084
+ param = rb_ary_entry(in_paramtypes, i);
1085
+ Check_Type(param, T_FIXNUM);
1086
+ if(param == Qnil)
1087
+ paramTypes[i] = 0;
1088
+ else
1089
+ paramTypes[i] = NUM2INT(param);
1090
+ }
1091
+ }
1092
+ result = PQprepare(conn, StringValuePtr(name), StringValuePtr(command),
1093
+ nParams, paramTypes);
1094
+
1095
+ xfree(paramTypes);
1096
+
1097
+ rb_pgresult = new_pgresult(result, conn);
1098
+ pgresult_check(self, rb_pgresult);
1099
+ return rb_pgresult;
1100
+ }
1101
+
1102
+ /*
1103
+ * call-seq:
1104
+ * conn.exec_prepared(statement_name [, params, result_format ] ) -> PGresult
1105
+ *
1106
+ * Execute prepared named statement specified by _statement_name_.
1107
+ * Returns a PGresult instance on success.
1108
+ * On failure, it raises a PGError exception.
1109
+ *
1110
+ * +params+ is an array of the optional bind parameters for the
1111
+ * SQL query. Each element of the +params+ array may be either:
1112
+ * a hash of the form:
1113
+ * {:value => String (value of bind parameter)
1114
+ * :format => Fixnum (0 for text, 1 for binary)
1115
+ * }
1116
+ * or, it may be a String. If it is a string, that is equivalent to the hash:
1117
+ * { :value => <string value>, :format => 0 }
1118
+ *
1119
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1120
+ * inside the SQL query. The 0th element of the +params+ array is bound
1121
+ * to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
1122
+ *
1123
+ * The optional +result_format+ should be 0 for text results, 1
1124
+ * for binary.
1125
+ */
1126
+ static VALUE
1127
+ pgconn_exec_prepared(int argc, VALUE *argv, VALUE self)
1128
+ {
1129
+ PGconn *conn = get_pgconn(self);
1130
+ PGresult *result = NULL;
1131
+ VALUE rb_pgresult;
1132
+ VALUE name, params, in_res_fmt;
1133
+ VALUE param, param_value, param_format;
1134
+ VALUE param_value_tmp;
1135
+ VALUE sym_value, sym_format;
1136
+ VALUE gc_array;
1137
+ int i = 0;
1138
+ int nParams;
1139
+ char ** paramValues;
1140
+ int *paramLengths;
1141
+ int *paramFormats;
1142
+ int resultFormat;
1143
+
1144
+
1145
+ rb_scan_args(argc, argv, "12", &name, &params, &in_res_fmt);
1146
+ Check_Type(name, T_STRING);
1147
+
1148
+ if(NIL_P(params)) {
1149
+ params = rb_ary_new2(0);
1150
+ resultFormat = 0;
1151
+ }
1152
+ else {
1153
+ Check_Type(params, T_ARRAY);
1154
+ }
1155
+
1156
+ if(NIL_P(in_res_fmt)) {
1157
+ resultFormat = 0;
1158
+ }
1159
+ else {
1160
+ resultFormat = NUM2INT(in_res_fmt);
1161
+ }
1162
+
1163
+ gc_array = rb_ary_new();
1164
+ rb_gc_register_address(&gc_array);
1165
+ sym_value = ID2SYM(rb_intern("value"));
1166
+ sym_format = ID2SYM(rb_intern("format"));
1167
+ nParams = RARRAY_LEN(params);
1168
+ paramValues = ALLOC_N(char *, nParams);
1169
+ paramLengths = ALLOC_N(int, nParams);
1170
+ paramFormats = ALLOC_N(int, nParams);
1171
+ for(i = 0; i < nParams; i++) {
1172
+ param = rb_ary_entry(params, i);
1173
+ if (TYPE(param) == T_HASH) {
1174
+ param_value_tmp = rb_hash_aref(param, sym_value);
1175
+ if(param_value_tmp == Qnil)
1176
+ param_value = param_value_tmp;
1177
+ else
1178
+ param_value = rb_obj_as_string(param_value_tmp);
1179
+ param_format = rb_hash_aref(param, sym_format);
1180
+ }
1181
+ else {
1182
+ if(param == Qnil)
1183
+ param_value = param;
1184
+ else
1185
+ param_value = rb_obj_as_string(param);
1186
+ param_format = INT2NUM(0);
1187
+ }
1188
+ if(param_value == Qnil) {
1189
+ paramValues[i] = NULL;
1190
+ paramLengths[i] = 0;
1191
+ }
1192
+ else {
1193
+ Check_Type(param_value, T_STRING);
1194
+ /* make sure param_value doesn't get freed by the GC */
1195
+ rb_ary_push(gc_array, param_value);
1196
+ paramValues[i] = StringValuePtr(param_value);
1197
+ paramLengths[i] = RSTRING_LEN(param_value);
1198
+ }
1199
+
1200
+ if(param_format == Qnil)
1201
+ paramFormats[i] = 0;
1202
+ else
1203
+ paramFormats[i] = NUM2INT(param_format);
1204
+ }
1205
+
1206
+ result = PQexecPrepared(conn, StringValuePtr(name), nParams,
1207
+ (const char * const *)paramValues, paramLengths, paramFormats,
1208
+ resultFormat);
1209
+
1210
+ rb_gc_unregister_address(&gc_array);
1211
+
1212
+ xfree(paramValues);
1213
+ xfree(paramLengths);
1214
+ xfree(paramFormats);
1215
+
1216
+ rb_pgresult = new_pgresult(result, conn);
1217
+ pgresult_check(self, rb_pgresult);
1218
+ if (rb_block_given_p()) {
1219
+ return rb_ensure(yield_pgresult, rb_pgresult,
1220
+ pgresult_clear, rb_pgresult);
1221
+ }
1222
+ return rb_pgresult;
1223
+ }
1224
+
1225
+ /*
1226
+ * call-seq:
1227
+ * conn.describe_prepared( statement_name ) -> PGresult
1228
+ *
1229
+ * Retrieve information about the prepared statement
1230
+ * _statement_name_.
1231
+ */
1232
+ static VALUE
1233
+ pgconn_describe_prepared(VALUE self, VALUE stmt_name)
1234
+ {
1235
+ PGresult *result;
1236
+ VALUE rb_pgresult;
1237
+ PGconn *conn = get_pgconn(self);
1238
+ char *stmt;
1239
+ if(stmt_name == Qnil) {
1240
+ stmt = NULL;
1241
+ }
1242
+ else {
1243
+ Check_Type(stmt_name, T_STRING);
1244
+ stmt = StringValuePtr(stmt_name);
1245
+ }
1246
+ result = PQdescribePrepared(conn, stmt);
1247
+ rb_pgresult = new_pgresult(result, conn);
1248
+ pgresult_check(self, rb_pgresult);
1249
+ return rb_pgresult;
1250
+ }
1251
+
1252
+
1253
+ /*
1254
+ * call-seq:
1255
+ * conn.describe_portal( portal_name ) -> PGresult
1256
+ *
1257
+ * Retrieve information about the portal _portal_name_.
1258
+ */
1259
+ static VALUE
1260
+ pgconn_describe_portal(self, stmt_name)
1261
+ VALUE self, stmt_name;
1262
+ {
1263
+ PGresult *result;
1264
+ VALUE rb_pgresult;
1265
+ PGconn *conn = get_pgconn(self);
1266
+ char *stmt;
1267
+ if(stmt_name == Qnil) {
1268
+ stmt = NULL;
1269
+ }
1270
+ else {
1271
+ Check_Type(stmt_name, T_STRING);
1272
+ stmt = StringValuePtr(stmt_name);
1273
+ }
1274
+ result = PQdescribePortal(conn, stmt);
1275
+ rb_pgresult = new_pgresult(result, conn);
1276
+ pgresult_check(self, rb_pgresult);
1277
+ return rb_pgresult;
1278
+ }
1279
+
1280
+
1281
+ /*
1282
+ * call-seq:
1283
+ * conn.make_empty_pgresult( status ) -> PGresult
1284
+ *
1285
+ * Constructs and empty PGresult with status _status_.
1286
+ * _status_ may be one of:
1287
+ * * +PGRES_EMPTY_QUERY+
1288
+ * * +PGRES_COMMAND_OK+
1289
+ * * +PGRES_TUPLES_OK+
1290
+ * * +PGRES_COPY_OUT+
1291
+ * * +PGRES_COPY_IN+
1292
+ * * +PGRES_BAD_RESPONSE+
1293
+ * * +PGRES_NONFATAL_ERROR+
1294
+ * * +PGRES_FATAL_ERROR+
1295
+ */
1296
+ static VALUE
1297
+ pgconn_make_empty_pgresult(VALUE self, VALUE status)
1298
+ {
1299
+ PGresult *result;
1300
+ VALUE rb_pgresult;
1301
+ PGconn *conn = get_pgconn(self);
1302
+ result = PQmakeEmptyPGresult(conn, NUM2INT(status));
1303
+ rb_pgresult = new_pgresult(result, conn);
1304
+ pgresult_check(self, rb_pgresult);
1305
+ return rb_pgresult;
1306
+ }
1307
+
1308
+
1309
+ /*
1310
+ * call-seq:
1311
+ * conn.escape_string( str ) -> String
1312
+ * PGconn.escape_string( str ) -> String # DEPRECATED
1313
+ *
1314
+ * Connection instance method for versions of 8.1 and higher of libpq
1315
+ * uses PQescapeStringConn, which is safer. Avoid calling as a class method,
1316
+ * the class method uses the deprecated PQescapeString() API function.
1317
+ *
1318
+ * Returns a SQL-safe version of the String _str_.
1319
+ * This is the preferred way to make strings safe for inclusion in
1320
+ * SQL queries.
1321
+ *
1322
+ * Consider using exec_params, which avoids the need for passing values
1323
+ * inside of SQL commands.
1324
+ */
1325
+ static VALUE
1326
+ pgconn_s_escape(VALUE self, VALUE string)
1327
+ {
1328
+ char *escaped;
1329
+ int size,error;
1330
+ VALUE result;
1331
+
1332
+ Check_Type(string, T_STRING);
1333
+
1334
+ escaped = ALLOC_N(char, RSTRING_LEN(string) * 2 + 1);
1335
+ if(CLASS_OF(self) == rb_cPGconn) {
1336
+ size = PQescapeStringConn(get_pgconn(self), escaped,
1337
+ RSTRING_PTR(string), RSTRING_LEN(string), &error);
1338
+ if(error) {
1339
+ rb_raise(rb_ePGError, PQerrorMessage(get_pgconn(self)));
1340
+ }
1341
+ } else {
1342
+ size = PQescapeString(escaped, RSTRING_PTR(string),
1343
+ RSTRING_LEN(string));
1344
+ }
1345
+ result = rb_str_new(escaped, size);
1346
+ xfree(escaped);
1347
+ OBJ_INFECT(result, string);
1348
+ return result;
1349
+ }
1350
+
1351
+ /*
1352
+ * call-seq:
1353
+ * conn.escape_bytea( string ) -> String
1354
+ * PGconn.escape_bytea( string ) -> String # DEPRECATED
1355
+ *
1356
+ * Connection instance method for versions of 8.1 and higher of libpq
1357
+ * uses PQescapeByteaConn, which is safer. Avoid calling as a class method,
1358
+ * the class method uses the deprecated PQescapeBytea() API function.
1359
+ *
1360
+ * Use the instance method version of this function, it is safer than the
1361
+ * class method.
1362
+ *
1363
+ * Escapes binary data for use within an SQL command with the type +bytea+.
1364
+ *
1365
+ * Certain byte values must be escaped (but all byte values may be escaped)
1366
+ * when used as part of a +bytea+ literal in an SQL statement. In general, to
1367
+ * escape a byte, it is converted into the three digit octal number equal to
1368
+ * the octet value, and preceded by two backslashes. The single quote (') and
1369
+ * backslash (\) characters have special alternative escape sequences.
1370
+ * #escape_bytea performs this operation, escaping only the minimally required
1371
+ * bytes.
1372
+ *
1373
+ * Consider using exec_params, which avoids the need for passing values inside of
1374
+ * SQL commands.
1375
+ */
1376
+ static VALUE
1377
+ pgconn_s_escape_bytea(VALUE self, VALUE str)
1378
+ {
1379
+ unsigned char *from, *to;
1380
+ size_t from_len, to_len;
1381
+ VALUE ret;
1382
+
1383
+ Check_Type(str, T_STRING);
1384
+ from = (unsigned char*)RSTRING_PTR(str);
1385
+ from_len = RSTRING_LEN(str);
1386
+
1387
+ if(CLASS_OF(self) == rb_cPGconn) {
1388
+ to = PQescapeByteaConn(get_pgconn(self), from, from_len, &to_len);
1389
+ } else {
1390
+ to = PQescapeBytea( from, from_len, &to_len);
1391
+ }
1392
+
1393
+ ret = rb_str_new((char*)to, to_len - 1);
1394
+ OBJ_INFECT(ret, str);
1395
+ PQfreemem(to);
1396
+ return ret;
1397
+ }
1398
+
1399
+
1400
+ /*
1401
+ * call-seq:
1402
+ * PGconn.unescape_bytea( string )
1403
+ *
1404
+ * Converts an escaped string representation of binary data into binary data --- the
1405
+ * reverse of #escape_bytea. This is needed when retrieving +bytea+ data in text format,
1406
+ * but not when retrieving it in binary format.
1407
+ *
1408
+ */
1409
+ static VALUE
1410
+ pgconn_s_unescape_bytea(VALUE self, VALUE str)
1411
+ {
1412
+ unsigned char *from, *to;
1413
+ size_t to_len;
1414
+ VALUE ret;
1415
+
1416
+ Check_Type(str, T_STRING);
1417
+ from = (unsigned char*)StringValuePtr(str);
1418
+
1419
+ to = PQunescapeBytea(from, &to_len);
1420
+
1421
+ ret = rb_str_new((char*)to, to_len);
1422
+ OBJ_INFECT(ret, str);
1423
+ PQfreemem(to);
1424
+ return ret;
1425
+ }
1426
+
1427
+ /*
1428
+ * call-seq:
1429
+ * conn.send_query(sql [, params, result_format ] ) -> nil
1430
+ *
1431
+ * Sends SQL query request specified by _sql_ to PostgreSQL for
1432
+ * asynchronous processing, and immediately returns.
1433
+ * On failure, it raises a PGError exception.
1434
+ *
1435
+ * +params+ is an optional array of the bind parameters for the SQL query.
1436
+ * Each element of the +params+ array may be either:
1437
+ * a hash of the form:
1438
+ * {:value => String (value of bind parameter)
1439
+ * :type => Fixnum (oid of type of bind parameter)
1440
+ * :format => Fixnum (0 for text, 1 for binary)
1441
+ * }
1442
+ * or, it may be a String. If it is a string, that is equivalent to the hash:
1443
+ * { :value => <string value>, :type => 0, :format => 0 }
1444
+ *
1445
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1446
+ * inside the SQL query. The 0th element of the +params+ array is bound
1447
+ * to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
1448
+ *
1449
+ * If the types are not specified, they will be inferred by PostgreSQL.
1450
+ * Instead of specifying type oids, it's recommended to simply add
1451
+ * explicit casts in the query to ensure that the right type is used.
1452
+ *
1453
+ * For example: "SELECT $1::int"
1454
+ *
1455
+ * The optional +result_format+ should be 0 for text results, 1
1456
+ * for binary.
1457
+ */
1458
+ static VALUE
1459
+ pgconn_send_query(int argc, VALUE *argv, VALUE self)
1460
+ {
1461
+ PGconn *conn = get_pgconn(self);
1462
+ int result;
1463
+ VALUE command, params, in_res_fmt;
1464
+ VALUE param, param_type, param_value, param_format;
1465
+ VALUE param_value_tmp;
1466
+ VALUE sym_type, sym_value, sym_format;
1467
+ VALUE gc_array;
1468
+ VALUE error;
1469
+ int i=0;
1470
+ int nParams;
1471
+ Oid *paramTypes;
1472
+ char ** paramValues;
1473
+ int *paramLengths;
1474
+ int *paramFormats;
1475
+ int resultFormat;
1476
+
1477
+ rb_scan_args(argc, argv, "12", &command, &params, &in_res_fmt);
1478
+ Check_Type(command, T_STRING);
1479
+
1480
+ /* If called with no parameters, use PQsendQuery */
1481
+ if(NIL_P(params)) {
1482
+ if(PQsendQuery(conn,StringValuePtr(command)) == 0) {
1483
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1484
+ rb_iv_set(error, "@connection", self);
1485
+ rb_exc_raise(error);
1486
+ }
1487
+ return Qnil;
1488
+ }
1489
+
1490
+ /* If called with parameters, and optionally result_format,
1491
+ * use PQsendQueryParams
1492
+ */
1493
+ Check_Type(params, T_ARRAY);
1494
+
1495
+ if(NIL_P(in_res_fmt)) {
1496
+ resultFormat = 0;
1497
+ }
1498
+ else {
1499
+ resultFormat = NUM2INT(in_res_fmt);
1500
+ }
1501
+
1502
+ gc_array = rb_ary_new();
1503
+ rb_gc_register_address(&gc_array);
1504
+ sym_type = ID2SYM(rb_intern("type"));
1505
+ sym_value = ID2SYM(rb_intern("value"));
1506
+ sym_format = ID2SYM(rb_intern("format"));
1507
+ nParams = RARRAY_LEN(params);
1508
+ paramTypes = ALLOC_N(Oid, nParams);
1509
+ paramValues = ALLOC_N(char *, nParams);
1510
+ paramLengths = ALLOC_N(int, nParams);
1511
+ paramFormats = ALLOC_N(int, nParams);
1512
+ for(i = 0; i < nParams; i++) {
1513
+ param = rb_ary_entry(params, i);
1514
+ if (TYPE(param) == T_HASH) {
1515
+ param_type = rb_hash_aref(param, sym_type);
1516
+ param_value_tmp = rb_hash_aref(param, sym_value);
1517
+ if(param_value_tmp == Qnil)
1518
+ param_value = param_value_tmp;
1519
+ else
1520
+ param_value = rb_obj_as_string(param_value_tmp);
1521
+ param_format = rb_hash_aref(param, sym_format);
1522
+ }
1523
+ else {
1524
+ param_type = INT2NUM(0);
1525
+ if(param == Qnil)
1526
+ param_value = param;
1527
+ else
1528
+ param_value = rb_obj_as_string(param);
1529
+ param_format = INT2NUM(0);
1530
+ }
1531
+
1532
+ if(param_type == Qnil)
1533
+ paramTypes[i] = 0;
1534
+ else
1535
+ paramTypes[i] = NUM2INT(param_type);
1536
+
1537
+ if(param_value == Qnil) {
1538
+ paramValues[i] = NULL;
1539
+ paramLengths[i] = 0;
1540
+ }
1541
+ else {
1542
+ Check_Type(param_value, T_STRING);
1543
+ /* make sure param_value doesn't get freed by the GC */
1544
+ rb_ary_push(gc_array, param_value);
1545
+ paramValues[i] = StringValuePtr(param_value);
1546
+ paramLengths[i] = RSTRING_LEN(param_value);
1547
+ }
1548
+
1549
+ if(param_format == Qnil)
1550
+ paramFormats[i] = 0;
1551
+ else
1552
+ paramFormats[i] = NUM2INT(param_format);
1553
+ }
1554
+
1555
+ result = PQsendQueryParams(conn, StringValuePtr(command), nParams, paramTypes,
1556
+ (const char * const *)paramValues, paramLengths, paramFormats, resultFormat);
1557
+
1558
+ rb_gc_unregister_address(&gc_array);
1559
+
1560
+ xfree(paramTypes);
1561
+ xfree(paramValues);
1562
+ xfree(paramLengths);
1563
+ xfree(paramFormats);
1564
+
1565
+ if(result == 0) {
1566
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1567
+ rb_iv_set(error, "@connection", self);
1568
+ rb_exc_raise(error);
1569
+ }
1570
+ return Qnil;
1571
+ }
1572
+
1573
+ /*
1574
+ * call-seq:
1575
+ * conn.send_prepare( stmt_name, sql [, param_types ] ) -> nil
1576
+ *
1577
+ * Prepares statement _sql_ with name _name_ to be executed later.
1578
+ * Sends prepare command asynchronously, and returns immediately.
1579
+ * On failure, it raises a PGError exception.
1580
+ *
1581
+ * +param_types+ is an optional parameter to specify the Oids of the
1582
+ * types of the parameters.
1583
+ *
1584
+ * If the types are not specified, they will be inferred by PostgreSQL.
1585
+ * Instead of specifying type oids, it's recommended to simply add
1586
+ * explicit casts in the query to ensure that the right type is used.
1587
+ *
1588
+ * For example: "SELECT $1::int"
1589
+ *
1590
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1591
+ * inside the SQL query.
1592
+ */
1593
+ static VALUE
1594
+ pgconn_send_prepare(int argc, VALUE *argv, VALUE self)
1595
+ {
1596
+ PGconn *conn = get_pgconn(self);
1597
+ int result;
1598
+ VALUE name, command, in_paramtypes;
1599
+ VALUE param;
1600
+ VALUE error;
1601
+ int i = 0;
1602
+ int nParams = 0;
1603
+ Oid *paramTypes = NULL;
1604
+
1605
+ rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
1606
+ Check_Type(name, T_STRING);
1607
+ Check_Type(command, T_STRING);
1608
+
1609
+ if(! NIL_P(in_paramtypes)) {
1610
+ Check_Type(in_paramtypes, T_ARRAY);
1611
+ nParams = RARRAY_LEN(in_paramtypes);
1612
+ paramTypes = ALLOC_N(Oid, nParams);
1613
+ for(i = 0; i < nParams; i++) {
1614
+ param = rb_ary_entry(in_paramtypes, i);
1615
+ Check_Type(param, T_FIXNUM);
1616
+ if(param == Qnil)
1617
+ paramTypes[i] = 0;
1618
+ else
1619
+ paramTypes[i] = NUM2INT(param);
1620
+ }
1621
+ }
1622
+ result = PQsendPrepare(conn, StringValuePtr(name), StringValuePtr(command),
1623
+ nParams, paramTypes);
1624
+
1625
+ xfree(paramTypes);
1626
+
1627
+ if(result == 0) {
1628
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1629
+ rb_iv_set(error, "@connection", self);
1630
+ rb_exc_raise(error);
1631
+ }
1632
+ return Qnil;
1633
+ }
1634
+
1635
+ /*
1636
+ * call-seq:
1637
+ * conn.send_query_prepared( statement_name [, params, result_format ] )
1638
+ * -> nil
1639
+ *
1640
+ * Execute prepared named statement specified by _statement_name_
1641
+ * asynchronously, and returns immediately.
1642
+ * On failure, it raises a PGError exception.
1643
+ *
1644
+ * +params+ is an array of the optional bind parameters for the
1645
+ * SQL query. Each element of the +params+ array may be either:
1646
+ * a hash of the form:
1647
+ * {:value => String (value of bind parameter)
1648
+ * :format => Fixnum (0 for text, 1 for binary)
1649
+ * }
1650
+ * or, it may be a String. If it is a string, that is equivalent to the hash:
1651
+ * { :value => <string value>, :format => 0 }
1652
+ *
1653
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1654
+ * inside the SQL query. The 0th element of the +params+ array is bound
1655
+ * to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
1656
+ *
1657
+ * The optional +result_format+ should be 0 for text results, 1
1658
+ * for binary.
1659
+ */
1660
+ static VALUE
1661
+ pgconn_send_query_prepared(int argc, VALUE *argv, VALUE self)
1662
+ {
1663
+ PGconn *conn = get_pgconn(self);
1664
+ int result;
1665
+ VALUE name, params, in_res_fmt;
1666
+ VALUE param, param_value, param_format;
1667
+ VALUE param_value_tmp;
1668
+ VALUE sym_value, sym_format;
1669
+ VALUE gc_array;
1670
+ VALUE error;
1671
+ int i = 0;
1672
+ int nParams;
1673
+ char ** paramValues;
1674
+ int *paramLengths;
1675
+ int *paramFormats;
1676
+ int resultFormat;
1677
+
1678
+ rb_scan_args(argc, argv, "12", &name, &params, &in_res_fmt);
1679
+ Check_Type(name, T_STRING);
1680
+
1681
+ if(NIL_P(params)) {
1682
+ params = rb_ary_new2(0);
1683
+ resultFormat = 0;
1684
+ }
1685
+ else {
1686
+ Check_Type(params, T_ARRAY);
1687
+ }
1688
+
1689
+ if(NIL_P(in_res_fmt)) {
1690
+ resultFormat = 0;
1691
+ }
1692
+ else {
1693
+ resultFormat = NUM2INT(in_res_fmt);
1694
+ }
1695
+
1696
+ gc_array = rb_ary_new();
1697
+ rb_gc_register_address(&gc_array);
1698
+ sym_value = ID2SYM(rb_intern("value"));
1699
+ sym_format = ID2SYM(rb_intern("format"));
1700
+ nParams = RARRAY_LEN(params);
1701
+ paramValues = ALLOC_N(char *, nParams);
1702
+ paramLengths = ALLOC_N(int, nParams);
1703
+ paramFormats = ALLOC_N(int, nParams);
1704
+ for(i = 0; i < nParams; i++) {
1705
+ param = rb_ary_entry(params, i);
1706
+ if (TYPE(param) == T_HASH) {
1707
+ param_value_tmp = rb_hash_aref(param, sym_value);
1708
+ if(param_value_tmp == Qnil)
1709
+ param_value = param_value_tmp;
1710
+ else
1711
+ param_value = rb_obj_as_string(param_value_tmp);
1712
+ param_format = rb_hash_aref(param, sym_format);
1713
+ }
1714
+ else {
1715
+ if(param == Qnil)
1716
+ param_value = param;
1717
+ else
1718
+ param_value = rb_obj_as_string(param);
1719
+ param_format = INT2NUM(0);
1720
+ }
1721
+
1722
+ if(param_value == Qnil) {
1723
+ paramValues[i] = NULL;
1724
+ paramLengths[i] = 0;
1725
+ }
1726
+ else {
1727
+ Check_Type(param_value, T_STRING);
1728
+ /* make sure param_value doesn't get freed by the GC */
1729
+ rb_ary_push(gc_array, param_value);
1730
+ paramValues[i] = StringValuePtr(param_value);
1731
+ paramLengths[i] = RSTRING_LEN(param_value);
1732
+ }
1733
+
1734
+ if(param_format == Qnil)
1735
+ paramFormats[i] = 0;
1736
+ else
1737
+ paramFormats[i] = NUM2INT(param_format);
1738
+ }
1739
+
1740
+ result = PQsendQueryPrepared(conn, StringValuePtr(name), nParams,
1741
+ (const char * const *)paramValues, paramLengths, paramFormats,
1742
+ resultFormat);
1743
+
1744
+ rb_gc_unregister_address(&gc_array);
1745
+
1746
+ xfree(paramValues);
1747
+ xfree(paramLengths);
1748
+ xfree(paramFormats);
1749
+
1750
+ if(result == 0) {
1751
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1752
+ rb_iv_set(error, "@connection", self);
1753
+ rb_exc_raise(error);
1754
+ }
1755
+ return Qnil;
1756
+ }
1757
+
1758
+ /*
1759
+ * call-seq:
1760
+ * conn.send_describe_prepared( statement_name ) -> nil
1761
+ *
1762
+ * Asynchronously send _command_ to the server. Does not block.
1763
+ * Use in combination with +conn.get_result+.
1764
+ */
1765
+ static VALUE
1766
+ pgconn_send_describe_prepared(VALUE self, VALUE stmt_name)
1767
+ {
1768
+ VALUE error;
1769
+ PGconn *conn = get_pgconn(self);
1770
+ /* returns 0 on failure */
1771
+ if(PQsendDescribePrepared(conn,StringValuePtr(stmt_name)) == 0) {
1772
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1773
+ rb_iv_set(error, "@connection", self);
1774
+ rb_exc_raise(error);
1775
+ }
1776
+ return Qnil;
1777
+ }
1778
+
1779
+
1780
+ /*
1781
+ * call-seq:
1782
+ * conn.send_describe_portal( portal_name ) -> nil
1783
+ *
1784
+ * Asynchronously send _command_ to the server. Does not block.
1785
+ * Use in combination with +conn.get_result+.
1786
+ */
1787
+ static VALUE
1788
+ pgconn_send_describe_portal(VALUE self, VALUE portal)
1789
+ {
1790
+ VALUE error;
1791
+ PGconn *conn = get_pgconn(self);
1792
+ /* returns 0 on failure */
1793
+ if(PQsendDescribePortal(conn,StringValuePtr(portal)) == 0) {
1794
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1795
+ rb_iv_set(error, "@connection", self);
1796
+ rb_exc_raise(error);
1797
+ }
1798
+ return Qnil;
1799
+ }
1800
+
1801
+
1802
+ /*
1803
+ * call-seq:
1804
+ * conn.get_result() -> PGresult
1805
+ *
1806
+ * Blocks waiting for the next result from a call to
1807
+ * +PGconn#send_query+ (or another asynchronous command), and returns
1808
+ * it. Returns +nil+ if no more results are available.
1809
+ *
1810
+ * Note: call this function repeatedly until it returns +nil+, or else
1811
+ * you will not be able to issue further commands.
1812
+ */
1813
+ static VALUE
1814
+ pgconn_get_result(VALUE self)
1815
+ {
1816
+ PGconn *conn = get_pgconn(self);
1817
+ PGresult *result;
1818
+ VALUE rb_pgresult;
1819
+
1820
+ result = PQgetResult(conn);
1821
+ if(result == NULL)
1822
+ return Qnil;
1823
+ rb_pgresult = new_pgresult(result, conn);
1824
+ if (rb_block_given_p()) {
1825
+ return rb_ensure(yield_pgresult, rb_pgresult,
1826
+ pgresult_clear, rb_pgresult);
1827
+ }
1828
+ return rb_pgresult;
1829
+ }
1830
+
1831
+ /*
1832
+ * call-seq:
1833
+ * conn.consume_input()
1834
+ *
1835
+ * If input is available from the server, consume it.
1836
+ * After calling +consume_input+, you can check +is_busy+
1837
+ * or *notifies* to see if the state has changed.
1838
+ */
1839
+ static VALUE
1840
+ pgconn_consume_input(self)
1841
+ VALUE self;
1842
+ {
1843
+ VALUE error;
1844
+ PGconn *conn = get_pgconn(self);
1845
+ /* returns 0 on error */
1846
+ if(PQconsumeInput(conn) == 0) {
1847
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1848
+ rb_iv_set(error, "@connection", self);
1849
+ rb_exc_raise(error);
1850
+ }
1851
+ return Qnil;
1852
+ }
1853
+
1854
+ /*
1855
+ * call-seq:
1856
+ * conn.is_busy() -> Boolean
1857
+ *
1858
+ * Returns +true+ if a command is busy, that is, if
1859
+ * PQgetResult would block. Otherwise returns +false+.
1860
+ */
1861
+ static VALUE
1862
+ pgconn_is_busy(self)
1863
+ VALUE self;
1864
+ {
1865
+ return PQisBusy(get_pgconn(self)) ? Qtrue : Qfalse;
1866
+ }
1867
+
1868
+ /*
1869
+ * call-seq:
1870
+ * conn.setnonblocking(Boolean) -> nil
1871
+ *
1872
+ * Sets the nonblocking status of the connection.
1873
+ * In the blocking state, calls to PGconn#send_query
1874
+ * will block until the message is sent to the server,
1875
+ * but will not wait for the query results.
1876
+ * In the nonblocking state, calls to PGconn#send_query
1877
+ * will return an error if the socket is not ready for
1878
+ * writing.
1879
+ * Note: This function does not affect PGconn#exec, because
1880
+ * that function doesn't return until the server has
1881
+ * processed the query and returned the results.
1882
+ * Returns +nil+.
1883
+ */
1884
+ static VALUE
1885
+ pgconn_setnonblocking(self, state)
1886
+ VALUE self, state;
1887
+ {
1888
+ int arg;
1889
+ VALUE error;
1890
+ PGconn *conn = get_pgconn(self);
1891
+ if(state == Qtrue)
1892
+ arg = 1;
1893
+ else if (state == Qfalse)
1894
+ arg = 0;
1895
+ else
1896
+ rb_raise(rb_eArgError, "Boolean value expected");
1897
+
1898
+ if(PQsetnonblocking(conn, arg) == -1) {
1899
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1900
+ rb_iv_set(error, "@connection", self);
1901
+ rb_exc_raise(error);
1902
+ }
1903
+ return Qnil;
1904
+ }
1905
+
1906
+
1907
+ /*
1908
+ * call-seq:
1909
+ * conn.isnonblocking() -> Boolean
1910
+ *
1911
+ * Returns +true+ if a command is busy, that is, if
1912
+ * PQgetResult would block. Otherwise returns +false+.
1913
+ */
1914
+ static VALUE
1915
+ pgconn_isnonblocking(self)
1916
+ VALUE self;
1917
+ {
1918
+ return PQisnonblocking(get_pgconn(self)) ? Qtrue : Qfalse;
1919
+ }
1920
+
1921
+ /*
1922
+ * call-seq:
1923
+ * conn.flush() -> Boolean
1924
+ *
1925
+ * Attempts to flush any queued output data to the server.
1926
+ * Returns +true+ if data is successfully flushed, +false+
1927
+ * if not (can only return +false+ if connection is
1928
+ * nonblocking.
1929
+ * Raises PGError exception if some other failure occurred.
1930
+ */
1931
+ static VALUE
1932
+ pgconn_flush(self)
1933
+ VALUE self;
1934
+ {
1935
+ PGconn *conn = get_pgconn(self);
1936
+ int ret;
1937
+ VALUE error;
1938
+ ret = PQflush(conn);
1939
+ if(ret == -1) {
1940
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1941
+ rb_iv_set(error, "@connection", self);
1942
+ rb_exc_raise(error);
1943
+ }
1944
+ return (ret) ? Qfalse : Qtrue;
1945
+ }
1946
+
1947
+ /*
1948
+ * call-seq:
1949
+ * conn.cancel() -> String
1950
+ *
1951
+ * Requests cancellation of the command currently being
1952
+ * processed.
1953
+ *
1954
+ * Returns +nil+ on success, or a string containing the
1955
+ * error message if a failure occurs.
1956
+ */
1957
+ static VALUE
1958
+ pgconn_cancel(VALUE self)
1959
+ {
1960
+ char errbuf[256];
1961
+ PGcancel *cancel;
1962
+ VALUE retval;
1963
+ int ret;
1964
+
1965
+ cancel = PQgetCancel(get_pgconn(self));
1966
+ if(cancel == NULL)
1967
+ rb_raise(rb_ePGError,"Invalid connection!");
1968
+
1969
+ ret = PQcancel(cancel, errbuf, 256);
1970
+ if(ret == 1)
1971
+ retval = Qnil;
1972
+ else
1973
+ retval = rb_str_new2(errbuf);
1974
+
1975
+ PQfreeCancel(cancel);
1976
+ return retval;
1977
+ }
1978
+
1979
+ /*
1980
+ * call-seq:
1981
+ * conn.notifies()
1982
+ *
1983
+ * Returns an array of the unprocessed notifiers.
1984
+ * If there is no unprocessed notifier, it returns +nil+.
1985
+ */
1986
+ static VALUE
1987
+ pgconn_notifies(VALUE self)
1988
+ {
1989
+ PGconn* conn = get_pgconn(self);
1990
+ PGnotify *notify;
1991
+ VALUE hash;
1992
+ VALUE sym_relname, sym_be_pid, sym_extra;
1993
+ VALUE relname, be_pid, extra;
1994
+
1995
+ sym_relname = ID2SYM(rb_intern("relname"));
1996
+ sym_be_pid = ID2SYM(rb_intern("be_pid"));
1997
+ sym_extra = ID2SYM(rb_intern("extra"));
1998
+
1999
+ notify = PQnotifies(conn);
2000
+ if (notify == NULL) {
2001
+ return Qnil;
2002
+ }
2003
+
2004
+ hash = rb_hash_new();
2005
+ relname = rb_tainted_str_new2(notify->relname);
2006
+ be_pid = INT2NUM(notify->be_pid);
2007
+ extra = rb_tainted_str_new2(PGNOTIFY_EXTRA(notify));
2008
+
2009
+ rb_hash_aset(hash, sym_relname, relname);
2010
+ rb_hash_aset(hash, sym_be_pid, be_pid);
2011
+ rb_hash_aset(hash, sym_extra, extra);
2012
+
2013
+ PQfreemem(notify);
2014
+ return hash;
2015
+ }
2016
+
2017
+
2018
+ /*
2019
+ * call-seq:
2020
+ * conn.put_copy_data( buffer ) -> Boolean
2021
+ *
2022
+ * Transmits _buffer_ as copy data to the server.
2023
+ * Returns true if the data was sent, false if it was
2024
+ * not sent (false is only possible if the connection
2025
+ * is in nonblocking mode, and this command would block).
2026
+ *
2027
+ * Raises an exception if an error occurs.
2028
+ */
2029
+ static VALUE
2030
+ pgconn_put_copy_data(self, buffer)
2031
+ VALUE self, buffer;
2032
+ {
2033
+ int ret;
2034
+ VALUE error;
2035
+ PGconn *conn = get_pgconn(self);
2036
+ Check_Type(buffer, T_STRING);
2037
+
2038
+ ret = PQputCopyData(conn, RSTRING_PTR(buffer),
2039
+ RSTRING_LEN(buffer));
2040
+ if(ret == -1) {
2041
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
2042
+ rb_iv_set(error, "@connection", self);
2043
+ rb_exc_raise(error);
2044
+ }
2045
+ return (ret) ? Qtrue : Qfalse;
2046
+ }
2047
+
2048
+ /*
2049
+ * call-seq:
2050
+ * conn.put_copy_end( [ error_message ] ) -> Boolean
2051
+ *
2052
+ * Sends end-of-data indication to the server.
2053
+ *
2054
+ * _error_message_ is an optional parameter, and if set,
2055
+ * forces the COPY command to fail with the string
2056
+ * _error_message_.
2057
+ *
2058
+ * Returns true if the end-of-data was sent, false if it was
2059
+ * not sent (false is only possible if the connection
2060
+ * is in nonblocking mode, and this command would block).
2061
+ */
2062
+ static VALUE
2063
+ pgconn_put_copy_end(int argc, VALUE *argv, VALUE self)
2064
+ {
2065
+ VALUE str;
2066
+ VALUE error;
2067
+ int ret;
2068
+ char *error_message = NULL;
2069
+ PGconn *conn = get_pgconn(self);
2070
+
2071
+ if (rb_scan_args(argc, argv, "01", &str) == 0)
2072
+ error_message = NULL;
2073
+ else
2074
+ error_message = StringValuePtr(str);
2075
+
2076
+ ret = PQputCopyEnd(conn, error_message);
2077
+ if(ret == -1) {
2078
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
2079
+ rb_iv_set(error, "@connection", self);
2080
+ rb_exc_raise(error);
2081
+ }
2082
+ return (ret) ? Qtrue : Qfalse;
2083
+ }
2084
+
2085
+ /*
2086
+ * call-seq:
2087
+ * conn.get_copy_data( [ async = false ] ) -> String
2088
+ *
2089
+ * Return a string containing one row of data, +nil+
2090
+ * if the copy is done, or +false+ if the call would
2091
+ * block (only possible if _async_ is true).
2092
+ *
2093
+ */
2094
+ static VALUE
2095
+ pgconn_get_copy_data(int argc, VALUE *argv, VALUE self )
2096
+ {
2097
+ VALUE async_in;
2098
+ VALUE error;
2099
+ VALUE result_str;
2100
+ int ret;
2101
+ int async;
2102
+ char *buffer;
2103
+ PGconn *conn = get_pgconn(self);
2104
+
2105
+ if (rb_scan_args(argc, argv, "01", &async_in) == 0)
2106
+ async = 0;
2107
+ else
2108
+ async = (async_in == Qfalse || async_in == Qnil) ? 0 : 1;
2109
+
2110
+ ret = PQgetCopyData(conn, &buffer, async);
2111
+ if(ret == -2) { // error
2112
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
2113
+ rb_iv_set(error, "@connection", self);
2114
+ rb_exc_raise(error);
2115
+ }
2116
+ if(ret == -1) { // No data left
2117
+ return Qnil;
2118
+ }
2119
+ if(ret == 0) { // would block
2120
+ return Qfalse;
2121
+ }
2122
+ result_str = rb_tainted_str_new(buffer, ret);
2123
+ PQfreemem(buffer);
2124
+ return result_str;
2125
+ }
2126
+
2127
+ /*
2128
+ * call-seq:
2129
+ * conn.set_error_verbosity( verbosity ) -> Fixnum
2130
+ *
2131
+ * Sets connection's verbosity to _verbosity_ and returns
2132
+ * the previous setting. Available settings are:
2133
+ * * PQERRORS_TERSE
2134
+ * * PQERRORS_DEFAULT
2135
+ * * PQERRORS_VERBOSE
2136
+ */
2137
+ static VALUE
2138
+ pgconn_set_error_verbosity(VALUE self, VALUE in_verbosity)
2139
+ {
2140
+ PGconn *conn = get_pgconn(self);
2141
+ PGVerbosity verbosity = NUM2INT(in_verbosity);
2142
+ return INT2FIX(PQsetErrorVerbosity(conn, verbosity));
2143
+ }
2144
+
2145
+ /*
2146
+ * call-seq:
2147
+ * conn.trace( stream ) -> nil
2148
+ *
2149
+ * Enables tracing message passing between backend. The
2150
+ * trace message will be written to the stream _stream_,
2151
+ * which must implement a method +fileno+ that returns
2152
+ * a writable file descriptor.
2153
+ */
2154
+ static VALUE
2155
+ pgconn_trace(VALUE self, VALUE stream)
2156
+ {
2157
+ VALUE fileno;
2158
+ FILE *new_fp;
2159
+ int old_fd, new_fd;
2160
+ VALUE new_file;
2161
+
2162
+ if(rb_respond_to(stream,rb_intern("fileno")) == Qfalse)
2163
+ rb_raise(rb_eArgError, "stream does not respond to method: fileno");
2164
+
2165
+ fileno = rb_funcall(stream, rb_intern("fileno"), 0);
2166
+ if(fileno == Qnil)
2167
+ rb_raise(rb_eArgError, "can't get file descriptor from stream");
2168
+
2169
+ /* Duplicate the file descriptor and re-open
2170
+ * it. Then, make it into a ruby File object
2171
+ * and assign it to an instance variable.
2172
+ * This prevents a problem when the File
2173
+ * object passed to this function is closed
2174
+ * before the connection object is. */
2175
+ old_fd = NUM2INT(fileno);
2176
+ new_fd = dup(old_fd);
2177
+ new_fp = fdopen(new_fd, "w");
2178
+
2179
+ if(new_fp == NULL)
2180
+ rb_raise(rb_eArgError, "stream is not writable");
2181
+
2182
+ new_file = rb_funcall(rb_cIO, rb_intern("new"), 1, INT2NUM(new_fd));
2183
+ rb_iv_set(self, "@trace_stream", new_file);
2184
+
2185
+ PQtrace(get_pgconn(self), new_fp);
2186
+ return Qnil;
2187
+ }
2188
+
2189
+ /*
2190
+ * call-seq:
2191
+ * conn.untrace() -> nil
2192
+ *
2193
+ * Disables the message tracing.
2194
+ */
2195
+ static VALUE
2196
+ pgconn_untrace(VALUE self)
2197
+ {
2198
+ VALUE trace_stream;
2199
+ PQuntrace(get_pgconn(self));
2200
+ trace_stream = rb_iv_get(self, "@trace_stream");
2201
+ rb_funcall(trace_stream, rb_intern("close"), 0);
2202
+ rb_iv_set(self, "@trace_stream", Qnil);
2203
+ return Qnil;
2204
+ }
2205
+
2206
+ /*
2207
+ * call-seq:
2208
+ * conn.set_notice_receiver {|result| ... } -> Proc
2209
+ *
2210
+ * Notice and warning messages generated by the server are not returned
2211
+ * by the query execution functions, since they do not imply failure of
2212
+ * the query. Instead they are passed to a notice handling function, and
2213
+ * execution continues normally after the handler returns. The default
2214
+ * notice handling function prints the message on <tt>stderr</tt>, but the
2215
+ * application can override this behavior by supplying its own handling
2216
+ * function.
2217
+ *
2218
+ * This function takes a new block to act as the handler, which should
2219
+ * accept a single parameter that will be a PGresult object, and returns
2220
+ * the Proc object previously set, or +nil+ if it was previously the default.
2221
+ *
2222
+ * If you pass no arguments, it will reset the handler to the default.
2223
+ */
2224
+ static VALUE
2225
+ pgconn_set_notice_receiver(VALUE self)
2226
+ {
2227
+ VALUE proc, old_proc;
2228
+ PGconn *conn = get_pgconn(self);
2229
+
2230
+ /* If default_notice_receiver is unset, assume that the current
2231
+ * notice receiver is the default, and save it to a global variable.
2232
+ * This should not be a problem because the default receiver is
2233
+ * always the same, so won't vary among connections.
2234
+ */
2235
+ if(default_notice_receiver == NULL)
2236
+ default_notice_receiver = PQsetNoticeReceiver(conn, NULL, NULL);
2237
+
2238
+ old_proc = rb_iv_get(self, "@notice_receiver");
2239
+ if( rb_block_given_p() ) {
2240
+ proc = rb_block_proc();
2241
+ PQsetNoticeReceiver(conn, notice_receiver_proxy, (void *)self);
2242
+ } else {
2243
+ /* if no block is given, set back to default */
2244
+ proc = Qnil;
2245
+ PQsetNoticeReceiver(conn, default_notice_receiver, NULL);
2246
+ }
2247
+
2248
+ rb_iv_set(self, "@notice_receiver", proc);
2249
+ return old_proc;
2250
+ }
2251
+
2252
+ /*
2253
+ * call-seq:
2254
+ * conn.set_notice_processor {|message| ... } -> Proc
2255
+ *
2256
+ * Notice and warning messages generated by the server are not returned
2257
+ * by the query execution functions, since they do not imply failure of
2258
+ * the query. Instead they are passed to a notice handling function, and
2259
+ * execution continues normally after the handler returns. The default
2260
+ * notice handling function prints the message on <tt>stderr</tt>, but the
2261
+ * application can override this behavior by supplying its own handling
2262
+ * function.
2263
+ *
2264
+ * This function takes a new block to act as the handler, which should
2265
+ * accept a single parameter that will be a PGresult object, and returns
2266
+ * the Proc object previously set, or +nil+ if it was previously the default.
2267
+ *
2268
+ * If you pass no arguments, it will reset the handler to the default.
2269
+ */
2270
+ static VALUE
2271
+ pgconn_set_notice_processor(VALUE self)
2272
+ {
2273
+ VALUE proc, old_proc;
2274
+ PGconn *conn = get_pgconn(self);
2275
+
2276
+ /* If default_notice_processor is unset, assume that the current
2277
+ * notice processor is the default, and save it to a global variable.
2278
+ * This should not be a problem because the default processor is
2279
+ * always the same, so won't vary among connections.
2280
+ */
2281
+ if(default_notice_processor == NULL)
2282
+ default_notice_processor = PQsetNoticeProcessor(conn, NULL, NULL);
2283
+
2284
+ old_proc = rb_iv_get(self, "@notice_processor");
2285
+ if( rb_block_given_p() ) {
2286
+ proc = rb_block_proc();
2287
+ PQsetNoticeProcessor(conn, notice_processor_proxy, (void *)self);
2288
+ } else {
2289
+ /* if no block is given, set back to default */
2290
+ proc = Qnil;
2291
+ PQsetNoticeProcessor(conn, default_notice_processor, NULL);
2292
+ }
2293
+
2294
+ rb_iv_set(self, "@notice_processor", proc);
2295
+ return old_proc;
2296
+ }
2297
+ /*
2298
+ * call-seq:
2299
+ * conn.get_client_encoding() -> String
2300
+ *
2301
+ * Returns the client encoding as a String.
2302
+ */
2303
+ static VALUE
2304
+ pgconn_get_client_encoding(VALUE self)
2305
+ {
2306
+ char *encoding = (char *)pg_encoding_to_char(PQclientEncoding(get_pgconn(self)));
2307
+ return rb_tainted_str_new2(encoding);
2308
+ }
2309
+
2310
+ /*
2311
+ * call-seq:
2312
+ * conn.set_client_encoding( encoding )
2313
+ *
2314
+ * Sets the client encoding to the _encoding_ String.
2315
+ */
2316
+ static VALUE
2317
+ pgconn_set_client_encoding(VALUE self, VALUE str)
2318
+ {
2319
+ Check_Type(str, T_STRING);
2320
+ if ((PQsetClientEncoding(get_pgconn(self), StringValuePtr(str))) == -1){
2321
+ rb_raise(rb_ePGError, "invalid encoding name: %s",StringValuePtr(str));
2322
+ }
2323
+ return Qnil;
2324
+ }
2325
+
2326
+ /*
2327
+ * call-seq:
2328
+ * conn.transaction { |conn| ... } -> nil
2329
+ *
2330
+ * Executes a +BEGIN+ at the start of the block,
2331
+ * and a +COMMIT+ at the end of the block, or
2332
+ * +ROLLBACK+ if any exception occurs.
2333
+ */
2334
+ static VALUE
2335
+ pgconn_transaction(VALUE self)
2336
+ {
2337
+ PGconn *conn = get_pgconn(self);
2338
+ PGresult *result;
2339
+ VALUE rb_pgresult;
2340
+ int status;
2341
+
2342
+ if (rb_block_given_p()) {
2343
+ result = PQexec(conn, "BEGIN");
2344
+ rb_pgresult = new_pgresult(result, conn);
2345
+ pgresult_check(self, rb_pgresult);
2346
+ rb_protect(rb_yield, self, &status);
2347
+ if(status == 0) {
2348
+ result = PQexec(conn, "COMMIT");
2349
+ rb_pgresult = new_pgresult(result, conn);
2350
+ pgresult_check(self, rb_pgresult);
2351
+ }
2352
+ else {
2353
+ /* exception occurred, ROLLBACK and re-raise */
2354
+ result = PQexec(conn, "ROLLBACK");
2355
+ rb_pgresult = new_pgresult(result, conn);
2356
+ pgresult_check(self, rb_pgresult);
2357
+ rb_jump_tag(status);
2358
+ }
2359
+
2360
+ }
2361
+ else {
2362
+ /* no block supplied? */
2363
+ rb_raise(rb_eArgError, "Must supply block for PGconn#transaction");
2364
+ }
2365
+ return Qnil;
2366
+ }
2367
+
2368
+ /*
2369
+ * call-seq:
2370
+ * PGconn.quote_ident( str ) -> String
2371
+ * conn.quote_ident( str ) -> String
2372
+ *
2373
+ * Returns a string that is safe for inclusion in a SQL query
2374
+ * as an identifier. Note: this is not a quote function for values,
2375
+ * but for identifiers.
2376
+ *
2377
+ * For example, in a typical SQL query: +SELECT FOO FROM MYTABLE+
2378
+ * The identifier +FOO+ is folded to lower case, so it actually means
2379
+ * +foo+. If you really want to access the case-sensitive field name
2380
+ * +FOO+, use this function like +PGconn.quote_ident('FOO')+, which
2381
+ * will return +"FOO"+ (with double-quotes). PostgreSQL will see the
2382
+ * double-quotes, and it will not fold to lower case.
2383
+ *
2384
+ * Similarly, this function also protects against special characters,
2385
+ * and other things that might allow SQL injection if the identifier
2386
+ * comes from an untrusted source.
2387
+ */
2388
+ static VALUE
2389
+ pgconn_s_quote_ident(VALUE self, VALUE in_str)
2390
+ {
2391
+ VALUE ret;
2392
+ char *str = StringValuePtr(in_str);
2393
+ /* result size at most NAMEDATALEN*2 plus surrounding
2394
+ * double-quotes. */
2395
+ char buffer[NAMEDATALEN*2+2];
2396
+ unsigned int i=0,j=0;
2397
+
2398
+ if(strlen(str) >= NAMEDATALEN) {
2399
+ rb_raise(rb_eArgError,
2400
+ "Input string is longer than NAMEDATALEN-1 (%d)",
2401
+ NAMEDATALEN-1);
2402
+ }
2403
+ buffer[j++] = '"';
2404
+ for(i = 0; i < strlen(str) && str[i]; i++) {
2405
+ if(str[i] == '"')
2406
+ buffer[j++] = '"';
2407
+ buffer[j++] = str[i];
2408
+ }
2409
+ buffer[j++] = '"';
2410
+ ret = rb_str_new(buffer,j);
2411
+ OBJ_INFECT(ret, in_str);
2412
+ return ret;
2413
+ }
2414
+
2415
+
2416
+ /*
2417
+ * call-seq:
2418
+ * conn.block( [ timeout ] ) -> Boolean
2419
+ *
2420
+ * Blocks until the server is no longer busy, or until the
2421
+ * optional _timeout_ is reached, whichever comes first.
2422
+ * _timeout_ is measured in seconds and can be fractional.
2423
+ *
2424
+ * Returns +false+ if _timeout_ is reached, +true+ otherwise.
2425
+ *
2426
+ * If +true+ is returned, +conn.is_busy+ will return +false+
2427
+ * and +conn.get_result+ will not block.
2428
+ */
2429
+ static VALUE
2430
+ pgconn_block(int argc, VALUE *argv, VALUE self)
2431
+ {
2432
+ PGconn *conn = get_pgconn(self);
2433
+ int sd = PQsocket(conn);
2434
+ int ret;
2435
+ struct timeval timeout;
2436
+ struct timeval *ptimeout = NULL;
2437
+ VALUE timeout_in;
2438
+ double timeout_sec;
2439
+ fd_set sd_rset;
2440
+
2441
+ if (rb_scan_args(argc, argv, "01", &timeout_in) == 1) {
2442
+ timeout_sec = NUM2DBL(timeout_in);
2443
+ timeout.tv_sec = (long)timeout_sec;
2444
+ timeout.tv_usec = (long)((timeout_sec - (long)timeout_sec) * 1e6);
2445
+ ptimeout = &timeout;
2446
+ }
2447
+
2448
+ PQconsumeInput(conn);
2449
+ while(PQisBusy(conn)) {
2450
+ FD_ZERO(&sd_rset);
2451
+ FD_SET(sd, &sd_rset);
2452
+ ret = rb_thread_select(sd+1, &sd_rset, NULL, NULL, ptimeout);
2453
+ /* if select() times out, return false */
2454
+ if(ret == 0)
2455
+ return Qfalse;
2456
+ PQconsumeInput(conn);
2457
+ }
2458
+
2459
+ return Qtrue;
2460
+ }
2461
+
2462
+ /*
2463
+ * call-seq:
2464
+ * conn.get_last_result( ) -> PGresult
2465
+ *
2466
+ * This function retrieves all available results
2467
+ * on the current connection (from previously issued
2468
+ * asynchronous commands like +send_query()+) and
2469
+ * returns the last non-NULL result, or +nil+ if no
2470
+ * results are available.
2471
+ *
2472
+ * This function is similar to +PGconn#get_result+
2473
+ * except that it is designed to get one and only
2474
+ * one result.
2475
+ */
2476
+ static VALUE
2477
+ pgconn_get_last_result(VALUE self)
2478
+ {
2479
+ VALUE ret, result;
2480
+ ret = Qnil;
2481
+ while((result = pgconn_get_result(self)) != Qnil) {
2482
+ ret = result;
2483
+ }
2484
+ pgresult_check(self, ret);
2485
+ return ret;
2486
+ }
2487
+
2488
+
2489
+ /*
2490
+ * call-seq:
2491
+ * conn.async_exec(sql [, params, result_format ] ) -> PGresult
2492
+ *
2493
+ * This function has the same behavior as +PGconn#exec+,
2494
+ * except that it's implemented using asynchronous command
2495
+ * processing and ruby's +rb_thread_select+ in order to
2496
+ * allow other threads to process while waiting for the
2497
+ * server to complete the request.
2498
+ */
2499
+ static VALUE
2500
+ pgconn_async_exec(int argc, VALUE *argv, VALUE self)
2501
+ {
2502
+ pgconn_send_query(argc, argv, self);
2503
+ pgconn_block(0, NULL, self);
2504
+ return pgconn_get_last_result(self);
2505
+ }
2506
+
2507
+ /**************************************************************************
2508
+ * LARGE OBJECT SUPPORT
2509
+ **************************************************************************/
2510
+
2511
+ /*
2512
+ * call-seq:
2513
+ * conn.lo_creat( [mode] ) -> Fixnum
2514
+ *
2515
+ * Creates a large object with mode _mode_. Returns a large object Oid.
2516
+ * On failure, it raises PGError exception.
2517
+ */
2518
+ static VALUE
2519
+ pgconn_locreat(int argc, VALUE *argv, VALUE self)
2520
+ {
2521
+ Oid lo_oid;
2522
+ int mode;
2523
+ VALUE nmode;
2524
+ PGconn *conn = get_pgconn(self);
2525
+
2526
+ if (rb_scan_args(argc, argv, "01", &nmode) == 0)
2527
+ mode = INV_READ;
2528
+ else
2529
+ mode = NUM2INT(nmode);
2530
+
2531
+ lo_oid = lo_creat(conn, mode);
2532
+ if (lo_oid == 0)
2533
+ rb_raise(rb_ePGError, "lo_creat failed");
2534
+
2535
+ return INT2FIX(lo_oid);
2536
+ }
2537
+
2538
+ /*
2539
+ * call-seq:
2540
+ * conn.lo_create( oid ) -> Fixnum
2541
+ *
2542
+ * Creates a large object with oid _oid_. Returns the large object Oid.
2543
+ * On failure, it raises PGError exception.
2544
+ */
2545
+ static VALUE
2546
+ pgconn_locreate(VALUE self, VALUE in_lo_oid)
2547
+ {
2548
+ Oid ret, lo_oid;
2549
+ PGconn *conn = get_pgconn(self);
2550
+ lo_oid = NUM2INT(in_lo_oid);
2551
+
2552
+ ret = lo_create(conn, in_lo_oid);
2553
+ if (ret == InvalidOid)
2554
+ rb_raise(rb_ePGError, "lo_create failed");
2555
+
2556
+ return INT2FIX(ret);
2557
+ }
2558
+
2559
+ /*
2560
+ * call-seq:
2561
+ * conn.lo_import(file) -> Fixnum
2562
+ *
2563
+ * Import a file to a large object. Returns a large object Oid.
2564
+ *
2565
+ * On failure, it raises a PGError exception.
2566
+ */
2567
+ static VALUE
2568
+ pgconn_loimport(VALUE self, VALUE filename)
2569
+ {
2570
+ Oid lo_oid;
2571
+
2572
+ PGconn *conn = get_pgconn(self);
2573
+
2574
+ Check_Type(filename, T_STRING);
2575
+
2576
+ lo_oid = lo_import(conn, StringValuePtr(filename));
2577
+ if (lo_oid == 0) {
2578
+ rb_raise(rb_ePGError, PQerrorMessage(conn));
2579
+ }
2580
+ return INT2FIX(lo_oid);
2581
+ }
2582
+
2583
+ /*
2584
+ * call-seq:
2585
+ * conn.lo_export( oid, file ) -> nil
2586
+ *
2587
+ * Saves a large object of _oid_ to a _file_.
2588
+ */
2589
+ static VALUE
2590
+ pgconn_loexport(VALUE self, VALUE lo_oid, VALUE filename)
2591
+ {
2592
+ PGconn *conn = get_pgconn(self);
2593
+ int oid;
2594
+ Check_Type(filename, T_STRING);
2595
+
2596
+ oid = NUM2INT(lo_oid);
2597
+ if (oid < 0) {
2598
+ rb_raise(rb_ePGError, "invalid large object oid %d",oid);
2599
+ }
2600
+
2601
+ if (lo_export(conn, oid, StringValuePtr(filename)) < 0) {
2602
+ rb_raise(rb_ePGError, PQerrorMessage(conn));
2603
+ }
2604
+ return Qnil;
2605
+ }
2606
+
2607
+ /*
2608
+ * call-seq:
2609
+ * conn.lo_open( oid, [mode] ) -> Fixnum
2610
+ *
2611
+ * Open a large object of _oid_. Returns a large object descriptor
2612
+ * instance on success. The _mode_ argument specifies the mode for
2613
+ * the opened large object,which is either +INV_READ+, or +INV_WRITE+.
2614
+ *
2615
+ * If _mode_ is omitted, the default is +INV_READ+.
2616
+ */
2617
+ static VALUE
2618
+ pgconn_loopen(int argc, VALUE *argv, VALUE self)
2619
+ {
2620
+ Oid lo_oid;
2621
+ int fd, mode;
2622
+ VALUE nmode, selfid;
2623
+ PGconn *conn = get_pgconn(self);
2624
+
2625
+ rb_scan_args(argc, argv, "11", &selfid, &nmode);
2626
+ lo_oid = NUM2INT(selfid);
2627
+ if(NIL_P(nmode))
2628
+ mode = INV_READ;
2629
+ else
2630
+ mode = NUM2INT(nmode);
2631
+
2632
+ if((fd = lo_open(conn, lo_oid, mode)) < 0) {
2633
+ rb_raise(rb_ePGError, "can't open large object");
2634
+ }
2635
+ return INT2FIX(fd);
2636
+ }
2637
+
2638
+ /*
2639
+ * call-seq:
2640
+ * conn.lo_write( lo_desc, buffer ) -> Fixnum
2641
+ *
2642
+ * Writes the string _buffer_ to the large object _lo_desc_.
2643
+ * Returns the number of bytes written.
2644
+ */
2645
+ static VALUE
2646
+ pgconn_lowrite(VALUE self, VALUE in_lo_desc, VALUE buffer)
2647
+ {
2648
+ int n;
2649
+ PGconn *conn = get_pgconn(self);
2650
+ int fd = NUM2INT(in_lo_desc);
2651
+
2652
+ Check_Type(buffer, T_STRING);
2653
+
2654
+ if( RSTRING_LEN(buffer) < 0) {
2655
+ rb_raise(rb_ePGError, "write buffer zero string");
2656
+ }
2657
+ if((n = lo_write(conn, fd, StringValuePtr(buffer),
2658
+ RSTRING_LEN(buffer))) < 0) {
2659
+ rb_raise(rb_ePGError, "lo_write failed");
2660
+ }
2661
+
2662
+ return INT2FIX(n);
2663
+ }
2664
+
2665
+ /*
2666
+ * call-seq:
2667
+ * conn.lo_read( lo_desc, len ) -> String
2668
+ *
2669
+ * Attempts to read _len_ bytes from large object _lo_desc_,
2670
+ * returns resulting data.
2671
+ */
2672
+ static VALUE
2673
+ pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
2674
+ {
2675
+ int ret;
2676
+ PGconn *conn = get_pgconn(self);
2677
+ int len = NUM2INT(in_len);
2678
+ int lo_desc = NUM2INT(in_lo_desc);
2679
+ VALUE str;
2680
+ char *buffer;
2681
+
2682
+ buffer = ALLOC_N(char, len);
2683
+ if(buffer == NULL)
2684
+ rb_raise(rb_eNoMemError, "ALLOC failed!");
2685
+
2686
+ if (len < 0){
2687
+ rb_raise(rb_ePGError,"nagative length %d given", len);
2688
+ }
2689
+
2690
+ if((ret = lo_read(conn, lo_desc, buffer, len)) < 0)
2691
+ rb_raise(rb_ePGError, "lo_read failed");
2692
+
2693
+ if(ret == 0) {
2694
+ xfree(buffer);
2695
+ return Qnil;
2696
+ }
2697
+
2698
+ str = rb_tainted_str_new(buffer, len);
2699
+ xfree(buffer);
2700
+
2701
+ return str;
2702
+ }
2703
+
2704
+
2705
+ /*
2706
+ * call-seq
2707
+ * conn.lo_lseek( lo_desc, offset, whence ) -> Fixnum
2708
+ *
2709
+ * Move the large object pointer _lo_desc_ to offset _offset_.
2710
+ * Valid values for _whence_ are +SEEK_SET+, +SEEK_CUR+, and +SEEK_END+.
2711
+ * (Or 0, 1, or 2.)
2712
+ */
2713
+ static VALUE
2714
+ pgconn_lolseek(VALUE self, VALUE in_lo_desc, VALUE offset, VALUE whence)
2715
+ {
2716
+ PGconn *conn = get_pgconn(self);
2717
+ int lo_desc = NUM2INT(in_lo_desc);
2718
+ int ret;
2719
+
2720
+ if((ret = lo_lseek(conn, lo_desc, NUM2INT(offset), NUM2INT(whence))) < 0) {
2721
+ rb_raise(rb_ePGError, "lo_lseek failed");
2722
+ }
2723
+
2724
+ return INT2FIX(ret);
2725
+ }
2726
+
2727
+ /*
2728
+ * call-seq:
2729
+ * conn.lo_tell( lo_desc ) -> Fixnum
2730
+ *
2731
+ * Returns the current position of the large object _lo_desc_.
2732
+ */
2733
+ static VALUE
2734
+ pgconn_lotell(VALUE self, VALUE in_lo_desc)
2735
+ {
2736
+ int position;
2737
+ PGconn *conn = get_pgconn(self);
2738
+ int lo_desc = NUM2INT(in_lo_desc);
2739
+
2740
+ if((position = lo_tell(conn, lo_desc)) < 0)
2741
+ rb_raise(rb_ePGError,"lo_tell failed");
2742
+
2743
+ return INT2FIX(position);
2744
+ }
2745
+
2746
+ /*
2747
+ * call-seq:
2748
+ * conn.lo_truncate( lo_desc, len ) -> nil
2749
+ *
2750
+ * Truncates the large object _lo_desc_ to size _len_.
2751
+ */
2752
+ static VALUE
2753
+ pgconn_lotruncate(VALUE self, VALUE in_lo_desc, VALUE in_len)
2754
+ {
2755
+ PGconn *conn = get_pgconn(self);
2756
+ int lo_desc = NUM2INT(in_lo_desc);
2757
+ size_t len = NUM2INT(in_len);
2758
+
2759
+ if(lo_truncate(conn,lo_desc,len) < 0)
2760
+ rb_raise(rb_ePGError,"lo_truncate failed");
2761
+
2762
+ return Qnil;
2763
+ }
2764
+
2765
+ /*
2766
+ * call-seq:
2767
+ * conn.lo_close( lo_desc ) -> nil
2768
+ *
2769
+ * Closes the postgres large object of _lo_desc_.
2770
+ */
2771
+ static VALUE
2772
+ pgconn_loclose(VALUE self, VALUE in_lo_desc)
2773
+ {
2774
+ PGconn *conn = get_pgconn(self);
2775
+ int lo_desc = NUM2INT(in_lo_desc);
2776
+
2777
+ if(lo_close(conn,lo_desc) < 0)
2778
+ rb_raise(rb_ePGError,"lo_close failed");
2779
+
2780
+ return Qnil;
2781
+ }
2782
+
2783
+ /*
2784
+ * call-seq:
2785
+ * conn.lo_unlink( oid ) -> nil
2786
+ *
2787
+ * Unlinks (deletes) the postgres large object of _oid_.
2788
+ */
2789
+ static VALUE
2790
+ pgconn_lounlink(VALUE self, VALUE in_oid)
2791
+ {
2792
+ PGconn *conn = get_pgconn(self);
2793
+ int oid = NUM2INT(in_oid);
2794
+
2795
+ if (oid < 0)
2796
+ rb_raise(rb_ePGError, "invalid oid %d",oid);
2797
+
2798
+ if(lo_unlink(conn,oid) < 0)
2799
+ rb_raise(rb_ePGError,"lo_unlink failed");
2800
+
2801
+ return Qnil;
2802
+ }
2803
+
2804
+ /********************************************************************
2805
+ *
2806
+ * Document-class: PGresult
2807
+ *
2808
+ * The class to represent the query result tuples (rows).
2809
+ * An instance of this class is created as the result of every query.
2810
+ * You may need to invoke the #clear method of the instance when finished with
2811
+ * the result for better memory performance.
2812
+ *
2813
+ * Example:
2814
+ * require 'pg'
2815
+ * conn = PGconn.open(:dbname => 'test')
2816
+ * res = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
2817
+ * res.getvalue(0,0) # '1'
2818
+ * res[0]['b'] # '2'
2819
+ * res[0]['c'] # nil
2820
+ *
2821
+ */
2822
+
2823
+ /**************************************************************************
2824
+ * PGresult INSTANCE METHODS
2825
+ **************************************************************************/
2826
+
2827
+ /*
2828
+ * call-seq:
2829
+ * res.result_status() -> Fixnum
2830
+ *
2831
+ * Returns the status of the query. The status value is one of:
2832
+ * * +PGRES_EMPTY_QUERY+
2833
+ * * +PGRES_COMMAND_OK+
2834
+ * * +PGRES_TUPLES_OK+
2835
+ * * +PGRES_COPY_OUT+
2836
+ * * +PGRES_COPY_IN+
2837
+ * * +PGRES_BAD_RESPONSE+
2838
+ * * +PGRES_NONFATAL_ERROR+
2839
+ * * +PGRES_FATAL_ERROR+
2840
+ */
2841
+ static VALUE
2842
+ pgresult_result_status(VALUE self)
2843
+ {
2844
+ return INT2FIX(PQresultStatus(get_pgresult(self)));
2845
+ }
2846
+
2847
+ /*
2848
+ * call-seq:
2849
+ * res.res_status( status ) -> String
2850
+ *
2851
+ * Returns the string representation of status +status+.
2852
+ *
2853
+ */
2854
+ static VALUE
2855
+ pgresult_res_status(VALUE self, VALUE status)
2856
+ {
2857
+ VALUE ret = rb_tainted_str_new2(PQresStatus(NUM2INT(status)));
2858
+ ASSOCIATE_INDEX(ret, self);
2859
+ return ret;
2860
+ }
2861
+
2862
+ /*
2863
+ * call-seq:
2864
+ * res.result_error_message() -> String
2865
+ *
2866
+ * Returns the error message of the command as a string.
2867
+ */
2868
+ static VALUE
2869
+ pgresult_result_error_message(VALUE self)
2870
+ {
2871
+ VALUE ret = rb_tainted_str_new2(PQresultErrorMessage(get_pgresult(self)));
2872
+ ASSOCIATE_INDEX(ret, self);
2873
+ return ret;
2874
+ }
2875
+
2876
+ /*
2877
+ * call-seq:
2878
+ * res.result_error_field(fieldcode) -> String
2879
+ *
2880
+ * Returns the individual field of an error.
2881
+ *
2882
+ * +fieldcode+ is one of:
2883
+ * * +PG_DIAG_SEVERITY+
2884
+ * * +PG_DIAG_SQLSTATE+
2885
+ * * +PG_DIAG_MESSAGE_PRIMARY+
2886
+ * * +PG_DIAG_MESSAGE_DETAIL+
2887
+ * * +PG_DIAG_MESSAGE_HINT+
2888
+ * * +PG_DIAG_STATEMENT_POSITION+
2889
+ * * +PG_DIAG_INTERNAL_POSITION+
2890
+ * * +PG_DIAG_INTERNAL_QUERY+
2891
+ * * +PG_DIAG_CONTEXT+
2892
+ * * +PG_DIAG_SOURCE_FILE+
2893
+ * * +PG_DIAG_SOURCE_LINE+
2894
+ * * +PG_DIAG_SOURCE_FUNCTION+
2895
+ */
2896
+ static VALUE
2897
+ pgresult_result_error_field(VALUE self, VALUE field)
2898
+ {
2899
+ PGresult *result = get_pgresult(self);
2900
+ int fieldcode = NUM2INT(field);
2901
+ VALUE ret = rb_tainted_str_new2(PQresultErrorField(result,fieldcode));
2902
+ ASSOCIATE_INDEX(ret, self);
2903
+ return ret;
2904
+ }
2905
+
2906
+ /*
2907
+ * call-seq:
2908
+ * res.clear() -> nil
2909
+ *
2910
+ * Clears the PGresult object as the result of the query.
2911
+ */
2912
+ static VALUE
2913
+ pgresult_clear(VALUE self)
2914
+ {
2915
+ PQclear(get_pgresult(self));
2916
+ DATA_PTR(self) = NULL;
2917
+ return Qnil;
2918
+ }
2919
+
2920
+ /*
2921
+ * call-seq:
2922
+ * res.ntuples() -> Fixnum
2923
+ *
2924
+ * Returns the number of tuples in the query result.
2925
+ */
2926
+ static VALUE
2927
+ pgresult_ntuples(VALUE self)
2928
+ {
2929
+ return INT2FIX(PQntuples(get_pgresult(self)));
2930
+ }
2931
+
2932
+ /*
2933
+ * call-seq:
2934
+ * res.nfields() -> Fixnum
2935
+ *
2936
+ * Returns the number of columns in the query result.
2937
+ */
2938
+ static VALUE
2939
+ pgresult_nfields(VALUE self)
2940
+ {
2941
+ return INT2NUM(PQnfields(get_pgresult(self)));
2942
+ }
2943
+
2944
+ /*
2945
+ * call-seq:
2946
+ * res.fname( index ) -> String
2947
+ *
2948
+ * Returns the name of the column corresponding to _index_.
2949
+ */
2950
+ static VALUE
2951
+ pgresult_fname(VALUE self, VALUE index)
2952
+ {
2953
+ VALUE fname;
2954
+ PGresult *result;
2955
+ int i = NUM2INT(index);
2956
+
2957
+ result = get_pgresult(self);
2958
+ if (i < 0 || i >= PQnfields(result)) {
2959
+ rb_raise(rb_eArgError,"invalid field number %d", i);
2960
+ }
2961
+ fname = rb_tainted_str_new2(PQfname(result, i));
2962
+ ASSOCIATE_INDEX(fname, self);
2963
+ return fname;
2964
+ }
2965
+
2966
+ /*
2967
+ * call-seq:
2968
+ * res.fnumber( name ) -> Fixnum
2969
+ *
2970
+ * Returns the index of the field specified by the string _name_.
2971
+ *
2972
+ * Raises an ArgumentError if the specified _name_ isn't one of the field names;
2973
+ * raises a TypeError if _name_ is not a String.
2974
+ */
2975
+ static VALUE
2976
+ pgresult_fnumber(VALUE self, VALUE name)
2977
+ {
2978
+ int n;
2979
+
2980
+ Check_Type(name, T_STRING);
2981
+
2982
+ n = PQfnumber(get_pgresult(self), StringValuePtr(name));
2983
+ if (n == -1) {
2984
+ rb_raise(rb_eArgError,"Unknown field: %s", StringValuePtr(name));
2985
+ }
2986
+ return INT2FIX(n);
2987
+ }
2988
+
2989
+ /*
2990
+ * call-seq:
2991
+ * res.ftable( column_number ) -> Fixnum
2992
+ *
2993
+ * Returns the Oid of the table from which the column _column_number_
2994
+ * was fetched.
2995
+ *
2996
+ * Raises ArgumentError if _column_number_ is out of range or if
2997
+ * the Oid is undefined for that column.
2998
+ */
2999
+ static VALUE
3000
+ pgresult_ftable(VALUE self, VALUE column_number)
3001
+ {
3002
+ Oid n = PQftable(get_pgresult(self), NUM2INT(column_number));
3003
+ if (n == InvalidOid) {
3004
+ rb_raise(rb_eArgError,"Oid is undefined for column: %d",
3005
+ NUM2INT(column_number));
3006
+ }
3007
+ return INT2FIX(n);
3008
+ }
3009
+
3010
+ /*
3011
+ * call-seq:
3012
+ * res.ftablecol( column_number ) -> Fixnum
3013
+ *
3014
+ * Returns the column number (within its table) of the table from
3015
+ * which the column _column_number_ is made up.
3016
+ *
3017
+ * Raises ArgumentError if _column_number_ is out of range or if
3018
+ * the column number from its table is undefined for that column.
3019
+ */
3020
+ static VALUE
3021
+ pgresult_ftablecol(VALUE self, VALUE column_number)
3022
+ {
3023
+ int n = PQftablecol(get_pgresult(self), NUM2INT(column_number));
3024
+ if (n == 0) {
3025
+ rb_raise(rb_eArgError,
3026
+ "Column number from table is undefined for column: %d",
3027
+ NUM2INT(column_number));
3028
+ }
3029
+ return INT2FIX(n);
3030
+ }
3031
+
3032
+ /*
3033
+ * call-seq:
3034
+ * res.fformat( column_number ) -> Fixnum
3035
+ *
3036
+ * Returns the format (0 for text, 1 for binary) of column
3037
+ * _column_number_.
3038
+ *
3039
+ * Raises ArgumentError if _column_number_ is out of range.
3040
+ */
3041
+ static VALUE
3042
+ pgresult_fformat(VALUE self, VALUE column_number)
3043
+ {
3044
+ PGresult *result = get_pgresult(self);
3045
+ int fnumber = NUM2INT(column_number);
3046
+ if (fnumber >= PQnfields(result)) {
3047
+ rb_raise(rb_eArgError, "Column number is out of range: %d",
3048
+ fnumber);
3049
+ }
3050
+ return INT2FIX(PQfformat(result, fnumber));
3051
+ }
3052
+
3053
+ /*
3054
+ * call-seq:
3055
+ * res.ftype( column_number )
3056
+ *
3057
+ * Returns the data type associated with _column_number_.
3058
+ *
3059
+ * The integer returned is the internal +OID+ number (in PostgreSQL) of the type.
3060
+ */
3061
+ static VALUE
3062
+ pgresult_ftype(VALUE self, VALUE index)
3063
+ {
3064
+ PGresult* result = get_pgresult(self);
3065
+ int i = NUM2INT(index);
3066
+ if (i < 0 || i >= PQnfields(result)) {
3067
+ rb_raise(rb_eArgError, "invalid field number %d", i);
3068
+ }
3069
+ return INT2NUM(PQftype(result, i));
3070
+ }
3071
+
3072
+ /*
3073
+ * call-seq:
3074
+ * res.fmod( column_number )
3075
+ *
3076
+ * Returns the type modifier associated with column _column_number_.
3077
+ *
3078
+ * Raises ArgumentError if _column_number_ is out of range.
3079
+ */
3080
+ static VALUE
3081
+ pgresult_fmod(VALUE self, VALUE column_number)
3082
+ {
3083
+ PGresult *result = get_pgresult(self);
3084
+ int fnumber = NUM2INT(column_number);
3085
+ int modifier;
3086
+ if (fnumber >= PQnfields(result)) {
3087
+ rb_raise(rb_eArgError, "Column number is out of range: %d",
3088
+ fnumber);
3089
+ }
3090
+ if((modifier = PQfmod(result,fnumber)) == -1)
3091
+ rb_raise(rb_eArgError,
3092
+ "No modifier information available for column: %d",
3093
+ fnumber);
3094
+ return INT2NUM(modifier);
3095
+ }
3096
+
3097
+ /*
3098
+ * call-seq:
3099
+ * res.fsize( index )
3100
+ *
3101
+ * Returns the size of the field type in bytes. Returns <tt>-1</tt> if the field is variable sized.
3102
+ *
3103
+ * res = conn.exec("SELECT myInt, myVarChar50 FROM foo")
3104
+ * res.size(0) => 4
3105
+ * res.size(1) => -1
3106
+ */
3107
+ static VALUE
3108
+ pgresult_fsize(VALUE self, VALUE index)
3109
+ {
3110
+ PGresult *result;
3111
+ int i = NUM2INT(index);
3112
+
3113
+ result = get_pgresult(self);
3114
+ if (i < 0 || i >= PQnfields(result)) {
3115
+ rb_raise(rb_eArgError,"invalid field number %d", i);
3116
+ }
3117
+ return INT2NUM(PQfsize(result, i));
3118
+ }
3119
+
3120
+ /*
3121
+ * call-seq:
3122
+ * res.getvalue( tup_num, field_num )
3123
+ *
3124
+ * Returns the value in tuple number _tup_num_, field _field_num_,
3125
+ * or +nil+ if the field is +NULL+.
3126
+ */
3127
+ static VALUE
3128
+ pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num)
3129
+ {
3130
+ VALUE ret;
3131
+ PGresult *result;
3132
+ int i = NUM2INT(tup_num);
3133
+ int j = NUM2INT(field_num);
3134
+
3135
+ result = get_pgresult(self);
3136
+ if(i < 0 || i >= PQntuples(result)) {
3137
+ rb_raise(rb_eArgError,"invalid tuple number %d", i);
3138
+ }
3139
+ if(j < 0 || j >= PQnfields(result)) {
3140
+ rb_raise(rb_eArgError,"invalid field number %d", j);
3141
+ }
3142
+ if(PQgetisnull(result, i, j))
3143
+ return Qnil;
3144
+ ret = rb_tainted_str_new(PQgetvalue(result, i, j),
3145
+ PQgetlength(result, i, j));
3146
+ ASSOCIATE_INDEX(ret, self);
3147
+ return ret;
3148
+ }
3149
+
3150
+ /*
3151
+ * call-seq:
3152
+ * res.getisnull(tuple_position, field_position) -> boolean
3153
+ *
3154
+ * Returns +true+ if the specified value is +nil+; +false+ otherwise.
3155
+ */
3156
+ static VALUE
3157
+ pgresult_getisnull(VALUE self, VALUE tup_num, VALUE field_num)
3158
+ {
3159
+ PGresult *result;
3160
+ int i = NUM2INT(tup_num);
3161
+ int j = NUM2INT(field_num);
3162
+
3163
+ result = get_pgresult(self);
3164
+ if (i < 0 || i >= PQntuples(result)) {
3165
+ rb_raise(rb_eArgError,"invalid tuple number %d", i);
3166
+ }
3167
+ if (j < 0 || j >= PQnfields(result)) {
3168
+ rb_raise(rb_eArgError,"invalid field number %d", j);
3169
+ }
3170
+ return PQgetisnull(result, i, j) ? Qtrue : Qfalse;
3171
+ }
3172
+
3173
+ /*
3174
+ * call-seq:
3175
+ * res.getlength( tup_num, field_num ) -> Fixnum
3176
+ *
3177
+ * Returns the (String) length of the field in bytes.
3178
+ *
3179
+ * Equivalent to <tt>res.value(<i>tup_num</i>,<i>field_num</i>).length</tt>.
3180
+ */
3181
+ static VALUE
3182
+ pgresult_getlength(VALUE self, VALUE tup_num, VALUE field_num)
3183
+ {
3184
+ PGresult *result;
3185
+ int i = NUM2INT(tup_num);
3186
+ int j = NUM2INT(field_num);
3187
+
3188
+ result = get_pgresult(self);
3189
+ if (i < 0 || i >= PQntuples(result)) {
3190
+ rb_raise(rb_eArgError,"invalid tuple number %d", i);
3191
+ }
3192
+ if (j < 0 || j >= PQnfields(result)) {
3193
+ rb_raise(rb_eArgError,"invalid field number %d", j);
3194
+ }
3195
+ return INT2FIX(PQgetlength(result, i, j));
3196
+ }
3197
+
3198
+ /*
3199
+ * call-seq:
3200
+ * res.nparams() -> Fixnum
3201
+ *
3202
+ * Returns the number of parameters of a prepared statement.
3203
+ * Only useful for the result returned by conn.describePrepared
3204
+ */
3205
+ static VALUE
3206
+ pgresult_nparams(VALUE self)
3207
+ {
3208
+ PGresult *result;
3209
+
3210
+ result = get_pgresult(self);
3211
+ return INT2FIX(PQnparams(result));
3212
+ }
3213
+
3214
+ /*
3215
+ * call-seq:
3216
+ * res.paramtype( param_number ) -> Oid
3217
+ *
3218
+ * Returns the Oid of the data type of parameter _param_number_.
3219
+ * Only useful for the result returned by conn.describePrepared
3220
+ */
3221
+ static VALUE
3222
+ pgresult_paramtype(VALUE self, VALUE param_number)
3223
+ {
3224
+ PGresult *result;
3225
+
3226
+ result = get_pgresult(self);
3227
+ return INT2FIX(PQparamtype(result,NUM2INT(param_number)));
3228
+ }
3229
+
3230
+ /*
3231
+ * call-seq:
3232
+ * res.cmd_status() -> String
3233
+ *
3234
+ * Returns the status string of the last query command.
3235
+ */
3236
+ static VALUE
3237
+ pgresult_cmd_status(VALUE self)
3238
+ {
3239
+ VALUE ret = rb_tainted_str_new2(PQcmdStatus(get_pgresult(self)));
3240
+ ASSOCIATE_INDEX(ret, self);
3241
+ return ret;
3242
+ }
3243
+
3244
+ /*
3245
+ * call-seq:
3246
+ * res.cmd_tuples() -> Fixnum
3247
+ *
3248
+ * Returns the number of tuples (rows) affected by the SQL command.
3249
+ *
3250
+ * If the SQL command that generated the PGresult was not one of:
3251
+ * * +INSERT+
3252
+ * * +UPDATE+
3253
+ * * +DELETE+
3254
+ * * +MOVE+
3255
+ * * +FETCH+
3256
+ * or if no tuples were affected, <tt>0</tt> is returned.
3257
+ */
3258
+ static VALUE
3259
+ pgresult_cmd_tuples(VALUE self)
3260
+ {
3261
+ long n;
3262
+ n = strtol(PQcmdTuples(get_pgresult(self)),NULL, 10);
3263
+ return INT2NUM(n);
3264
+ }
3265
+
3266
+ /*
3267
+ * call-seq:
3268
+ * res.oid_value() -> Fixnum
3269
+ *
3270
+ * Returns the +oid+ of the inserted row if applicable,
3271
+ * otherwise +nil+.
3272
+ */
3273
+ static VALUE
3274
+ pgresult_oid_value(VALUE self)
3275
+ {
3276
+ Oid n = PQoidValue(get_pgresult(self));
3277
+ if (n == InvalidOid)
3278
+ return Qnil;
3279
+ else
3280
+ return INT2FIX(n);
3281
+ }
3282
+
3283
+ /* Utility methods not in libpq */
3284
+
3285
+ /*
3286
+ * call-seq:
3287
+ * res[ n ] -> Hash
3288
+ *
3289
+ * Returns tuple _n_ as a hash.
3290
+ */
3291
+ static VALUE
3292
+ pgresult_aref(VALUE self, VALUE index)
3293
+ {
3294
+ PGresult *result = get_pgresult(self);
3295
+ int tuple_num = NUM2INT(index);
3296
+ int field_num;
3297
+ VALUE fname,val;
3298
+ VALUE tuple;
3299
+
3300
+ if(tuple_num >= PQntuples(result))
3301
+ rb_raise(rb_eIndexError, "Index %d is out of range", tuple_num);
3302
+ tuple = rb_hash_new();
3303
+ for(field_num = 0; field_num < PQnfields(result); field_num++) {
3304
+ fname = rb_tainted_str_new2(PQfname(result,field_num));
3305
+ ASSOCIATE_INDEX(fname, self);
3306
+ if(PQgetisnull(result, tuple_num, field_num)) {
3307
+ rb_hash_aset(tuple, fname, Qnil);
3308
+ }
3309
+ else {
3310
+ val = rb_tainted_str_new(PQgetvalue(result, tuple_num, field_num),
3311
+ PQgetlength(result, tuple_num, field_num));
3312
+ ASSOCIATE_INDEX(val, self);
3313
+ rb_hash_aset(tuple, fname, val);
3314
+ }
3315
+ }
3316
+ return tuple;
3317
+ }
3318
+
3319
+ /*
3320
+ * call-seq:
3321
+ * res.each{ |tuple| ... }
3322
+ *
3323
+ * Invokes block for each tuple in the result set.
3324
+ */
3325
+ static VALUE
3326
+ pgresult_each(VALUE self)
3327
+ {
3328
+ PGresult *result = get_pgresult(self);
3329
+ int tuple_num;
3330
+
3331
+ for(tuple_num = 0; tuple_num < PQntuples(result); tuple_num++) {
3332
+ rb_yield(pgresult_aref(self, INT2NUM(tuple_num)));
3333
+ }
3334
+ return self;
3335
+ }
3336
+
3337
+ /*
3338
+ * call-seq:
3339
+ * res.fields() -> Array
3340
+ *
3341
+ * Returns an array of Strings representing the names of the fields in the result.
3342
+ */
3343
+ static VALUE
3344
+ pgresult_fields(VALUE self)
3345
+ {
3346
+ PGresult *result;
3347
+ VALUE ary;
3348
+ int n, i;
3349
+
3350
+ result = get_pgresult(self);
3351
+ n = PQnfields(result);
3352
+ ary = rb_ary_new2(n);
3353
+ for (i=0;i<n;i++) {
3354
+ VALUE val = rb_tainted_str_new2(PQfname(result, i));
3355
+ ASSOCIATE_INDEX(val, self);
3356
+ rb_ary_push(ary, val);
3357
+ }
3358
+ return ary;
3359
+ }
3360
+
3361
+ #ifdef M17N_SUPPORTED
3362
+ /**
3363
+ * The mapping from canonical encoding names in PostgreSQL to ones in Ruby.
3364
+ */
3365
+ static const char * const (enc_pg2ruby_mapping[][2]) = {
3366
+ {"BIG5", "Big5" },
3367
+ {"EUC_CN", "GB2312" },
3368
+ {"EUC_JP", "EUC-JP" },
3369
+ {"EUC_JIS_2004", "EUC-JP" },
3370
+ {"EUC_KR", "EUC-KR" },
3371
+ {"EUC_TW", "EUC-TW" },
3372
+ {"GB18030", "GB18030" },
3373
+ {"GBK", "GBK" },
3374
+ {"ISO_8859_5", "ISO-8859-5" },
3375
+ {"ISO_8859_6", "ISO-8859-6" },
3376
+ {"ISO_8859_7", "ISO-8859-7" },
3377
+ {"ISO_8859_8", "ISO-8859-8" },
3378
+ /* {"JOHAB", "JOHAB" }, dummy */
3379
+ {"KOI8", "KOI8-U" },
3380
+ {"LATIN1", "ISO-8859-1" },
3381
+ {"LATIN2", "ISO-8859-2" },
3382
+ {"LATIN3", "ISO-8859-3" },
3383
+ {"LATIN4", "ISO-8859-4" },
3384
+ {"LATIN5", "ISO-8859-5" },
3385
+ {"LATIN6", "ISO-8859-6" },
3386
+ {"LATIN7", "ISO-8859-7" },
3387
+ {"LATIN8", "ISO-8859-8" },
3388
+ {"LATIN9", "ISO-8859-9" },
3389
+ {"LATIN10", "ISO-8859-10" },
3390
+ {"MULE_INTERNAL", "Emacs-Mule" },
3391
+ {"SJIS", "Windows-31J" },
3392
+ {"SHIFT_JIS_2004","Windows-31J" },
3393
+ /*{"SQL_ASCII", NULL }, special case*/
3394
+ {"UHC", "CP949" },
3395
+ {"UTF8", "UTF-8" },
3396
+ {"WIN866", "IBM866" },
3397
+ {"WIN874", "Windows-874" },
3398
+ {"WIN1250", "Windows-1250"},
3399
+ {"WIN1251", "Windows-1251"},
3400
+ {"WIN1252", "Windows-1252"},
3401
+ {"WIN1253", "Windows-1253"},
3402
+ {"WIN1254", "Windows-1254"},
3403
+ {"WIN1255", "Windows-1255"},
3404
+ {"WIN1256", "Windows-1256"},
3405
+ {"WIN1257", "Windows-1257"},
3406
+ {"WIN1258", "Windows-1258"}
3407
+ };
3408
+
3409
+
3410
+ /*
3411
+ * A cache of mapping from PostgreSQL's encoding indices to Ruby's rb_encoding*s.
3412
+ */
3413
+ static struct st_table *enc_pg2ruby;
3414
+ static ID s_id_index;
3415
+
3416
+ static int enc_get_index(VALUE val)
3417
+ {
3418
+ int i = ENCODING_GET_INLINED(val);
3419
+ if (i == ENCODING_INLINE_MAX) {
3420
+ VALUE iv = rb_ivar_get(val, s_id_index);
3421
+ i = NUM2INT(iv);
3422
+ }
3423
+ return i;
3424
+ }
3425
+
3426
+ extern int rb_enc_alias(const char *alias, const char *orig); /* declaration missing in Ruby 1.9.1 */
3427
+ static rb_encoding *
3428
+ find_or_create_johab(void)
3429
+ {
3430
+ static const char * const aliases[] = { "JOHAB", "Windows-1361", "CP1361" };
3431
+ int enc_index;
3432
+ int i;
3433
+ for (i = 0; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
3434
+ enc_index = rb_enc_find_index(aliases[i]);
3435
+ if (enc_index > 0) return rb_enc_from_index(enc_index);
3436
+ }
3437
+
3438
+ enc_index = rb_define_dummy_encoding(aliases[0]);
3439
+ for (i = 1; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
3440
+ rb_enc_alias(aliases[i], aliases[0]);
3441
+ }
3442
+ return rb_enc_from_index(enc_index);
3443
+ }
3444
+
3445
+ /*
3446
+ * Returns the client_encoding of the given connection as a rb_encoding*
3447
+ *
3448
+ * * returns NULL if the client encoding is 'SQL_ASCII'.
3449
+ * * returns ASCII-8BIT if the client encoding is unknown.
3450
+ */
3451
+ static rb_encoding *
3452
+ pgconn_get_client_encoding_as_rb_encoding(PGconn* conn)
3453
+ {
3454
+ rb_encoding *enc;
3455
+ int enc_id = PQclientEncoding(conn);
3456
+
3457
+ if (st_lookup(enc_pg2ruby, (st_data_t)enc_id, (st_data_t*)&enc)) {
3458
+ return enc;
3459
+ }
3460
+ else {
3461
+ int i;
3462
+ const char *name = pg_encoding_to_char(enc_id);
3463
+ if (strcmp("SQL_ASCII", name) == 0) {
3464
+ enc = NULL;
3465
+ goto cache;
3466
+ }
3467
+ for (i = 0; i < sizeof(enc_pg2ruby_mapping)/sizeof(enc_pg2ruby_mapping[0]); ++i) {
3468
+ if (strcmp(name, enc_pg2ruby_mapping[i][0]) == 0) {
3469
+ enc = rb_enc_find(enc_pg2ruby_mapping[i][1]);
3470
+ goto cache;
3471
+ }
3472
+ }
3473
+
3474
+ /* Ruby 1.9.1 does not supoort JOHAB */
3475
+ if (strcmp(name, "JOHAB") == 0) {
3476
+ enc = find_or_create_johab();
3477
+ goto cache;
3478
+ }
3479
+
3480
+ enc = rb_ascii8bit_encoding();
3481
+ }
3482
+ cache:
3483
+ st_insert(enc_pg2ruby, (st_data_t)enc_id, (st_data_t)enc);
3484
+ return enc;
3485
+ }
3486
+
3487
+ /*
3488
+ * call-seq:
3489
+ * conn.internal_encoding() -> Encoding
3490
+ *
3491
+ * defined in Ruby 1.9 or later.
3492
+ *
3493
+ * Returns:
3494
+ * * an Encoding - client_encoding of the connection as a Ruby's Encoding object.
3495
+ * * nil - the client_encoding is 'SQL_ASCII'
3496
+ */
3497
+ static VALUE
3498
+ pgconn_internal_encoding(VALUE self)
3499
+ {
3500
+ return rb_enc_from_encoding(pgconn_get_client_encoding_as_rb_encoding(get_pgconn(self)));
3501
+ }
3502
+
3503
+ static VALUE pgconn_external_encoding(VALUE self);
3504
+
3505
+ /*
3506
+ * call-seq:
3507
+ * conn.internal_encoding = value
3508
+ *
3509
+ * A wrapper of +PGconn#set_client_encoding+.
3510
+ * defined in Ruby 1.9 or later.
3511
+ *
3512
+ * +value+ can be one of:
3513
+ * * an Encoding
3514
+ * * a String - a name of Encoding
3515
+ * * +nil+ - sets 'SQL_ASCII' to the client_encoding.
3516
+ */
3517
+ static VALUE
3518
+ pgconn_internal_encoding_set(VALUE self, VALUE enc)
3519
+ {
3520
+ if (NIL_P(enc)) {
3521
+ pgconn_set_client_encoding(self, rb_usascii_str_new_cstr("SQL_ASCII"));
3522
+ return enc;
3523
+ }
3524
+ else if (TYPE(enc) == T_STRING && strcasecmp("JOHAB", RSTRING_PTR(enc)) == 0) {
3525
+ pgconn_set_client_encoding(self, rb_usascii_str_new_cstr("JOHAB"));
3526
+ return enc;
3527
+ }
3528
+ else {
3529
+ int i;
3530
+ const char *name;
3531
+ name = rb_enc_name(rb_to_encoding(enc));
3532
+
3533
+ /* sequential search becuase rarely called */
3534
+ for (i = 0; i < sizeof(enc_pg2ruby_mapping)/sizeof(enc_pg2ruby_mapping[0]); ++i) {
3535
+ if (strcmp(name, enc_pg2ruby_mapping[i][1]) == 0) {
3536
+ if (PQsetClientEncoding(get_pgconn(self), enc_pg2ruby_mapping[i][0]) == -1) {
3537
+ VALUE server_encoding = pgconn_external_encoding(self);
3538
+ rb_raise(rb_eEncCompatError, "imcompatible character encodings: %s and %s",
3539
+ rb_enc_name(rb_to_encoding(server_encoding)),
3540
+ enc_pg2ruby_mapping[i][0]);
3541
+ }
3542
+ return enc;
3543
+ }
3544
+ }
3545
+
3546
+ /* Ruby 1.9.1 does not support JOHAB */
3547
+ if (strcasecmp(name, "JOHAB") == 0) {
3548
+ pgconn_set_client_encoding(self, rb_usascii_str_new_cstr("JOHAB"));
3549
+ return enc;
3550
+ }
3551
+ }
3552
+
3553
+ enc = rb_inspect(enc);
3554
+ rb_raise(rb_ePGError, "unknown encoding: %s", StringValuePtr(enc));
3555
+ }
3556
+
3557
+
3558
+
3559
+ static VALUE enc_server_encoding_getvalue(VALUE pgresult)
3560
+ {
3561
+ return pgresult_getvalue(pgresult, INT2FIX(0), INT2FIX(0));
3562
+ }
3563
+
3564
+ /*
3565
+ * call-seq:
3566
+ * conn.external_encoding() -> Encoding
3567
+ *
3568
+ * defined in Ruby 1.9 or later.
3569
+ * * Returns the server_encoding of the connected database as a Ruby's Encoding object.
3570
+ * * Maps 'SQL_ASCII' to ASCII-8BIT.
3571
+ */
3572
+ static VALUE
3573
+ pgconn_external_encoding(VALUE self)
3574
+ {
3575
+ VALUE enc;
3576
+ enc = rb_iv_get(self, "@external_encoding");
3577
+ if (RTEST(enc)) {
3578
+ return enc;
3579
+ }
3580
+ else {
3581
+ int i;
3582
+ VALUE query = rb_usascii_str_new_cstr("SHOW server_encoding");
3583
+ VALUE pgresult = pgconn_exec(1, &query, self);
3584
+ VALUE enc_name = rb_ensure(enc_server_encoding_getvalue, pgresult, pgresult_clear, pgresult);
3585
+
3586
+ if (strcmp("SQL_ASCII", StringValuePtr(enc_name)) == 0) {
3587
+ enc = rb_enc_from_encoding(rb_ascii8bit_encoding());
3588
+ goto cache;
3589
+ }
3590
+ for (i = 0; i < sizeof(enc_pg2ruby_mapping)/sizeof(enc_pg2ruby_mapping[0]); ++i) {
3591
+ if (strcmp(StringValuePtr(enc_name), enc_pg2ruby_mapping[i][0]) == 0) {
3592
+ enc = rb_enc_from_encoding(rb_enc_find(enc_pg2ruby_mapping[i][1]));
3593
+ goto cache;
3594
+ }
3595
+ }
3596
+
3597
+ /* Ruby 1.9.1 does not supoort JOHAB */
3598
+ if (strcmp(StringValuePtr(enc_name), "JOHAB") == 0) {
3599
+ enc = rb_enc_from_encoding(find_or_create_johab());
3600
+ goto cache;
3601
+ }
3602
+
3603
+ /* fallback */
3604
+ enc = rb_enc_from_encoding(rb_enc_find(StringValuePtr(enc_name)));
3605
+ }
3606
+
3607
+ cache:
3608
+ rb_iv_set(self, "@external_encoding", enc);
3609
+ return enc;
3610
+ }
3611
+
3612
+ static void
3613
+ init_m17n(void)
3614
+ {
3615
+ enc_pg2ruby = st_init_numtable();
3616
+ s_id_index = rb_intern("@encoding");
3617
+ rb_define_method(rb_cPGconn, "internal_encoding", pgconn_internal_encoding, 0);
3618
+ rb_define_method(rb_cPGconn, "internal_encoding=", pgconn_internal_encoding_set, 1);
3619
+ rb_define_method(rb_cPGconn, "external_encoding", pgconn_external_encoding, 0);
3620
+ }
3621
+
3622
+
3623
+ #endif
3624
+ /**************************************************************************/
3625
+
3626
+ void
3627
+ Init_pg()
3628
+ {
3629
+ rb_ePGError = rb_define_class("PGError", rb_eStandardError);
3630
+ rb_cPGconn = rb_define_class("PGconn", rb_cObject);
3631
+ rb_cPGresult = rb_define_class("PGresult", rb_cObject);
3632
+
3633
+
3634
+ /*************************
3635
+ * PGError
3636
+ *************************/
3637
+ rb_define_alias(rb_ePGError, "error", "message");
3638
+ rb_define_attr(rb_ePGError, "connection", 1, 0);
3639
+ rb_define_attr(rb_ePGError, "result", 1, 0);
3640
+
3641
+ /*************************
3642
+ * PGconn
3643
+ *************************/
3644
+
3645
+ /****** PGconn CLASS METHODS ******/
3646
+ rb_define_alloc_func(rb_cPGconn, pgconn_alloc);
3647
+ rb_define_singleton_alias(rb_cPGconn, "connect", "new");
3648
+ rb_define_singleton_alias(rb_cPGconn, "open", "new");
3649
+ rb_define_singleton_alias(rb_cPGconn, "setdb", "new");
3650
+ rb_define_singleton_alias(rb_cPGconn, "setdblogin", "new");
3651
+ rb_define_singleton_method(rb_cPGconn, "escape_string", pgconn_s_escape, 1);
3652
+ rb_define_singleton_alias(rb_cPGconn, "escape", "escape_string");
3653
+ rb_define_singleton_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
3654
+ rb_define_singleton_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
3655
+ rb_define_singleton_method(rb_cPGconn, "isthreadsafe", pgconn_s_isthreadsafe, 0);
3656
+ rb_define_singleton_method(rb_cPGconn, "encrypt_password", pgconn_s_encrypt_password, 0);
3657
+ rb_define_singleton_method(rb_cPGconn, "quote_ident", pgconn_s_quote_ident, 1);
3658
+ rb_define_singleton_method(rb_cPGconn, "connect_start", pgconn_s_connect_start, -1);
3659
+ rb_define_singleton_method(rb_cPGconn, "conndefaults", pgconn_s_conndefaults, 0);
3660
+
3661
+ /****** PGconn CLASS CONSTANTS: Connection Status ******/
3662
+ rb_define_const(rb_cPGconn, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
3663
+ rb_define_const(rb_cPGconn, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
3664
+
3665
+ /****** PGconn CLASS CONSTANTS: Nonblocking connection status ******/
3666
+ rb_define_const(rb_cPGconn, "CONNECTION_STARTED", INT2FIX(CONNECTION_STARTED));
3667
+ rb_define_const(rb_cPGconn, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
3668
+ rb_define_const(rb_cPGconn, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
3669
+ rb_define_const(rb_cPGconn, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
3670
+ rb_define_const(rb_cPGconn, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
3671
+ rb_define_const(rb_cPGconn, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
3672
+
3673
+ /****** PGconn CLASS CONSTANTS: Nonblocking connection polling status ******/
3674
+ rb_define_const(rb_cPGconn, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
3675
+ rb_define_const(rb_cPGconn, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
3676
+ rb_define_const(rb_cPGconn, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
3677
+ rb_define_const(rb_cPGconn, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
3678
+
3679
+ /****** PGconn CLASS CONSTANTS: Transaction Status ******/
3680
+ rb_define_const(rb_cPGconn, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
3681
+ rb_define_const(rb_cPGconn, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
3682
+ rb_define_const(rb_cPGconn, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
3683
+ rb_define_const(rb_cPGconn, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
3684
+ rb_define_const(rb_cPGconn, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
3685
+
3686
+ /****** PGconn CLASS CONSTANTS: Error Verbosity ******/
3687
+ rb_define_const(rb_cPGconn, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
3688
+ rb_define_const(rb_cPGconn, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
3689
+ rb_define_const(rb_cPGconn, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
3690
+
3691
+ /****** PGconn CLASS CONSTANTS: Large Objects ******/
3692
+ rb_define_const(rb_cPGconn, "INV_WRITE", INT2FIX(INV_WRITE));
3693
+ rb_define_const(rb_cPGconn, "INV_READ", INT2FIX(INV_READ));
3694
+ rb_define_const(rb_cPGconn, "SEEK_SET", INT2FIX(SEEK_SET));
3695
+ rb_define_const(rb_cPGconn, "SEEK_CUR", INT2FIX(SEEK_CUR));
3696
+ rb_define_const(rb_cPGconn, "SEEK_END", INT2FIX(SEEK_END));
3697
+
3698
+ /****** PGconn INSTANCE METHODS: Connection Control ******/
3699
+ rb_define_method(rb_cPGconn, "initialize", pgconn_init, -1);
3700
+ rb_define_method(rb_cPGconn, "connect_poll", pgconn_connect_poll, 0);
3701
+ rb_define_method(rb_cPGconn, "finish", pgconn_finish, 0);
3702
+ rb_define_method(rb_cPGconn, "reset", pgconn_reset, 0);
3703
+ rb_define_method(rb_cPGconn, "reset_start", pgconn_reset_start, 0);
3704
+ rb_define_method(rb_cPGconn, "reset_poll", pgconn_reset_poll, 0);
3705
+ rb_define_method(rb_cPGconn, "conndefaults", pgconn_s_conndefaults, 0);
3706
+ rb_define_alias(rb_cPGconn, "close", "finish");
3707
+
3708
+ /****** PGconn INSTANCE METHODS: Connection Status ******/
3709
+ rb_define_method(rb_cPGconn, "db", pgconn_db, 0);
3710
+ rb_define_method(rb_cPGconn, "user", pgconn_user, 0);
3711
+ rb_define_method(rb_cPGconn, "pass", pgconn_pass, 0);
3712
+ rb_define_method(rb_cPGconn, "host", pgconn_host, 0);
3713
+ rb_define_method(rb_cPGconn, "port", pgconn_port, 0);
3714
+ rb_define_method(rb_cPGconn, "tty", pgconn_tty, 0);
3715
+ rb_define_method(rb_cPGconn, "options", pgconn_options, 0);
3716
+ rb_define_method(rb_cPGconn, "status", pgconn_status, 0);
3717
+ rb_define_method(rb_cPGconn, "transaction_status", pgconn_transaction_status, 0);
3718
+ rb_define_method(rb_cPGconn, "parameter_status", pgconn_parameter_status, 1);
3719
+ rb_define_method(rb_cPGconn, "protocol_version", pgconn_protocol_version, 0);
3720
+ rb_define_method(rb_cPGconn, "server_version", pgconn_server_version, 0);
3721
+ rb_define_method(rb_cPGconn, "error_message", pgconn_error_message, 0);
3722
+ rb_define_method(rb_cPGconn, "socket", pgconn_socket, 0);
3723
+ rb_define_method(rb_cPGconn, "backend_pid", pgconn_backend_pid, 0);
3724
+ rb_define_method(rb_cPGconn, "connection_needs_password", pgconn_connection_needs_password, 0);
3725
+ rb_define_method(rb_cPGconn, "connection_used_password", pgconn_connection_used_password, 0);
3726
+ //rb_define_method(rb_cPGconn, "getssl", pgconn_getssl, 0);
3727
+
3728
+ /****** PGconn INSTANCE METHODS: Command Execution ******/
3729
+ rb_define_method(rb_cPGconn, "exec", pgconn_exec, -1);
3730
+ rb_define_alias(rb_cPGconn, "query", "exec");
3731
+ rb_define_method(rb_cPGconn, "prepare", pgconn_prepare, -1);
3732
+ rb_define_method(rb_cPGconn, "exec_prepared", pgconn_exec_prepared, -1);
3733
+ rb_define_method(rb_cPGconn, "describe_prepared", pgconn_describe_prepared, 1);
3734
+ rb_define_method(rb_cPGconn, "describe_portal", pgconn_describe_portal, 1);
3735
+ rb_define_method(rb_cPGconn, "make_empty_pgresult", pgconn_make_empty_pgresult, 1);
3736
+ rb_define_method(rb_cPGconn, "escape_string", pgconn_s_escape, 1);
3737
+ rb_define_alias(rb_cPGconn, "escape", "escape_string");
3738
+ rb_define_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
3739
+ rb_define_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
3740
+
3741
+ /****** PGconn INSTANCE METHODS: Asynchronous Command Processing ******/
3742
+ rb_define_method(rb_cPGconn, "send_query", pgconn_send_query, -1);
3743
+ rb_define_method(rb_cPGconn, "send_prepare", pgconn_send_prepare, -1);
3744
+ rb_define_method(rb_cPGconn, "send_query_prepared", pgconn_send_query_prepared, -1);
3745
+ rb_define_method(rb_cPGconn, "send_describe_prepared", pgconn_send_describe_prepared, 1);
3746
+ rb_define_method(rb_cPGconn, "send_describe_portal", pgconn_send_describe_portal, 1);
3747
+ rb_define_method(rb_cPGconn, "get_result", pgconn_get_result, 0);
3748
+ rb_define_method(rb_cPGconn, "consume_input", pgconn_consume_input, 0);
3749
+ rb_define_method(rb_cPGconn, "is_busy", pgconn_is_busy, 0);
3750
+ rb_define_method(rb_cPGconn, "setnonblocking", pgconn_setnonblocking, 1);
3751
+ rb_define_method(rb_cPGconn, "isnonblocking", pgconn_isnonblocking, 0);
3752
+ rb_define_method(rb_cPGconn, "flush", pgconn_flush, 0);
3753
+
3754
+ /****** PGconn INSTANCE METHODS: Cancelling Queries in Progress ******/
3755
+ rb_define_method(rb_cPGconn, "cancel", pgconn_cancel, 0);
3756
+
3757
+ /****** PGconn INSTANCE METHODS: NOTIFY ******/
3758
+ rb_define_method(rb_cPGconn, "notifies", pgconn_notifies, 0);
3759
+
3760
+ /****** PGconn INSTANCE METHODS: COPY ******/
3761
+ rb_define_method(rb_cPGconn, "put_copy_data", pgconn_put_copy_data, 1);
3762
+ rb_define_method(rb_cPGconn, "put_copy_end", pgconn_put_copy_end, -1);
3763
+ rb_define_method(rb_cPGconn, "get_copy_data", pgconn_get_copy_data, -1);
3764
+
3765
+ /****** PGconn INSTANCE METHODS: Control Functions ******/
3766
+ rb_define_method(rb_cPGconn, "set_error_verbosity", pgconn_set_error_verbosity, 1);
3767
+ rb_define_method(rb_cPGconn, "trace", pgconn_trace, 1);
3768
+ rb_define_method(rb_cPGconn, "untrace", pgconn_untrace, 0);
3769
+
3770
+ /****** PGconn INSTANCE METHODS: Notice Processing ******/
3771
+ rb_define_method(rb_cPGconn, "set_notice_receiver", pgconn_set_notice_receiver, 0);
3772
+ rb_define_method(rb_cPGconn, "set_notice_processor", pgconn_set_notice_processor, 0);
3773
+
3774
+ /****** PGconn INSTANCE METHODS: Other ******/
3775
+ rb_define_method(rb_cPGconn, "get_client_encoding", pgconn_get_client_encoding, 0);
3776
+ rb_define_method(rb_cPGconn, "set_client_encoding", pgconn_set_client_encoding, 1);
3777
+ rb_define_method(rb_cPGconn, "transaction", pgconn_transaction, 0);
3778
+ rb_define_method(rb_cPGconn, "block", pgconn_block, -1);
3779
+ rb_define_method(rb_cPGconn, "quote_ident", pgconn_s_quote_ident, 1);
3780
+ rb_define_method(rb_cPGconn, "async_exec", pgconn_async_exec, -1);
3781
+ rb_define_alias(rb_cPGconn, "async_query", "async_exec");
3782
+ rb_define_method(rb_cPGconn, "get_last_result", pgconn_get_last_result, 0);
3783
+
3784
+ /****** PGconn INSTANCE METHODS: Large Object Support ******/
3785
+ rb_define_method(rb_cPGconn, "lo_creat", pgconn_locreat, -1);
3786
+ rb_define_alias(rb_cPGconn, "locreat", "lo_creat");
3787
+ rb_define_method(rb_cPGconn, "lo_create", pgconn_locreate, 1);
3788
+ rb_define_alias(rb_cPGconn, "locreate", "lo_create");
3789
+ rb_define_method(rb_cPGconn, "lo_import", pgconn_loimport, 1);
3790
+ rb_define_alias(rb_cPGconn, "loimport", "lo_import");
3791
+ rb_define_method(rb_cPGconn, "lo_export", pgconn_loexport, 2);
3792
+ rb_define_alias(rb_cPGconn, "loexport", "lo_export");
3793
+ rb_define_method(rb_cPGconn, "lo_open", pgconn_loopen, -1);
3794
+ rb_define_alias(rb_cPGconn, "loopen", "lo_open");
3795
+ rb_define_method(rb_cPGconn, "lo_write",pgconn_lowrite, 2);
3796
+ rb_define_alias(rb_cPGconn, "lowrite", "lo_write");
3797
+ rb_define_method(rb_cPGconn, "lo_read",pgconn_loread, 2);
3798
+ rb_define_alias(rb_cPGconn, "loread", "lo_read");
3799
+ rb_define_method(rb_cPGconn, "lo_lseek",pgconn_lolseek, 3);
3800
+ rb_define_alias(rb_cPGconn, "lolseek", "lo_lseek");
3801
+ rb_define_alias(rb_cPGconn, "lo_seek", "lo_lseek");
3802
+ rb_define_alias(rb_cPGconn, "loseek", "lo_lseek");
3803
+ rb_define_method(rb_cPGconn, "lo_tell",pgconn_lotell, 1);
3804
+ rb_define_alias(rb_cPGconn, "lotell", "lo_tell");
3805
+ rb_define_method(rb_cPGconn, "lo_truncate", pgconn_lotruncate, 2);
3806
+ rb_define_alias(rb_cPGconn, "lotruncate", "lo_truncate");
3807
+ rb_define_method(rb_cPGconn, "lo_close",pgconn_loclose, 1);
3808
+ rb_define_alias(rb_cPGconn, "loclose", "lo_close");
3809
+ rb_define_method(rb_cPGconn, "lo_unlink", pgconn_lounlink, 1);
3810
+ rb_define_alias(rb_cPGconn, "lounlink", "lo_unlink");
3811
+
3812
+ /*************************
3813
+ * PGresult
3814
+ *************************/
3815
+ rb_include_module(rb_cPGresult, rb_mEnumerable);
3816
+
3817
+ /****** PGresult CONSTANTS: result status ******/
3818
+ rb_define_const(rb_cPGresult, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
3819
+ rb_define_const(rb_cPGresult, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
3820
+ rb_define_const(rb_cPGresult, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
3821
+ rb_define_const(rb_cPGresult, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
3822
+ rb_define_const(rb_cPGresult, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
3823
+ rb_define_const(rb_cPGresult, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
3824
+ rb_define_const(rb_cPGresult, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
3825
+ rb_define_const(rb_cPGresult, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
3826
+
3827
+ /****** PGresult CONSTANTS: result error field codes ******/
3828
+ rb_define_const(rb_cPGresult, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
3829
+ rb_define_const(rb_cPGresult, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
3830
+ rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
3831
+ rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
3832
+ rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
3833
+ rb_define_const(rb_cPGresult, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
3834
+ rb_define_const(rb_cPGresult, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
3835
+ rb_define_const(rb_cPGresult, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
3836
+ rb_define_const(rb_cPGresult, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
3837
+ rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
3838
+ rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
3839
+ rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
3840
+
3841
+ /****** PGresult INSTANCE METHODS: libpq ******/
3842
+ rb_define_method(rb_cPGresult, "result_status", pgresult_result_status, 0);
3843
+ rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, 1);
3844
+ rb_define_method(rb_cPGresult, "result_error_message", pgresult_result_error_message, 0);
3845
+ rb_define_method(rb_cPGresult, "result_error_field", pgresult_result_error_field, 1);
3846
+ rb_define_method(rb_cPGresult, "clear", pgresult_clear, 0);
3847
+ rb_define_method(rb_cPGresult, "ntuples", pgresult_ntuples, 0);
3848
+ rb_define_alias(rb_cPGresult, "num_tuples", "ntuples");
3849
+ rb_define_method(rb_cPGresult, "nfields", pgresult_nfields, 0);
3850
+ rb_define_alias(rb_cPGresult, "num_fields", "nfields");
3851
+ rb_define_method(rb_cPGresult, "fname", pgresult_fname, 1);
3852
+ rb_define_method(rb_cPGresult, "fnumber", pgresult_fnumber, 1);
3853
+ rb_define_method(rb_cPGresult, "ftable", pgresult_ftable, 1);
3854
+ rb_define_method(rb_cPGresult, "ftablecol", pgresult_ftablecol, 1);
3855
+ rb_define_method(rb_cPGresult, "fformat", pgresult_fformat, 1);
3856
+ rb_define_method(rb_cPGresult, "ftype", pgresult_ftype, 1);
3857
+ rb_define_method(rb_cPGresult, "fmod", pgresult_fmod, 1);
3858
+ rb_define_method(rb_cPGresult, "fsize", pgresult_fsize, 1);
3859
+ rb_define_method(rb_cPGresult, "getvalue", pgresult_getvalue, 2);
3860
+ rb_define_method(rb_cPGresult, "getisnull", pgresult_getisnull, 2);
3861
+ rb_define_method(rb_cPGresult, "getlength", pgresult_getlength, 2);
3862
+ rb_define_method(rb_cPGresult, "nparams", pgresult_nparams, 0);
3863
+ rb_define_method(rb_cPGresult, "paramtype", pgresult_paramtype, 0);
3864
+ rb_define_method(rb_cPGresult, "cmd_status", pgresult_cmd_status, 0);
3865
+ rb_define_method(rb_cPGresult, "cmd_tuples", pgresult_cmd_tuples, 0);
3866
+ rb_define_alias(rb_cPGresult, "cmdtuples", "cmd_tuples");
3867
+ rb_define_method(rb_cPGresult, "oid_value", pgresult_oid_value, 0);
3868
+
3869
+ /****** PGresult INSTANCE METHODS: other ******/
3870
+ rb_define_method(rb_cPGresult, "[]", pgresult_aref, 1);
3871
+ rb_define_method(rb_cPGresult, "each", pgresult_each, 0);
3872
+ rb_define_method(rb_cPGresult, "fields", pgresult_fields, 0);
3873
+
3874
+ #ifdef M17N_SUPPORTED
3875
+ init_m17n();
3876
+ #endif
3877
+ }