pgsql 1.4 → 1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd9c6d654678f0a738b6607d7f9fb8edc562d7634a099c5d246105d24bd8c5ee
4
- data.tar.gz: d6430b794796c7d6fd068b40084d01dac66469149a5c54ac3b5b8477daaaafa3
3
+ metadata.gz: aa864a9b8413673f784d9ac667c8204642f3b3efc213afe2f40260f567db2aab
4
+ data.tar.gz: aff2d0ffa8a8db8bbf8880ef62dae6b0389a7329c3f2e0cb55153c0f9f00e60f
5
5
  SHA512:
6
- metadata.gz: 92df57a64ecd2bf7a09f28f2de2f0403b215d19883f259cf0c275fd2f8df9aa7fbdd853ef63b11ee658fd960a475b5c9352f93ab8a9a29fc4b600d84619b41d3
7
- data.tar.gz: 987b07b360cd45b8e112f12a4240b6269554758904cb2076a9574afb03448311a44da6deffd0177b18202b6f1ce2135a4ecac08ac529accbd7435ca352a37243
6
+ metadata.gz: 640b0dc2721468bd7734d21d4dcfb696719450f484d3be61a9c2aa190074487b3fd32d56a821320ef047a358287f1de272bb87507180dd38625f6ff90ecee690
7
+ data.tar.gz: 0f40813ca8581653da842afe6beb8da87f486005b22b3719af5324440504b40c4b3bfee3deace4ac55145d612835ce9966cae596c7be2170ec6bd79a4bbd672d
data/LICENSE CHANGED
@@ -1,12 +1,6 @@
1
- _
2
- _ __ __ _ ___ __ _| |
3
- | '_ \ / _` / __|/ _` | |
4
- | |_) | (_| \__ \ (_| | |
5
- | .__/ \__, |___/\__, |_|
6
- |_| |___/ |_|
1
+ = pgsql Ruby Gem
7
2
 
8
-
9
- Copyright (C) 2011-2019, 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
 
data/lib/conn.c CHANGED
@@ -200,7 +200,7 @@ pgconn_alloc( VALUE cls)
200
200
  }
201
201
 
202
202
  /*
203
- * Document-method: connect
203
+ * Document-method: Pg::Conn.connect
204
204
  *
205
205
  * call-seq:
206
206
  * Pg::Conn.connect( hash) -> conn
@@ -241,7 +241,7 @@ pgconn_s_parse( VALUE cls, VALUE str)
241
241
 
242
242
 
243
243
  /*
244
- * Document-method: new
244
+ * Document-method: Pg::Conn.new
245
245
  *
246
246
  * call-seq:
247
247
  * Pg::Conn.new( hash) -> conn
@@ -775,14 +775,14 @@ pgconn_untrace( VALUE self)
775
775
  * == Example
776
776
  *
777
777
  * conn.exec <<-EOT
778
- * create or replace function noise() returns void as $$
779
- * begin
780
- * raise notice 'Hi!';
781
- * end;
782
- * $$ language plpgsql;
778
+ * CREATE OR REPLACE FUNCTION noise() RETURNS VOID AS $$
779
+ * BEGIN
780
+ * RAISE NOTICE 'Hi!';
781
+ * END;
782
+ * $$ LANGUAGE plpgsql;
783
783
  * EOT
784
784
  * conn.on_notice { |e| puts e.inspect }
785
- * conn.exec "select noise();"
785
+ * conn.exec "SELECT noise();"
786
786
  */
787
787
  VALUE
788
788
  pgconn_on_notice( VALUE self)
@@ -814,23 +814,20 @@ notice_receiver( void *self, const PGresult *result)
814
814
 
815
815
 
816
816
 
817
- /********************************************************************
818
- *
817
+ /*
819
818
  * Document-class: Pg::Conn::Failed
820
819
  *
821
820
  * Error while establishing a connection to the PostgreSQL server.
822
821
  */
823
822
 
824
- /********************************************************************
825
- *
823
+ /*
826
824
  * Document-class: Pg::Conn::Invalid
827
825
  *
828
826
  * Invalid (closed) connection.
829
827
  */
830
828
 
831
829
 
832
- /********************************************************************
833
- *
830
+ /*
834
831
  * Document-class: Pg::Conn
835
832
  *
836
833
  * The class to access a PostgreSQL database.
@@ -839,7 +836,7 @@ notice_receiver( void *self, const PGresult *result)
839
836
  *
840
837
  * require "pgsql"
841
838
  * conn = Pg::Conn.open :dbname => "test1"
842
- * res = conn.exec "select * from mytable;"
839
+ * res = conn.exec "SELECT * FROM mytable;"
843
840
  *
844
841
  * See the Pg::Result class for information on working with the results of a
845
842
  * query.
@@ -848,10 +845,6 @@ notice_receiver( void *self, const PGresult *result)
848
845
  void
849
846
  Init_pgsql_conn( void)
850
847
  {
851
- #ifdef RDOC_NEEDS_THIS
852
- rb_mPg = rb_define_module( "Pg");
853
- #endif
854
-
855
848
  rb_cPgConn = rb_define_class_under( rb_mPg, "Conn", rb_cObject);
856
849
 
857
850
  rb_ePgConnFailed = rb_define_class_under( rb_cPgConn, "Failed", rb_ePgError);
@@ -207,7 +207,7 @@ pgconn_exec( int argc, VALUE *argv, VALUE self)
207
207
  * Use Pg::Conn#fetch to fetch the results after you waited for data.
208
208
  *
209
209
  * Pg::Conn.connect do |conn|
210
- * conn.send "select pg_sleep(3), * from t;" do
210
+ * conn.send "SELECT pg_sleep(3), * FROM t;" do
211
211
  * ins = [ conn.socket]
212
212
  * loop do
213
213
  * r = IO.select ins, nil, nil, 0.5
@@ -474,7 +474,7 @@ yield_transaction( VALUE self)
474
474
  VALUE
475
475
  rollback_transaction( VALUE self)
476
476
  {
477
- pgresult_clear( pg_statement_exec( self, rb_str_new2( "rollback;"), Qnil));
477
+ pgresult_clear( pg_statement_exec( self, rb_str_new2( "ROLLBACK;"), Qnil));
478
478
  rb_exc_raise( RB_ERRINFO);
479
479
  return Qnil;
480
480
  }
@@ -486,7 +486,7 @@ commit_transaction( VALUE self)
486
486
 
487
487
  Data_Get_Struct( self, struct pgconn_data, c);
488
488
  if (PQtransactionStatus( c->conn) > PQTRANS_IDLE)
489
- pgresult_clear( pg_statement_exec( self, rb_str_new2( "commit;"), Qnil));
489
+ pgresult_clear( pg_statement_exec( self, rb_str_new2( "COMMIT;"), Qnil));
490
490
  return Qnil;
491
491
  }
492
492
 
@@ -595,7 +595,7 @@ pgconn_transaction_status( VALUE self)
595
595
  * Write lines into a +COPY+ command. See +stringize_line+ for how to build
596
596
  * standard lines.
597
597
  *
598
- * conn.copy_stdin "copy t from stdin;" do
598
+ * conn.copy_stdin "COPY t FROM STDIN;" do
599
599
  * ary = ...
600
600
  * l = stringize_line ary
601
601
  * conn.put l
@@ -700,7 +700,7 @@ pgconn_putline( VALUE self, VALUE arg)
700
700
  * Read lines from a +COPY+ command. The form of the lines depends
701
701
  * on the statement's parameters.
702
702
  *
703
- * conn.copy_stdout "copy t to stdout;" do
703
+ * conn.copy_stdout "COPY t TO STDOUT;" do
704
704
  * l = conn.getline
705
705
  * ary = l.split /\t/
706
706
  * ary.map! { |x|
@@ -821,7 +821,7 @@ pgconn_backup( VALUE self, VALUE label)
821
821
  {
822
822
  VALUE cmd, arg;
823
823
 
824
- cmd = rb_str_new2( "select pg_start_backup($1);");
824
+ cmd = rb_str_new2( "SELECT pg_start_backup($1);");
825
825
  arg = rb_ary_new3( 1, label);
826
826
  pgresult_clear( pg_statement_exec( self, cmd, arg));
827
827
  return rb_ensure( rb_yield, Qnil, backup_end, self);
@@ -833,31 +833,28 @@ backup_end( VALUE self)
833
833
  {
834
834
  VALUE cmd;
835
835
 
836
- cmd = rb_str_new2( "select pg_stop_backup();");
836
+ cmd = rb_str_new2( "SELECT pg_stop_backup();");
837
837
  pgresult_clear( pg_statement_exec( self, cmd, Qnil));
838
838
  return Qnil;
839
839
  }
840
840
 
841
841
 
842
842
 
843
- /********************************************************************
844
- *
843
+ /*
845
844
  * Document-class: Pg::Conn::ExecError
846
845
  *
847
846
  * Error while querying from a PostgreSQL connection.
848
847
  */
849
848
 
850
849
 
851
- /********************************************************************
852
- *
850
+ /*
853
851
  * Document-class: Pg::Conn::TransactionError
854
852
  *
855
853
  * Nested transaction blocks. Use savepoints.
856
854
  */
857
855
 
858
856
 
859
- /********************************************************************
860
- *
857
+ /*
861
858
  * Document-class: Pg::Conn::CopyError
862
859
  *
863
860
  * Nested transaction blocks. Use savepoints.
@@ -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
 
@@ -37,30 +37,58 @@ static VALUE pgconn_quote_identifier( VALUE self, VALUE value);
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 (id_currency && NIL_P( rb_cCurrency)) {
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
@@ -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)) {
@@ -496,7 +524,7 @@ VALUE pgconn_quote( VALUE self, VALUE obj)
496
524
  } else if (co == rb_cDateTime) {
497
525
  res = rb_obj_as_string( obj);
498
526
  type = "timestamptz";
499
- } else if (co == pg_currency_class() &&
527
+ } else if (co == pg_monetary_class() &&
500
528
  rb_respond_to( obj, id_raw)) {
501
529
  res = rb_funcall( obj, id_raw, 0);
502
530
  StringValue( res);
@@ -628,7 +656,7 @@ Init_pgsql_conn_quote( void)
628
656
  rb_require( "time");
629
657
  rb_cDate = rb_const_get( rb_cObject, rb_intern( "Date"));
630
658
  rb_cDateTime = rb_const_get( rb_cObject, rb_intern( "DateTime"));
631
- rb_cCurrency = Qnil;
659
+ rb_cMoney = Qnil;
632
660
 
633
661
  #ifdef RDOC_NEEDS_THIS
634
662
  rb_cPgConn = rb_define_class_under( rb_mPg, "Conn", rb_cObject);
@@ -662,7 +690,7 @@ Init_pgsql_conn_quote( void)
662
690
  id_to_postgres = rb_intern( "to_postgres");
663
691
  id_gsub = rb_intern( "gsub");
664
692
 
665
- id_currency = rb_intern( "Currency");
693
+ lookup_monetary = 1;
666
694
 
667
695
  pg_string_null = rb_str_new2( "NULL"); rb_global_variable( &pg_string_null); rb_str_freeze( pg_string_null);
668
696
  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);
@@ -8,15 +8,14 @@
8
8
  #include "result.h"
9
9
 
10
10
 
11
- #define PGSQL_VERSION "1.4"
11
+ #define PGSQL_VERSION "1.5"
12
12
 
13
13
 
14
14
  VALUE rb_mPg;
15
15
  VALUE rb_ePgError;
16
16
 
17
17
 
18
- /********************************************************************
19
- *
18
+ /*
20
19
  * Document-module: Pg
21
20
  *
22
21
  * The module to enclose everything.
@@ -25,8 +24,7 @@ VALUE rb_ePgError;
25
24
  * connection.
26
25
  */
27
26
 
28
- /********************************************************************
29
- *
27
+ /*
30
28
  * Document-class: Pg::Error
31
29
  *
32
30
  * Generic PostgreSQL error.
@@ -57,8 +57,6 @@ static VALUE pgresult_cmdstatus( VALUE self);
57
57
  static VALUE pgresult_oid( VALUE self);
58
58
 
59
59
 
60
- static VALUE rb_cBigDecimal;
61
-
62
60
  static VALUE rb_cPgResult;
63
61
  static VALUE rb_ePgResError;
64
62
 
@@ -525,7 +523,7 @@ pg_fetchresult( struct pgresult_data *r, int row, int col)
525
523
  {
526
524
  char *string;
527
525
  Oid typ;
528
- VALUE ret;
526
+ VALUE cls, ret;
529
527
 
530
528
  if (PQgetisnull( r->res, row, col))
531
529
  return Qnil;
@@ -538,50 +536,59 @@ pg_fetchresult( struct pgresult_data *r, int row, int col)
538
536
  return pgconn_mkstring( r->conn, string);
539
537
 
540
538
  typ = PQftype( r->res, col);
539
+ cls = Qnil;
540
+ ret = Qnil;
541
541
  switch (typ) {
542
542
  case NUMERICOID:
543
543
  {
544
544
  int typmod;
545
545
 
546
546
  typmod = PQfmod( r->res, col);
547
- if (typmod == -1 || (typmod - VARHDRSZ) & 0xffff)
547
+ if (typmod == -1 || (typmod - VARHDRSZ) & 0xffff) {
548
+ ret = rb_funcall( Qnil, rb_intern( "BigDecimal"), 1, rb_str_new2( string));
548
549
  break;
550
+ }
549
551
  }
550
- /* if scale == 0 fall through and return inum */
552
+ /* fall through if scale == 0 */
551
553
  case INT8OID:
552
554
  case INT4OID:
553
555
  case INT2OID:
554
556
  case OIDOID:
555
- return rb_cstr_to_inum( string, 10, 0);
557
+ ret = rb_cstr_to_inum( string, 10, 0);
558
+ break;
556
559
  case FLOAT8OID:
557
560
  case FLOAT4OID:
558
- return rb_float_new( rb_cstr_to_dbl( string, Qfalse));
561
+ ret = rb_float_new( rb_cstr_to_dbl( string, Qfalse));
562
+ break;
559
563
  case BOOLOID:
560
- return *string == 't' ? Qtrue : Qfalse;
561
- /* strchr( "tTyY", *string) != NULL */
564
+ ret = strchr( "tTyY", *string) != NULL ? Qtrue : Qfalse;
565
+ break;
562
566
  case BYTEAOID:
563
- return rb_str_new2( string);
564
- default:
567
+ ret = rb_str_new2( string);
565
568
  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
569
  case DATEOID:
572
- return rb_funcall( rb_cDate, id_parse, 1, ret);
570
+ cls = rb_cDate;
571
+ break;
573
572
  case TIMEOID:
574
573
  case TIMETZOID:
575
- return rb_funcall( rb_cTime, id_parse, 1, ret);
574
+ cls = rb_cTime;
575
+ break;
576
576
  case TIMESTAMPOID:
577
577
  case TIMESTAMPTZOID:
578
- return rb_funcall( rb_cDateTime, id_parse, 1, ret);
578
+ cls = rb_cDateTime;
579
+ break;
579
580
  case CASHOID:
580
- return RTEST( pg_currency_class()) ?
581
- rb_funcall( rb_cCurrency, id_parse, 1, ret) : ret;
581
+ cls = pg_monetary_class();
582
+ break;
582
583
  default:
583
- return ret;
584
+ break;
585
+ }
586
+ if (NIL_P( ret)) {
587
+ ret = pgconn_mkstring( r->conn, string);
588
+ if (RTEST( cls))
589
+ ret = rb_funcall( cls, id_parse, 1, ret);
584
590
  }
591
+ return ret;
585
592
  }
586
593
 
587
594
  /*
@@ -804,7 +811,6 @@ void
804
811
  Init_pgsql_result( void)
805
812
  {
806
813
  rb_require( "bigdecimal");
807
- rb_cBigDecimal = rb_const_get( rb_cObject, rb_intern( "BigDecimal"));
808
814
 
809
815
  rb_cPgResult = rb_define_class_under( rb_mPg, "Result", rb_cObject);
810
816
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgsql
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.4'
4
+ version: '1.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-31 00:00:00.000000000 Z
11
+ date: 2020-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: autorake
@@ -57,8 +57,6 @@ licenses:
57
57
  metadata: {}
58
58
  post_install_message:
59
59
  rdoc_options:
60
- - "--charset"
61
- - utf-8
62
60
  - "--main"
63
61
  - README
64
62
  require_paths: