pgsql 1.2 → 1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZWE3YWVlMjYyYWFkNzg5YmMwMTUwMjZlYWE4YmE0NDE5MTQ3OWJlZQ==
5
- data.tar.gz: !binary |-
6
- ZjRhNzNhMmUwMzVkMzM1YjNhMTMwM2U3OTIxODY1MjhhOTI0OWMxZg==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- ZjNlNzJjZjBmYWZkYTc2NzM2YmZlNjY0MzA2MDQ1ZmEzY2Q2NGVlMDBlMGI4
10
- NjFlYTM0MTk4Yjk1ZjJmN2M1ODRhNGQzOGY3MWJjNGU4ZmE0NmJjYjA4NzZj
11
- ZjA2NjY3NmE2ZjcwYmEyNWU5MDQ0MTJiZmMyMTMwMmQzN2UxNmY=
12
- data.tar.gz: !binary |-
13
- NDI2NGYzYzczNzFkODcxMDM2YzAyZWU2NzFjNjBhYjk2MWNmOWVlOTAxNjJk
14
- ODIyZDUwOGYwYzUzOTY5YWZlOGI1YzQzZWQzMmY5NjgyOWFkZTYwYzFjOGRm
15
- NjVlMzA2YzAxZjMwMWI0MmFmMWYyMjYwNTA3ZTYwMzJhOGI4NTY=
2
+ SHA256:
3
+ metadata.gz: 5906425e386ca8404df02a1d14dec2ec92e730cabd03c8192f624112d537c3d2
4
+ data.tar.gz: 5bc40a7923bc2076aef6cdd8a2af71b679f01bee63fed44bbc475bf8e3a12dbb
5
+ SHA512:
6
+ metadata.gz: a32d74928d3a5c2d04dd30d4453e11a346ce7f4bb24db40665d375776b6caa89dabed59fa87774ab52fdb1d09a9d37e3e8eca13118e04a35572c33c8e827856c
7
+ data.tar.gz: c405985704ae2c8a1bb52d957965a9b6ad4811bec41fe9785b3c56dba79c463fe1569ab27426a5853bfb1c5dfea4b10ae85505f7944bf5f6cdb6a0595174b895
data/LICENSE CHANGED
@@ -1,12 +1,6 @@
1
- _
2
- _ __ __ _ ___ __ _| |
3
- | '_ \ / _` / __|/ _` | |
4
- | |_) | (_| \__ \ (_| | |
5
- | .__/ \__, |___/\__, |_|
6
- |_| |___/ |_|
1
+ = pgsql Ruby Gem
7
2
 
8
-
9
- Copyright (c) 2011-2013, Bertram Scharpf <software@bertram-scharpf.de>.
3
+ Copyright (C) 2011-2020, Bertram Scharpf <software@bertram-scharpf.de>.
10
4
  All rights reserved.
11
5
 
12
6
  Redistribution and use in source and binary forms, with or without
data/README CHANGED
@@ -1,14 +1,13 @@
1
- _
2
- _ __ __ _ ___ __ _| |
3
- | '_ \ / _` / __|/ _` | |
4
- | |_) | (_| \__ \ (_| | |
5
- | .__/ \__, |___/\__, |_|
6
- |_| |___/ |_|
7
-
8
- == Description
1
+ = pgsql Ruby Gem
9
2
 
10
3
  A PostgreSQL library that was carefully designed.
11
4
 
5
+
6
+ == Author
7
+
8
+ Bertram Scharpf <software@bertram-scharpf.de>
9
+
10
+
12
11
  == Features
13
12
 
14
13
  * Connection parameters from hash
@@ -18,28 +17,30 @@ A PostgreSQL library that was carefully designed.
18
17
  * Full PostgreSQL quoting support
19
18
  * Built-in transactions and savepoints by Ruby blocks
20
19
 
20
+
21
21
  == Example
22
22
 
23
23
  Write something like this:
24
24
 
25
- require "pgsql"
26
-
27
- Pg::Conn.open :dbname => "test1", :user => "jdoe" do |conn|
28
- conn.exec "select * from mytable;" do |result|
29
- result.each { |row|
30
- l = row.join ", "
31
- ...
32
- }
25
+ require "pgsql"
26
+
27
+ Pg::Conn.open :dbname => "test1", :user => "jdoe" do |conn|
28
+ conn.exec "SELECT * FROM mytable;" do |result|
29
+ result.each { |row|
30
+ l = row.join ", "
31
+ ...
32
+ }
33
+ end
34
+ cmd = <<-ENDSQL
35
+ SELECT * FROM mytable WHERE num=$1::INTEGER;
36
+ ENDSQL
37
+ conn.query cmd, 42 do |row|
38
+ l = row.join ", "
39
+ ...
40
+ end
41
+ ...
33
42
  end
34
- cmd = <<-ENDSQL
35
- select * from mytable where num=$1::integer;
36
- ENDSQL
37
- conn.query cmd, 42 do |row|
38
- l = row.join ", "
39
- ...
40
- end
41
- ...
42
- end
43
+
43
44
 
44
45
  == Thanks
45
46
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  require "autorake"
6
6
 
7
- c = compiler "-O2", "-fPIC"
7
+ c = compiler "-O2", "-fPIC", "-Wall"
8
8
  l = linker "-shared"
9
9
 
10
10
  rule ".o" => ".c" do |t|
data/lib/conn.c CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  #include "conn.h"
7
7
 
8
+ #include "conn_quote.h"
9
+ #include "conn_exec.h"
8
10
 
9
11
  #if defined( HAVE_HEADER_ST_H)
10
12
  #include <st.h>
@@ -29,7 +31,7 @@ static VALUE pgconn_init( int argc, VALUE *argv, VALUE self);
29
31
  static int set_connect_params( st_data_t key, st_data_t val, st_data_t args);
30
32
  static void connstr_to_hash( VALUE params, VALUE str);
31
33
  static void connstr_passwd( VALUE self, VALUE params);
32
- static VALUE connstr_getparam( VALUE yielded, VALUE params);
34
+ static VALUE connstr_getparam( RB_BLOCK_CALL_FUNC_ARGLIST( yielded, params));
33
35
 
34
36
  static VALUE pgconn_close( VALUE self);
35
37
  static VALUE pgconn_reset( VALUE self);
@@ -198,7 +200,7 @@ pgconn_alloc( VALUE cls)
198
200
  }
199
201
 
200
202
  /*
201
- * Document-method: connect
203
+ * Document-method: Pg::Conn.connect
202
204
  *
203
205
  * call-seq:
204
206
  * Pg::Conn.connect( hash) -> conn
@@ -239,7 +241,7 @@ pgconn_s_parse( VALUE cls, VALUE str)
239
241
 
240
242
 
241
243
  /*
242
- * Document-method: new
244
+ * Document-method: Pg::Conn.new
243
245
  *
244
246
  * call-seq:
245
247
  * Pg::Conn.new( hash) -> conn
@@ -391,7 +393,8 @@ connstr_passwd( VALUE self, VALUE params)
391
393
  }
392
394
  }
393
395
 
394
- VALUE connstr_getparam( VALUE yielded, VALUE params)
396
+ VALUE
397
+ connstr_getparam( RB_BLOCK_CALL_FUNC_ARGLIST( yielded, params))
395
398
  {
396
399
  return rb_hash_aref( params, yielded);
397
400
  }
@@ -454,7 +457,7 @@ pgconn_set_client_encoding( VALUE self, VALUE str)
454
457
  {
455
458
  StringValue( str);
456
459
  if ((PQsetClientEncoding( get_pgconn( self)->conn, RSTRING_PTR( str))) == -1)
457
- rb_raise( rb_ePgError, "Invalid encoding name %s", str);
460
+ rb_raise( rb_ePgError, "Invalid encoding name %s", RSTRING_PTR( str));
458
461
  return Qnil;
459
462
  }
460
463
 
@@ -491,6 +494,8 @@ pgconn_set_externalenc( VALUE self, VALUE enc)
491
494
  e = NIL_P( enc) ? rb_to_encoding( enc) : rb_default_external_encoding();
492
495
  Data_Get_Struct( self, struct pgconn_data, c);
493
496
  c->external = rb_enc_from_encoding( e);
497
+
498
+ return Qnil;
494
499
  }
495
500
 
496
501
  /*
@@ -523,6 +528,8 @@ pgconn_set_internalenc( VALUE self, VALUE enc)
523
528
  e = NIL_P( enc) ? rb_to_encoding( enc) : rb_default_internal_encoding();
524
529
  Data_Get_Struct( self, struct pgconn_data, c);
525
530
  c->internal = rb_enc_from_encoding( e);
531
+
532
+ return Qnil;
526
533
  }
527
534
 
528
535
  #endif
@@ -769,14 +776,14 @@ pgconn_untrace( VALUE self)
769
776
  * == Example
770
777
  *
771
778
  * conn.exec <<-EOT
772
- * create or replace function noise() returns void as $$
773
- * begin
774
- * raise notice 'Hi!';
775
- * end;
776
- * $$ language plpgsql;
779
+ * CREATE OR REPLACE FUNCTION noise() RETURNS VOID AS $$
780
+ * BEGIN
781
+ * RAISE NOTICE 'Hi!';
782
+ * END;
783
+ * $$ LANGUAGE plpgsql;
777
784
  * EOT
778
785
  * conn.on_notice { |e| puts e.inspect }
779
- * conn.exec "select noise();"
786
+ * conn.exec "SELECT noise();"
780
787
  */
781
788
  VALUE
782
789
  pgconn_on_notice( VALUE self)
@@ -808,23 +815,20 @@ notice_receiver( void *self, const PGresult *result)
808
815
 
809
816
 
810
817
 
811
- /********************************************************************
812
- *
818
+ /*
813
819
  * Document-class: Pg::Conn::Failed
814
820
  *
815
821
  * Error while establishing a connection to the PostgreSQL server.
816
822
  */
817
823
 
818
- /********************************************************************
819
- *
824
+ /*
820
825
  * Document-class: Pg::Conn::Invalid
821
826
  *
822
827
  * Invalid (closed) connection.
823
828
  */
824
829
 
825
830
 
826
- /********************************************************************
827
- *
831
+ /*
828
832
  * Document-class: Pg::Conn
829
833
  *
830
834
  * The class to access a PostgreSQL database.
@@ -833,7 +837,7 @@ notice_receiver( void *self, const PGresult *result)
833
837
  *
834
838
  * require "pgsql"
835
839
  * conn = Pg::Conn.open :dbname => "test1"
836
- * res = conn.exec "select * from mytable;"
840
+ * res = conn.exec "SELECT * FROM mytable;"
837
841
  *
838
842
  * See the Pg::Result class for information on working with the results of a
839
843
  * query.
@@ -842,9 +846,14 @@ notice_receiver( void *self, const PGresult *result)
842
846
  void
843
847
  Init_pgsql_conn( void)
844
848
  {
845
- #ifdef RDOC_NEEDS_THIS
846
- rb_mPg = rb_define_module( "Pg");
847
- #endif
849
+ {
850
+ ID id_require;
851
+
852
+ id_require = rb_intern( "require");
853
+ rb_funcall( Qnil, id_require, 1, rb_str_new2( "date"));
854
+ rb_funcall( Qnil, id_require, 1, rb_str_new2( "time"));
855
+ rb_funcall( Qnil, id_require, 1, rb_str_new2( "bigdecimal"));
856
+ }
848
857
 
849
858
  rb_cPgConn = rb_define_class_under( rb_mPg, "Conn", rb_cObject);
850
859
 
@@ -9,13 +9,6 @@
9
9
  #include "result.h"
10
10
 
11
11
 
12
- #ifdef HAVE_FUNC_RB_ERRINFO
13
- #define RB_ERRINFO (rb_errinfo())
14
- #else
15
- #define RB_ERRINFO ruby_errinfo
16
- #endif
17
-
18
-
19
12
  static void pg_raise_connexec( struct pgconn_data *c);
20
13
 
21
14
  static VALUE pg_statement_exec( VALUE conn, VALUE cmd, VALUE par);
@@ -37,10 +30,12 @@ static VALUE pgconn_select_values( int argc, VALUE *argv, VALUE self);
37
30
  static VALUE pgconn_get_notify( VALUE self);
38
31
 
39
32
  static VALUE pgconn_transaction( int argc, VALUE *argv, VALUE self);
40
- static VALUE rescue_transaction( VALUE self);
33
+ static VALUE rollback_transaction( VALUE self, VALUE err);
34
+ static VALUE commit_transaction( VALUE self);
41
35
  static VALUE yield_transaction( VALUE self);
42
36
  static VALUE pgconn_subtransaction( int argc, VALUE *argv, VALUE self);
43
- static VALUE rescue_subtransaction( VALUE ary);
37
+ static VALUE rollback_subtransaction( VALUE ary, VALUE err);
38
+ static VALUE release_subtransaction( VALUE ary);
44
39
  static VALUE yield_subtransaction( VALUE ary);
45
40
  static VALUE pgconn_transaction_status( VALUE self);
46
41
 
@@ -67,7 +62,7 @@ static ID id_to_a;
67
62
  void
68
63
  pg_raise_connexec( struct pgconn_data *c)
69
64
  {
70
- rb_raise( rb_ePgConnExec, PQerrorMessage( c->conn));
65
+ rb_raise( rb_ePgConnExec, "%s", PQerrorMessage( c->conn));
71
66
  }
72
67
 
73
68
 
@@ -142,7 +137,7 @@ params_to_strings( VALUE conn, VALUE params, int *len)
142
137
 
143
138
  q = pgconn_destring( c, pgconn_stringize( conn, *ptr), &n);
144
139
  a = ALLOC_N( char, n + 1);
145
- for (p = a; *p = n ? *q : '\0'; ++p, ++q, --n)
140
+ for (p = a; (*p = n ? *q : '\0'); ++p, ++q, --n)
146
141
  ;
147
142
  *v = a;
148
143
  }
@@ -164,8 +159,6 @@ free_strings( char **strs, int len)
164
159
  void
165
160
  pg_parse_parameters( int argc, VALUE *argv, VALUE *cmd, VALUE *par)
166
161
  {
167
- int len;
168
-
169
162
  rb_scan_args( argc, argv, "1*", cmd, par);
170
163
  StringValue( *cmd);
171
164
  if (RARRAY_LEN( *par) <= 0)
@@ -205,7 +198,7 @@ pgconn_exec( int argc, VALUE *argv, VALUE self)
205
198
  * Use Pg::Conn#fetch to fetch the results after you waited for data.
206
199
  *
207
200
  * Pg::Conn.connect do |conn|
208
- * conn.send "select pg_sleep(3), * from t;" do
201
+ * conn.send "SELECT pg_sleep(3), * FROM t;" do
209
202
  * ins = [ conn.socket]
210
203
  * loop do
211
204
  * r = IO.select ins, nil, nil, 0.5
@@ -242,7 +235,6 @@ pgconn_fetch( VALUE self)
242
235
  {
243
236
  struct pgconn_data *c;
244
237
  PGresult *result;
245
- VALUE res;
246
238
 
247
239
  Data_Get_Struct( self, struct pgconn_data, c);
248
240
  pg_check_conninvalid( c);
@@ -258,9 +250,6 @@ pgconn_fetch( VALUE self)
258
250
  VALUE
259
251
  yield_or_return_result( VALUE result)
260
252
  {
261
- struct pgresult_data *r;
262
-
263
- Data_Get_Struct( result, struct pgresult_data, r);
264
253
  return rb_block_given_p() ?
265
254
  rb_ensure( rb_yield, result, pgresult_clear, result) : result;
266
255
  }
@@ -460,25 +449,32 @@ pgconn_transaction( int argc, VALUE *argv, VALUE self)
460
449
  rb_raise( rb_ePgConnTrans,
461
450
  "Nested transaction block. Use Conn#subtransaction.");
462
451
  pgresult_clear( pg_statement_exec( self, cmd, Qnil));
463
- return rb_rescue( yield_transaction, self, rescue_transaction, self);
452
+ return rb_ensure( yield_transaction, self, commit_transaction, self);
453
+ }
454
+
455
+ VALUE
456
+ yield_transaction( VALUE self)
457
+ {
458
+ return rb_rescue( rb_yield, self, rollback_transaction, self);
464
459
  }
465
460
 
466
461
  VALUE
467
- rescue_transaction( VALUE self)
462
+ rollback_transaction( VALUE self, VALUE err)
468
463
  {
469
- pgresult_clear( pg_statement_exec( self, rb_str_new2( "rollback;"), Qnil));
470
- rb_exc_raise( RB_ERRINFO);
464
+ pgresult_clear( pg_statement_exec( self, rb_str_new2( "ROLLBACK;"), Qnil));
465
+ rb_exc_raise( err);
471
466
  return Qnil;
472
467
  }
473
468
 
474
469
  VALUE
475
- yield_transaction( VALUE self)
470
+ commit_transaction( VALUE self)
476
471
  {
477
- VALUE r;
472
+ struct pgconn_data *c;
478
473
 
479
- r = rb_yield( self);
480
- pgresult_clear( pg_statement_exec( self, rb_str_new2( "commit;"), Qnil));
481
- return r;
474
+ Data_Get_Struct( self, struct pgconn_data, c);
475
+ if (PQtransactionStatus( c->conn) > PQTRANS_IDLE)
476
+ pgresult_clear( pg_statement_exec( self, rb_str_new2( "COMMIT;"), Qnil));
477
+ return Qnil;
482
478
  }
483
479
 
484
480
 
@@ -514,11 +510,17 @@ pgconn_subtransaction( int argc, VALUE *argv, VALUE self)
514
510
  rb_str_buf_cat2( cmd, ";");
515
511
 
516
512
  pgresult_clear( pg_statement_exec( self, cmd, Qnil));
517
- return rb_rescue( yield_subtransaction, ya, rescue_subtransaction, ya);
513
+ return rb_ensure( yield_subtransaction, ya, release_subtransaction, ya);
514
+ }
515
+
516
+ VALUE
517
+ yield_subtransaction( VALUE ary)
518
+ {
519
+ return rb_rescue( rb_yield, ary, rollback_subtransaction, ary);
518
520
  }
519
521
 
520
522
  VALUE
521
- rescue_subtransaction( VALUE ary)
523
+ rollback_subtransaction( VALUE ary, VALUE err)
522
524
  {
523
525
  VALUE cmd;
524
526
 
@@ -526,21 +528,25 @@ rescue_subtransaction( VALUE ary)
526
528
  rb_str_buf_append( cmd, rb_ary_entry( ary, 1));
527
529
  rb_str_buf_cat2( cmd, ";");
528
530
  pgresult_clear( pg_statement_exec( rb_ary_entry( ary, 0), cmd, Qnil));
529
- rb_exc_raise( RB_ERRINFO);
531
+ rb_ary_store( ary, 1, Qnil);
532
+ rb_exc_raise( err);
530
533
  return Qnil;
531
534
  }
532
535
 
533
536
  VALUE
534
- yield_subtransaction( VALUE ary)
537
+ release_subtransaction( VALUE ary)
535
538
  {
536
- VALUE r, cmd;
537
-
538
- r = rb_yield( ary);
539
- cmd = rb_str_buf_new2( "release savepoint ");
540
- rb_str_buf_append( cmd, rb_ary_entry( ary, 1));
541
- rb_str_buf_cat2( cmd, ";");
542
- pgresult_clear( pg_statement_exec( rb_ary_entry( ary, 0), cmd, Qnil));
543
- return r;
539
+ VALUE cmd;
540
+ VALUE n;
541
+
542
+ n = rb_ary_entry( ary, 1);
543
+ if (!NIL_P( n)) {
544
+ cmd = rb_str_buf_new2( "release savepoint ");
545
+ rb_str_buf_append( cmd, n);
546
+ rb_str_buf_cat2( cmd, ";");
547
+ pgresult_clear( pg_statement_exec( rb_ary_entry( ary, 0), cmd, Qnil));
548
+ }
549
+ return Qnil;
544
550
  }
545
551
 
546
552
 
@@ -576,7 +582,7 @@ pgconn_transaction_status( VALUE self)
576
582
  * Write lines into a +COPY+ command. See +stringize_line+ for how to build
577
583
  * standard lines.
578
584
  *
579
- * conn.copy_stdin "copy t from stdin;" do
585
+ * conn.copy_stdin "COPY t FROM STDIN;" do
580
586
  * ary = ...
581
587
  * l = stringize_line ary
582
588
  * conn.put l
@@ -606,7 +612,7 @@ put_end( VALUE self)
606
612
  Data_Get_Struct( self, struct pgconn_data, c);
607
613
  /*
608
614
  * I would like to hand over something like
609
- * RSTRING_PTR( rb_obj_as_string( RB_ERRINFO))
615
+ * RSTRING_PTR( rb_obj_as_string( rb_errinfo()))
610
616
  * here but when execution is inside a rescue block
611
617
  * the error info will be non-null even though the
612
618
  * exception just has been caught.
@@ -681,7 +687,7 @@ pgconn_putline( VALUE self, VALUE arg)
681
687
  * Read lines from a +COPY+ command. The form of the lines depends
682
688
  * on the statement's parameters.
683
689
  *
684
- * conn.copy_stdout "copy t to stdout;" do
690
+ * conn.copy_stdout "COPY t TO STDOUT;" do
685
691
  * l = conn.getline
686
692
  * ary = l.split /\t/
687
693
  * ary.map! { |x|
@@ -802,7 +808,7 @@ pgconn_backup( VALUE self, VALUE label)
802
808
  {
803
809
  VALUE cmd, arg;
804
810
 
805
- cmd = rb_str_new2( "select pg_start_backup($1);");
811
+ cmd = rb_str_new2( "SELECT pg_start_backup($1);");
806
812
  arg = rb_ary_new3( 1, label);
807
813
  pgresult_clear( pg_statement_exec( self, cmd, arg));
808
814
  return rb_ensure( rb_yield, Qnil, backup_end, self);
@@ -814,31 +820,28 @@ backup_end( VALUE self)
814
820
  {
815
821
  VALUE cmd;
816
822
 
817
- cmd = rb_str_new2( "select pg_stop_backup();");
823
+ cmd = rb_str_new2( "SELECT pg_stop_backup();");
818
824
  pgresult_clear( pg_statement_exec( self, cmd, Qnil));
819
825
  return Qnil;
820
826
  }
821
827
 
822
828
 
823
829
 
824
- /********************************************************************
825
- *
830
+ /*
826
831
  * Document-class: Pg::Conn::ExecError
827
832
  *
828
833
  * Error while querying from a PostgreSQL connection.
829
834
  */
830
835
 
831
836
 
832
- /********************************************************************
833
- *
837
+ /*
834
838
  * Document-class: Pg::Conn::TransactionError
835
839
  *
836
840
  * Nested transaction blocks. Use savepoints.
837
841
  */
838
842
 
839
843
 
840
- /********************************************************************
841
- *
844
+ /*
842
845
  * Document-class: Pg::Conn::CopyError
843
846
  *
844
847
  * Nested transaction blocks. Use savepoints.
@@ -892,6 +895,6 @@ Init_pgsql_conn_exec( void)
892
895
 
893
896
  rb_define_method( rb_cPgConn, "backup", &pgconn_backup, 1);
894
897
 
895
- ID id_to_a = 0;
898
+ id_to_a = 0;
896
899
  }
897
900
 
@@ -6,7 +6,7 @@
6
6
  #include "conn_quote.h"
7
7
 
8
8
 
9
- extern VALUE pg_currency_class( void);
9
+ extern VALUE pg_monetary_class( void);
10
10
 
11
11
  static VALUE pgconn_format( VALUE self, VALUE obj);
12
12
 
@@ -23,7 +23,8 @@ extern VALUE pgconn_for_copy( VALUE self, VALUE str);
23
23
  static int needs_dquote_string( VALUE str);
24
24
  static VALUE dquote_string( VALUE str);
25
25
  static VALUE stringize_array( VALUE self, VALUE result, VALUE ary);
26
- static VALUE gsub_escape_i( VALUE c, VALUE arg);
26
+ static VALUE gsub_escape_i( RB_BLOCK_CALL_FUNC_ARGLIST( c, arg));
27
+
27
28
 
28
29
  static VALUE pgconn_quote( VALUE self, VALUE obj);
29
30
  static VALUE pgconn_quote_all( int argc, VALUE *argv, VALUE self);
@@ -34,33 +35,60 @@ static void quote_all( VALUE self, VALUE ary, VALUE res);
34
35
  static VALUE pgconn_quote_identifier( VALUE self, VALUE value);
35
36
 
36
37
 
37
-
38
38
  VALUE rb_cDate;
39
39
  VALUE rb_cDateTime;
40
- VALUE rb_cCurrency;
40
+
41
+ static VALUE rb_cMoney;
41
42
 
42
43
  static ID id_format;
43
44
  static ID id_iso8601;
44
45
  static ID id_raw;
45
46
  static ID id_to_postgres;
46
47
  static ID id_gsub;
47
- static ID id_currency;
48
+
49
+ static int lookup_monetary;
48
50
 
49
51
  static VALUE pg_string_null;
50
52
  static VALUE pg_string_bsl_N;
51
53
  static VALUE pg_escape_regex;
52
54
 
53
55
 
56
+ static const char *monetary[] = { "Money", "Monetary", "Amount", "Currency"};
54
57
 
58
+ /*
59
+ * Document-class: Pg::Money
60
+ *
61
+ * The class the PostgreSQL-builtin type +MONEY+ wil be mapped to.
62
+ * If not set, some global classes will be searched
63
+ * (+Money+, +Monetary+, +Amount+, +Currency+) and the constant will be
64
+ * set to that.
65
+ *
66
+ * Set this constant to your own class before you do the first query,
67
+ * or set it to +nil+ to inhibit the auto-detect.
68
+ *
69
+ * The resulting class must have a method +parse+ to build a new object
70
+ * from a string. It may respond to +raw+ which overwrites +to_s+.
71
+ *
72
+ */
55
73
  VALUE
56
- pg_currency_class( void)
74
+ pg_monetary_class( void)
57
75
  {
58
- if (NIL_P( rb_cCurrency) && id_currency) {
59
- if (rb_const_defined( rb_cObject, id_currency))
60
- rb_cCurrency = rb_const_get( rb_cObject, id_currency);
61
- id_currency = 0;
76
+ if (lookup_monetary) {
77
+ ID id_MONEY = rb_intern( "Money");
78
+ if (rb_const_defined( rb_mPg, id_MONEY))
79
+ rb_cMoney = rb_const_get( rb_mPg, id_MONEY);
80
+ else {
81
+ int i;
82
+ for (i = 0; i < (sizeof monetary) / sizeof (char *) && NIL_P( rb_cMoney); i++) {
83
+ ID id = rb_intern( monetary[ i]);
84
+ if (rb_const_defined( rb_cObject, id))
85
+ rb_cMoney = rb_const_get( rb_cObject, id);
86
+ }
87
+ rb_const_set( rb_mPg, id_MONEY, rb_cMoney);
88
+ }
89
+ lookup_monetary = 0;
62
90
  }
63
- return rb_cCurrency;
91
+ return rb_cMoney;
64
92
  }
65
93
 
66
94
 
@@ -80,8 +108,8 @@ pg_currency_class( void)
80
108
  * class MyConn < Pg::Conn
81
109
  * def format obj
82
110
  * case obj
83
- * when Currency then obj.to_s_by_locale
84
- * else obj
111
+ * when Money then obj.to_s_by_locale
112
+ * else obj
85
113
  * end
86
114
  * end
87
115
  * end
@@ -121,7 +149,7 @@ VALUE
121
149
  pgconn_escape_bytea( VALUE self, VALUE str)
122
150
  {
123
151
  unsigned char *s;
124
- int l;
152
+ size_t l;
125
153
  VALUE ret;
126
154
 
127
155
  if (NIL_P( str))
@@ -172,11 +200,11 @@ pgconn_unescape_bytea( VALUE self, VALUE obj)
172
200
  size_t l;
173
201
  VALUE ret;
174
202
 
175
- if (NIL_P( obj))
176
- return Qnil;
177
203
  #ifdef RUBY_ENCODING
178
204
  rb_scan_args( argc, argv, "11", &obj, &enc);
179
205
  #endif
206
+ if (NIL_P( obj))
207
+ return Qnil;
180
208
  StringValue( obj);
181
209
 
182
210
  s = PQunescapeBytea( (unsigned char *) RSTRING_PTR( obj), &l);
@@ -264,7 +292,7 @@ pgconn_stringize( VALUE self, VALUE obj)
264
292
  result = rb_obj_as_string( obj);
265
293
  else if (co == rb_cDateTime)
266
294
  result = rb_obj_as_string( obj);
267
- else if (co == pg_currency_class() &&
295
+ else if (co == pg_monetary_class() &&
268
296
  rb_respond_to( obj, id_raw))
269
297
  result = rb_funcall( obj, id_raw, 0);
270
298
  else if (rb_respond_to( obj, id_to_postgres)) {
@@ -292,7 +320,7 @@ pgconn_stringize_line( VALUE self, VALUE ary)
292
320
  VALUE a;
293
321
  VALUE *p;
294
322
  int l;
295
- VALUE ret, s;
323
+ VALUE ret;
296
324
 
297
325
  a = rb_check_convert_type( ary, T_ARRAY, "Array", "to_ary");
298
326
  if (NIL_P(a))
@@ -384,13 +412,13 @@ dquote_string( VALUE str)
384
412
  VALUE
385
413
  stringize_array( VALUE self, VALUE result, VALUE ary)
386
414
  {
387
- long i, j;
415
+ long i;
388
416
  VALUE *o;
389
417
  VALUE cf, co;
390
418
  VALUE r;
391
419
 
392
420
  cf = Qundef;
393
- for (o = RARRAY_PTR( ary), j = RARRAY_LEN( ary); j; ++o, --j) {
421
+ for (o = RARRAY_PTR( ary), i = RARRAY_LEN( ary); i; ++o, --i) {
394
422
  co = CLASS_OF( *o);
395
423
  if (cf == Qundef)
396
424
  cf = co;
@@ -411,7 +439,7 @@ stringize_array( VALUE self, VALUE result, VALUE ary)
411
439
 
412
440
 
413
441
  VALUE
414
- gsub_escape_i( VALUE c, VALUE arg)
442
+ gsub_escape_i( RB_BLOCK_CALL_FUNC_ARGLIST( c, arg))
415
443
  {
416
444
  const char *r;
417
445
 
@@ -453,7 +481,9 @@ gsub_escape_i( VALUE c, VALUE arg)
453
481
  * Call Pg::Conn#escape_bytea first if you want to tell your string is a byte
454
482
  * array and the quote that result.
455
483
  */
456
- VALUE pgconn_quote( VALUE self, VALUE obj) { VALUE o, res;
484
+ VALUE pgconn_quote( VALUE self, VALUE obj)
485
+ {
486
+ VALUE o, res;
457
487
 
458
488
  o = rb_funcall( self, id_format, 1, obj);
459
489
  if (!NIL_P( o))
@@ -494,7 +524,7 @@ VALUE pgconn_quote( VALUE self, VALUE obj) { VALUE o, res;
494
524
  } else if (co == rb_cDateTime) {
495
525
  res = rb_obj_as_string( obj);
496
526
  type = "timestamptz";
497
- } else if (co == pg_currency_class() &&
527
+ } else if (co == pg_monetary_class() &&
498
528
  rb_respond_to( obj, id_raw)) {
499
529
  res = rb_funcall( obj, id_raw, 0);
500
530
  StringValue( res);
@@ -555,12 +585,12 @@ quote_string( VALUE conn, VALUE str)
555
585
  VALUE
556
586
  quote_array( VALUE self, VALUE result, VALUE ary)
557
587
  {
558
- long i, j;
588
+ long i;
559
589
  VALUE *o;
560
590
  VALUE cf, co;
561
591
 
562
592
  cf = Qundef;
563
- for (o = RARRAY_PTR( ary), j = RARRAY_LEN( ary); j; ++o, --j) {
593
+ for (o = RARRAY_PTR( ary), i = RARRAY_LEN( ary); i; ++o, --i) {
564
594
  co = CLASS_OF( *o);
565
595
  if (cf == Qundef)
566
596
  cf = co;
@@ -622,11 +652,9 @@ pgconn_quote_identifier( VALUE self, VALUE str)
622
652
  void
623
653
  Init_pgsql_conn_quote( void)
624
654
  {
625
- rb_require( "date");
626
- rb_require( "time");
627
655
  rb_cDate = rb_const_get( rb_cObject, rb_intern( "Date"));
628
656
  rb_cDateTime = rb_const_get( rb_cObject, rb_intern( "DateTime"));
629
- rb_cCurrency = Qnil;
657
+ rb_cMoney = Qnil;
630
658
 
631
659
  #ifdef RDOC_NEEDS_THIS
632
660
  rb_cPgConn = rb_define_class_under( rb_mPg, "Conn", rb_cObject);
@@ -660,7 +688,7 @@ Init_pgsql_conn_quote( void)
660
688
  id_to_postgres = rb_intern( "to_postgres");
661
689
  id_gsub = rb_intern( "gsub");
662
690
 
663
- id_currency = rb_intern( "Currency");
691
+ lookup_monetary = 1;
664
692
 
665
693
  pg_string_null = rb_str_new2( "NULL"); rb_global_variable( &pg_string_null); rb_str_freeze( pg_string_null);
666
694
  pg_string_bsl_N = rb_str_new2( "\\N"); rb_global_variable( &pg_string_bsl_N); rb_str_freeze( pg_string_bsl_N);
@@ -10,10 +10,9 @@
10
10
 
11
11
  extern VALUE rb_cDate;
12
12
  extern VALUE rb_cDateTime;
13
- extern VALUE rb_cCurrency;
14
13
 
15
14
 
16
- extern VALUE pg_currency_class( void);
15
+ extern VALUE pg_monetary_class( void);
17
16
 
18
17
 
19
18
  extern VALUE pgconn_stringize( VALUE self, VALUE obj);
@@ -10,27 +10,19 @@ Autorake.configure {
10
10
 
11
11
  extending_ruby
12
12
 
13
- if RUBY_VERSION < "1.9" then
14
- have_header "ruby.h"
15
- have_header "rubyio.h"
16
- have_header "st.h"
17
- else
18
- have_header "ruby/ruby.h"
19
- have_header "ruby/io.h"
20
- end
13
+ need_header "ruby/ruby.h"
14
+ need_header "ruby/io.h"
15
+
21
16
 
22
17
  incdir :postgres, `pg_config --pkgincludedir`
23
18
  incdir :postgres_server, `pg_config --includedir-server`
24
19
 
25
- have_library "crypto"
26
- have_library "ssl"
27
- have_library "pq"
20
+ need_library "pq"
28
21
 
29
- have_header "postgres.h"
30
- have_header "libpq-fe.h"
31
- have_header "catalog/pg_type.h"
22
+ need_header "postgres.h"
23
+ need_header "libpq-fe.h"
24
+ need_header "catalog/pg_type.h"
32
25
 
33
- have_func "rb_errinfo"
34
26
  have_func "rb_io_stdio_file"
35
27
  have_func "rb_locale_encoding"
36
28
 
@@ -5,17 +5,17 @@
5
5
  #include "module.h"
6
6
 
7
7
  #include "conn.h"
8
+ #include "result.h"
8
9
 
9
10
 
10
- #define PGSQL_VERSION "1.2"
11
+ #define PGSQL_VERSION "1.6"
11
12
 
12
13
 
13
14
  VALUE rb_mPg;
14
15
  VALUE rb_ePgError;
15
16
 
16
17
 
17
- /********************************************************************
18
- *
18
+ /*
19
19
  * Document-module: Pg
20
20
  *
21
21
  * The module to enclose everything.
@@ -24,8 +24,7 @@ VALUE rb_ePgError;
24
24
  * connection.
25
25
  */
26
26
 
27
- /********************************************************************
28
- *
27
+ /*
29
28
  * Document-class: Pg::Error
30
29
  *
31
30
  * Generic PostgreSQL error.
@@ -18,15 +18,9 @@
18
18
  #endif
19
19
  #include "undef.h"
20
20
 
21
- #ifdef HAVE_HEADER_POSTGRES_H
22
- #include <postgres.h>
23
- #endif
24
- #ifdef HAVE_HEADER_LIBPQ_FE_H
25
- #include <libpq-fe.h>
26
- #endif
27
- #ifdef HAVE_HEADER_CATALOG_PG_TYPE_H
28
- #include <catalog/pg_type.h>
29
- #endif
21
+ #include <postgres.h>
22
+ #include <libpq-fe.h>
23
+ #include <catalog/pg_type.h>
30
24
  #include "undef.h"
31
25
 
32
26
 
@@ -11,9 +11,6 @@
11
11
  static void pgresult_init( struct pgresult_data *r, PGresult *result, struct pgconn_data *conn);
12
12
  static VALUE pgreserror_new( VALUE result, VALUE cmd, VALUE par);
13
13
 
14
- static VALUE pgreserror_command( VALUE self);
15
- static VALUE pgreserror_params( VALUE self);
16
-
17
14
  static struct pgresult_data *pgreserror_result( VALUE self);
18
15
  static VALUE pgreserror_status( VALUE self);
19
16
  static VALUE pgreserror_sqlst( VALUE self);
@@ -25,7 +22,6 @@ static VALUE pgreserror_diag( VALUE self, VALUE field);
25
22
 
26
23
  static VALUE pgresult_s_translate_results_set( VALUE cls, VALUE fact);
27
24
 
28
- static void pgresult_mark( struct pgresult_data *ptr);
29
25
  static void pgresult_free( struct pgresult_data *ptr);
30
26
  extern VALUE pgresult_new( PGresult *result, struct pgconn_data *conn, VALUE cmd, VALUE par);
31
27
 
@@ -57,8 +53,6 @@ static VALUE pgresult_cmdstatus( VALUE self);
57
53
  static VALUE pgresult_oid( VALUE self);
58
54
 
59
55
 
60
- static VALUE rb_cBigDecimal;
61
-
62
56
  static VALUE rb_cPgResult;
63
57
  static VALUE rb_ePgResError;
64
58
 
@@ -216,13 +210,6 @@ pgresult_s_translate_results_set( VALUE cls, VALUE fact)
216
210
 
217
211
 
218
212
 
219
- void
220
- pgresult_mark( struct pgresult_data *ptr)
221
- {
222
- rb_gc_mark( ptr->fields);
223
- rb_gc_mark( ptr->indices);
224
- }
225
-
226
213
  void
227
214
  pgresult_free( struct pgresult_data *ptr)
228
215
  {
@@ -280,8 +267,8 @@ pgresult_clear( VALUE self)
280
267
 
281
268
  Data_Get_Struct( self, struct pgresult_data, r);
282
269
  if (r->res != NULL) {
283
- PQclear( r->res);
284
- r->res = NULL;
270
+ PQclear( r->res);
271
+ r->res = NULL;
285
272
  }
286
273
  return Qnil;
287
274
  }
@@ -513,7 +500,7 @@ pg_fetchrow( struct pgresult_data *r, int num)
513
500
  n = PQnfields( r->res);
514
501
  if (num < PQntuples( r->res)) {
515
502
  row = rb_ary_new2( n);
516
- for (i = 0, n; n; ++i, --n)
503
+ for (i = 0; n; ++i, --n)
517
504
  rb_ary_store( row, i, pg_fetchresult( r, num, i));
518
505
  } else
519
506
  row = Qnil;
@@ -525,7 +512,7 @@ pg_fetchresult( struct pgresult_data *r, int row, int col)
525
512
  {
526
513
  char *string;
527
514
  Oid typ;
528
- VALUE ret;
515
+ VALUE cls, ret;
529
516
 
530
517
  if (PQgetisnull( r->res, row, col))
531
518
  return Qnil;
@@ -538,50 +525,59 @@ pg_fetchresult( struct pgresult_data *r, int row, int col)
538
525
  return pgconn_mkstring( r->conn, string);
539
526
 
540
527
  typ = PQftype( r->res, col);
528
+ cls = Qnil;
529
+ ret = Qnil;
541
530
  switch (typ) {
542
531
  case NUMERICOID:
543
532
  {
544
533
  int typmod;
545
534
 
546
535
  typmod = PQfmod( r->res, col);
547
- if (typmod == -1 || (typmod - VARHDRSZ) & 0xffff)
536
+ if (typmod == -1 || (typmod - VARHDRSZ) & 0xffff) {
537
+ ret = rb_funcall( Qnil, rb_intern( "BigDecimal"), 1, rb_str_new2( string));
548
538
  break;
539
+ }
549
540
  }
550
- /* if scale == 0 fall through and return inum */
541
+ /* fall through if scale == 0 */
551
542
  case INT8OID:
552
543
  case INT4OID:
553
544
  case INT2OID:
554
545
  case OIDOID:
555
- return rb_cstr_to_inum( string, 10, 0);
546
+ ret = rb_cstr_to_inum( string, 10, 0);
547
+ break;
556
548
  case FLOAT8OID:
557
549
  case FLOAT4OID:
558
- return rb_float_new( rb_cstr_to_dbl( string, Qfalse));
550
+ ret = rb_float_new( rb_cstr_to_dbl( string, Qfalse));
551
+ break;
559
552
  case BOOLOID:
560
- return *string == 't' ? Qtrue : Qfalse;
561
- /* strchr( "tTyY", *string) != NULL */
553
+ ret = strchr( "tTyY", *string) != NULL ? Qtrue : Qfalse;
554
+ break;
562
555
  case BYTEAOID:
563
- return rb_str_new2( string);
564
- default:
556
+ ret = rb_str_new2( string);
565
557
  break;
566
- }
567
- ret = pgconn_mkstring( r->conn, string);
568
- switch (typ) {
569
- case NUMERICOID:
570
- return rb_funcall( rb_cBigDecimal, id_new, 1, ret);
571
558
  case DATEOID:
572
- return rb_funcall( rb_cDate, id_parse, 1, ret);
559
+ cls = rb_cDate;
560
+ break;
573
561
  case TIMEOID:
574
562
  case TIMETZOID:
575
- return rb_funcall( rb_cTime, id_parse, 1, ret);
563
+ cls = rb_cTime;
564
+ break;
576
565
  case TIMESTAMPOID:
577
566
  case TIMESTAMPTZOID:
578
- return rb_funcall( rb_cDateTime, id_parse, 1, ret);
567
+ cls = rb_cDateTime;
568
+ break;
579
569
  case CASHOID:
580
- return RTEST( pg_currency_class()) ?
581
- rb_funcall( rb_cCurrency, id_parse, 1, ret) : ret;
570
+ cls = pg_monetary_class();
571
+ break;
582
572
  default:
583
- return ret;
573
+ break;
584
574
  }
575
+ if (NIL_P( ret)) {
576
+ ret = pgconn_mkstring( r->conn, string);
577
+ if (RTEST( cls))
578
+ ret = rb_funcall( cls, id_parse, 1, ret);
579
+ }
580
+ return ret;
585
581
  }
586
582
 
587
583
  /*
@@ -803,9 +799,6 @@ pgresult_oid( VALUE self)
803
799
  void
804
800
  Init_pgsql_result( void)
805
801
  {
806
- rb_require( "bigdecimal");
807
- rb_cBigDecimal = rb_const_get( rb_cObject, rb_intern( "BigDecimal"));
808
-
809
802
  rb_cPgResult = rb_define_class_under( rb_mPg, "Result", rb_cObject);
810
803
 
811
804
 
metadata CHANGED
@@ -1,38 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgsql
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2020-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: autorake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
- description: ! 'This is not the official PostgreSQL library that was originally written
28
- by Guy
29
-
30
- Decoux. As the project wasn''t maintained a long time after Guy''s decease, I
31
-
27
+ description: |
28
+ This is not the official PostgreSQL library that was originally written by Guy
29
+ Decoux. As the project wasn't maintained a long time after Guy's decease, I
32
30
  decided to fork my own project.
33
-
34
- '
35
- email: <software@bertram-scharpf.de>
31
+ email: "<software@bertram-scharpf.de>"
36
32
  executables: []
37
33
  extensions:
38
34
  - lib/mkrf_conf
@@ -40,47 +36,45 @@ extra_rdoc_files:
40
36
  - README
41
37
  - LICENSE
42
38
  files:
39
+ - LICENSE
40
+ - README
43
41
  - lib/Rakefile
44
- - lib/mkrf_conf
45
- - lib/undef.h
46
- - lib/module.h
47
- - lib/module.c
48
- - lib/conn.h
49
42
  - lib/conn.c
50
- - lib/conn_quote.h
51
- - lib/conn_quote.c
52
- - lib/conn_exec.h
43
+ - lib/conn.h
53
44
  - lib/conn_exec.c
54
- - lib/result.h
45
+ - lib/conn_exec.h
46
+ - lib/conn_quote.c
47
+ - lib/conn_quote.h
48
+ - lib/mkrf_conf
49
+ - lib/module.c
50
+ - lib/module.h
55
51
  - lib/result.c
56
- - README
57
- - LICENSE
52
+ - lib/result.h
53
+ - lib/undef.h
58
54
  homepage: http://www.bertram-scharpf.de/software/pgsql
59
- licenses: []
55
+ licenses:
56
+ - BSD-2-Clause
60
57
  metadata: {}
61
- post_install_message:
58
+ post_install_message:
62
59
  rdoc_options:
63
- - --charset
64
- - utf-8
65
- - --main
60
+ - "--main"
66
61
  - README
67
62
  require_paths:
68
63
  - lib
69
64
  required_ruby_version: !ruby/object:Gem::Requirement
70
65
  requirements:
71
- - - ! '>='
66
+ - - ">="
72
67
  - !ruby/object:Gem::Version
73
68
  version: '0'
74
69
  required_rubygems_version: !ruby/object:Gem::Requirement
75
70
  requirements:
76
- - - ! '>='
71
+ - - ">="
77
72
  - !ruby/object:Gem::Version
78
73
  version: '0'
79
74
  requirements:
80
75
  - PostgreSQL
81
- rubyforge_project: NONE
82
- rubygems_version: 2.0.7
83
- signing_key:
76
+ rubygems_version: 3.0.8
77
+ signing_key:
84
78
  specification_version: 4
85
79
  summary: PostgreSQL-API for Ruby
86
80
  test_files: []