pg 0.7.9.2008.01.28

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