ruby-postgres 0.7.1.2005.11.24 → 0.7.1.2005.11.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Contributors +1 -1
  2. data/postgres.c +33 -15
  3. data/ruby-postgres.gemspec +1 -1
  4. data/test.rb +45 -0
  5. metadata +3 -2
data/Contributors CHANGED
@@ -23,4 +23,4 @@ shibata <kshibata@vesta.ocn.ne.jp>
23
23
  <greentea@fa2.so-net.ne.jp>
24
24
  ts <decoux@moulon.inra.fr>
25
25
  Yuta TSUBOI <yuuta-t@is.aist-nara.ac.jp>
26
- meadow.nnick@gmail.com
26
+ <meadow.nnick@gmail.com>
data/postgres.c CHANGED
@@ -75,9 +75,13 @@ static VALUE rb_ePGError;
75
75
  static VALUE rb_cPGlarge;
76
76
  static VALUE rb_cPGrow;
77
77
 
78
+ static VALUE pgconn_lastval _((VALUE));
79
+
78
80
  static VALUE pgresult_result_with_clear _((VALUE));
79
81
  static VALUE pgresult_new _((PGresult*));
80
82
 
83
+ static int translate_results = 1;
84
+
81
85
  /* Large Object support */
82
86
  typedef struct pglarge_object
83
87
  {
@@ -168,10 +172,22 @@ pgconn_connect(argc, argv, self)
168
172
  rb_raise(rb_ePGError, StringValuePtr(message));
169
173
  }
170
174
 
175
+ if (PQserverVersion(conn) >= 80100) {
176
+ rb_define_singleton_method(self, "lastval", pgconn_lastval, 0);
177
+ }
178
+
171
179
  Data_Set_Struct(self, conn);
172
180
  return self;
173
181
  }
174
182
 
183
+ static VALUE
184
+ pgconn_s_translate_results_set(self, fact)
185
+ VALUE self, fact;
186
+ {
187
+ translate_results = (fact == Qfalse || fact == Qnil) ? 0 : 1;
188
+ return Qnil;
189
+ }
190
+
175
191
  /*
176
192
  * See #new.
177
193
  */
@@ -223,7 +239,7 @@ pgconn_s_format(self, obj)
223
239
  return result;
224
240
 
225
241
  default:
226
- if (rb_class_of(obj) == rb_cBigDecimal) {
242
+ if (CLASS_OF(obj) == rb_cBigDecimal) {
227
243
  return rb_funcall(obj, rb_intern("to_s"), 1, rb_str_new2("F"));
228
244
  }
229
245
  else if (rb_block_given_p()) {
@@ -393,7 +409,6 @@ pgconn_s_unescape_bytea(self, obj)
393
409
  return ret;
394
410
  }
395
411
 
396
- #ifndef HAVE_RB_DEFINE_ALLOC_FUNC
397
412
  /*
398
413
  * call-seq:
399
414
  * PGconn.connect(host, port, options, tty, dbname, login, passwd) => conn
@@ -410,6 +425,7 @@ pgconn_s_unescape_bytea(self, obj)
410
425
  *
411
426
  * On failure, it raises a PGError exception.
412
427
  */
428
+ #ifndef HAVE_RB_DEFINE_ALLOC_FUNC
413
429
  static VALUE
414
430
  pgconn_s_new(argc, argv, klass)
415
431
  int argc;
@@ -422,9 +438,6 @@ pgconn_s_new(argc, argv, klass)
422
438
  }
423
439
  #endif
424
440
 
425
- /*
426
- * See #new.
427
- */
428
441
  static VALUE
429
442
  pgconn_init(argc, argv, self)
430
443
  int argc;
@@ -441,7 +454,7 @@ get_pgconn(obj)
441
454
  PGconn *conn;
442
455
 
443
456
  Data_Get_Struct(obj, PGconn, conn);
444
- if (conn == 0) rb_raise(rb_ePGError, "closed connection");
457
+ if (conn == NULL) rb_raise(rb_ePGError, "closed connection");
445
458
  return conn;
446
459
  }
447
460
 
@@ -1079,8 +1092,6 @@ pgconn_lastval(obj)
1079
1092
  PGresult *result;
1080
1093
  VALUE lastval, error;
1081
1094
 
1082
- if (PQserverVersion(conn) < 80100) return Qnil;
1083
-
1084
1095
  result = PQexec(conn, "select lastval()");
1085
1096
  if (!result) rb_raise(rb_ePGError, PQerrorMessage(conn));
1086
1097
 
@@ -1146,11 +1157,11 @@ free_pgresult(ptr)
1146
1157
  #define SCALE_MASK 0xffff
1147
1158
 
1148
1159
  static int
1149
- numeric_scale(typmod)
1160
+ has_numeric_scale(typmod)
1150
1161
  int typmod;
1151
1162
  {
1152
- if ((typmod -= VARHDRSZ) >= 0) return -1;
1153
- return typmod & SCALE_MASK;
1163
+ if (typmod == -1) return 1;
1164
+ return (typmod - VARHDRSZ) & SCALE_MASK;
1154
1165
  }
1155
1166
 
1156
1167
  #define PARSE(klass, string) rb_funcall(klass, rb_intern("parse"), 1, rb_tainted_str_new2(string));
@@ -1161,11 +1172,18 @@ fetch_pgresult(result, row, column)
1161
1172
  int row;
1162
1173
  int column;
1163
1174
  {
1175
+ char* string;
1176
+
1164
1177
  if (PQgetisnull(result, row, column)) {
1165
1178
  return Qnil;
1166
1179
  }
1167
1180
 
1168
- char* string = PQgetvalue(result, row, column);
1181
+ string = PQgetvalue(result, row, column);
1182
+
1183
+ if (!translate_results) {
1184
+ return rb_tainted_str_new2(string);
1185
+ }
1186
+
1169
1187
  switch (PQftype(result, column)) {
1170
1188
 
1171
1189
  case BOOLOID:
@@ -1175,7 +1193,7 @@ fetch_pgresult(result, row, column)
1175
1193
  return pgconn_s_unescape_bytea(NULL, rb_tainted_str_new2(string));
1176
1194
 
1177
1195
  case NUMERICOID:
1178
- if (numeric_scale(PQfmod(result, column)) > 0) {
1196
+ if (has_numeric_scale(PQfmod(result, column))) {
1179
1197
  return rb_funcall(rb_cBigDecimal, rb_intern("new"), 1, rb_tainted_str_new2(string));
1180
1198
  }
1181
1199
  /* when scale == 0 return inum */
@@ -1726,7 +1744,7 @@ get_pglarge(obj)
1726
1744
  {
1727
1745
  PGlarge *pglarge;
1728
1746
  Data_Get_Struct(obj, PGlarge, pglarge);
1729
- if (pglarge == 0) rb_raise(rb_ePGError, "invalid large object");
1747
+ if (pglarge == NULL) rb_raise(rb_ePGError, "invalid large object");
1730
1748
  return pglarge;
1731
1749
  }
1732
1750
 
@@ -2337,6 +2355,7 @@ Init_postgres()
2337
2355
  rb_define_singleton_method(rb_cPGconn, "quote", pgconn_s_quote, 1);
2338
2356
  rb_define_singleton_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
2339
2357
  rb_define_singleton_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
2358
+ rb_define_singleton_method(rb_cPGconn, "translate_results=", pgconn_s_translate_results_set, 1);
2340
2359
 
2341
2360
  rb_define_const(rb_cPGconn, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
2342
2361
  rb_define_const(rb_cPGconn, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
@@ -2368,7 +2387,6 @@ Init_postgres()
2368
2387
  rb_define_method(rb_cPGconn, "transaction_status", pgconn_transaction_status, 0);
2369
2388
  rb_define_method(rb_cPGconn, "protocol_version", pgconn_protocol_version, 0);
2370
2389
  rb_define_method(rb_cPGconn, "server_version", pgconn_server_version, 0);
2371
- rb_define_method(rb_cPGconn, "lastval", pgconn_lastval, 0);
2372
2390
 
2373
2391
  #ifdef HAVE_PQSETCLIENTENCODING
2374
2392
  rb_define_method(rb_cPGconn, "client_encoding", pgconn_client_encoding, 0);
@@ -14,7 +14,7 @@ SPEC = Gem::Specification.new do |s|
14
14
  s.require_path = '.'
15
15
  s.autorequire = 'postgres'
16
16
 
17
- if File.exists? 'postgres.so' and PLATFORM =~ /mingw/
17
+ if File.exists? 'postgres.so' and PLATFORM =~ /mingw|mswin/
18
18
  s.platform = Gem::Platform::WIN32
19
19
  else
20
20
  s.platform = Gem::Platform::RUBY
data/test.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'postgres'
2
+ require 'date'
3
+ require 'test/unit'
4
+ class TC_MyTest < Test::Unit::TestCase
5
+ def setup
6
+ @conn = PGconn.new('dbname' => 'template1')
7
+ end
8
+
9
+ def test_conversion
10
+ query = <<-EOT
11
+ select true as true_value,
12
+ false as false_value,
13
+ '12345\\\\111\\\\000\\\\111TEST'::bytea as bytea_value,
14
+ '2005-11-30'::date as date_value,
15
+ '12:00:00'::time as time_value,
16
+ now() as date_time_value,
17
+ 1.5::float as float_value,
18
+ 12345.5678::numeric as numeric_value,
19
+ 1234.56::numeric(10) as numeric_10_value,
20
+ 12345.12345::numeric(10,5) as numeric_10_5_value
21
+ EOT
22
+ res = @conn.exec(query)
23
+ assert_equal(res.num_tuples, 1)
24
+ assert_equal(res.num_fields, 10)
25
+ tuple = res.result[0]
26
+ puts tuple
27
+ assert_equal(true, tuple['true_value'])
28
+ assert_equal(false, tuple['false_value'])
29
+ assert_equal("12345\111\000\111TEST", tuple['bytea_value'])
30
+ puts PGconn.escape_bytea(tuple['bytea_value'])
31
+ assert_equal("12345I\\\\000ITEST", PGconn.escape_bytea(tuple['bytea_value']))
32
+ assert_equal(Date.parse('2005-11-30'), tuple['date_value'])
33
+ puts tuple['date_value']
34
+ assert_kind_of(Time, tuple['time_value'])
35
+ puts tuple['time_value']
36
+ assert_kind_of(DateTime, tuple['date_time_value'])
37
+ puts tuple['date_time_value']
38
+ assert_equal(1.5, tuple['float_value'])
39
+ puts PGconn.quote(tuple['numeric_value'])
40
+ assert_equal(BigDecimal("12345.5678"), tuple['numeric_value'])
41
+ assert_equal(1235, tuple['numeric_10_value'])
42
+ assert_kind_of(Integer, tuple['numeric_10_value'])
43
+ assert_equal(BigDecimal("12345.12345"), tuple['numeric_10_5_value'])
44
+ end
45
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ruby-postgres
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.1.2005.11.24
7
- date: 2005-11-24 00:00:00 -06:00
6
+ version: 0.7.1.2005.11.26
7
+ date: 2005-11-26 00:00:00 -06:00
8
8
  summary: Ruby extension for PostgreSQL database coordination
9
9
  require_paths:
10
10
  - .
@@ -30,6 +30,7 @@ authors:
30
30
  files:
31
31
  - sample
32
32
  - Contributors
33
+ - test.rb
33
34
  - ruby-postgres.gemspec
34
35
  - ChangeLog
35
36
  - libpq-compat.c