ruby-pg 0.7.9.2008.01.24 → 0.7.9.2008.01.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/ext/pg.c +72 -9
  2. metadata +2 -2
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: 2008-01-24 10:47:02 -0800 (Thu, 24 Jan 2008) $
12
+ $Date: 2008-01-28 11:21:07 -0800 (Mon, 28 Jan 2008) $
13
13
  ************************************************/
14
14
 
15
15
  #include "pg.h"
@@ -123,7 +123,7 @@ get_pgconn(VALUE self)
123
123
  {
124
124
  PGconn *conn;
125
125
  Data_Get_Struct(self, PGconn, conn);
126
- if (conn == NULL) rb_raise(rb_eStandardError, "not connected");
126
+ if (conn == NULL) rb_raise(rb_ePGError, "not connected");
127
127
  return conn;
128
128
  }
129
129
 
@@ -132,7 +132,7 @@ get_pgresult(VALUE self)
132
132
  {
133
133
  PGresult *result;
134
134
  Data_Get_Struct(self, PGresult, result);
135
- if (result == NULL) rb_raise(rb_eStandardError, "result has been cleared");
135
+ if (result == NULL) rb_raise(rb_ePGError, "result has been cleared");
136
136
  return result;
137
137
  }
138
138
 
@@ -1525,20 +1525,25 @@ pgconn_send_describe_portal(self, portal)
1525
1525
  * call-seq:
1526
1526
  * conn.get_result() -> PGresult
1527
1527
  *
1528
- * Asynchronously send _command_ to the server. Does not block.
1529
- * Use in combination with +conn.get_result+.
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.
1530
1535
  */
1531
1536
  static VALUE
1532
1537
  pgconn_get_result(self)
1533
1538
  VALUE self;
1534
1539
  {
1540
+ PGconn *conn = get_pgconn(self);
1535
1541
  PGresult *result;
1536
1542
  VALUE rb_pgresult;
1537
-
1538
- result = PQgetResult(get_pgconn(self));
1543
+
1544
+ result = PQgetResult(conn);
1539
1545
  if(result == NULL)
1540
1546
  return Qnil;
1541
-
1542
1547
  rb_pgresult = new_pgresult(result);
1543
1548
  pgresult_check(self, rb_pgresult);
1544
1549
  if (rb_block_given_p()) {
@@ -2041,6 +2046,53 @@ pgconn_block(int argc, VALUE *argv, VALUE self)
2041
2046
  return Qtrue;
2042
2047
  }
2043
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
+
2044
2096
  /*TODO */
2045
2097
  static void
2046
2098
  notice_proxy(self, message)
@@ -2708,7 +2760,8 @@ pgresult_fsize(self, index)
2708
2760
  * call-seq:
2709
2761
  * res.getvalue( tup_num, field_num )
2710
2762
  *
2711
- * Returns the value in tuple number _tup_num_, field _field_num_.
2763
+ * Returns the value in tuple number _tup_num_, field _field_num_,
2764
+ * or +nil+ if the field is +NULL+.
2712
2765
  */
2713
2766
  static VALUE
2714
2767
  pgresult_getvalue(self, tup_num, field_num)
@@ -2725,6 +2778,8 @@ pgresult_getvalue(self, tup_num, field_num)
2725
2778
  if(j < 0 || j >= PQnfields(result)) {
2726
2779
  rb_raise(rb_eArgError,"invalid field number %d", j);
2727
2780
  }
2781
+ if(PQgetisnull(result, i, j))
2782
+ return Qnil;
2728
2783
  return rb_str_new2(PQgetvalue(result, i, j));
2729
2784
  }
2730
2785
 
@@ -2884,6 +2939,8 @@ pgresult_aref(self, index)
2884
2939
  VALUE fname,val;
2885
2940
  VALUE tuple;
2886
2941
 
2942
+ if(tuple_num >= PQntuples(result))
2943
+ rb_raise(rb_eIndexError, "Index %d is out of range", tuple_num);
2887
2944
  tuple = rb_hash_new();
2888
2945
  for(field_num = 0; field_num < PQnfields(result); field_num++) {
2889
2946
  fname = rb_str_new2(PQfname(result,field_num));
@@ -3097,6 +3154,9 @@ Init_pg()
3097
3154
  rb_define_method(rb_cPGconn, "transaction", pgconn_transaction, 0);
3098
3155
  rb_define_method(rb_cPGconn, "block", pgconn_block, -1);
3099
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);
3100
3160
 
3101
3161
  /****** PGconn INSTANCE METHODS: Large Object Support ******/
3102
3162
  rb_define_method(rb_cPGconn, "lo_creat", pgconn_locreat, -1);
@@ -3162,7 +3222,9 @@ Init_pg()
3162
3222
  rb_define_method(rb_cPGresult, "result_error_field", pgresult_result_error_field, 0);
3163
3223
  rb_define_method(rb_cPGresult, "clear", pgresult_clear, 0);
3164
3224
  rb_define_method(rb_cPGresult, "ntuples", pgresult_ntuples, 0);
3225
+ rb_define_alias(rb_cPGresult, "num_tuples", "ntuples");
3165
3226
  rb_define_method(rb_cPGresult, "nfields", pgresult_nfields, 0);
3227
+ rb_define_alias(rb_cPGresult, "num_fields", "nfields");
3166
3228
  rb_define_method(rb_cPGresult, "fname", pgresult_fname, 1);
3167
3229
  rb_define_method(rb_cPGresult, "fnumber", pgresult_fnumber, 1);
3168
3230
  rb_define_method(rb_cPGresult, "ftable", pgresult_ftable, 1);
@@ -3178,6 +3240,7 @@ Init_pg()
3178
3240
  rb_define_method(rb_cPGresult, "paramtype", pgresult_paramtype, 0);
3179
3241
  rb_define_method(rb_cPGresult, "cmd_status", pgresult_cmd_status, 0);
3180
3242
  rb_define_method(rb_cPGresult, "cmd_tuples", pgresult_cmd_tuples, 0);
3243
+ rb_define_alias(rb_cPGresult, "cmdtuples", "cmd_tuples");
3181
3244
  rb_define_method(rb_cPGresult, "oid_value", pgresult_oid_value, 0);
3182
3245
 
3183
3246
  /****** PGresult INSTANCE METHODS: other ******/
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: ruby-pg
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.9.2008.01.24
7
- date: 2008-01-24 00:00:00 -08:00
6
+ version: 0.7.9.2008.01.28
7
+ date: 2008-01-28 00:00:00 -08:00
8
8
  summary: Ruby extension library providing an API to PostgreSQL
9
9
  require_paths:
10
10
  - lib