pg 1.6.0.rc1-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +4 -0
  3. data/BSDL +22 -0
  4. data/Contributors.rdoc +46 -0
  5. data/Gemfile +23 -0
  6. data/History.md +958 -0
  7. data/LICENSE +56 -0
  8. data/Manifest.txt +72 -0
  9. data/POSTGRES +23 -0
  10. data/README-OS_X.rdoc +68 -0
  11. data/README-Windows.rdoc +56 -0
  12. data/README.ja.md +300 -0
  13. data/README.md +286 -0
  14. data/Rakefile +161 -0
  15. data/certs/ged.pem +24 -0
  16. data/certs/kanis@comcard.de.pem +20 -0
  17. data/certs/larskanis-2022.pem +26 -0
  18. data/certs/larskanis-2023.pem +24 -0
  19. data/certs/larskanis-2024.pem +24 -0
  20. data/ext/errorcodes.def +1043 -0
  21. data/ext/errorcodes.rb +45 -0
  22. data/ext/errorcodes.txt +494 -0
  23. data/ext/extconf.rb +282 -0
  24. data/ext/gvl_wrappers.c +32 -0
  25. data/ext/gvl_wrappers.h +297 -0
  26. data/ext/pg.c +703 -0
  27. data/ext/pg.h +390 -0
  28. data/ext/pg_binary_decoder.c +460 -0
  29. data/ext/pg_binary_encoder.c +583 -0
  30. data/ext/pg_cancel_connection.c +360 -0
  31. data/ext/pg_coder.c +622 -0
  32. data/ext/pg_connection.c +4869 -0
  33. data/ext/pg_copy_coder.c +921 -0
  34. data/ext/pg_errors.c +95 -0
  35. data/ext/pg_record_coder.c +522 -0
  36. data/ext/pg_result.c +1764 -0
  37. data/ext/pg_text_decoder.c +1008 -0
  38. data/ext/pg_text_encoder.c +833 -0
  39. data/ext/pg_tuple.c +572 -0
  40. data/ext/pg_type_map.c +200 -0
  41. data/ext/pg_type_map_all_strings.c +130 -0
  42. data/ext/pg_type_map_by_class.c +271 -0
  43. data/ext/pg_type_map_by_column.c +355 -0
  44. data/ext/pg_type_map_by_mri_type.c +313 -0
  45. data/ext/pg_type_map_by_oid.c +388 -0
  46. data/ext/pg_type_map_in_ruby.c +333 -0
  47. data/ext/pg_util.c +149 -0
  48. data/ext/pg_util.h +65 -0
  49. data/ext/vc/pg.sln +26 -0
  50. data/ext/vc/pg_18/pg.vcproj +216 -0
  51. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  52. data/lib/2.7/pg_ext.so +0 -0
  53. data/lib/3.0/pg_ext.so +0 -0
  54. data/lib/3.1/pg_ext.so +0 -0
  55. data/lib/3.2/pg_ext.so +0 -0
  56. data/lib/3.3/pg_ext.so +0 -0
  57. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  58. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  59. data/lib/pg/basic_type_map_for_results.rb +104 -0
  60. data/lib/pg/basic_type_registry.rb +311 -0
  61. data/lib/pg/binary_decoder/date.rb +9 -0
  62. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  63. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  64. data/lib/pg/cancel_connection.rb +30 -0
  65. data/lib/pg/coder.rb +106 -0
  66. data/lib/pg/connection.rb +1027 -0
  67. data/lib/pg/exceptions.rb +31 -0
  68. data/lib/pg/result.rb +43 -0
  69. data/lib/pg/text_decoder/date.rb +21 -0
  70. data/lib/pg/text_decoder/inet.rb +9 -0
  71. data/lib/pg/text_decoder/json.rb +17 -0
  72. data/lib/pg/text_decoder/numeric.rb +9 -0
  73. data/lib/pg/text_decoder/timestamp.rb +30 -0
  74. data/lib/pg/text_encoder/date.rb +13 -0
  75. data/lib/pg/text_encoder/inet.rb +31 -0
  76. data/lib/pg/text_encoder/json.rb +17 -0
  77. data/lib/pg/text_encoder/numeric.rb +9 -0
  78. data/lib/pg/text_encoder/timestamp.rb +24 -0
  79. data/lib/pg/tuple.rb +30 -0
  80. data/lib/pg/type_map_by_column.rb +16 -0
  81. data/lib/pg/version.rb +4 -0
  82. data/lib/pg.rb +144 -0
  83. data/misc/openssl-pg-segfault.rb +31 -0
  84. data/misc/postgres/History.txt +9 -0
  85. data/misc/postgres/Manifest.txt +5 -0
  86. data/misc/postgres/README.txt +21 -0
  87. data/misc/postgres/Rakefile +21 -0
  88. data/misc/postgres/lib/postgres.rb +16 -0
  89. data/misc/ruby-pg/History.txt +9 -0
  90. data/misc/ruby-pg/Manifest.txt +5 -0
  91. data/misc/ruby-pg/README.txt +21 -0
  92. data/misc/ruby-pg/Rakefile +21 -0
  93. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  94. data/pg.gemspec +36 -0
  95. data/ports/x86_64-linux/lib/libpq-ruby-pg.so.1 +0 -0
  96. data/rakelib/task_extension.rb +46 -0
  97. data/sample/array_insert.rb +20 -0
  98. data/sample/async_api.rb +102 -0
  99. data/sample/async_copyto.rb +39 -0
  100. data/sample/async_mixed.rb +56 -0
  101. data/sample/check_conn.rb +21 -0
  102. data/sample/copydata.rb +71 -0
  103. data/sample/copyfrom.rb +81 -0
  104. data/sample/copyto.rb +19 -0
  105. data/sample/cursor.rb +21 -0
  106. data/sample/disk_usage_report.rb +177 -0
  107. data/sample/issue-119.rb +94 -0
  108. data/sample/losample.rb +69 -0
  109. data/sample/minimal-testcase.rb +17 -0
  110. data/sample/notify_wait.rb +72 -0
  111. data/sample/pg_statistics.rb +285 -0
  112. data/sample/replication_monitor.rb +222 -0
  113. data/sample/test_binary_values.rb +33 -0
  114. data/sample/wal_shipper.rb +434 -0
  115. data/sample/warehouse_partitions.rb +311 -0
  116. data.tar.gz.sig +0 -0
  117. metadata +252 -0
  118. metadata.gz.sig +0 -0
data/ext/pg.c ADDED
@@ -0,0 +1,703 @@
1
+ /*
2
+ * pg.c - Toplevel extension
3
+ * $Id$
4
+ *
5
+ * Author/s:
6
+ *
7
+ * - Jeff Davis <ruby-pg@j-davis.com>
8
+ * - Guy Decoux (ts) <decoux@moulon.inra.fr>
9
+ * - Michael Granger <ged@FaerieMUD.org>
10
+ * - Lars Kanis <lars@greiz-reinsdorf.de>
11
+ * - Dave Lee
12
+ * - Eiji Matsumoto <usagi@ruby.club.or.jp>
13
+ * - Yukihiro Matsumoto <matz@ruby-lang.org>
14
+ * - Noboru Saitou <noborus@netlab.jp>
15
+ *
16
+ * See Contributors.rdoc for the many additional fine people that have contributed
17
+ * to this library over the years.
18
+ *
19
+ * Copyright (c) 1997-2019 by the authors.
20
+ *
21
+ * You may redistribute this software under the same terms as Ruby itself; see
22
+ * https://www.ruby-lang.org/en/about/license.txt or the BSDL file in the source
23
+ * for details.
24
+ *
25
+ * Portions of the code are from the PostgreSQL project, and are distributed
26
+ * under the terms of the PostgreSQL license, included in the file "POSTGRES".
27
+ *
28
+ * Portions copyright LAIKA, Inc.
29
+ *
30
+ *
31
+ * The following functions are part of libpq, but not available from ruby-pg,
32
+ * because they are deprecated, obsolete, or generally not useful:
33
+ *
34
+ * - PQfreemem -- unnecessary: copied to ruby object, then freed. Ruby object's
35
+ * memory is freed when it is garbage collected.
36
+ * - PQprint -- not very useful
37
+ * - PQsetdb -- not very useful
38
+ * - PQoidStatus -- deprecated, use PQoidValue
39
+ * - PQrequestCancel -- deprecated, use PQcancel
40
+ * - PQfn -- use a prepared statement instead
41
+ * - PQgetline -- deprecated, use PQgetCopyData
42
+ * - PQgetlineAsync -- deprecated, use PQgetCopyData
43
+ * - PQputline -- deprecated, use PQputCopyData
44
+ * - PQputnbytes -- deprecated, use PQputCopyData
45
+ * - PQendcopy -- deprecated, use PQputCopyEnd
46
+ */
47
+
48
+ #include "pg.h"
49
+
50
+ int pg_skip_deprecation_warning;
51
+ VALUE rb_mPG;
52
+ VALUE rb_mPGconstants;
53
+
54
+
55
+ /*
56
+ * Document-class: PG::Error
57
+ *
58
+ * This is the exception class raised when an error is returned from
59
+ * a libpq API call.
60
+ *
61
+ * The attributes +connection+ and +result+ are set to the connection
62
+ * object and result set object, respectively.
63
+ *
64
+ * If the connection object or result set object is not available from
65
+ * the context in which the error was encountered, it is +nil+.
66
+ */
67
+
68
+ /*
69
+ * M17n functions
70
+ */
71
+
72
+ /**
73
+ * The mapping from canonical encoding names in PostgreSQL to ones in Ruby.
74
+ */
75
+ const char * const (pg_enc_pg2ruby_mapping[][2]) = {
76
+ {"UTF8", "UTF-8" },
77
+ {"BIG5", "Big5" },
78
+ {"EUC_CN", "GB2312" },
79
+ {"EUC_JP", "EUC-JP" },
80
+ {"EUC_JIS_2004", "EUC-JP" },
81
+ {"EUC_KR", "EUC-KR" },
82
+ {"EUC_TW", "EUC-TW" },
83
+ {"GB18030", "GB18030" },
84
+ {"GBK", "GBK" },
85
+ {"ISO_8859_5", "ISO-8859-5" },
86
+ {"ISO_8859_6", "ISO-8859-6" },
87
+ {"ISO_8859_7", "ISO-8859-7" },
88
+ {"ISO_8859_8", "ISO-8859-8" },
89
+ /* {"JOHAB", "JOHAB" }, dummy */
90
+ {"KOI8", "KOI8-R" },
91
+ {"KOI8R", "KOI8-R" },
92
+ {"KOI8U", "KOI8-U" },
93
+ {"LATIN1", "ISO-8859-1" },
94
+ {"LATIN2", "ISO-8859-2" },
95
+ {"LATIN3", "ISO-8859-3" },
96
+ {"LATIN4", "ISO-8859-4" },
97
+ {"LATIN5", "ISO-8859-9" },
98
+ {"LATIN6", "ISO-8859-10" },
99
+ {"LATIN7", "ISO-8859-13" },
100
+ {"LATIN8", "ISO-8859-14" },
101
+ {"LATIN9", "ISO-8859-15" },
102
+ {"LATIN10", "ISO-8859-16" },
103
+ {"MULE_INTERNAL", "Emacs-Mule" },
104
+ {"SJIS", "Windows-31J" },
105
+ {"SHIFT_JIS_2004","Windows-31J" },
106
+ /* {"SQL_ASCII", NULL }, special case*/
107
+ {"UHC", "CP949" },
108
+ {"WIN866", "IBM866" },
109
+ {"WIN874", "Windows-874" },
110
+ {"WIN1250", "Windows-1250"},
111
+ {"WIN1251", "Windows-1251"},
112
+ {"WIN1252", "Windows-1252"},
113
+ {"WIN1253", "Windows-1253"},
114
+ {"WIN1254", "Windows-1254"},
115
+ {"WIN1255", "Windows-1255"},
116
+ {"WIN1256", "Windows-1256"},
117
+ {"WIN1257", "Windows-1257"},
118
+ {"WIN1258", "Windows-1258"}
119
+ };
120
+
121
+
122
+ /*
123
+ * Return the given PostgreSQL encoding ID as an rb_encoding.
124
+ *
125
+ * - returns NULL if the client encoding is 'SQL_ASCII'.
126
+ * - returns ASCII-8BIT if the client encoding is unknown.
127
+ */
128
+ static rb_encoding *
129
+ pg_get_pg_encoding_as_rb_encoding( int enc_id )
130
+ {
131
+ const char *name = pg_encoding_to_char( enc_id );
132
+ return pg_get_pg_encname_as_rb_encoding( name );
133
+ }
134
+
135
+ /*
136
+ * Return the given PostgreSQL encoding name as an rb_encoding.
137
+ */
138
+ rb_encoding *
139
+ pg_get_pg_encname_as_rb_encoding( const char *pg_encname )
140
+ {
141
+ size_t i;
142
+
143
+ /* Trying looking it up in the conversion table */
144
+ for ( i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i ) {
145
+ if ( strcmp(pg_encname, pg_enc_pg2ruby_mapping[i][0]) == 0 )
146
+ return rb_enc_find( pg_enc_pg2ruby_mapping[i][1] );
147
+ }
148
+
149
+ /* Fallthrough to ASCII-8BIT */
150
+ return rb_ascii8bit_encoding();
151
+ }
152
+
153
+ /*
154
+ * Get the client encoding of the specified connection handle and return it as a rb_encoding.
155
+ */
156
+ rb_encoding *
157
+ pg_conn_enc_get( PGconn *conn )
158
+ {
159
+ int enc_id = PQclientEncoding( conn );
160
+ return pg_get_pg_encoding_as_rb_encoding( enc_id );
161
+ }
162
+
163
+
164
+ /*
165
+ * Returns the given rb_encoding as the equivalent PostgreSQL encoding string.
166
+ */
167
+ const char *
168
+ pg_get_rb_encoding_as_pg_encoding( rb_encoding *enc )
169
+ {
170
+ const char *rb_encname = rb_enc_name( enc );
171
+ const char *encname = NULL;
172
+ size_t i;
173
+
174
+ for (i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i) {
175
+ if (strcmp(rb_encname, pg_enc_pg2ruby_mapping[i][1]) == 0) {
176
+ encname = pg_enc_pg2ruby_mapping[i][0];
177
+ }
178
+ }
179
+
180
+ if ( !encname ) encname = "SQL_ASCII";
181
+
182
+ return encname;
183
+ }
184
+
185
+
186
+ /*
187
+ * Ensures that the given string has enough capacity to take expand_len
188
+ * more data bytes. The new data part of the String is not initialized.
189
+ *
190
+ * current_out must be a pointer within the data part of the String object.
191
+ * This pointer is returned and possibly adjusted, because the location of the data
192
+ * part of the String can change through this function.
193
+ *
194
+ * PG_RB_STR_ENSURE_CAPA can be used to do fast inline checks of the remaining capacity.
195
+ * end_capa it is then set to the first byte after the currently reserved memory,
196
+ * if not NULL.
197
+ *
198
+ * Before the String can be used with other string functions or returned to Ruby space,
199
+ * the string length has to be set with rb_str_set_len().
200
+ *
201
+ * Usage example:
202
+ *
203
+ * VALUE string;
204
+ * char *current_out, *end_capa;
205
+ * PG_RB_STR_NEW( string, current_out, end_capa );
206
+ * while( data_is_going_to_be_processed ){
207
+ * PG_RB_STR_ENSURE_CAPA( string, 2, current_out, end_capa );
208
+ * *current_out++ = databyte1;
209
+ * *current_out++ = databyte2;
210
+ * }
211
+ * rb_str_set_len( string, current_out - RSTRING_PTR(string) );
212
+ *
213
+ */
214
+ char *
215
+ pg_rb_str_ensure_capa( VALUE str, long expand_len, char *curr_ptr, char **end_ptr )
216
+ {
217
+ long curr_len = curr_ptr - RSTRING_PTR(str);
218
+ long curr_capa = rb_str_capacity( str );
219
+ if( curr_capa < curr_len + expand_len ){
220
+ rb_str_set_len( str, curr_len );
221
+ rb_str_modify_expand( str, (curr_len + expand_len) * 2 - curr_capa );
222
+ curr_ptr = RSTRING_PTR(str) + curr_len;
223
+ }
224
+ if( end_ptr )
225
+ *end_ptr = RSTRING_PTR(str) + rb_str_capacity( str );
226
+ return curr_ptr;
227
+ }
228
+
229
+
230
+ /**************************************************************************
231
+ * Module Methods
232
+ **************************************************************************/
233
+
234
+ /*
235
+ * call-seq:
236
+ * PG.library_version -> Integer
237
+ *
238
+ * Get the version of the libpq library in use. The number is formed by
239
+ * converting the major, minor, and revision numbers into two-decimal-
240
+ * digit numbers and appending them together.
241
+ * For example, version 7.4.2 will be returned as 70402, and version
242
+ * 8.1 will be returned as 80100 (leading zeroes are not shown). Zero
243
+ * is returned if the connection is bad.
244
+ */
245
+ static VALUE
246
+ pg_s_library_version(VALUE self)
247
+ {
248
+ UNUSED( self );
249
+ return INT2NUM(PQlibVersion());
250
+ }
251
+
252
+
253
+ /*
254
+ * call-seq:
255
+ * PG.isthreadsafe -> Boolean
256
+ * PG.is_threadsafe? -> Boolean
257
+ * PG.threadsafe? -> Boolean
258
+ *
259
+ * Returns +true+ if libpq is thread-safe, +false+ otherwise.
260
+ */
261
+ static VALUE
262
+ pg_s_threadsafe_p(VALUE self)
263
+ {
264
+ UNUSED( self );
265
+ return PQisthreadsafe() ? Qtrue : Qfalse;
266
+ }
267
+
268
+ static int
269
+ pg_to_bool_int(VALUE value)
270
+ {
271
+ switch( TYPE(value) ){
272
+ case T_FALSE:
273
+ return 0;
274
+ case T_TRUE:
275
+ return 1;
276
+ default:
277
+ return NUM2INT(value);
278
+ }
279
+ }
280
+
281
+ /*
282
+ * call-seq:
283
+ * PG.init_openssl(do_ssl, do_crypto) -> nil
284
+ *
285
+ * Allows applications to select which security libraries to initialize.
286
+ *
287
+ * If your application initializes libssl and/or libcrypto libraries and libpq is
288
+ * built with SSL support, you should call PG.init_openssl() to tell libpq that the
289
+ * libssl and/or libcrypto libraries have been initialized by your application,
290
+ * so that libpq will not also initialize those libraries.
291
+ *
292
+ * When do_ssl is +true+, libpq will initialize the OpenSSL library before first
293
+ * opening a database connection. When do_crypto is +true+, the libcrypto library
294
+ * will be initialized. By default (if PG.init_openssl() is not called), both libraries
295
+ * are initialized. When SSL support is not compiled in, this function is present but does nothing.
296
+ *
297
+ * If your application uses and initializes either OpenSSL or its underlying libcrypto library,
298
+ * you must call this function with +false+ for the appropriate parameter(s) before first opening
299
+ * a database connection. Also be sure that you have done that initialization before opening a
300
+ * database connection.
301
+ *
302
+ */
303
+ static VALUE
304
+ pg_s_init_openssl(VALUE self, VALUE do_ssl, VALUE do_crypto)
305
+ {
306
+ UNUSED( self );
307
+ PQinitOpenSSL(pg_to_bool_int(do_ssl), pg_to_bool_int(do_crypto));
308
+ return Qnil;
309
+ }
310
+
311
+
312
+ /*
313
+ * call-seq:
314
+ * PG.init_ssl(do_ssl) -> nil
315
+ *
316
+ * Allows applications to select which security libraries to initialize.
317
+ *
318
+ * This function is equivalent to <tt>PG.init_openssl(do_ssl, do_ssl)</tt> . It is sufficient for
319
+ * applications that initialize both or neither of OpenSSL and libcrypto.
320
+ */
321
+ static VALUE
322
+ pg_s_init_ssl(VALUE self, VALUE do_ssl)
323
+ {
324
+ UNUSED( self );
325
+ PQinitSSL(pg_to_bool_int(do_ssl));
326
+ return Qnil;
327
+ }
328
+
329
+
330
+ /**************************************************************************
331
+ * Initializer
332
+ **************************************************************************/
333
+
334
+ void
335
+ Init_pg_ext(void)
336
+ {
337
+
338
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
339
+ rb_ext_ractor_safe(PQisthreadsafe());
340
+ #endif
341
+
342
+ if( RTEST(rb_eval_string("ENV['PG_SKIP_DEPRECATION_WARNING']")) ){
343
+ /* Set all bits to disable all deprecation warnings. */
344
+ pg_skip_deprecation_warning = 0xFFFF;
345
+ } else {
346
+ pg_skip_deprecation_warning = 0;
347
+ }
348
+
349
+ rb_mPG = rb_define_module( "PG" );
350
+ rb_mPGconstants = rb_define_module_under( rb_mPG, "Constants" );
351
+
352
+ /*************************
353
+ * PG module methods
354
+ *************************/
355
+ rb_define_singleton_method( rb_mPG, "library_version", pg_s_library_version, 0 );
356
+ rb_define_singleton_method( rb_mPG, "isthreadsafe", pg_s_threadsafe_p, 0 );
357
+ SINGLETON_ALIAS( rb_mPG, "is_threadsafe?", "isthreadsafe" );
358
+ SINGLETON_ALIAS( rb_mPG, "threadsafe?", "isthreadsafe" );
359
+
360
+ rb_define_singleton_method( rb_mPG, "init_openssl", pg_s_init_openssl, 2 );
361
+ rb_define_singleton_method( rb_mPG, "init_ssl", pg_s_init_ssl, 1 );
362
+
363
+
364
+ /****** PG::Connection CLASS CONSTANTS: Connection Status ******/
365
+
366
+ /* Connection succeeded */
367
+ rb_define_const(rb_mPGconstants, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
368
+ /* Connection failed */
369
+ rb_define_const(rb_mPGconstants, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
370
+
371
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection status ******/
372
+
373
+ /* Waiting for connection to be made. */
374
+ rb_define_const(rb_mPGconstants, "CONNECTION_STARTED", INT2FIX(CONNECTION_STARTED));
375
+ /* Connection OK; waiting to send. */
376
+ rb_define_const(rb_mPGconstants, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
377
+ /* Waiting for a response from the server. */
378
+ rb_define_const(rb_mPGconstants, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
379
+ /* Received authentication; waiting for backend startup. */
380
+ rb_define_const(rb_mPGconstants, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
381
+ /* This state is no longer used. */
382
+ rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
383
+ /* Negotiating SSL encryption. */
384
+ rb_define_const(rb_mPGconstants, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
385
+ /* Internal state - PG.connect() needed. */
386
+ rb_define_const(rb_mPGconstants, "CONNECTION_NEEDED", INT2FIX(CONNECTION_NEEDED));
387
+ #if PG_MAJORVERSION_NUM >= 10
388
+ /* Checking if session is read-write. Available since PostgreSQL-10. */
389
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_WRITABLE", INT2FIX(CONNECTION_CHECK_WRITABLE));
390
+ #endif
391
+ #if PG_MAJORVERSION_NUM >= 10
392
+ /* Consuming any extra messages. Available since PostgreSQL-10. */
393
+ rb_define_const(rb_mPGconstants, "CONNECTION_CONSUME", INT2FIX(CONNECTION_CONSUME));
394
+ #endif
395
+ #if PG_MAJORVERSION_NUM >= 12
396
+ /* Negotiating GSSAPI. Available since PostgreSQL-12. */
397
+ rb_define_const(rb_mPGconstants, "CONNECTION_GSS_STARTUP", INT2FIX(CONNECTION_GSS_STARTUP));
398
+ #endif
399
+ #if PG_MAJORVERSION_NUM >= 13
400
+ /* Checking target server properties. Available since PostgreSQL-13. */
401
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_TARGET", INT2FIX(CONNECTION_CHECK_TARGET));
402
+ #endif
403
+ #if PG_MAJORVERSION_NUM >= 14
404
+ /* Checking if server is in standby mode. Available since PostgreSQL-14. */
405
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_STANDBY", INT2FIX(CONNECTION_CHECK_STANDBY));
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
411
+
412
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
413
+
414
+ /* Async connection is waiting to read */
415
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
416
+ /* Async connection is waiting to write */
417
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
418
+ /* Async connection failed or was reset */
419
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
420
+ /* Async connection succeeded */
421
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
422
+
423
+ /****** PG::Connection CLASS CONSTANTS: Transaction Status ******/
424
+
425
+ /* Transaction is currently idle ( Connection#transaction_status ) */
426
+ rb_define_const(rb_mPGconstants, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
427
+ /* Transaction is currently active; query has been sent to the server, but not yet completed. ( Connection#transaction_status ) */
428
+ rb_define_const(rb_mPGconstants, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
429
+ /* Transaction is currently idle, in a valid transaction block ( Connection#transaction_status ) */
430
+ rb_define_const(rb_mPGconstants, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
431
+ /* Transaction is currently idle, in a failed transaction block ( Connection#transaction_status ) */
432
+ rb_define_const(rb_mPGconstants, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
433
+ /* Transaction's connection is bad ( Connection#transaction_status ) */
434
+ rb_define_const(rb_mPGconstants, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
435
+
436
+ /****** PG::Connection CLASS CONSTANTS: Error Verbosity ******/
437
+
438
+ /* Error verbosity level ( Connection#set_error_verbosity ).
439
+ * In TERSE mode, returned messages include severity, primary text, and position only; this will normally fit on a single line. */
440
+ rb_define_const(rb_mPGconstants, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
441
+ /* Error verbosity level ( Connection#set_error_verbosity ).
442
+ * The DEFAULT mode produces messages that include the above plus any detail, hint, or context fields (these might span multiple lines). */
443
+ rb_define_const(rb_mPGconstants, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
444
+ /* Error verbosity level ( Connection#set_error_verbosity ).
445
+ * The VERBOSE mode includes all available fields. */
446
+ rb_define_const(rb_mPGconstants, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
447
+
448
+ /* PQERRORS_SQLSTATE was introduced in PG-12 together with PQresultMemorySize() */
449
+ #ifdef HAVE_PQRESULTMEMORYSIZE
450
+ /* Error verbosity level ( Connection#set_error_verbosity ).
451
+ * The SQLSTATE mode includes only the error severity and the SQLSTATE error code, if one is available (if not, the output is like TERSE mode).
452
+ *
453
+ * Available since PostgreSQL-12.
454
+ */
455
+ rb_define_const(rb_mPGconstants, "PQERRORS_SQLSTATE", INT2FIX(PQERRORS_SQLSTATE));
456
+ #endif
457
+
458
+ /* See Connection#set_error_context_visibility */
459
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_NEVER", INT2FIX(PQSHOW_CONTEXT_NEVER));
460
+ /* See Connection#set_error_context_visibility */
461
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ERRORS", INT2FIX(PQSHOW_CONTEXT_ERRORS));
462
+ /* See Connection#set_error_context_visibility */
463
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ALWAYS", INT2FIX(PQSHOW_CONTEXT_ALWAYS));
464
+
465
+ /****** PG::Connection CLASS CONSTANTS: Check Server Status ******/
466
+
467
+ /* Server is accepting connections. */
468
+ rb_define_const(rb_mPGconstants, "PQPING_OK", INT2FIX(PQPING_OK));
469
+ /* Server is alive but rejecting connections. */
470
+ rb_define_const(rb_mPGconstants, "PQPING_REJECT", INT2FIX(PQPING_REJECT));
471
+ /* Could not establish connection. */
472
+ rb_define_const(rb_mPGconstants, "PQPING_NO_RESPONSE", INT2FIX(PQPING_NO_RESPONSE));
473
+ /* Connection not attempted (bad params). */
474
+ rb_define_const(rb_mPGconstants, "PQPING_NO_ATTEMPT", INT2FIX(PQPING_NO_ATTEMPT));
475
+
476
+ /****** PG::Connection CLASS CONSTANTS: Large Objects ******/
477
+
478
+ /* Flag for Connection#lo_creat, Connection#lo_open -- open for writing */
479
+ rb_define_const(rb_mPGconstants, "INV_WRITE", INT2FIX(INV_WRITE));
480
+ /* Flag for Connection#lo_creat, Connection#lo_open -- open for reading */
481
+ rb_define_const(rb_mPGconstants, "INV_READ", INT2FIX(INV_READ));
482
+ /* Flag for Connection#lo_lseek -- seek from object start */
483
+ rb_define_const(rb_mPGconstants, "SEEK_SET", INT2FIX(SEEK_SET));
484
+ /* Flag for Connection#lo_lseek -- seek from current position */
485
+ rb_define_const(rb_mPGconstants, "SEEK_CUR", INT2FIX(SEEK_CUR));
486
+ /* Flag for Connection#lo_lseek -- seek from object end */
487
+ rb_define_const(rb_mPGconstants, "SEEK_END", INT2FIX(SEEK_END));
488
+
489
+ /****** PG::Result CONSTANTS: result status ******/
490
+
491
+ /* Result#result_status constant - The string sent to the server was empty. */
492
+ rb_define_const(rb_mPGconstants, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
493
+ /* Result#result_status constant - Successful completion of a command returning no data. */
494
+ rb_define_const(rb_mPGconstants, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
495
+ /* Result#result_status constant - Successful completion of a command returning data (such as a SELECT or SHOW). */
496
+ rb_define_const(rb_mPGconstants, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
497
+ /* Result#result_status constant - Copy Out (from server) data transfer started. */
498
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
499
+ /* Result#result_status constant - Copy In (to server) data transfer started. */
500
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
501
+ /* Result#result_status constant - The server’s response was not understood. */
502
+ rb_define_const(rb_mPGconstants, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
503
+ /* Result#result_status constant - A nonfatal error (a notice or warning) occurred. */
504
+ rb_define_const(rb_mPGconstants, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
505
+ /* Result#result_status constant - A fatal error occurred. */
506
+ rb_define_const(rb_mPGconstants, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
507
+ /* Result#result_status constant - Copy In/Out data transfer in progress. */
508
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_BOTH", INT2FIX(PGRES_COPY_BOTH));
509
+ /* Result#result_status constant - Single tuple from larger resultset. */
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
515
+
516
+ #ifdef HAVE_PQENTERPIPELINEMODE
517
+ /* Result#result_status constant - The PG::Result represents a synchronization point in pipeline mode, requested by Connection#pipeline_sync.
518
+ *
519
+ * This status occurs only when pipeline mode has been selected. */
520
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_SYNC", INT2FIX(PGRES_PIPELINE_SYNC));
521
+
522
+ /* Result#result_status constant - The PG::Result represents a pipeline that has received an error from the server.
523
+ *
524
+ * Connection#get_result must be called repeatedly, and each time it will return this status code until the end of the current pipeline, at which point it will return PG::PGRES_PIPELINE_SYNC and normal processing can resume.
525
+ */
526
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_ABORTED", INT2FIX(PGRES_PIPELINE_ABORTED));
527
+ #endif
528
+
529
+ /****** Result CONSTANTS: result error field codes ******/
530
+
531
+ /* Result#result_error_field argument constant
532
+ *
533
+ * 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), or a localized translation
534
+ * of one of these.
535
+ * Always present.
536
+ */
537
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
538
+
539
+ /* Result#result_error_field argument constant
540
+ *
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).
542
+ * This is identical to the PG_DIAG_SEVERITY field except that the contents are never localized.
543
+ *
544
+ */
545
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY_NONLOCALIZED", INT2FIX(PG_DIAG_SEVERITY_NONLOCALIZED));
546
+
547
+ /* Result#result_error_field argument constant
548
+ *
549
+ * The SQLSTATE code for the 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.
551
+ * For a list of the possible SQLSTATE codes, see Appendix A.
552
+ * This field is not localizable, and is always present.
553
+ */
554
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
555
+ /* Result#result_error_field argument constant
556
+ *
557
+ * The primary human-readable error message (typically one line).
558
+ * Always present. */
559
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
560
+ /* Result#result_error_field argument constant
561
+ *
562
+ * Detail: an optional secondary error message carrying more detail about the problem.
563
+ * Might run to multiple lines.
564
+ */
565
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
566
+ /* Result#result_error_field argument constant
567
+ *
568
+ * Hint: an optional suggestion what to do about the problem.
569
+ * This is intended to differ from detail in that it offers advice (potentially inappropriate) rather than hard facts.
570
+ * Might run to multiple lines.
571
+ */
572
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
573
+ /* Result#result_error_field argument constant
574
+ *
575
+ * A string containing a decimal integer indicating an error cursor position as an index into the original statement string.
576
+ *
577
+ * The first character has index 1, and positions are measured in characters not bytes.
578
+ */
579
+ rb_define_const(rb_mPGconstants, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
580
+ /* Result#result_error_field argument constant
581
+ *
582
+ * This is defined the same as the PG_DIAG_STATEMENT_POSITION field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client.
583
+ * The PG_DIAG_INTERNAL_QUERY field will always appear when this field appears.
584
+ */
585
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
586
+ /* Result#result_error_field argument constant
587
+ *
588
+ * The text of a failed internally-generated command.
589
+ * This could be, for example, a SQL query issued by a PL/pgSQL function.
590
+ */
591
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
592
+ /* Result#result_error_field argument constant
593
+ *
594
+ * An indication of the context in which the error occurred.
595
+ * Presently this includes a call stack traceback of active procedural language functions and internally-generated queries.
596
+ * The trace is one entry per line, most recent first.
597
+ */
598
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
599
+ /* Result#result_error_field argument constant
600
+ *
601
+ * The file name of the source-code location where the error was reported. */
602
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
603
+
604
+ /* Result#result_error_field argument constant
605
+ *
606
+ * The line number of the source-code location where the error was reported. */
607
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
608
+
609
+ /* Result#result_error_field argument constant
610
+ *
611
+ * The name of the source-code function reporting the error. */
612
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
613
+
614
+ #ifdef PG_DIAG_TABLE_NAME
615
+ /* Result#result_error_field argument constant
616
+ *
617
+ * If the error was associated with a specific database object, the name of the schema containing that object, if any. */
618
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SCHEMA_NAME", INT2FIX(PG_DIAG_SCHEMA_NAME));
619
+
620
+ /* Result#result_error_field argument constant
621
+ *
622
+ * If the error was associated with a specific table, the name of the table.
623
+ * (When this field is present, the schema name field provides the name of the table's schema.) */
624
+ rb_define_const(rb_mPGconstants, "PG_DIAG_TABLE_NAME", INT2FIX(PG_DIAG_TABLE_NAME));
625
+
626
+ /* Result#result_error_field argument constant
627
+ *
628
+ * If the error was associated with a specific table column, the name of the column.
629
+ * (When this field is present, the schema and table name fields identify the table.) */
630
+ rb_define_const(rb_mPGconstants, "PG_DIAG_COLUMN_NAME", INT2FIX(PG_DIAG_COLUMN_NAME));
631
+
632
+ /* Result#result_error_field argument constant
633
+ *
634
+ * If the error was associated with a specific datatype, the name of the datatype.
635
+ * (When this field is present, the schema name field provides the name of the datatype's schema.) */
636
+ rb_define_const(rb_mPGconstants, "PG_DIAG_DATATYPE_NAME", INT2FIX(PG_DIAG_DATATYPE_NAME));
637
+
638
+ /* Result#result_error_field argument constant
639
+ *
640
+ * If the error was associated with a specific constraint, the name of the constraint.
641
+ * The table or domain that the constraint belongs to is reported using the fields listed above.
642
+ * (For this purpose, indexes are treated as constraints, even if they weren't created with constraint syntax.) */
643
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONSTRAINT_NAME", INT2FIX(PG_DIAG_CONSTRAINT_NAME));
644
+ #endif
645
+
646
+ #ifdef HAVE_PQENTERPIPELINEMODE
647
+ /* Connection#pipeline_status constant
648
+ *
649
+ * The libpq connection is in pipeline mode.
650
+ */
651
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ON", INT2FIX(PQ_PIPELINE_ON));
652
+
653
+ /* Connection#pipeline_status constant
654
+ *
655
+ * The libpq connection is not in pipeline mode.
656
+ */
657
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_OFF", INT2FIX(PQ_PIPELINE_OFF));
658
+
659
+ /* Connection#pipeline_status constant
660
+ *
661
+ * The libpq connection is in pipeline mode and an error occurred while processing the current pipeline.
662
+ * The aborted flag is cleared when PQgetResult returns a result of type PGRES_PIPELINE_SYNC.
663
+ */
664
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ABORTED", INT2FIX(PQ_PIPELINE_ABORTED));
665
+ #endif
666
+
667
+ /* Invalid OID constant */
668
+ rb_define_const(rb_mPGconstants, "INVALID_OID", INT2FIX(InvalidOid));
669
+ rb_define_const(rb_mPGconstants, "InvalidOid", INT2FIX(InvalidOid));
670
+
671
+ /* PostgreSQL compiled in default port */
672
+ rb_define_const(rb_mPGconstants, "DEF_PGPORT", INT2FIX(DEF_PGPORT));
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
+
680
+ /* Add the constants to the toplevel namespace */
681
+ rb_include_module( rb_mPG, rb_mPGconstants );
682
+
683
+ /* Initialize the main extension classes */
684
+ init_pg_connection();
685
+ init_pg_result();
686
+ init_pg_errors();
687
+ init_pg_type_map();
688
+ init_pg_type_map_all_strings();
689
+ init_pg_type_map_by_class();
690
+ init_pg_type_map_by_column();
691
+ init_pg_type_map_by_mri_type();
692
+ init_pg_type_map_by_oid();
693
+ init_pg_type_map_in_ruby();
694
+ init_pg_coder();
695
+ init_pg_text_encoder();
696
+ init_pg_text_decoder();
697
+ init_pg_binary_encoder();
698
+ init_pg_binary_decoder();
699
+ init_pg_copycoder();
700
+ init_pg_recordcoder();
701
+ init_pg_tuple();
702
+ init_pg_cancon();
703
+ }