qoobaa-pg 0.8.1

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