pg 1.5.4 → 1.6.1

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 (78) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/{History.md → CHANGELOG.md} +106 -4
  4. data/Gemfile +12 -3
  5. data/README-Windows.rdoc +1 -1
  6. data/README.ja.md +4 -4
  7. data/README.md +58 -17
  8. data/Rakefile +95 -14
  9. data/certs/kanis@comcard.de.pem +20 -0
  10. data/certs/larskanis-2024.pem +24 -0
  11. data/ext/errorcodes.def +4 -5
  12. data/ext/errorcodes.txt +2 -5
  13. data/ext/extconf.rb +161 -14
  14. data/ext/gvl_wrappers.c +13 -2
  15. data/ext/gvl_wrappers.h +33 -0
  16. data/ext/pg.c +17 -6
  17. data/ext/pg.h +9 -9
  18. data/ext/pg_binary_decoder.c +152 -0
  19. data/ext/pg_binary_encoder.c +211 -8
  20. data/ext/pg_cancel_connection.c +360 -0
  21. data/ext/pg_coder.c +54 -5
  22. data/ext/pg_connection.c +409 -167
  23. data/ext/pg_copy_coder.c +19 -15
  24. data/ext/pg_record_coder.c +7 -7
  25. data/ext/pg_result.c +11 -13
  26. data/ext/pg_text_decoder.c +4 -1
  27. data/ext/pg_text_encoder.c +37 -18
  28. data/ext/pg_tuple.c +2 -2
  29. data/ext/pg_type_map.c +1 -1
  30. data/ext/pg_type_map_all_strings.c +1 -1
  31. data/ext/pg_type_map_by_class.c +1 -1
  32. data/ext/pg_type_map_by_column.c +2 -1
  33. data/ext/pg_type_map_by_mri_type.c +1 -1
  34. data/ext/pg_type_map_by_oid.c +3 -1
  35. data/ext/pg_type_map_in_ruby.c +1 -1
  36. data/lib/pg/basic_type_map_for_queries.rb +15 -7
  37. data/lib/pg/basic_type_registry.rb +16 -4
  38. data/lib/pg/cancel_connection.rb +53 -0
  39. data/lib/pg/coder.rb +4 -2
  40. data/lib/pg/connection.rb +310 -167
  41. data/lib/pg/exceptions.rb +6 -0
  42. data/lib/pg/text_decoder/date.rb +3 -0
  43. data/lib/pg/text_decoder/json.rb +3 -0
  44. data/lib/pg/text_encoder/date.rb +1 -0
  45. data/lib/pg/text_encoder/inet.rb +3 -0
  46. data/lib/pg/text_encoder/json.rb +3 -0
  47. data/lib/pg/version.rb +1 -1
  48. data/lib/pg.rb +23 -8
  49. data/misc/yugabyte/Dockerfile +9 -0
  50. data/misc/yugabyte/docker-compose.yml +28 -0
  51. data/misc/yugabyte/pg-test.rb +45 -0
  52. data/pg.gemspec +8 -4
  53. data/ports/patches/krb5/1.21.3/0001-Allow-static-linking-krb5-library.patch +30 -0
  54. data/ports/patches/openssl/3.5.1/0001-aarch64-mingw.patch +21 -0
  55. data/ports/patches/postgresql/17.5/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +42 -0
  56. data/ports/patches/postgresql/17.5/0001-libpq-Process-buffered-SSL-read-bytes-to-support-rec.patch +52 -0
  57. data/rakelib/pg_gem_helper.rb +64 -0
  58. data.tar.gz.sig +0 -0
  59. metadata +45 -47
  60. metadata.gz.sig +0 -0
  61. data/.appveyor.yml +0 -42
  62. data/.gems +0 -6
  63. data/.gemtest +0 -0
  64. data/.github/workflows/binary-gems.yml +0 -117
  65. data/.github/workflows/source-gem.yml +0 -141
  66. data/.gitignore +0 -22
  67. data/.hgsigs +0 -34
  68. data/.hgtags +0 -41
  69. data/.irbrc +0 -23
  70. data/.pryrc +0 -23
  71. data/.tm_properties +0 -21
  72. data/.travis.yml +0 -49
  73. data/Manifest.txt +0 -72
  74. data/Rakefile.cross +0 -298
  75. data/translation/.po4a-version +0 -7
  76. data/translation/po/all.pot +0 -936
  77. data/translation/po/ja.po +0 -1036
  78. data/translation/po4a.cfg +0 -12
data/ext/extconf.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'pp'
2
2
  require 'mkmf'
3
3
 
4
-
5
4
  if ENV['MAINTAINER_MODE']
6
5
  $stderr.puts "Maintainer mode enabled."
7
6
  $CFLAGS <<
@@ -9,6 +8,8 @@ if ENV['MAINTAINER_MODE']
9
8
  ' -ggdb' <<
10
9
  ' -DDEBUG' <<
11
10
  ' -pedantic'
11
+ $LDFLAGS <<
12
+ ' -ggdb'
12
13
  end
13
14
 
14
15
  if pgdir = with_config( 'pg' )
@@ -22,12 +23,163 @@ else
22
23
  $stderr.puts "Calling libpq with GVL locked"
23
24
  end
24
25
 
25
- if enable_config("windows-cross")
26
+ if gem_platform=with_config("cross-build")
27
+ gem 'mini_portile2', '~>2.1'
28
+ require 'mini_portile2'
29
+
30
+ OPENSSL_VERSION = ENV['OPENSSL_VERSION'] || '3.5.1'
31
+ OPENSSL_SOURCE_URI = "http://www.openssl.org/source/openssl-#{OPENSSL_VERSION}.tar.gz"
32
+
33
+ KRB5_VERSION = ENV['KRB5_VERSION'] || '1.21.3'
34
+ KRB5_SOURCE_URI = "http://kerberos.org/dist/krb5/#{KRB5_VERSION[/^(\d+\.\d+)/]}/krb5-#{KRB5_VERSION}.tar.gz"
35
+
36
+ POSTGRESQL_VERSION = ENV['POSTGRESQL_VERSION'] || '17.5'
37
+ POSTGRESQL_SOURCE_URI = "http://ftp.postgresql.org/pub/source/v#{POSTGRESQL_VERSION}/postgresql-#{POSTGRESQL_VERSION}.tar.bz2"
38
+
39
+ class BuildRecipe < MiniPortile
40
+ def initialize(name, version, files)
41
+ super(name, version)
42
+ self.files = files
43
+ rootdir = File.expand_path('../..', __FILE__)
44
+ self.target = File.join(rootdir, "ports")
45
+ self.patch_files = Dir[File.join(target, "patches", self.name, self.version, "*.patch")].sort
46
+ end
47
+
48
+ def port_path
49
+ "#{target}/#{RUBY_PLATFORM}"
50
+ end
51
+
52
+ def cook_and_activate
53
+ checkpoint = File.join(self.target, "#{self.name}-#{self.version}-#{RUBY_PLATFORM}.installed")
54
+ unless File.exist?(checkpoint)
55
+ self.cook
56
+ FileUtils.touch checkpoint
57
+ end
58
+ self.activate
59
+ self
60
+ end
61
+ end
62
+
63
+ openssl_platform = with_config("openssl-platform")
64
+ toolchain = with_config("toolchain")
65
+
66
+ openssl_recipe = BuildRecipe.new("openssl", OPENSSL_VERSION, [OPENSSL_SOURCE_URI]).tap do |recipe|
67
+ class << recipe
68
+ attr_accessor :openssl_platform
69
+ def configure
70
+ envs = []
71
+ envs << "CFLAGS=-DDSO_WIN32 -DOPENSSL_THREADS" if RUBY_PLATFORM =~ /mingw|mswin/
72
+ envs << "CFLAGS=-fPIC -DOPENSSL_THREADS" if RUBY_PLATFORM =~ /linux|darwin/
73
+ execute('configure', ['env', *envs, "./Configure", openssl_platform, "threads", "-static", "CROSS_COMPILE=#{host}-", configure_prefix], altlog: "config.log")
74
+ end
75
+ def compile
76
+ execute('compile', "#{make_cmd} build_libs")
77
+ end
78
+ def install
79
+ execute('install', "#{make_cmd} install_dev")
80
+ end
81
+ end
82
+
83
+ recipe.openssl_platform = openssl_platform
84
+ recipe.host = toolchain
85
+ recipe.cook_and_activate
86
+ end
87
+
88
+ if RUBY_PLATFORM =~ /linux|darwin/
89
+ krb5_recipe = BuildRecipe.new("krb5", KRB5_VERSION, [KRB5_SOURCE_URI]).tap do |recipe|
90
+ class << recipe
91
+ def work_path
92
+ File.join(super, "src")
93
+ end
94
+ def configure
95
+ if RUBY_PLATFORM=~/darwin/
96
+ ENV["CC"] = host[/^.*[^\.\d]/] + "-clang"
97
+ ENV["CXX"] = host[/^.*[^\.\d]/] + "-c++"
98
+
99
+ # Manually set the correct values for configure checks that libkrb5 won't be
100
+ # able to perform because we're cross-compiling.
101
+ ENV["krb5_cv_attr_constructor_destructor"] = "yes"
102
+ ENV["ac_cv_func_regcomp"] = "yes"
103
+ ENV["ac_cv_printf_positional"] = "yes"
104
+ end
105
+ super
106
+ end
107
+ end
108
+ # We specify -fcommon to get around duplicate definition errors in recent gcc.
109
+ # See https://github.com/cockroachdb/cockroach/issues/49734
110
+ recipe.configure_options << "CFLAGS=-fcommon#{" -fPIC" if RUBY_PLATFORM =~ /linux/}"
111
+ recipe.configure_options << "LDFLAGS=-framework Kerberos" if RUBY_PLATFORM =~ /darwin/
112
+ recipe.configure_options << "--without-keyutils"
113
+ recipe.configure_options << "--disable-nls"
114
+ recipe.configure_options << "--disable-silent-rules"
115
+ recipe.configure_options << "--without-system-verto"
116
+ recipe.configure_options << "krb5_cv_attr_constructor_destructor=yes"
117
+ recipe.configure_options << "ac_cv_func_regcomp=yes"
118
+ recipe.configure_options << "ac_cv_printf_positional=yes"
119
+ recipe.host = toolchain
120
+ recipe.cook_and_activate
121
+ end
122
+ end
123
+
124
+ # We build a libpq library file which static links OpenSSL and krb5.
125
+ # Our builtin libpq is referenced in different ways depending on the OS:
126
+ # - Window: Add the ports directory at runtime per RubyInstaller::Runtime.add_dll_directory
127
+ # The file is called "libpq.dll"
128
+ # - Linux: Add a rpath to pg_ext.so which references the ports directory.
129
+ # The file is called "libpq-ruby-pg.so.1" to avoid loading of system libpq by accident.
130
+ # - Macos: Add a reference with relative path in pg_ext.so to the ports directory.
131
+ # The file is called "libpq-ruby-pg.1.dylib" to avoid loading of other libpq by accident.
132
+ libpq_orig, libpq_rubypg = case RUBY_PLATFORM
133
+ when /linux/ then ["libpq.so.5", "libpq-ruby-pg.so.1"]
134
+ when /darwin/ then ["libpq.5.dylib", "libpq-ruby-pg.1.dylib"]
135
+ # when /mingw/ then ["libpq.dll", "libpq.dll"] # renaming not needed
136
+ end
137
+
138
+ postgresql_recipe = BuildRecipe.new("postgresql", POSTGRESQL_VERSION, [POSTGRESQL_SOURCE_URI]).tap do |recipe|
139
+ class << recipe
140
+ def configure_defaults
141
+ [
142
+ "--target=#{host}",
143
+ "--host=#{host}",
144
+ '--with-openssl',
145
+ *(RUBY_PLATFORM=~/linux|darwin/ ? ['--with-gssapi'] : []),
146
+ '--without-zlib',
147
+ '--without-icu',
148
+ '--without-readline',
149
+ 'ac_cv_search_gss_store_cred_into=',
150
+ ]
151
+ end
152
+ def compile
153
+ execute 'compile include', "#{make_cmd} -C src/include install"
154
+ execute 'compile interfaces', "#{make_cmd} -C src/interfaces install"
155
+ end
156
+ def install
157
+ end
158
+ end
159
+
160
+ recipe.host = toolchain
161
+ recipe.configure_options << "CFLAGS=#{" -fPIC" if RUBY_PLATFORM =~ /linux|darwin/}"
162
+ recipe.configure_options << "LDFLAGS=-L#{openssl_recipe.path}/lib -L#{openssl_recipe.path}/lib64 -L#{openssl_recipe.path}/lib-arm64 #{"-Wl,-soname,#{libpq_rubypg} -lgssapi_krb5 -lkrb5 -lk5crypto -lkrb5support -ldl" if RUBY_PLATFORM =~ /linux/} #{"-Wl,-install_name,@loader_path/../../ports/#{gem_platform}/lib/#{libpq_rubypg} -lgssapi_krb5 -lkrb5 -lk5crypto -lkrb5support -lresolv -framework Kerberos" if RUBY_PLATFORM =~ /darwin/}"
163
+ recipe.configure_options << "LIBS=-lkrb5 -lcom_err -lk5crypto -lkrb5support -lresolv" if RUBY_PLATFORM =~ /linux/
164
+ recipe.configure_options << "LIBS=-lssl -lwsock32 -lgdi32 -lws2_32 -lcrypt32" if RUBY_PLATFORM =~ /mingw|mswin/
165
+ recipe.configure_options << "CPPFLAGS=-I#{openssl_recipe.path}/include"
166
+ recipe.cook_and_activate
167
+ end
168
+
169
+ # Use our own library name for libpq to avoid loading of system libpq by accident.
170
+ FileUtils.ln_sf File.join(postgresql_recipe.port_path, "lib/#{libpq_orig}"),
171
+ File.join(postgresql_recipe.port_path, "lib/#{libpq_rubypg}")
26
172
  # Avoid dependency to external libgcc.dll on x86-mingw32
27
- $LDFLAGS << " -static-libgcc"
173
+ $LDFLAGS << " -static-libgcc" if RUBY_PLATFORM =~ /mingw|mswin/
174
+ # Avoid: "libpq.so: undefined reference to `dlopen'" in cross-ruby-2.7.8
175
+ $LDFLAGS << " -Wl,--no-as-needed" if RUBY_PLATFORM !~ /aarch64|arm64|darwin/
176
+ # Find libpq in the ports directory coming from lib/3.x
177
+ # It is shared between all compiled ruby versions.
178
+ $LDFLAGS << " '-Wl,-rpath=$$ORIGIN/../../ports/#{gem_platform}/lib'" if RUBY_PLATFORM =~ /linux/
28
179
  # Don't use pg_config for cross build, but --with-pg-* path options
29
- dir_config 'pg'
180
+ dir_config('pg', "#{postgresql_recipe.path}/include", "#{postgresql_recipe.path}/lib")
30
181
 
182
+ $defs.push( "-DPG_IS_BINARY_GEM")
31
183
  else
32
184
  # Native build
33
185
 
@@ -77,7 +229,7 @@ $INSTALLFILES = {
77
229
  "./postgresql_lib_path.rb" => "$(RUBYLIBDIR)/pg/"
78
230
  }
79
231
 
80
- if RUBY_VERSION >= '2.3.0' && /solaris/ =~ RUBY_PLATFORM
232
+ if /solaris/ =~ RUBY_PLATFORM
81
233
  append_cppflags( '-D__EXTENSIONS__' )
82
234
  end
83
235
 
@@ -142,25 +294,20 @@ if /mingw/ =~ RUBY_PLATFORM && RbConfig::MAKEFILE_CONFIG['CC'] =~ /gcc/
142
294
  end
143
295
  end
144
296
 
145
- have_func 'PQconninfo', 'libpq-fe.h' or
297
+ have_func 'PQencryptPasswordConn', 'libpq-fe.h' or # since PostgreSQL-10
146
298
  abort "Your PostgreSQL is too old. Either install an older version " +
147
- "of this gem or upgrade your database to at least PostgreSQL-9.3."
299
+ "of this gem or upgrade your database to at least PostgreSQL-10."
148
300
  # optional headers/functions
149
- have_func 'PQsslAttribute', 'libpq-fe.h' # since PostgreSQL-9.5
150
- have_func 'PQresultVerboseErrorMessage', 'libpq-fe.h' # since PostgreSQL-9.6
151
- have_func 'PQencryptPasswordConn', 'libpq-fe.h' # since PostgreSQL-10
152
301
  have_func 'PQresultMemorySize', 'libpq-fe.h' # since PostgreSQL-12
153
302
  have_func 'PQenterPipelineMode', 'libpq-fe.h' do |src| # since PostgreSQL-14
154
303
  # Ensure header files fit as well
155
304
  src + " int con(){ return PGRES_PIPELINE_SYNC; }"
156
305
  end
306
+ have_func 'PQsetChunkedRowsMode', 'libpq-fe.h' # since PostgreSQL-17
157
307
  have_func 'timegm'
158
- have_func 'rb_gc_adjust_memory_usage' # since ruby-2.4
159
- have_func 'rb_gc_mark_movable' # since ruby-2.7
160
308
  have_func 'rb_io_wait' # since ruby-3.0
309
+ have_func 'rb_io_descriptor' # since ruby-3.1
161
310
 
162
- # unistd.h confilicts with ruby/win32.h when cross compiling for win32 and ruby 1.9.1
163
- have_header 'unistd.h'
164
311
  have_header 'inttypes.h'
165
312
  have_header('ruby/fiber/scheduler.h') if RUBY_PLATFORM=~/mingw|mswin/
166
313
 
data/ext/gvl_wrappers.c CHANGED
@@ -5,8 +5,19 @@
5
5
 
6
6
  #include "pg.h"
7
7
 
8
- #ifndef HAVE_PQENCRYPTPASSWORDCONN
9
- char *PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm){return NULL;}
8
+
9
+ #ifndef HAVE_PQSETCHUNKEDROWSMODE
10
+ PGresult *PQclosePrepared(PGconn *conn, const char *stmtName){return NULL;}
11
+ PGresult *PQclosePortal(PGconn *conn, const char *portalName){return NULL;}
12
+ int PQsendClosePrepared(PGconn *conn, const char *stmtName){return 0;}
13
+ int PQsendClosePortal(PGconn *conn, const char *portalName){return 0;}
14
+ int PQsendPipelineSync(PGconn *conn){return 0;}
15
+ int PQcancelBlocking(PGcancelConn *cancelConn){return 0;}
16
+ int PQcancelStart(PGcancelConn *cancelConn){return 0;}
17
+ PostgresPollingStatusType PQcancelPoll(PGcancelConn *cancelConn){return PGRES_POLLING_FAILED;}
18
+ #endif
19
+ #ifndef HAVE_PQENTERPIPELINEMODE
20
+ int PQpipelineSync(PGconn *conn){return 0;}
10
21
  #endif
11
22
 
12
23
  #ifdef ENABLE_GVL_UNLOCK
data/ext/gvl_wrappers.h CHANGED
@@ -21,6 +21,10 @@
21
21
  # include RUBY_EXTCONF_H
22
22
  #endif
23
23
 
24
+ #ifndef HAVE_PQSETCHUNKEDROWSMODE
25
+ typedef struct pg_cancel_conn PGcancelConn;
26
+ #endif
27
+
24
28
  #define DEFINE_PARAM_LIST1(type, name) \
25
29
  name,
26
30
 
@@ -149,6 +153,12 @@
149
153
  #define FOR_EACH_PARAM_OF_PQdescribePortal(param) \
150
154
  param(PGconn *, conn)
151
155
 
156
+ #define FOR_EACH_PARAM_OF_PQclosePrepared(param) \
157
+ param(PGconn *, conn)
158
+
159
+ #define FOR_EACH_PARAM_OF_PQclosePortal(param) \
160
+ param(PGconn *, conn)
161
+
152
162
  #define FOR_EACH_PARAM_OF_PQgetResult(param)
153
163
 
154
164
  #define FOR_EACH_PARAM_OF_PQputCopyData(param) \
@@ -196,11 +206,25 @@
196
206
  #define FOR_EACH_PARAM_OF_PQsendDescribePortal(param) \
197
207
  param(PGconn *, conn)
198
208
 
209
+ #define FOR_EACH_PARAM_OF_PQsendClosePrepared(param) \
210
+ param(PGconn *, conn)
211
+
212
+ #define FOR_EACH_PARAM_OF_PQsendClosePortal(param) \
213
+ param(PGconn *, conn)
214
+
215
+ #define FOR_EACH_PARAM_OF_PQpipelineSync(param)
216
+
217
+ #define FOR_EACH_PARAM_OF_PQsendPipelineSync(param)
218
+
199
219
  #define FOR_EACH_PARAM_OF_PQsetClientEncoding(param) \
200
220
  param(PGconn *, conn)
201
221
 
202
222
  #define FOR_EACH_PARAM_OF_PQisBusy(param)
203
223
 
224
+ #define FOR_EACH_PARAM_OF_PQcancelBlocking(param)
225
+ #define FOR_EACH_PARAM_OF_PQcancelStart(param)
226
+ #define FOR_EACH_PARAM_OF_PQcancelPoll(param)
227
+
204
228
  #define FOR_EACH_PARAM_OF_PQencryptPasswordConn(param) \
205
229
  param(PGconn *, conn) \
206
230
  param(const char *, passwd) \
@@ -225,6 +249,8 @@
225
249
  function(PQprepare, GVL_TYPE_NONVOID, PGresult *, const Oid *, paramTypes) \
226
250
  function(PQdescribePrepared, GVL_TYPE_NONVOID, PGresult *, const char *, stmtName) \
227
251
  function(PQdescribePortal, GVL_TYPE_NONVOID, PGresult *, const char *, portalName) \
252
+ function(PQclosePrepared, GVL_TYPE_NONVOID, PGresult *, const char *, stmtName) \
253
+ function(PQclosePortal, GVL_TYPE_NONVOID, PGresult *, const char *, portalName) \
228
254
  function(PQgetResult, GVL_TYPE_NONVOID, PGresult *, PGconn *, conn) \
229
255
  function(PQputCopyData, GVL_TYPE_NONVOID, int, int, nbytes) \
230
256
  function(PQputCopyEnd, GVL_TYPE_NONVOID, int, const char *, errormsg) \
@@ -236,8 +262,15 @@
236
262
  function(PQsendQueryPrepared, GVL_TYPE_NONVOID, int, int, resultFormat) \
237
263
  function(PQsendDescribePrepared, GVL_TYPE_NONVOID, int, const char *, stmt) \
238
264
  function(PQsendDescribePortal, GVL_TYPE_NONVOID, int, const char *, portal) \
265
+ function(PQsendClosePrepared, GVL_TYPE_NONVOID, int, const char *, stmt) \
266
+ function(PQsendClosePortal, GVL_TYPE_NONVOID, int, const char *, portal) \
267
+ function(PQpipelineSync, GVL_TYPE_NONVOID, int, PGconn *, conn) \
268
+ function(PQsendPipelineSync, GVL_TYPE_NONVOID, int, PGconn *, conn) \
239
269
  function(PQsetClientEncoding, GVL_TYPE_NONVOID, int, const char *, encoding) \
240
270
  function(PQisBusy, GVL_TYPE_NONVOID, int, PGconn *, conn) \
271
+ function(PQcancelBlocking, GVL_TYPE_NONVOID, int, PGcancelConn *, conn) \
272
+ function(PQcancelStart, GVL_TYPE_NONVOID, int, PGcancelConn *, conn) \
273
+ function(PQcancelPoll, GVL_TYPE_NONVOID, PostgresPollingStatusType, PGcancelConn *, conn) \
241
274
  function(PQencryptPasswordConn, GVL_TYPE_NONVOID, char *, const char *, algorithm) \
242
275
  function(PQcancel, GVL_TYPE_NONVOID, int, int, errbufsize);
243
276
 
data/ext/pg.c CHANGED
@@ -404,6 +404,10 @@ Init_pg_ext(void)
404
404
  /* Checking if server is in standby mode. Available since PostgreSQL-14. */
405
405
  rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_STANDBY", INT2FIX(CONNECTION_CHECK_STANDBY));
406
406
  #endif
407
+ #if PG_MAJORVERSION_NUM >= 17
408
+ /* Waiting for connection attempt to be started. Available since PostgreSQL-17. */
409
+ rb_define_const(rb_mPGconstants, "CONNECTION_ALLOCATED", INT2FIX(CONNECTION_ALLOCATED));
410
+ #endif
407
411
 
408
412
  /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
409
413
 
@@ -451,14 +455,12 @@ Init_pg_ext(void)
451
455
  rb_define_const(rb_mPGconstants, "PQERRORS_SQLSTATE", INT2FIX(PQERRORS_SQLSTATE));
452
456
  #endif
453
457
 
454
- #ifdef HAVE_PQRESULTVERBOSEERRORMESSAGE
455
458
  /* See Connection#set_error_context_visibility */
456
459
  rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_NEVER", INT2FIX(PQSHOW_CONTEXT_NEVER));
457
460
  /* See Connection#set_error_context_visibility */
458
461
  rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ERRORS", INT2FIX(PQSHOW_CONTEXT_ERRORS));
459
462
  /* See Connection#set_error_context_visibility */
460
463
  rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ALWAYS", INT2FIX(PQSHOW_CONTEXT_ALWAYS));
461
- #endif
462
464
 
463
465
  /****** PG::Connection CLASS CONSTANTS: Check Server Status ******/
464
466
 
@@ -506,6 +508,10 @@ Init_pg_ext(void)
506
508
  rb_define_const(rb_mPGconstants, "PGRES_COPY_BOTH", INT2FIX(PGRES_COPY_BOTH));
507
509
  /* Result#result_status constant - Single tuple from larger resultset. */
508
510
  rb_define_const(rb_mPGconstants, "PGRES_SINGLE_TUPLE", INT2FIX(PGRES_SINGLE_TUPLE));
511
+ #ifdef HAVE_PQSETCHUNKEDROWSMODE
512
+ /* Result#result_status constant - tuple chunk from larger resultset. */
513
+ rb_define_const(rb_mPGconstants, "PGRES_TUPLES_CHUNK", INT2FIX(PGRES_TUPLES_CHUNK));
514
+ #endif
509
515
 
510
516
  #ifdef HAVE_PQENTERPIPELINEMODE
511
517
  /* Result#result_status constant - The PG::Result represents a synchronization point in pipeline mode, requested by Connection#pipeline_sync.
@@ -530,20 +536,18 @@ Init_pg_ext(void)
530
536
  */
531
537
  rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
532
538
 
533
- #ifdef PG_DIAG_SEVERITY_NONLOCALIZED
534
539
  /* Result#result_error_field argument constant
535
540
  *
536
541
  * The severity; the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message).
537
542
  * This is identical to the PG_DIAG_SEVERITY field except that the contents are never localized.
538
543
  *
539
- * Available since PostgreSQL-9.6
540
544
  */
541
545
  rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY_NONLOCALIZED", INT2FIX(PG_DIAG_SEVERITY_NONLOCALIZED));
542
- #endif
546
+
543
547
  /* Result#result_error_field argument constant
544
548
  *
545
549
  * The SQLSTATE code for the error.
546
- * The SQLSTATE code identies the type of error that has occurred; it can be used by front-end applications to perform specific operations (such as error handling) in response to a particular database error.
550
+ * The SQLSTATE code identifies the type of error that has occurred; it can be used by front-end applications to perform specific operations (such as error handling) in response to a particular database error.
547
551
  * For a list of the possible SQLSTATE codes, see Appendix A.
548
552
  * This field is not localizable, and is always present.
549
553
  */
@@ -667,6 +671,12 @@ Init_pg_ext(void)
667
671
  /* PostgreSQL compiled in default port */
668
672
  rb_define_const(rb_mPGconstants, "DEF_PGPORT", INT2FIX(DEF_PGPORT));
669
673
 
674
+ #ifdef PG_IS_BINARY_GEM
675
+ rb_define_const(rb_mPG, "IS_BINARY_GEM", Qtrue);
676
+ #else
677
+ rb_define_const(rb_mPG, "IS_BINARY_GEM", Qfalse);
678
+ #endif
679
+
670
680
  /* Add the constants to the toplevel namespace */
671
681
  rb_include_module( rb_mPG, rb_mPGconstants );
672
682
 
@@ -689,4 +699,5 @@ Init_pg_ext(void)
689
699
  init_pg_copycoder();
690
700
  init_pg_recordcoder();
691
701
  init_pg_tuple();
702
+ init_pg_cancon();
692
703
  }
data/ext/pg.h CHANGED
@@ -76,14 +76,7 @@ typedef long suseconds_t;
76
76
  #define PG_MAX_COLUMNS 4000
77
77
  #endif
78
78
 
79
- #ifdef HAVE_RB_GC_MARK_MOVABLE
80
- #define pg_compact_callback(x) (x)
81
79
  #define pg_gc_location(x) x = rb_gc_location(x)
82
- #else
83
- #define rb_gc_mark_movable(x) rb_gc_mark(x)
84
- #define pg_compact_callback(x) {(x)}
85
- #define pg_gc_location(x) UNUSED(x)
86
- #endif
87
80
 
88
81
  /* For compatibility with ruby < 3.0 */
89
82
  #ifndef RUBY_TYPED_FROZEN_SHAREABLE
@@ -123,10 +116,8 @@ typedef struct {
123
116
  /* enable automatic flushing of send data at the end of send_query calls */
124
117
  unsigned int flush_data : 1;
125
118
 
126
- #if defined(_WIN32)
127
119
  /* File descriptor to be used for rb_w32_unwrap_io_handle() */
128
120
  int ruby_sd;
129
- #endif
130
121
  } t_pg_connection;
131
122
 
132
123
  typedef struct pg_coder t_pg_coder;
@@ -215,6 +206,7 @@ typedef struct {
215
206
  t_pg_coder comp;
216
207
  t_pg_coder *elem;
217
208
  int needs_quotation;
209
+ int dimensions;
218
210
  char delimiter;
219
211
  } t_pg_composite_coder;
220
212
 
@@ -312,6 +304,7 @@ void init_pg_text_decoder _(( void ));
312
304
  void init_pg_binary_encoder _(( void ));
313
305
  void init_pg_binary_decoder _(( void ));
314
306
  void init_pg_tuple _(( void ));
307
+ void init_pg_cancon _(( void ));
315
308
  VALUE lookup_error_class _(( const char * ));
316
309
  VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
317
310
  VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
@@ -351,6 +344,13 @@ void pg_typemap_compact _(( void * ));
351
344
  PGconn *pg_get_pgconn _(( VALUE ));
352
345
  t_pg_connection *pg_get_connection _(( VALUE ));
353
346
  VALUE pgconn_block _(( int, VALUE *, VALUE ));
347
+ #ifdef __GNUC__
348
+ __attribute__((format(printf, 3, 4)))
349
+ #endif
350
+ NORETURN(void pg_raise_conn_error _(( VALUE klass, VALUE self, const char *format, ...)));
351
+ VALUE pg_wrap_socket_io _(( int sd, VALUE self, VALUE *p_socket_io, int *p_ruby_sd ));
352
+ void pg_unwrap_socket_io _(( VALUE self, VALUE *p_socket_io, int ruby_sd ));
353
+
354
354
 
355
355
  VALUE pg_new_result _(( PGresult *, VALUE ));
356
356
  VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
@@ -133,6 +133,154 @@ pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int
133
133
  return out_value;
134
134
  }
135
135
 
136
+ /*
137
+ * Maximum number of array subscripts (arbitrary limit)
138
+ */
139
+ #define MAXDIM 6
140
+
141
+ /*
142
+ * Document-class: PG::BinaryDecoder::Array < PG::CompositeDecoder
143
+ *
144
+ * This is a decoder class for conversion of binary array types.
145
+ *
146
+ * It returns an Array with possibly an arbitrary number of sub-Arrays.
147
+ * All values are decoded according to the #elements_type accessor.
148
+ * Sub-arrays are decoded recursively.
149
+ *
150
+ * This decoder simply ignores any dimension decorations preceding the array values.
151
+ * It returns all array values as regular ruby Array with a zero based index, regardless of the index given in the dimension decoration.
152
+ *
153
+ * An array decoder which respects dimension decorations is waiting to be implemented.
154
+ *
155
+ */
156
+ static VALUE
157
+ pg_bin_dec_array(t_pg_coder *conv, const char *input_line, int len, int tuple, int field, int enc_idx)
158
+ {
159
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
160
+ t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
161
+
162
+ /* Current field */
163
+ VALUE field_str;
164
+
165
+ int32_t nitems32;
166
+ int i;
167
+ int ndim;
168
+ int nitems;
169
+ int flags;
170
+ int dim;
171
+ int dim_sizes[MAXDIM];
172
+ VALUE arrays[MAXDIM];
173
+ char *output_ptr;
174
+ const char *cur_ptr;
175
+ const char *line_end_ptr;
176
+ char *end_capa_ptr;
177
+
178
+ /* Allocate a new string with embedded capacity and realloc later with
179
+ * exponential growing size when needed. */
180
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
181
+
182
+ /* set pointer variables for loop */
183
+ cur_ptr = input_line;
184
+ line_end_ptr = input_line + len;
185
+
186
+ /* read number of dimensions */
187
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
188
+ ndim = read_nbo32(cur_ptr);
189
+ if (ndim < 0 || ndim > MAXDIM) {
190
+ rb_raise( rb_eArgError, "unsupported number of array dimensions: %d", ndim );
191
+ }
192
+ cur_ptr += 4;
193
+
194
+ /* read flags */
195
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
196
+ flags = read_nbo32(cur_ptr);
197
+ if (flags != 0 && flags != 1) {
198
+ rb_raise( rb_eArgError, "unsupported binary array flags: %d", flags );
199
+ }
200
+ cur_ptr += 4;
201
+
202
+ /* ignore element OID */
203
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
204
+ cur_ptr += 4;
205
+
206
+ nitems32 = ndim == 0 ? 0 : 1;
207
+ for (i = 0; i < ndim; i++) {
208
+ int64_t prod;
209
+
210
+ /* read size of dimensions and ignore lower bound */
211
+ if (line_end_ptr - cur_ptr < 8 ) goto length_error;
212
+ dim_sizes[i] = read_nbo32(cur_ptr);
213
+ prod = (int64_t) nitems32 * (int64_t) dim_sizes[i];
214
+ nitems32 = (int32_t) prod;
215
+ if (dim_sizes[i] < 0 || (int64_t) nitems32 != prod) {
216
+ rb_raise( rb_eArgError, "unsupported array size: %" PRId64, prod );
217
+ }
218
+ cur_ptr += 8;
219
+ }
220
+ nitems = (int)nitems32;
221
+
222
+ dim = 0;
223
+ arrays[dim] = rb_ary_new2(ndim == 0 ? 0 : dim_sizes[dim]);
224
+ for (i = 0; i < nitems; i++) {
225
+ int input_len;
226
+
227
+ /* traverse dimensions down */
228
+ while (dim < ndim - 1) {
229
+ dim++;
230
+ arrays[dim] = rb_ary_new2(dim_sizes[dim]);
231
+ rb_ary_push(arrays[dim - 1], arrays[dim]);
232
+ }
233
+
234
+ /* read element length */
235
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
236
+ input_len = read_nbo32(cur_ptr);
237
+ cur_ptr += 4;
238
+
239
+ /* convert and put element into array */
240
+ if (input_len < 0) {
241
+ if (input_len != -1) goto length_error;
242
+ /* NULL indicator */
243
+ rb_ary_push(arrays[dim], Qnil);
244
+ } else {
245
+ VALUE field_value;
246
+ if (line_end_ptr - cur_ptr < input_len ) goto length_error;
247
+
248
+ /* copy input data to field_str */
249
+ PG_RB_STR_ENSURE_CAPA( field_str, input_len, output_ptr, end_capa_ptr );
250
+ memcpy(output_ptr, cur_ptr, input_len);
251
+ cur_ptr += input_len;
252
+ output_ptr += input_len;
253
+ /* convert field_str through the type map */
254
+ rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
255
+ field_value = dec_func(this->elem, RSTRING_PTR(field_str), input_len, tuple, field, enc_idx);
256
+
257
+ rb_ary_push(arrays[dim], field_value);
258
+
259
+ if( field_value == field_str ){
260
+ /* Our output string will be send to the user, so we can not reuse
261
+ * it for the next field. */
262
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
263
+ }
264
+ }
265
+
266
+ /* Reset the pointer to the start of the output/buffer string. */
267
+ output_ptr = RSTRING_PTR(field_str);
268
+
269
+ /* traverse dimensions up */
270
+ while (RARRAY_LEN(arrays[dim]) >= dim_sizes[dim] && dim > 0) {
271
+ dim--;
272
+ }
273
+ }
274
+
275
+ if (cur_ptr < line_end_ptr)
276
+ rb_raise( rb_eArgError, "trailing data after binary array data at position: %ld", (long)(cur_ptr - input_line) + 1 );
277
+
278
+ return arrays[0];
279
+
280
+ length_error:
281
+ rb_raise( rb_eArgError, "premature end of binary array data at position: %ld", (long)(cur_ptr - input_line) + 1 );
282
+ }
283
+
136
284
  #define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
137
285
  #define PG_INT64_MAX 0x7FFFFFFFFFFFFFFFL
138
286
 
@@ -233,6 +381,8 @@ j2date(int jd, int *year, int *month, int *day)
233
381
  *
234
382
  * This is a decoder class for conversion of PostgreSQL binary date
235
383
  * to Ruby Date objects.
384
+ *
385
+ * As soon as this class is used, it requires the ruby standard library 'date'.
236
386
  */
237
387
  static VALUE
238
388
  pg_bin_dec_date(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
@@ -303,6 +453,8 @@ init_pg_binary_decoder(void)
303
453
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
304
454
  pg_define_coder( "Timestamp", pg_bin_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
305
455
 
456
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Array", rb_cPG_CompositeDecoder ); */
457
+ pg_define_coder( "Array", pg_bin_dec_array, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
306
458
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
307
459
  pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
308
460
  }