postgres 0.7.9.2008.01.03 → 0.7.9.2008.01.09

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