postgres 0.7.9.2007.12.22 → 0.7.9.2008.01.03

Sign up to get free protection for your applications and to get access to all the features.
data/ext/compat.h CHANGED
@@ -13,6 +13,10 @@
13
13
  #define rb_check_string_type(x) rb_check_convert_type(x, T_STRING, "String", "to_str")
14
14
  #endif /* RUBY_VERSION_CODE < 180 */
15
15
 
16
+ #ifndef RARRAY_LEN
17
+ #define RARRAY_LEN(x) RARRAY((x))->len
18
+ #endif /* RARRAY_LEN */
19
+
16
20
  #ifndef RSTRING_LEN
17
21
  #define RSTRING_LEN(x) RSTRING((x))->len
18
22
  #endif /* RSTRING_LEN */
@@ -114,6 +118,11 @@ int PQsendDescribePrepared(PGconn *conn, const char *stmtName);
114
118
  int PQsendDescribePortal(PGconn *conn, const char *portalName);
115
119
  #endif /* HAVE_PQSENDDESCRIBEPORTAL */
116
120
 
121
+ #ifndef HAVE_PQSENDPREPARE
122
+ int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query,
123
+ int nParams, const Oid *paramTypes);
124
+ #endif /* HAVE_PQSENDPREPARE */
125
+
117
126
  #ifndef HAVE_PQENCRYPTPASSWORD
118
127
  char *PQencryptPassword(const char *passwd, const char *user);
119
128
  #endif /* HAVE_PQENCRYPTPASSWORD */
data/ext/extconf.rb CHANGED
@@ -32,6 +32,7 @@ desired_functions = %w(
32
32
  PQparamtype
33
33
  PQsendDescribePrepared
34
34
  PQsendDescribePortal
35
+ PQsendPrepare
35
36
  PQencryptPassword
36
37
  PQdescribePrepared
37
38
  PQdescribePortal
data/ext/pg.c CHANGED
@@ -9,7 +9,7 @@
9
9
  modified at: Wed Jan 20 16:41:51 1999
10
10
 
11
11
  $Author: jdavis $
12
- $Date: 2007-12-13 18:58:32 -0800 (Thu, 13 Dec 2007) $
12
+ $Date: 2008-01-03 10:17:10 -0800 (Thu, 03 Jan 2008) $
13
13
  ************************************************/
14
14
 
15
15
  #include "pg.h"
@@ -21,19 +21,33 @@
21
21
  } \
22
22
  } while (0)
23
23
 
24
- #define rb_check_hash_type(x) rb_check_convert_type(x, T_HASH, "Hash", "to_hash")
25
-
26
24
  #define rb_define_singleton_alias(klass,new,old) rb_define_alias(rb_singleton_class(klass),new,old)
27
25
 
28
- #define Data_Set_Struct(self,ptr) do { \
29
- Check_Type(self, T_DATA); \
30
- DATA_PTR(self) = ptr; \
31
- } while (0)
32
-
33
26
  static VALUE rb_cPGconn;
34
27
  static VALUE rb_cPGresult;
35
28
  static VALUE rb_ePGError;
36
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
+
37
51
  /***************************************************************************
38
52
  * UTILITY FUNCTIONS
39
53
  **************************************************************************/
@@ -41,27 +55,28 @@ static VALUE rb_ePGError;
41
55
  static void free_pgconn(PGconn *);
42
56
  static void pgresult_check(VALUE, VALUE);
43
57
 
44
- static int build_key_value_string_i(VALUE key, VALUE value, VALUE result);
45
58
  static PGconn *get_pgconn(VALUE self);
46
59
  static VALUE pgconn_finish(VALUE self);
60
+ static VALUE pgresult_clear(VALUE self);
61
+ static VALUE pgresult_aref(VALUE self, VALUE index);
47
62
 
48
- //static VALUE pg_escape_regex;
49
- static VALUE pg_escape_str;
50
- static ID pg_gsub_bang_id;
51
-
63
+ /*
64
+ * Used to quote the values passed in a Hash to PGconn.init
65
+ * when building the connection string.
66
+ */
52
67
  static VALUE
53
68
  pgconn_s_quote_connstr(string)
54
69
  VALUE string;
55
70
  {
56
- char *str,*ptr;
57
- int i,j=0,len;
58
- VALUE result;
71
+ char *str,*ptr;
72
+ int i,j=0,len;
73
+ VALUE result;
74
+
75
+ Check_Type(string, T_STRING);
59
76
 
60
- Check_Type(string, T_STRING);
61
-
62
77
  ptr = RSTRING_PTR(string);
63
78
  len = RSTRING_LEN(string);
64
- str = ALLOCA_N(char, len * 2 + 2 + 1);
79
+ str = ALLOCA_N(char, len * 2 + 2 + 1);
65
80
  str[j++] = '\'';
66
81
  for(i = 0; i < len; i++) {
67
82
  if(ptr[i] == '\'' || ptr[i] == '\\')
@@ -69,119 +84,62 @@ pgconn_s_quote_connstr(string)
69
84
  str[j++] = ptr[i];
70
85
  }
71
86
  str[j++] = '\'';
72
- result = rb_str_new(str, j);
73
- OBJ_INFECT(result, string);
74
- return result;
75
- }
76
-
77
- //TODO broken
78
- static int
79
- build_key_value_string_i(key, value, result)
80
- VALUE key, value, result;
81
- {
82
- VALUE key_value;
83
- //if (key == Qundef) return ST_CONTINUE;
84
- key_value = (TYPE(key) == T_STRING ? rb_str_dup(key) : rb_obj_as_string(key));
85
- rb_str_cat(key_value, "=", 1);
86
- rb_str_concat(key_value, pgconn_s_quote_connstr(value));
87
- rb_ary_push(result, key_value);
88
- //return ST_CONTINUE;
89
- return 0;
87
+ result = rb_str_new(str, j);
88
+ return result;
90
89
  }
91
90
 
91
+ /*
92
+ * Appends key='hash[key]' to conninfo_rstr
93
+ */
92
94
  static void
93
- free_pgconn(ptr)
94
- PGconn *ptr;
95
- {
96
- PQfinish(ptr);
97
- }
98
-
99
- static VALUE
100
- pgconn_alloc(klass)
101
- VALUE klass;
102
- {
103
- return Data_Wrap_Struct(klass, 0, free_pgconn, NULL);
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;
104
107
  }
105
108
 
106
- //TODO broken on 1.9
107
- static PGconn *
108
- try_connectdb(arg)
109
- VALUE arg;
109
+ static void
110
+ free_pgconn(PGconn *conn)
110
111
  {
111
- VALUE conninfo;
112
-
113
- if (!NIL_P(conninfo = rb_check_string_type(arg))) {
114
- /* do nothing */
115
- }
116
- //else if (!NIL_P(conninfo = rb_check_hash_type(arg))) {
117
- // VALUE key_values = rb_ary_new2(RHASH(conninfo)->tbl->num_entries);
118
- // rb_hash_foreach(conninfo, build_key_value_string_i, key_values);
119
- // conninfo = rb_ary_join(key_values, rb_str_new2(" "));
120
- //}
121
- else {
122
- return NULL;
123
- }
124
-
125
- return PQconnectdb(StringValuePtr(conninfo));
112
+ PQfinish(conn);
126
113
  }
127
114
 
128
- static PGconn *
129
- try_setdbLogin(args)
130
- VALUE args;
115
+ static void
116
+ free_pgresult(PGresult *result)
131
117
  {
132
- VALUE temp;
133
- char *host, *port, *opt, *tty, *dbname, *login, *pwd;
134
- host=port=opt=tty=dbname=login=pwd=NULL;
135
-
136
- rb_funcall(args, rb_intern("flatten!"), 0);
137
-
138
- AssignCheckedStringValue(host, rb_ary_entry(args, 0));
139
- if (!NIL_P(temp = rb_ary_entry(args, 1)) && NUM2INT(temp) != -1) {
140
- temp = rb_obj_as_string(temp);
141
- port = StringValuePtr(temp);
142
- }
143
- AssignCheckedStringValue(opt, rb_ary_entry(args, 2));
144
- AssignCheckedStringValue(tty, rb_ary_entry(args, 3));
145
- AssignCheckedStringValue(dbname, rb_ary_entry(args, 4));
146
- AssignCheckedStringValue(login, rb_ary_entry(args, 5));
147
- AssignCheckedStringValue(pwd, rb_ary_entry(args, 6));
148
-
149
- return PQsetdbLogin(host, port, opt, tty, dbname, login, pwd);
118
+ PQclear(result);
150
119
  }
151
120
 
152
121
  static PGconn*
153
- get_pgconn(self)
154
- VALUE self;
122
+ get_pgconn(VALUE self)
155
123
  {
156
- PGconn *conn;
157
-
158
- Data_Get_Struct(self, PGconn, conn);
159
- if (conn == NULL) rb_raise(rb_ePGError, "closed connection");
160
- return conn;
124
+ PGconn *conn;
125
+ Data_Get_Struct(self, PGconn, conn);
126
+ if (conn == NULL) rb_raise(rb_eStandardError, "not connected");
127
+ return conn;
161
128
  }
162
129
 
163
130
  static PGresult*
164
- get_pgresult(self)
165
- VALUE self;
131
+ get_pgresult(VALUE self)
166
132
  {
167
133
  PGresult *result;
168
134
  Data_Get_Struct(self, PGresult, result);
169
- if (result == NULL) rb_raise(rb_ePGError, "query not performed");
135
+ if (result == NULL) rb_raise(rb_eStandardError, "result has been cleared");
170
136
  return result;
171
137
  }
172
138
 
173
- static void
174
- free_pgresult(ptr)
175
- PGresult *ptr;
176
- {
177
- PQclear(ptr);
178
- }
179
-
180
139
  static VALUE
181
- pgresult_new(ptr)
182
- PGresult *ptr;
140
+ new_pgresult(PGresult *result)
183
141
  {
184
- return Data_Wrap_Struct(rb_cPGresult, 0, free_pgresult, ptr);
142
+ return Data_Wrap_Struct(rb_cPGresult, NULL, free_pgresult, result);
185
143
  }
186
144
 
187
145
  /*
@@ -220,70 +178,52 @@ pgresult_check(VALUE rb_pgconn, VALUE rb_pgresult)
220
178
  return;
221
179
  }
222
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
+
223
191
  /********************************************************************
224
192
  *
225
193
  * Document-class: PGconn
226
194
  *
227
- * The class to access PostgreSQL database, based on libpq[http://www.postgresql.org/docs/current/interactive/libpq.html]
228
- * interface, provides convenient OO methods to query database and means for
229
- * transparent translation of query results (including PostgreSQL arrays and composite types) to
230
- * appropriate ruby class values and vice versa.
195
+ * The class to access PostgreSQL RDBMS, based on the libpq interface,
196
+ * provides convenient OO methods to interact with PostgreSQL.
231
197
  *
232
198
  * For example, to send query to the database on the localhost:
233
199
  * require 'pg'
234
- * conn = PGconn.open('dbname' => 'test1')
200
+ * conn = PGconn.open(:dbname => 'test')
235
201
  * res = conn.exec('select * from a')
236
202
  *
237
203
  * See the PGresult class for information on working with the results of a query.
238
204
  *
239
- * ------------------------
240
- * ==Functions overview
241
- *
242
- * 1. Connection Control functions:
243
- * #new (aliases: #connect , #open, #setdb, #setdblogin)
244
- * #close (alias: #finish )
245
- * #reset
246
- *
247
- * #trace
248
- * #untrace
249
- * #set_client_encoding
250
- *
251
- * 2. Connection Info Methods:
252
- * #db
253
- * #host
254
- * #user
255
- * #pass
256
- * #options
257
- * #port
258
- * #tty
259
- * #protocol_version
260
- * #server_version
261
- *
262
- * #status
263
- * #error
264
- * #transaction_status
265
- * #client_encoding
266
- *
267
- * 3. Query functions:
268
- * #exec
269
- * #query
270
- *
271
- *
272
- * #get_notify
273
- * #on_notice
274
- *
275
- * #putline
276
- * #getline
277
- * #endcopy
278
- *
279
- * classes: PGresult
280
- *
281
- * 4. Prepared statements:
282
- * #prepare
283
- * #exec_prepared
284
- *
285
205
  */
286
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
+
287
227
  /**************************************************************************
288
228
  * PGconn SINGLETON METHODS
289
229
  **************************************************************************/
@@ -294,73 +234,106 @@ pgresult_check(VALUE rb_pgconn, VALUE rb_pgresult)
294
234
  * call-seq:
295
235
  * PGconn.open(connection_hash) -> PGconn
296
236
  * PGconn.open(connection_string) -> PGconn
297
- * PGconn.open(host, port, options, tty, dbname, login, passwd) -> PGconn
298
- *
299
- * _host_:: server hostname
300
- * _port_:: server port number
301
- * _options_:: backend options (String)
302
- * _tty_:: tty to print backend debug message <i>(ignored in newer versions of PostgreSQL)</i> (String)
303
- * _dbname_:: connecting database name
304
- * _login_:: login user name
305
- * _passwd_:: login password
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)
306
256
  *
307
257
  * On failure, it raises a PGError exception.
308
258
  */
309
- #ifndef HAVE_RB_DEFINE_ALLOC_FUNC
310
- static VALUE
311
- pgconn_s_new(argc, argv, klass)
312
- int argc;
313
- VALUE *argv;
314
- VALUE klass;
315
- {
316
- VALUE self = rb_obj_alloc(klass);
317
- rb_obj_call_init(self, argc, argv);
318
- return self;
319
- }
320
- #endif
321
259
 
322
260
  static VALUE
323
- pgconn_connect(argc, argv, self)
261
+ pgconn_init(argc, argv, self)
324
262
  int argc;
325
263
  VALUE *argv;
326
264
  VALUE self;
327
265
  {
328
- VALUE args;
266
+ VALUE args,arg;
329
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;
330
274
 
331
275
  rb_scan_args(argc, argv, "0*", &args);
332
- if (RARRAY(args)->len == 1) {
333
- conn = try_connectdb(rb_ary_entry(args, 0));
334
- }
335
- if (conn == NULL) {
336
- conn = try_setdbLogin(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);
337
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
+ }
338
318
 
339
319
  if (PQstatus(conn) == CONNECTION_BAD) {
340
- VALUE message = rb_str_new2(PQerrorMessage(conn));
341
- PQfinish(conn);
342
- rb_raise(rb_ePGError, StringValuePtr(message));
320
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
321
+ rb_iv_set(error, "@connection", self);
322
+ rb_exc_raise(error);
343
323
  }
344
324
 
345
- Data_Set_Struct(self, conn);
346
- return self;
347
- }
348
-
349
- //TODO PGconn.conndefaults
350
-
351
- static VALUE
352
- pgconn_init(argc, argv, self)
353
- int argc;
354
- VALUE *argv;
355
- VALUE self;
356
- {
357
- pgconn_connect(argc, argv, self);
325
+ Check_Type(self, T_DATA);
326
+ DATA_PTR(self) = conn;
327
+
358
328
  if (rb_block_given_p()) {
359
329
  return rb_ensure(rb_yield, self, pgconn_finish, self);
360
330
  }
361
331
  return self;
362
332
  }
363
333
 
334
+ //TODO PGconn.conndefaults
335
+
336
+
364
337
  /*
365
338
  * call-seq:
366
339
  * PGconn.encrypt_password( password, username ) -> String
@@ -710,9 +683,13 @@ pgconn_exec(self, in_command)
710
683
 
711
684
  result = PQexec(conn, StringValuePtr(command));
712
685
 
713
- rb_pgresult = pgresult_new(result);
686
+ rb_pgresult = new_pgresult(result);
714
687
  pgresult_check(self, rb_pgresult);
715
688
 
689
+ if (rb_block_given_p()) {
690
+ return rb_ensure(yield_pgresult, rb_pgresult,
691
+ pgresult_clear, rb_pgresult);
692
+ }
716
693
  return rb_pgresult;
717
694
  }
718
695
 
@@ -720,9 +697,9 @@ pgconn_exec(self, in_command)
720
697
  * call-seq:
721
698
  * conn.exec_params(sql, params, result_format) -> PGresult
722
699
  *
723
- * Sends SQL query request specified by _sql_ to the PostgreSQL.
700
+ * Sends SQL query request specified by _sql_ to PostgreSQL.
724
701
  * Returns a PGresult instance on success.
725
- * On failure, it raises a PGErr or exception.
702
+ * On failure, it raises a PGError exception.
726
703
  *
727
704
  * +params+ is an array of the bind parameters for the SQL query.
728
705
  * Each element of the +params+ array may be either:
@@ -761,7 +738,6 @@ pgconn_exec_params(argc, argv, self)
761
738
  VALUE param_value_tmp;
762
739
  VALUE sym_type, sym_value, sym_format;
763
740
  int i=0;
764
-
765
741
  int nParams;
766
742
  Oid *paramTypes;
767
743
  char ** paramValues;
@@ -769,11 +745,9 @@ pgconn_exec_params(argc, argv, self)
769
745
  int *paramFormats;
770
746
  int resultFormat;
771
747
 
772
-
773
748
  rb_scan_args(argc, argv, "12", &command, &params, &in_res_fmt);
774
749
 
775
750
  Check_Type(command, T_STRING);
776
-
777
751
  if(NIL_P(params)) {
778
752
  params = rb_ary_new2(0);
779
753
  resultFormat = 0;
@@ -792,7 +766,6 @@ pgconn_exec_params(argc, argv, self)
792
766
  sym_type = ID2SYM(rb_intern("type"));
793
767
  sym_value = ID2SYM(rb_intern("value"));
794
768
  sym_format = ID2SYM(rb_intern("format"));
795
-
796
769
  nParams = RARRAY(params)->len;
797
770
  paramTypes = ALLOC_N(Oid, nParams);
798
771
  paramValues = ALLOC_N(char *, nParams);
@@ -832,11 +805,13 @@ pgconn_exec_params(argc, argv, self)
832
805
  free(paramLengths);
833
806
  free(paramFormats);
834
807
 
835
- rb_pgresult = pgresult_new(result);
808
+ rb_pgresult = new_pgresult(result);
836
809
  pgresult_check(self, rb_pgresult);
837
-
810
+ if (rb_block_given_p()) {
811
+ return rb_ensure(yield_pgresult, rb_pgresult,
812
+ pgresult_clear, rb_pgresult);
813
+ }
838
814
  return rb_pgresult;
839
-
840
815
  }
841
816
 
842
817
  /*
@@ -871,12 +846,10 @@ pgconn_prepare(argc, argv, self)
871
846
  VALUE name, command, in_paramtypes;
872
847
  VALUE param;
873
848
  int i = 0;
874
-
875
849
  int nParams = 0;
876
850
  Oid *paramTypes = NULL;
877
851
 
878
852
  rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
879
-
880
853
  Check_Type(name, T_STRING);
881
854
  Check_Type(command, T_STRING);
882
855
 
@@ -895,16 +868,14 @@ pgconn_prepare(argc, argv, self)
895
868
 
896
869
  free(paramTypes);
897
870
 
898
- rb_pgresult = pgresult_new(result);
871
+ rb_pgresult = new_pgresult(result);
899
872
  pgresult_check(self, rb_pgresult);
900
-
901
873
  return rb_pgresult;
902
-
903
874
  }
904
875
 
905
876
  /*
906
877
  * call-seq:
907
- * conn.exec_prepared(statement_name, params, result_format)
878
+ * conn.exec_prepared(statement_name, params, result_format) -> PGresult
908
879
  *
909
880
  * Execute prepared named statement specified by _statement_name_.
910
881
  * Returns a PGresult instance on success.
@@ -940,7 +911,6 @@ pgconn_exec_prepared(argc, argv, self)
940
911
  VALUE param_value_tmp;
941
912
  VALUE sym_value, sym_format;
942
913
  int i = 0;
943
-
944
914
  int nParams;
945
915
  char ** paramValues;
946
916
  int *paramLengths;
@@ -949,7 +919,6 @@ pgconn_exec_prepared(argc, argv, self)
949
919
 
950
920
 
951
921
  rb_scan_args(argc, argv, "12", &name, &params, &in_res_fmt);
952
-
953
922
  Check_Type(name, T_STRING);
954
923
 
955
924
  if(NIL_P(params)) {
@@ -969,7 +938,6 @@ pgconn_exec_prepared(argc, argv, self)
969
938
 
970
939
  sym_value = ID2SYM(rb_intern("value"));
971
940
  sym_format = ID2SYM(rb_intern("format"));
972
-
973
941
  nParams = RARRAY(params)->len;
974
942
  paramValues = ALLOC_N(char *, nParams);
975
943
  paramLengths = ALLOC_N(int, nParams);
@@ -1005,9 +973,12 @@ pgconn_exec_prepared(argc, argv, self)
1005
973
  free(paramLengths);
1006
974
  free(paramFormats);
1007
975
 
1008
- rb_pgresult = pgresult_new(result);
976
+ rb_pgresult = new_pgresult(result);
1009
977
  pgresult_check(self, rb_pgresult);
1010
-
978
+ if (rb_block_given_p()) {
979
+ return rb_ensure(yield_pgresult, rb_pgresult,
980
+ pgresult_clear, rb_pgresult);
981
+ }
1011
982
  return rb_pgresult;
1012
983
  }
1013
984
 
@@ -1034,7 +1005,7 @@ pgconn_describe_prepared(self, stmt_name)
1034
1005
  stmt = StringValuePtr(stmt_name);
1035
1006
  }
1036
1007
  result = PQdescribePrepared(conn, stmt);
1037
- rb_pgresult = pgresult_new(result);
1008
+ rb_pgresult = new_pgresult(result);
1038
1009
  pgresult_check(self, rb_pgresult);
1039
1010
  return rb_pgresult;
1040
1011
  }
@@ -1062,7 +1033,7 @@ pgconn_describe_portal(self, stmt_name)
1062
1033
  stmt = StringValuePtr(stmt_name);
1063
1034
  }
1064
1035
  result = PQdescribePortal(conn, stmt);
1065
- rb_pgresult = pgresult_new(result);
1036
+ rb_pgresult = new_pgresult(result);
1066
1037
  pgresult_check(self, rb_pgresult);
1067
1038
  return rb_pgresult;
1068
1039
  }
@@ -1081,10 +1052,11 @@ pgconn_describe_portal(self, stmt_name)
1081
1052
  * the class method uses the deprecated PQescapeString() API function.
1082
1053
  *
1083
1054
  * Returns a SQL-safe version of the String _str_.
1084
- * This is the preferred way to make strings safe for inclusion in SQL queries.
1055
+ * This is the preferred way to make strings safe for inclusion in
1056
+ * SQL queries.
1085
1057
  *
1086
- * Consider using exec_params, which avoids the need for passing values inside of
1087
- * SQL commands.
1058
+ * Consider using exec_params, which avoids the need for passing values
1059
+ * inside of SQL commands.
1088
1060
  */
1089
1061
  static VALUE
1090
1062
  pgconn_s_escape(self, string)
@@ -1099,8 +1071,8 @@ pgconn_s_escape(self, string)
1099
1071
 
1100
1072
  escaped = ALLOCA_N(char, RSTRING_LEN(string) * 2 + 1);
1101
1073
  if(CLASS_OF(self) == rb_cPGconn) {
1102
- size = PQescapeStringConn(get_pgconn(self),escaped, RSTRING_PTR(string),
1103
- RSTRING_LEN(string), &error);
1074
+ size = PQescapeStringConn(get_pgconn(self), escaped,
1075
+ RSTRING_PTR(string), RSTRING_LEN(string), &error);
1104
1076
  if(error) {
1105
1077
  rb_raise(rb_ePGError, PQerrorMessage(get_pgconn(self)));
1106
1078
  }
@@ -1115,8 +1087,8 @@ pgconn_s_escape(self, string)
1115
1087
 
1116
1088
  /*
1117
1089
  * call-seq:
1118
- * conn.escape_bytea( self ) -> String
1119
- * PGconn.escape_bytea( self ) -> String # DEPRECATED
1090
+ * conn.escape_bytea( string ) -> String
1091
+ * PGconn.escape_bytea( string ) -> String # DEPRECATED
1120
1092
  *
1121
1093
  * Connection instance method for versions of 8.1 and higher of libpq
1122
1094
  * uses PQescapeByteaConn, which is safer. Avoid calling as a class method,
@@ -1132,7 +1104,8 @@ pgconn_s_escape(self, string)
1132
1104
  * escape a byte, it is converted into the three digit octal number equal to
1133
1105
  * the octet value, and preceded by two backslashes. The single quote (') and
1134
1106
  * backslash (\) characters have special alternative escape sequences.
1135
- * #escape_bytea performs this operation, escaping only the minimally required bytes.
1107
+ * #escape_bytea performs this operation, escaping only the minimally required
1108
+ * bytes.
1136
1109
  *
1137
1110
  * Consider using exec_params, which avoids the need for passing values inside of
1138
1111
  * SQL commands.
@@ -1142,32 +1115,30 @@ pgconn_s_escape_bytea(self, str)
1142
1115
  VALUE self;
1143
1116
  VALUE str;
1144
1117
  {
1145
- char *from, *to;
1118
+ unsigned char *from, *to;
1146
1119
  size_t from_len, to_len;
1147
1120
  VALUE ret;
1148
1121
 
1149
1122
  Check_Type(str, T_STRING);
1150
- from = RSTRING_PTR(str);
1123
+ from = (unsigned char*)RSTRING_PTR(str);
1151
1124
  from_len = RSTRING_LEN(str);
1152
1125
 
1153
1126
  if(CLASS_OF(self) == rb_cPGconn) {
1154
- to = (char *)PQescapeByteaConn(get_pgconn(self),(unsigned char*)from, from_len, &to_len);
1127
+ to = PQescapeByteaConn(get_pgconn(self), from, from_len, &to_len);
1155
1128
  } else {
1156
- to = (char *)PQescapeBytea( (unsigned char*)from, from_len, &to_len);
1129
+ to = PQescapeBytea( from, from_len, &to_len);
1157
1130
  }
1158
1131
 
1159
- ret = rb_str_new(to, to_len - 1);
1132
+ ret = rb_str_new((char*)to, to_len - 1);
1160
1133
  OBJ_INFECT(ret, str);
1161
-
1162
1134
  PQfreemem(to);
1163
-
1164
1135
  return ret;
1165
1136
  }
1166
1137
 
1167
1138
 
1168
1139
  /*
1169
1140
  * call-seq:
1170
- * PGconn.unescape_bytea( self )
1141
+ * PGconn.unescape_bytea( string )
1171
1142
  *
1172
1143
  * Converts an escaped string representation of binary data into binary data --- the
1173
1144
  * reverse of #escape_bytea. This is needed when retrieving +bytea+ data in text format,
@@ -1178,19 +1149,18 @@ static VALUE
1178
1149
  pgconn_s_unescape_bytea(self, str)
1179
1150
  VALUE self, str;
1180
1151
  {
1181
- char *from, *to;
1152
+ unsigned char *from, *to;
1182
1153
  size_t to_len;
1183
1154
  VALUE ret;
1184
1155
 
1185
1156
  Check_Type(str, T_STRING);
1186
- from = StringValuePtr(str);
1157
+ from = (unsigned char*)StringValuePtr(str);
1187
1158
 
1188
- to = (char *) PQunescapeBytea( (unsigned char*) from, &to_len);
1159
+ to = PQunescapeBytea(from, &to_len);
1189
1160
 
1190
- ret = rb_str_new(to, to_len);
1161
+ ret = rb_str_new((char*)to, to_len);
1191
1162
  OBJ_INFECT(ret, str);
1192
1163
  PQfreemem(to);
1193
-
1194
1164
  return ret;
1195
1165
  }
1196
1166
 
@@ -1211,38 +1181,300 @@ pgconn_send_query(self, command)
1211
1181
  if(PQsendQuery(conn,StringValuePtr(command)) == 0) {
1212
1182
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1213
1183
  rb_iv_set(error, "@connection", self);
1214
- rb_raise(error, PQerrorMessage(conn));
1184
+ rb_exc_raise(error);
1215
1185
  }
1216
1186
  return Qnil;
1217
1187
  }
1218
1188
 
1219
1189
 
1220
- //TODO send_query_params
1190
+ /*
1191
+ * call-seq:
1192
+ * conn.send_query_params(sql, params, result_format) -> nil
1193
+ *
1194
+ * Sends SQL query request specified by _sql_ to PostgreSQL for
1195
+ * asynchronous processing, and immediately returns.
1196
+ * On failure, it raises a PGError exception.
1197
+ *
1198
+ * +params+ is an array of the bind parameters for the SQL query.
1199
+ * Each element of the +params+ array may be either:
1200
+ * a hash of the form:
1201
+ * {:value => String (value of bind parameter)
1202
+ * :type => Fixnum (oid of type of bind parameter)
1203
+ * :format => Fixnum (0 for text, 1 for binary)
1204
+ * }
1205
+ * or, it may be a String. If it is a string, that is equivalent to:
1206
+ * { :value => <string value>, :type => 0, :format => 0 }
1207
+ *
1208
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1209
+ * inside the SQL query. The 0th element of the +params+ array is bound
1210
+ * to $1, the 1st element is bound to $2, etc.
1211
+ *
1212
+ * If the types are not specified, they will be inferred by PostgreSQL.
1213
+ * Instead of specifying type oids, it's recommended to simply add
1214
+ * explicit casts in the query to ensure that the right type is used.
1215
+ *
1216
+ * For example: "SELECT $1::int"
1217
+ *
1218
+ * The optional +result_format+ should be 0 for text results, 1
1219
+ * for binary.
1220
+ */
1221
+ static VALUE
1222
+ pgconn_send_query_params(argc, argv, self)
1223
+ int argc;
1224
+ VALUE *argv;
1225
+ VALUE self;
1226
+ {
1227
+ PGconn *conn = get_pgconn(self);
1228
+ int result;
1229
+ VALUE command, params, in_res_fmt;
1230
+ VALUE param, param_type, param_value, param_format;
1231
+ VALUE param_value_tmp;
1232
+ VALUE sym_type, sym_value, sym_format;
1233
+ VALUE error;
1234
+ int i=0;
1235
+ int nParams;
1236
+ Oid *paramTypes;
1237
+ char ** paramValues;
1238
+ int *paramLengths;
1239
+ int *paramFormats;
1240
+ int resultFormat;
1241
+
1242
+ rb_scan_args(argc, argv, "12", &command, &params, &in_res_fmt);
1243
+ Check_Type(command, T_STRING);
1244
+
1245
+ if(NIL_P(params)) {
1246
+ params = rb_ary_new2(0);
1247
+ resultFormat = 0;
1248
+ }
1249
+ else {
1250
+ Check_Type(params, T_ARRAY);
1251
+ }
1221
1252
 
1222
- /*TODO
1253
+ if(NIL_P(in_res_fmt)) {
1254
+ resultFormat = 0;
1255
+ }
1256
+ else {
1257
+ resultFormat = NUM2INT(in_res_fmt);
1258
+ }
1259
+
1260
+ sym_type = ID2SYM(rb_intern("type"));
1261
+ sym_value = ID2SYM(rb_intern("value"));
1262
+ sym_format = ID2SYM(rb_intern("format"));
1263
+ nParams = RARRAY(params)->len;
1264
+ paramTypes = ALLOC_N(Oid, nParams);
1265
+ paramValues = ALLOC_N(char *, nParams);
1266
+ paramLengths = ALLOC_N(int, nParams);
1267
+ paramFormats = ALLOC_N(int, nParams);
1268
+ for(i = 0; i < nParams; i++) {
1269
+ param = rb_ary_entry(params, i);
1270
+ if (TYPE(param) == T_HASH) {
1271
+ param_type = rb_hash_aref(param, sym_type);
1272
+ param_value_tmp = rb_hash_aref(param, sym_value);
1273
+ if(TYPE(param_value_tmp) == T_STRING)
1274
+ param_value = param_value_tmp;
1275
+ else
1276
+ param_value = rb_funcall(param_value_tmp, rb_intern("to_s"), 0);
1277
+ param_format = rb_hash_aref(param, sym_format);
1278
+ }
1279
+ else {
1280
+ param_type = INT2NUM(0);
1281
+ if(TYPE(param) == T_STRING)
1282
+ param_value = param;
1283
+ else
1284
+ param_value = rb_funcall(param, rb_intern("to_s"), 0);
1285
+ param_format = INT2NUM(0);
1286
+ }
1287
+ Check_Type(param_value, T_STRING);
1288
+ paramTypes[i] = NUM2INT(param_type);
1289
+ paramValues[i] = RSTRING_PTR(param_value);
1290
+ paramLengths[i] = RSTRING_LEN(param_value) + 1;
1291
+ paramFormats[i] = NUM2INT(param_format);
1292
+ }
1293
+
1294
+ result = PQsendQueryParams(conn, StringValuePtr(command), nParams, paramTypes,
1295
+ (const char * const *)paramValues, paramLengths, paramFormats, resultFormat);
1296
+
1297
+ free(paramTypes);
1298
+ free(paramValues);
1299
+ free(paramLengths);
1300
+ free(paramFormats);
1301
+
1302
+ if(result == 0) {
1303
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1304
+ rb_iv_set(error, "@connection", self);
1305
+ rb_exc_raise(error);
1306
+ }
1307
+ return Qnil;
1308
+ }
1309
+
1310
+ /*
1223
1311
  * call-seq:
1224
- * conn.send_prepare( command ) -> nil
1312
+ * conn.send_prepare(sql, stmt_name, param_types) -> nil
1225
1313
  *
1226
- * Asynchronously send _command_ to the server. Does not block.
1227
- * Use in combination with +conn.get_result+.
1314
+ * Prepares statement _sql_ with name _name_ to be executed later.
1315
+ * Sends prepare command asynchronously, and returns immediately.
1316
+ * On failure, it raises a PGError exception.
1317
+ *
1318
+ * +param_types+ is an optional parameter to specify the Oids of the
1319
+ * types of the parameters.
1320
+ *
1321
+ * If the types are not specified, they will be inferred by PostgreSQL.
1322
+ * Instead of specifying type oids, it's recommended to simply add
1323
+ * explicit casts in the query to ensure that the right type is used.
1324
+ *
1325
+ * For example: "SELECT $1::int"
1326
+ *
1327
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1328
+ * inside the SQL query.
1228
1329
  */
1229
1330
  static VALUE
1230
- pgconn_send_prepare(self, command)
1231
- VALUE self, command;
1331
+ pgconn_send_prepare(argc, argv, self)
1332
+ int argc;
1333
+ VALUE *argv;
1334
+ VALUE self;
1232
1335
  {
1336
+ PGconn *conn = get_pgconn(self);
1337
+ int result;
1338
+ VALUE name, command, in_paramtypes;
1339
+ VALUE param;
1233
1340
  VALUE error;
1234
- PGconn *conn = get_pgconn(self);
1235
- /* returns 0 on failure */
1236
- if(PQsendQuery(conn,StringValuePtr(command)) == 0) {
1341
+ int i = 0;
1342
+ int nParams = 0;
1343
+ Oid *paramTypes = NULL;
1344
+
1345
+ rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
1346
+ Check_Type(name, T_STRING);
1347
+ Check_Type(command, T_STRING);
1348
+
1349
+ if(! NIL_P(in_paramtypes)) {
1350
+ Check_Type(in_paramtypes, T_ARRAY);
1351
+ nParams = RARRAY(in_paramtypes)->len;
1352
+ paramTypes = ALLOC_N(Oid, nParams);
1353
+ for(i = 0; i < nParams; i++) {
1354
+ param = rb_ary_entry(in_paramtypes, i);
1355
+ Check_Type(param, T_FIXNUM);
1356
+ paramTypes[i] = NUM2INT(param);
1357
+ }
1358
+ }
1359
+ result = PQsendPrepare(conn, StringValuePtr(name), StringValuePtr(command),
1360
+ nParams, paramTypes);
1361
+
1362
+ free(paramTypes);
1363
+
1364
+ if(result == 0) {
1237
1365
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1238
1366
  rb_iv_set(error, "@connection", self);
1239
- rb_raise(error, PQerrorMessage(conn));
1367
+ rb_exc_raise(error);
1240
1368
  }
1241
1369
  return Qnil;
1242
1370
  }
1243
1371
 
1372
+ /*
1373
+ * call-seq:
1374
+ * conn.send_query_prepared(statement_name, params, result_format) -> nil
1375
+ *
1376
+ * Execute prepared named statement specified by _statement_name_
1377
+ * asynchronously, and returns immediately.
1378
+ * On failure, it raises a PGError exception.
1379
+ *
1380
+ * +params+ is an array of the optional bind parameters for the
1381
+ * SQL query. Each element of the +params+ array may be either:
1382
+ * a hash of the form:
1383
+ * {:value => String (value of bind parameter)
1384
+ * :format => Fixnum (0 for text, 1 for binary)
1385
+ * }
1386
+ * or, it may be a String. If it is a string, that is equivalent to:
1387
+ * { :value => <string value>, :format => 0 }
1388
+ *
1389
+ * PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
1390
+ * inside the SQL query. The 0th element of the +params+ array is bound
1391
+ * to $1, the 1st element is bound to $2, etc.
1392
+ *
1393
+ * The optional +result_format+ should be 0 for text results, 1
1394
+ * for binary.
1395
+ */
1396
+ static VALUE
1397
+ pgconn_send_query_prepared(argc, argv, self)
1398
+ int argc;
1399
+ VALUE *argv;
1400
+ VALUE self;
1401
+ {
1402
+ PGconn *conn = get_pgconn(self);
1403
+ int result;
1404
+ VALUE name, params, in_res_fmt;
1405
+ VALUE param, param_value, param_format;
1406
+ VALUE param_value_tmp;
1407
+ VALUE sym_value, sym_format;
1408
+ VALUE error;
1409
+ int i = 0;
1410
+ int nParams;
1411
+ char ** paramValues;
1412
+ int *paramLengths;
1413
+ int *paramFormats;
1414
+ int resultFormat;
1244
1415
 
1245
- //TODO send_query_prepared
1416
+ rb_scan_args(argc, argv, "12", &name, &params, &in_res_fmt);
1417
+ Check_Type(name, T_STRING);
1418
+
1419
+ if(NIL_P(params)) {
1420
+ params = rb_ary_new2(0);
1421
+ resultFormat = 0;
1422
+ }
1423
+ else {
1424
+ Check_Type(params, T_ARRAY);
1425
+ }
1426
+
1427
+ if(NIL_P(in_res_fmt)) {
1428
+ resultFormat = 0;
1429
+ }
1430
+ else {
1431
+ resultFormat = NUM2INT(in_res_fmt);
1432
+ }
1433
+
1434
+ sym_value = ID2SYM(rb_intern("value"));
1435
+ sym_format = ID2SYM(rb_intern("format"));
1436
+ nParams = RARRAY(params)->len;
1437
+ paramValues = ALLOC_N(char *, nParams);
1438
+ paramLengths = ALLOC_N(int, nParams);
1439
+ paramFormats = ALLOC_N(int, nParams);
1440
+ for(i = 0; i < nParams; i++) {
1441
+ param = rb_ary_entry(params, i);
1442
+ if (TYPE(param) == T_HASH) {
1443
+ param_value_tmp = rb_hash_aref(param, sym_value);
1444
+ if(TYPE(param_value_tmp) == T_STRING)
1445
+ param_value = param_value_tmp;
1446
+ else
1447
+ param_value = rb_funcall(param_value_tmp, rb_intern("to_s"), 0);
1448
+ param_format = rb_hash_aref(param, sym_format);
1449
+ }
1450
+ else {
1451
+ if(TYPE(param) == T_STRING)
1452
+ param_value = param;
1453
+ else
1454
+ param_value = rb_funcall(param, rb_intern("to_s"), 0);
1455
+ param_format = INT2NUM(0);
1456
+ }
1457
+ Check_Type(param_value, T_STRING);
1458
+ paramValues[i] = RSTRING_PTR(param_value);
1459
+ paramLengths[i] = RSTRING_LEN(param_value) + 1;
1460
+ paramFormats[i] = NUM2INT(param_format);
1461
+ }
1462
+
1463
+ result = PQsendQueryPrepared(conn, StringValuePtr(name), nParams,
1464
+ (const char * const *)paramValues, paramLengths, paramFormats,
1465
+ resultFormat);
1466
+
1467
+ free(paramValues);
1468
+ free(paramLengths);
1469
+ free(paramFormats);
1470
+
1471
+ if(result == 0) {
1472
+ error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1473
+ rb_iv_set(error, "@connection", self);
1474
+ rb_exc_raise(error);
1475
+ }
1476
+ return Qnil;
1477
+ }
1246
1478
 
1247
1479
  /*
1248
1480
  * call-seq:
@@ -1261,7 +1493,7 @@ pgconn_send_describe_prepared(self, stmt_name)
1261
1493
  if(PQsendDescribePrepared(conn,StringValuePtr(stmt_name)) == 0) {
1262
1494
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1263
1495
  rb_iv_set(error, "@connection", self);
1264
- rb_raise(error, PQerrorMessage(conn));
1496
+ rb_exc_raise(error);
1265
1497
  }
1266
1498
  return Qnil;
1267
1499
  }
@@ -1284,7 +1516,7 @@ pgconn_send_describe_portal(self, portal)
1284
1516
  if(PQsendDescribePortal(conn,StringValuePtr(portal)) == 0) {
1285
1517
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1286
1518
  rb_iv_set(error, "@connection", self);
1287
- rb_raise(error, PQerrorMessage(conn));
1519
+ rb_exc_raise(error);
1288
1520
  }
1289
1521
  return Qnil;
1290
1522
  }
@@ -1308,9 +1540,12 @@ pgconn_get_result(self)
1308
1540
  if(result == NULL)
1309
1541
  return Qnil;
1310
1542
 
1311
- rb_pgresult = pgresult_new(result);
1543
+ rb_pgresult = new_pgresult(result);
1312
1544
  pgresult_check(self, rb_pgresult);
1313
-
1545
+ if (rb_block_given_p()) {
1546
+ return rb_ensure(yield_pgresult, rb_pgresult,
1547
+ pgresult_clear, rb_pgresult);
1548
+ }
1314
1549
  return rb_pgresult;
1315
1550
  }
1316
1551
 
@@ -1332,7 +1567,7 @@ pgconn_consume_input(self)
1332
1567
  if(PQconsumeInput(conn) == 0) {
1333
1568
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1334
1569
  rb_iv_set(error, "@connection", self);
1335
- rb_raise(error, PQerrorMessage(conn));
1570
+ rb_exc_raise(error);
1336
1571
  }
1337
1572
  return Qnil;
1338
1573
  }
@@ -1375,7 +1610,7 @@ pgconn_setnonblocking(self, state)
1375
1610
  if(PQsetnonblocking(conn, arg) == -1) {
1376
1611
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1377
1612
  rb_iv_set(error, "@connection", self);
1378
- rb_raise(error, PQerrorMessage(conn));
1613
+ rb_exc_raise(error);
1379
1614
  }
1380
1615
  return Qnil;
1381
1616
  }
@@ -1416,8 +1651,6 @@ pgconn_flush(self)
1416
1651
 
1417
1652
  //TODO cancel
1418
1653
 
1419
- //TODO fn
1420
-
1421
1654
  /*
1422
1655
  * call-seq:
1423
1656
  * conn.notifies()
@@ -1487,7 +1720,7 @@ pgconn_put_copy_data(self, buffer)
1487
1720
  if(ret == -1) {
1488
1721
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1489
1722
  rb_iv_set(error, "@connection", self);
1490
- rb_raise(error, PQerrorMessage(conn));
1723
+ rb_exc_raise(error);
1491
1724
  }
1492
1725
  return (ret) ? Qtrue : Qfalse;
1493
1726
  }
@@ -1527,7 +1760,7 @@ pgconn_put_copy_end(argc, argv, self)
1527
1760
  if(ret == -1) {
1528
1761
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1529
1762
  rb_iv_set(error, "@connection", self);
1530
- rb_raise(error, PQerrorMessage(conn));
1763
+ rb_exc_raise(error);
1531
1764
  }
1532
1765
  return (ret) ? Qtrue : Qfalse;
1533
1766
  }
@@ -1563,7 +1796,7 @@ pgconn_get_copy_data( argc, argv, self )
1563
1796
  if(ret == -2) { // error
1564
1797
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
1565
1798
  rb_iv_set(error, "@connection", self);
1566
- rb_raise(error, PQerrorMessage(conn));
1799
+ rb_exc_raise(error);
1567
1800
  }
1568
1801
  if(ret == -1) { // No data left
1569
1802
  return Qnil;
@@ -1581,7 +1814,7 @@ pgconn_get_copy_data( argc, argv, self )
1581
1814
  * conn.trace( port )
1582
1815
  *
1583
1816
  * Enables tracing message passing between backend.
1584
- * The trace message will be written to the _port_ selfect,
1817
+ * The trace message will be written to the _port_ object,
1585
1818
  * which is an instance of the class +File+.
1586
1819
  */
1587
1820
  static VALUE
@@ -1647,10 +1880,7 @@ pgconn_set_client_encoding(self, str)
1647
1880
  return Qnil;
1648
1881
  }
1649
1882
 
1650
- /**** TODO ?????????? ******/
1651
-
1652
-
1653
-
1883
+ /*TODO */
1654
1884
  static void
1655
1885
  notice_proxy(self, message)
1656
1886
  VALUE self;
@@ -1662,7 +1892,7 @@ notice_proxy(self, message)
1662
1892
  }
1663
1893
  }
1664
1894
 
1665
- /*
1895
+ /*TODO
1666
1896
  * call-seq:
1667
1897
  * conn.on_notice {|message| ... }
1668
1898
  *
@@ -1697,7 +1927,7 @@ pgconn_set_notice_processor(self)
1697
1927
  * call-seq:
1698
1928
  * conn.lo_creat( [mode] ) -> Fixnum
1699
1929
  *
1700
- * Creates a large selfect with mode _mode_. Returns a large selfect Oid.
1930
+ * Creates a large object with mode _mode_. Returns a large object Oid.
1701
1931
  * On failure, it raises PGError exception.
1702
1932
  */
1703
1933
  static VALUE
@@ -1727,7 +1957,7 @@ pgconn_locreat(argc, argv, self)
1727
1957
  * call-seq:
1728
1958
  * conn.lo_create( oid ) -> Fixnum
1729
1959
  *
1730
- * Creates a large selfect with oid _oid_. Returns the large selfect Oid.
1960
+ * Creates a large object with oid _oid_. Returns the large object Oid.
1731
1961
  * On failure, it raises PGError exception.
1732
1962
  */
1733
1963
  static VALUE
@@ -1749,7 +1979,7 @@ pgconn_locreate(self, in_lo_oid)
1749
1979
  * call-seq:
1750
1980
  * conn.lo_import(file) -> Fixnum
1751
1981
  *
1752
- * Import a file to a large selfect. Returns a large selfect Oid.
1982
+ * Import a file to a large object. Returns a large object Oid.
1753
1983
  *
1754
1984
  * On failure, it raises a PGError exception.
1755
1985
  */
@@ -1774,7 +2004,7 @@ pgconn_loimport(self, filename)
1774
2004
  * call-seq:
1775
2005
  * conn.lo_export( oid, file ) -> nil
1776
2006
  *
1777
- * Saves a large selfect of _oid_ to a _file_.
2007
+ * Saves a large object of _oid_ to a _file_.
1778
2008
  */
1779
2009
  static VALUE
1780
2010
  pgconn_loexport(self, lo_oid,filename)
@@ -1786,7 +2016,7 @@ pgconn_loexport(self, lo_oid,filename)
1786
2016
 
1787
2017
  oid = NUM2INT(lo_oid);
1788
2018
  if (oid < 0) {
1789
- rb_raise(rb_ePGError, "invalid large selfect oid %d",oid);
2019
+ rb_raise(rb_ePGError, "invalid large object oid %d",oid);
1790
2020
  }
1791
2021
 
1792
2022
  if (lo_export(conn, oid, StringValuePtr(filename)) < 0) {
@@ -1799,9 +2029,9 @@ pgconn_loexport(self, lo_oid,filename)
1799
2029
  * call-seq:
1800
2030
  * conn.lo_open( oid, [mode] ) -> Fixnum
1801
2031
  *
1802
- * Open a large selfect of _oid_. Returns a large selfect descriptor
2032
+ * Open a large object of _oid_. Returns a large object descriptor
1803
2033
  * instance on success. The _mode_ argument specifies the mode for
1804
- * the opened large selfect,which is either +INV_READ+, or +INV_WRITE+.
2034
+ * the opened large object,which is either +INV_READ+, or +INV_WRITE+.
1805
2035
  *
1806
2036
  * If _mode_ is omitted, the default is +INV_READ+.
1807
2037
  */
@@ -1824,7 +2054,7 @@ pgconn_loopen(argc, argv, self)
1824
2054
  mode = NUM2INT(nmode);
1825
2055
 
1826
2056
  if((fd = lo_open(conn, lo_oid, mode)) < 0) {
1827
- rb_raise(rb_ePGError, "can't open large selfect");
2057
+ rb_raise(rb_ePGError, "can't open large object");
1828
2058
  }
1829
2059
  return INT2FIX(fd);
1830
2060
  }
@@ -1833,7 +2063,7 @@ pgconn_loopen(argc, argv, self)
1833
2063
  * call-seq:
1834
2064
  * conn.lo_write( lo_desc, buffer ) -> Fixnum
1835
2065
  *
1836
- * Writes the string _buffer_ to the large selfect _lo_desc_.
2066
+ * Writes the string _buffer_ to the large object _lo_desc_.
1837
2067
  * Returns the number of bytes written.
1838
2068
  */
1839
2069
  static VALUE
@@ -1857,11 +2087,11 @@ pgconn_lowrite(self, in_lo_desc, buffer)
1857
2087
  return INT2FIX(n);
1858
2088
  }
1859
2089
 
1860
- /*TODO broken
2090
+ /*
1861
2091
  * call-seq:
1862
2092
  * conn.lo_read( lo_desc, len ) -> String
1863
2093
  *
1864
- * Attempts to read _len_ bytes from large selfect _lo_desc_,
2094
+ * Attempts to read _len_ bytes from large object _lo_desc_,
1865
2095
  * returns resulting data.
1866
2096
  */
1867
2097
  static VALUE
@@ -1872,28 +2102,37 @@ pgconn_loread(self, in_lo_desc, in_len)
1872
2102
  PGconn *conn = get_pgconn(self);
1873
2103
  int len = NUM2INT(in_len);
1874
2104
  int lo_desc = NUM2INT(in_lo_desc);
1875
- VALUE str = rb_tainted_str_new(0,len);
2105
+ VALUE str;
2106
+ char *buffer;
2107
+
2108
+ buffer = malloc(len);
2109
+ if(buffer == NULL)
2110
+ rb_raise(rb_eNoMemError, "Malloc failed!");
1876
2111
 
1877
2112
  if (len < 0){
1878
2113
  rb_raise(rb_ePGError,"nagative length %d given", len);
1879
2114
  }
1880
2115
 
1881
- if((ret = lo_read(conn, lo_desc, StringValuePtr(str), len)) < 0)
2116
+ if((ret = lo_read(conn, lo_desc, buffer, len)) < 0)
1882
2117
  rb_raise(rb_ePGError, "lo_read failed");
1883
2118
 
1884
- if (ret == 0)
2119
+ if(ret == 0) {
2120
+ free(buffer);
1885
2121
  return Qnil;
2122
+ }
2123
+
2124
+ str = rb_tainted_str_new(buffer, len);
2125
+ free(buffer);
1886
2126
 
1887
- //RSTRING_LEN(str) = ret;
1888
2127
  return str;
1889
2128
  }
1890
2129
 
1891
2130
 
1892
2131
  /*
1893
- * call-seq:
2132
+ * call-seq
1894
2133
  * conn.lo_lseek( lo_desc, offset, whence ) -> Fixnum
1895
2134
  *
1896
- * Move the large selfect pointer _lo_desc_ to offset _offset_.
2135
+ * Move the large object pointer _lo_desc_ to offset _offset_.
1897
2136
  * Valid values for _whence_ are +SEEK_SET+, +SEEK_CUR+, and +SEEK_END+.
1898
2137
  * (Or 0, 1, or 2.)
1899
2138
  */
@@ -1916,7 +2155,7 @@ pgconn_lolseek(self, in_lo_desc, offset, whence)
1916
2155
  * call-seq:
1917
2156
  * conn.lo_tell( lo_desc ) -> Fixnum
1918
2157
  *
1919
- * Returns the current position of the large selfect _lo_desc_.
2158
+ * Returns the current position of the large object _lo_desc_.
1920
2159
  */
1921
2160
  static VALUE
1922
2161
  pgconn_lotell(self,in_lo_desc)
@@ -1936,7 +2175,7 @@ pgconn_lotell(self,in_lo_desc)
1936
2175
  * call-seq:
1937
2176
  * conn.lo_truncate( lo_desc, len ) -> nil
1938
2177
  *
1939
- * Truncates the large selfect _lo_desc_ to size _len_.
2178
+ * Truncates the large object _lo_desc_ to size _len_.
1940
2179
  */
1941
2180
  static VALUE
1942
2181
  pgconn_lotruncate(self, in_lo_desc, in_len)
@@ -1956,7 +2195,7 @@ pgconn_lotruncate(self, in_lo_desc, in_len)
1956
2195
  * call-seq:
1957
2196
  * conn.lo_close( lo_desc ) -> nil
1958
2197
  *
1959
- * Closes the postgres large selfect of _lo_desc_.
2198
+ * Closes the postgres large object of _lo_desc_.
1960
2199
  */
1961
2200
  static VALUE
1962
2201
  pgconn_loclose(self, in_lo_desc)
@@ -1975,7 +2214,7 @@ pgconn_loclose(self, in_lo_desc)
1975
2214
  * call-seq:
1976
2215
  * conn.lo_unlink( oid ) -> nil
1977
2216
  *
1978
- * Unlinks (deletes) the postgres large selfect of _oid_.
2217
+ * Unlinks (deletes) the postgres large object of _oid_.
1979
2218
  */
1980
2219
  static VALUE
1981
2220
  pgconn_lounlink(self, in_oid)
@@ -2086,7 +2325,7 @@ pgresult_result_error_field(self)
2086
2325
  * call-seq:
2087
2326
  * res.clear() -> nil
2088
2327
  *
2089
- * Clears the PGresult selfect as the result of the query.
2328
+ * Clears the PGresult object as the result of the query.
2090
2329
  */
2091
2330
  static VALUE
2092
2331
  pgresult_clear(self)
@@ -2094,7 +2333,6 @@ pgresult_clear(self)
2094
2333
  {
2095
2334
  PQclear(get_pgresult(self));
2096
2335
  DATA_PTR(self) = 0;
2097
-
2098
2336
  return Qnil;
2099
2337
  }
2100
2338
 
@@ -2541,26 +2779,16 @@ pgresult_fields(self)
2541
2779
  return ary;
2542
2780
  }
2543
2781
 
2544
-
2545
-
2546
2782
  /**************************************************************************/
2547
-
2548
2783
 
2549
2784
  void
2550
2785
  Init_pg()
2551
2786
  {
2552
- pg_gsub_bang_id = rb_intern("gsub!");
2553
- //TODO pg_escape_regex = rb_reg_new("([\\t\\n\\\\])", 10, 0);
2554
- //rb_global_variable(&pg_escape_regex);
2555
- pg_escape_str = rb_str_new("\\\\\\1", 4);
2556
- rb_global_variable(&pg_escape_str);
2557
-
2558
2787
  rb_ePGError = rb_define_class("PGError", rb_eStandardError);
2559
2788
  rb_cPGconn = rb_define_class("PGconn", rb_cObject);
2560
2789
  rb_cPGresult = rb_define_class("PGresult", rb_cObject);
2561
2790
 
2562
2791
 
2563
-
2564
2792
  /*************************
2565
2793
  * PGError
2566
2794
  *************************/
@@ -2571,6 +2799,8 @@ Init_pg()
2571
2799
  /*************************
2572
2800
  * PGconn
2573
2801
  *************************/
2802
+
2803
+ /****** PGconn CLASS METHODS ******/
2574
2804
  #ifdef HAVE_RB_DEFINE_ALLOC_FUNC
2575
2805
  rb_define_alloc_func(rb_cPGconn, pgconn_alloc);
2576
2806
  #else
@@ -2588,13 +2818,11 @@ Init_pg()
2588
2818
  rb_define_singleton_method(rb_cPGconn, "isthreadsafe", pgconn_s_isthreadsafe, 0);
2589
2819
  rb_define_singleton_method(rb_cPGconn, "encrypt_password", pgconn_s_encrypt_password, 0);
2590
2820
 
2591
- /****** CONSTANTS ******/
2592
-
2593
- /* Connection Status */
2821
+ /****** PGconn CLASS CONSTANTS: Connection Status ******/
2594
2822
  rb_define_const(rb_cPGconn, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
2595
2823
  rb_define_const(rb_cPGconn, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
2596
2824
 
2597
- /* Connection Status of nonblocking connections */
2825
+ /****** PGconn CLASS CONSTANTS: Nonblocking connection status ******/
2598
2826
  rb_define_const(rb_cPGconn, "CONNECTION_STARTED", INT2FIX(CONNECTION_STARTED));
2599
2827
  rb_define_const(rb_cPGconn, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
2600
2828
  rb_define_const(rb_cPGconn, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
@@ -2602,35 +2830,32 @@ Init_pg()
2602
2830
  rb_define_const(rb_cPGconn, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
2603
2831
  rb_define_const(rb_cPGconn, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
2604
2832
 
2605
- /* Nonblocking connection polling status */
2833
+ /****** PGconn CLASS CONSTANTS: Nonblocking connection polling status ******/
2606
2834
  rb_define_const(rb_cPGconn, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
2607
2835
  rb_define_const(rb_cPGconn, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
2608
2836
  rb_define_const(rb_cPGconn, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
2609
2837
  rb_define_const(rb_cPGconn, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
2610
2838
 
2611
- /* Transaction Status */
2839
+ /****** PGconn CLASS CONSTANTS: Transaction Status ******/
2612
2840
  rb_define_const(rb_cPGconn, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
2613
2841
  rb_define_const(rb_cPGconn, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
2614
2842
  rb_define_const(rb_cPGconn, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
2615
2843
  rb_define_const(rb_cPGconn, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
2616
2844
  rb_define_const(rb_cPGconn, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
2617
2845
 
2618
- /* Large Objects */
2846
+ /****** PGconn CLASS CONSTANTS: Large Objects ******/
2619
2847
  rb_define_const(rb_cPGconn, "INV_WRITE", INT2FIX(INV_WRITE));
2620
2848
  rb_define_const(rb_cPGconn, "INV_READ", INT2FIX(INV_READ));
2621
2849
  rb_define_const(rb_cPGconn, "SEEK_SET", INT2FIX(SEEK_SET));
2622
2850
  rb_define_const(rb_cPGconn, "SEEK_CUR", INT2FIX(SEEK_CUR));
2623
2851
  rb_define_const(rb_cPGconn, "SEEK_END", INT2FIX(SEEK_END));
2624
2852
 
2625
- /****** INSTANCE METHODS ******/
2626
-
2627
- /* Connection Control */
2853
+ /****** PGconn INSTANCE METHODS: Connection Control ******/
2628
2854
  rb_define_method(rb_cPGconn, "initialize", pgconn_init, -1);
2629
- rb_define_method(rb_cPGconn, "finish", pgconn_finish, 0);
2630
- rb_define_alias(rb_cPGconn, "close", "finish");
2631
2855
  rb_define_method(rb_cPGconn, "reset", pgconn_reset, 0);
2856
+ rb_define_method(rb_cPGconn, "finish", pgconn_finish, 0);
2632
2857
 
2633
- /* Connection Status Functions */
2858
+ /****** PGconn INSTANCE METHODS: Connection Status ******/
2634
2859
  rb_define_method(rb_cPGconn, "db", pgconn_db, 0);
2635
2860
  rb_define_method(rb_cPGconn, "user", pgconn_user, 0);
2636
2861
  rb_define_method(rb_cPGconn, "pass", pgconn_pass, 0);
@@ -2649,7 +2874,7 @@ Init_pg()
2649
2874
  rb_define_method(rb_cPGconn, "connection_used_password", pgconn_connection_used_password, 0);
2650
2875
  //rb_define_method(rb_cPGconn, "getssl", pgconn_getssl, 0);
2651
2876
 
2652
- /* Command Execution Functions */
2877
+ /****** PGconn INSTANCE METHODS: Command Execution ******/
2653
2878
  rb_define_method(rb_cPGconn, "exec", pgconn_exec, 1);
2654
2879
  rb_define_method(rb_cPGconn, "exec_params", pgconn_exec_params, -1);
2655
2880
  rb_define_method(rb_cPGconn, "prepare", pgconn_prepare, -1);
@@ -2661,11 +2886,11 @@ Init_pg()
2661
2886
  rb_define_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
2662
2887
  rb_define_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
2663
2888
 
2664
- /* Asynchronous Command Processing */
2889
+ /****** PGconn INSTANCE METHODS: Asynchronous Command Processing ******/
2665
2890
  rb_define_method(rb_cPGconn, "send_query", pgconn_send_query, 0);
2666
- //rb_define_method(rb_cPGconn, "send_query_params", pgconn_send_query_params, 0);
2891
+ rb_define_method(rb_cPGconn, "send_query_params", pgconn_send_query_params, 0);
2667
2892
  rb_define_method(rb_cPGconn, "send_prepare", pgconn_send_prepare, 0);
2668
- //rb_define_method(rb_cPGconn, "send_query_prepared", pgconn_send_query_prepared, 0);
2893
+ rb_define_method(rb_cPGconn, "send_query_prepared", pgconn_send_query_prepared, 0);
2669
2894
  rb_define_method(rb_cPGconn, "send_describe_prepared", pgconn_send_describe_prepared, 0);
2670
2895
  rb_define_method(rb_cPGconn, "send_describe_portal", pgconn_send_describe_portal, 0);
2671
2896
  rb_define_method(rb_cPGconn, "get_result", pgconn_get_result, 0);
@@ -2675,36 +2900,33 @@ Init_pg()
2675
2900
  rb_define_method(rb_cPGconn, "isnonblocking", pgconn_isnonblocking, 0);
2676
2901
  rb_define_method(rb_cPGconn, "flush", pgconn_flush, 0);
2677
2902
 
2678
- /* Cancelling Queries in Progress */
2903
+ /****** PGconn INSTANCE METHODS: Cancelling Queries in Progress ******/
2679
2904
  //rb_define_method(rb_cPGconn, "get_cancel", pgconn_get_result, 0);
2680
2905
  //rb_define_method(rb_cPGconn, "free_cancel", pgconn_get_result, 0);
2681
2906
  //rb_define_method(rb_cPGconn, "cancel", pgconn_get_result, 0);
2682
2907
 
2683
- /* Fast-Path Interface */
2684
- //rb_define_method(rb_cPGconn, "fn", pgconn_fn, 0);
2685
-
2686
- /* NOTIFY */
2908
+ /****** PGconn INSTANCE METHODS: NOTIFY ******/
2687
2909
  rb_define_method(rb_cPGconn, "notifies", pgconn_notifies, 0);
2688
2910
 
2689
- /* COPY */
2911
+ /****** PGconn INSTANCE METHODS: COPY ******/
2690
2912
  rb_define_method(rb_cPGconn, "put_copy_data", pgconn_put_copy_data, 1);
2691
2913
  rb_define_method(rb_cPGconn, "put_copy_end", pgconn_put_copy_end, -1);
2692
2914
  rb_define_method(rb_cPGconn, "get_copy_data", pgconn_get_copy_data, -1);
2693
2915
 
2694
- /* Control Functions */
2916
+ /****** PGconn INSTANCE METHODS: Control Functions ******/
2695
2917
  //rb_define_method(rb_cPGconn, "set_error_verbosity", pgconn_set_error_verbosity, 0);
2696
2918
  rb_define_method(rb_cPGconn, "trace", pgconn_trace, 1);
2697
2919
  rb_define_method(rb_cPGconn, "untrace", pgconn_untrace, 0);
2698
2920
 
2699
- /* Notice Processing */
2921
+ /****** PGconn INSTANCE METHODS: Notice Processing ******/
2700
2922
  //rb_define_method(rb_cPGconn, "set_notice_receiver", pgconn_set_notice_receiver, 0);
2701
2923
  rb_define_method(rb_cPGconn, "set_notice_processor", pgconn_set_notice_processor, 0);
2702
2924
 
2703
- /* TODO Other */
2925
+ /****** PGconn INSTANCE METHODS: Other TODO ******/
2704
2926
  rb_define_method(rb_cPGconn, "client_encoding", pgconn_client_encoding, 0);
2705
2927
  rb_define_method(rb_cPGconn, "set_client_encoding", pgconn_set_client_encoding, 1);
2706
2928
 
2707
- /* Large Object support */
2929
+ /****** PGconn INSTANCE METHODS: Large Object Support ******/
2708
2930
  rb_define_method(rb_cPGconn, "lo_creat", pgconn_locreat, -1);
2709
2931
  rb_define_alias(rb_cPGconn, "locreat", "lo_creat");
2710
2932
  rb_define_method(rb_cPGconn, "lo_create", pgconn_locreate, 1);
@@ -2735,12 +2957,9 @@ Init_pg()
2735
2957
  /*************************
2736
2958
  * PGresult
2737
2959
  *************************/
2738
-
2739
2960
  rb_include_module(rb_cPGresult, rb_mEnumerable);
2740
2961
 
2741
- /****** CONSTANTS ******/
2742
-
2743
- /* result status */
2962
+ /****** PGresult CONSTANTS: result status ******/
2744
2963
  rb_define_const(rb_cPGresult, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
2745
2964
  rb_define_const(rb_cPGresult, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
2746
2965
  rb_define_const(rb_cPGresult, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
@@ -2750,7 +2969,7 @@ Init_pg()
2750
2969
  rb_define_const(rb_cPGresult, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
2751
2970
  rb_define_const(rb_cPGresult, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
2752
2971
 
2753
- /* result error field codes */
2972
+ /****** PGresult CONSTANTS: result error field codes ******/
2754
2973
  rb_define_const(rb_cPGresult, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
2755
2974
  rb_define_const(rb_cPGresult, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
2756
2975
  rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
@@ -2764,10 +2983,9 @@ Init_pg()
2764
2983
  rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
2765
2984
  rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
2766
2985
 
2767
- /****** INSTANCE METHODS: libpq ******/
2768
-
2986
+ /****** PGresult INSTANCE METHODS: libpq ******/
2769
2987
  rb_define_method(rb_cPGresult, "result_status", pgresult_result_status, 0);
2770
- rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, 0);
2988
+ rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, 1);
2771
2989
  rb_define_method(rb_cPGresult, "result_error_message", pgresult_result_error_message, 0);
2772
2990
  rb_define_method(rb_cPGresult, "result_error_field", pgresult_result_error_field, 0);
2773
2991
  rb_define_method(rb_cPGresult, "ntuples", pgresult_ntuples, 0);
@@ -2790,7 +3008,7 @@ Init_pg()
2790
3008
  rb_define_method(rb_cPGresult, "oid_value", pgresult_oid_value, 0);
2791
3009
  rb_define_method(rb_cPGresult, "clear", pgresult_clear, 0);
2792
3010
 
2793
- /****** INSTANCE METHODS: other ******/
3011
+ /****** PGresult INSTANCE METHODS: other ******/
2794
3012
  rb_define_method(rb_cPGresult, "[]", pgresult_aref, 1);
2795
3013
  rb_define_method(rb_cPGresult, "each", pgresult_each, 0);
2796
3014
  rb_define_method(rb_cPGresult, "fields", pgresult_fields, 0);