yugabyte_ysql 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.appveyor.yml +42 -0
  3. data/.gems +6 -0
  4. data/.gemtest +0 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +143 -0
  7. data/.gitignore +24 -0
  8. data/.hgsigs +34 -0
  9. data/.hgtags +41 -0
  10. data/.irbrc +23 -0
  11. data/.pryrc +23 -0
  12. data/.tm_properties +21 -0
  13. data/.travis.yml +49 -0
  14. data/BSDL +22 -0
  15. data/Contributors.rdoc +46 -0
  16. data/Gemfile +18 -0
  17. data/History.md +901 -0
  18. data/LICENSE +56 -0
  19. data/Manifest.txt +73 -0
  20. data/POSTGRES +23 -0
  21. data/README-OS_X.rdoc +68 -0
  22. data/README-Windows.rdoc +56 -0
  23. data/README.ja.md +302 -0
  24. data/README.md +373 -0
  25. data/Rakefile +118 -0
  26. data/Rakefile.cross +299 -0
  27. data/certs/ged.pem +24 -0
  28. data/certs/kanis@comcard.de.pem +20 -0
  29. data/certs/larskanis-2022.pem +26 -0
  30. data/certs/larskanis-2023.pem +24 -0
  31. data/certs/larskanis-2024.pem +24 -0
  32. data/ext/errorcodes.def +1044 -0
  33. data/ext/errorcodes.rb +45 -0
  34. data/ext/errorcodes.txt +497 -0
  35. data/ext/extconf.rb +174 -0
  36. data/ext/gvl_wrappers.c +21 -0
  37. data/ext/gvl_wrappers.h +264 -0
  38. data/ext/pg.c +692 -0
  39. data/ext/pg.h +392 -0
  40. data/ext/pg_binary_decoder.c +308 -0
  41. data/ext/pg_binary_encoder.c +387 -0
  42. data/ext/pg_coder.c +624 -0
  43. data/ext/pg_connection.c +4681 -0
  44. data/ext/pg_copy_coder.c +917 -0
  45. data/ext/pg_errors.c +95 -0
  46. data/ext/pg_record_coder.c +522 -0
  47. data/ext/pg_result.c +1766 -0
  48. data/ext/pg_text_decoder.c +1005 -0
  49. data/ext/pg_text_encoder.c +827 -0
  50. data/ext/pg_tuple.c +572 -0
  51. data/ext/pg_type_map.c +200 -0
  52. data/ext/pg_type_map_all_strings.c +130 -0
  53. data/ext/pg_type_map_by_class.c +271 -0
  54. data/ext/pg_type_map_by_column.c +355 -0
  55. data/ext/pg_type_map_by_mri_type.c +313 -0
  56. data/ext/pg_type_map_by_oid.c +388 -0
  57. data/ext/pg_type_map_in_ruby.c +333 -0
  58. data/ext/pg_util.c +149 -0
  59. data/ext/pg_util.h +65 -0
  60. data/ext/vc/pg.sln +26 -0
  61. data/ext/vc/pg_18/pg.vcproj +216 -0
  62. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  63. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  64. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  65. data/lib/pg/basic_type_map_for_results.rb +104 -0
  66. data/lib/pg/basic_type_registry.rb +303 -0
  67. data/lib/pg/binary_decoder/date.rb +9 -0
  68. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  69. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  70. data/lib/pg/coder.rb +106 -0
  71. data/lib/pg/connection.rb +990 -0
  72. data/lib/pg/exceptions.rb +25 -0
  73. data/lib/pg/load_balance_service.rb +406 -0
  74. data/lib/pg/result.rb +43 -0
  75. data/lib/pg/text_decoder/date.rb +18 -0
  76. data/lib/pg/text_decoder/inet.rb +9 -0
  77. data/lib/pg/text_decoder/json.rb +14 -0
  78. data/lib/pg/text_decoder/numeric.rb +9 -0
  79. data/lib/pg/text_decoder/timestamp.rb +30 -0
  80. data/lib/pg/text_encoder/date.rb +12 -0
  81. data/lib/pg/text_encoder/inet.rb +28 -0
  82. data/lib/pg/text_encoder/json.rb +14 -0
  83. data/lib/pg/text_encoder/numeric.rb +9 -0
  84. data/lib/pg/text_encoder/timestamp.rb +24 -0
  85. data/lib/pg/tuple.rb +30 -0
  86. data/lib/pg/type_map_by_column.rb +16 -0
  87. data/lib/pg/version.rb +5 -0
  88. data/lib/yugabyte_ysql.rb +130 -0
  89. data/misc/openssl-pg-segfault.rb +31 -0
  90. data/misc/postgres/History.txt +9 -0
  91. data/misc/postgres/Manifest.txt +5 -0
  92. data/misc/postgres/README.txt +21 -0
  93. data/misc/postgres/Rakefile +21 -0
  94. data/misc/postgres/lib/postgres.rb +16 -0
  95. data/misc/ruby-pg/History.txt +9 -0
  96. data/misc/ruby-pg/Manifest.txt +5 -0
  97. data/misc/ruby-pg/README.txt +21 -0
  98. data/misc/ruby-pg/Rakefile +21 -0
  99. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  100. data/rakelib/task_extension.rb +46 -0
  101. data/sample/array_insert.rb +20 -0
  102. data/sample/async_api.rb +102 -0
  103. data/sample/async_copyto.rb +39 -0
  104. data/sample/async_mixed.rb +56 -0
  105. data/sample/check_conn.rb +21 -0
  106. data/sample/copydata.rb +71 -0
  107. data/sample/copyfrom.rb +81 -0
  108. data/sample/copyto.rb +19 -0
  109. data/sample/cursor.rb +21 -0
  110. data/sample/disk_usage_report.rb +177 -0
  111. data/sample/issue-119.rb +94 -0
  112. data/sample/losample.rb +69 -0
  113. data/sample/minimal-testcase.rb +17 -0
  114. data/sample/notify_wait.rb +72 -0
  115. data/sample/pg_statistics.rb +285 -0
  116. data/sample/replication_monitor.rb +222 -0
  117. data/sample/test_binary_values.rb +33 -0
  118. data/sample/wal_shipper.rb +434 -0
  119. data/sample/warehouse_partitions.rb +311 -0
  120. data/yugabyte_ysql.gemspec +33 -0
  121. metadata +232 -0
data/ext/pg.c ADDED
@@ -0,0 +1,692 @@
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( "YugabyteYSQL" );
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
+
408
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
409
+
410
+ /* Async connection is waiting to read */
411
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
412
+ /* Async connection is waiting to write */
413
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
414
+ /* Async connection failed or was reset */
415
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
416
+ /* Async connection succeeded */
417
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
418
+
419
+ /****** PG::Connection CLASS CONSTANTS: Transaction Status ******/
420
+
421
+ /* Transaction is currently idle ( Connection#transaction_status ) */
422
+ rb_define_const(rb_mPGconstants, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
423
+ /* Transaction is currently active; query has been sent to the server, but not yet completed. ( Connection#transaction_status ) */
424
+ rb_define_const(rb_mPGconstants, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
425
+ /* Transaction is currently idle, in a valid transaction block ( Connection#transaction_status ) */
426
+ rb_define_const(rb_mPGconstants, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
427
+ /* Transaction is currently idle, in a failed transaction block ( Connection#transaction_status ) */
428
+ rb_define_const(rb_mPGconstants, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
429
+ /* Transaction's connection is bad ( Connection#transaction_status ) */
430
+ rb_define_const(rb_mPGconstants, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
431
+
432
+ /****** PG::Connection CLASS CONSTANTS: Error Verbosity ******/
433
+
434
+ /* Error verbosity level ( Connection#set_error_verbosity ).
435
+ * In TERSE mode, returned messages include severity, primary text, and position only; this will normally fit on a single line. */
436
+ rb_define_const(rb_mPGconstants, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
437
+ /* Error verbosity level ( Connection#set_error_verbosity ).
438
+ * The DEFAULT mode produces messages that include the above plus any detail, hint, or context fields (these might span multiple lines). */
439
+ rb_define_const(rb_mPGconstants, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
440
+ /* Error verbosity level ( Connection#set_error_verbosity ).
441
+ * The VERBOSE mode includes all available fields. */
442
+ rb_define_const(rb_mPGconstants, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
443
+
444
+ /* PQERRORS_SQLSTATE was introduced in PG-12 together with PQresultMemorySize() */
445
+ #ifdef HAVE_PQRESULTMEMORYSIZE
446
+ /* Error verbosity level ( Connection#set_error_verbosity ).
447
+ * 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).
448
+ *
449
+ * Available since PostgreSQL-12.
450
+ */
451
+ rb_define_const(rb_mPGconstants, "PQERRORS_SQLSTATE", INT2FIX(PQERRORS_SQLSTATE));
452
+ #endif
453
+
454
+ #ifdef HAVE_PQRESULTVERBOSEERRORMESSAGE
455
+ /* See Connection#set_error_context_visibility */
456
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_NEVER", INT2FIX(PQSHOW_CONTEXT_NEVER));
457
+ /* See Connection#set_error_context_visibility */
458
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ERRORS", INT2FIX(PQSHOW_CONTEXT_ERRORS));
459
+ /* See Connection#set_error_context_visibility */
460
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ALWAYS", INT2FIX(PQSHOW_CONTEXT_ALWAYS));
461
+ #endif
462
+
463
+ /****** PG::Connection CLASS CONSTANTS: Check Server Status ******/
464
+
465
+ /* Server is accepting connections. */
466
+ rb_define_const(rb_mPGconstants, "PQPING_OK", INT2FIX(PQPING_OK));
467
+ /* Server is alive but rejecting connections. */
468
+ rb_define_const(rb_mPGconstants, "PQPING_REJECT", INT2FIX(PQPING_REJECT));
469
+ /* Could not establish connection. */
470
+ rb_define_const(rb_mPGconstants, "PQPING_NO_RESPONSE", INT2FIX(PQPING_NO_RESPONSE));
471
+ /* Connection not attempted (bad params). */
472
+ rb_define_const(rb_mPGconstants, "PQPING_NO_ATTEMPT", INT2FIX(PQPING_NO_ATTEMPT));
473
+
474
+ /****** PG::Connection CLASS CONSTANTS: Large Objects ******/
475
+
476
+ /* Flag for Connection#lo_creat, Connection#lo_open -- open for writing */
477
+ rb_define_const(rb_mPGconstants, "INV_WRITE", INT2FIX(INV_WRITE));
478
+ /* Flag for Connection#lo_creat, Connection#lo_open -- open for reading */
479
+ rb_define_const(rb_mPGconstants, "INV_READ", INT2FIX(INV_READ));
480
+ /* Flag for Connection#lo_lseek -- seek from object start */
481
+ rb_define_const(rb_mPGconstants, "SEEK_SET", INT2FIX(SEEK_SET));
482
+ /* Flag for Connection#lo_lseek -- seek from current position */
483
+ rb_define_const(rb_mPGconstants, "SEEK_CUR", INT2FIX(SEEK_CUR));
484
+ /* Flag for Connection#lo_lseek -- seek from object end */
485
+ rb_define_const(rb_mPGconstants, "SEEK_END", INT2FIX(SEEK_END));
486
+
487
+ /****** PG::Result CONSTANTS: result status ******/
488
+
489
+ /* Result#result_status constant - The string sent to the server was empty. */
490
+ rb_define_const(rb_mPGconstants, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
491
+ /* Result#result_status constant - Successful completion of a command returning no data. */
492
+ rb_define_const(rb_mPGconstants, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
493
+ /* Result#result_status constant - Successful completion of a command returning data (such as a SELECT or SHOW). */
494
+ rb_define_const(rb_mPGconstants, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
495
+ /* Result#result_status constant - Copy Out (from server) data transfer started. */
496
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
497
+ /* Result#result_status constant - Copy In (to server) data transfer started. */
498
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
499
+ /* Result#result_status constant - The server’s response was not understood. */
500
+ rb_define_const(rb_mPGconstants, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
501
+ /* Result#result_status constant - A nonfatal error (a notice or warning) occurred. */
502
+ rb_define_const(rb_mPGconstants, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
503
+ /* Result#result_status constant - A fatal error occurred. */
504
+ rb_define_const(rb_mPGconstants, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
505
+ /* Result#result_status constant - Copy In/Out data transfer in progress. */
506
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_BOTH", INT2FIX(PGRES_COPY_BOTH));
507
+ /* Result#result_status constant - Single tuple from larger resultset. */
508
+ rb_define_const(rb_mPGconstants, "PGRES_SINGLE_TUPLE", INT2FIX(PGRES_SINGLE_TUPLE));
509
+
510
+ #ifdef HAVE_PQENTERPIPELINEMODE
511
+ /* Result#result_status constant - The PG::Result represents a synchronization point in pipeline mode, requested by Connection#pipeline_sync.
512
+ *
513
+ * This status occurs only when pipeline mode has been selected. */
514
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_SYNC", INT2FIX(PGRES_PIPELINE_SYNC));
515
+
516
+ /* Result#result_status constant - The PG::Result represents a pipeline that has received an error from the server.
517
+ *
518
+ * 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.
519
+ */
520
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_ABORTED", INT2FIX(PGRES_PIPELINE_ABORTED));
521
+ #endif
522
+
523
+ /****** Result CONSTANTS: result error field codes ******/
524
+
525
+ /* Result#result_error_field argument constant
526
+ *
527
+ * 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
528
+ * of one of these.
529
+ * Always present.
530
+ */
531
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
532
+
533
+ #ifdef PG_DIAG_SEVERITY_NONLOCALIZED
534
+ /* Result#result_error_field argument constant
535
+ *
536
+ * 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
+ * This is identical to the PG_DIAG_SEVERITY field except that the contents are never localized.
538
+ *
539
+ * Available since PostgreSQL-9.6
540
+ */
541
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY_NONLOCALIZED", INT2FIX(PG_DIAG_SEVERITY_NONLOCALIZED));
542
+ #endif
543
+ /* Result#result_error_field argument constant
544
+ *
545
+ * 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.
547
+ * For a list of the possible SQLSTATE codes, see Appendix A.
548
+ * This field is not localizable, and is always present.
549
+ */
550
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
551
+ /* Result#result_error_field argument constant
552
+ *
553
+ * The primary human-readable error message (typically one line).
554
+ * Always present. */
555
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
556
+ /* Result#result_error_field argument constant
557
+ *
558
+ * Detail: an optional secondary error message carrying more detail about the problem.
559
+ * Might run to multiple lines.
560
+ */
561
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
562
+ /* Result#result_error_field argument constant
563
+ *
564
+ * Hint: an optional suggestion what to do about the problem.
565
+ * This is intended to differ from detail in that it offers advice (potentially inappropriate) rather than hard facts.
566
+ * Might run to multiple lines.
567
+ */
568
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
569
+ /* Result#result_error_field argument constant
570
+ *
571
+ * A string containing a decimal integer indicating an error cursor position as an index into the original statement string.
572
+ *
573
+ * The first character has index 1, and positions are measured in characters not bytes.
574
+ */
575
+ rb_define_const(rb_mPGconstants, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
576
+ /* Result#result_error_field argument constant
577
+ *
578
+ * 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.
579
+ * The PG_DIAG_INTERNAL_QUERY field will always appear when this field appears.
580
+ */
581
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
582
+ /* Result#result_error_field argument constant
583
+ *
584
+ * The text of a failed internally-generated command.
585
+ * This could be, for example, a SQL query issued by a PL/pgSQL function.
586
+ */
587
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
588
+ /* Result#result_error_field argument constant
589
+ *
590
+ * An indication of the context in which the error occurred.
591
+ * Presently this includes a call stack traceback of active procedural language functions and internally-generated queries.
592
+ * The trace is one entry per line, most recent first.
593
+ */
594
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
595
+ /* Result#result_error_field argument constant
596
+ *
597
+ * The file name of the source-code location where the error was reported. */
598
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
599
+
600
+ /* Result#result_error_field argument constant
601
+ *
602
+ * The line number of the source-code location where the error was reported. */
603
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
604
+
605
+ /* Result#result_error_field argument constant
606
+ *
607
+ * The name of the source-code function reporting the error. */
608
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
609
+
610
+ #ifdef PG_DIAG_TABLE_NAME
611
+ /* Result#result_error_field argument constant
612
+ *
613
+ * If the error was associated with a specific database object, the name of the schema containing that object, if any. */
614
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SCHEMA_NAME", INT2FIX(PG_DIAG_SCHEMA_NAME));
615
+
616
+ /* Result#result_error_field argument constant
617
+ *
618
+ * If the error was associated with a specific table, the name of the table.
619
+ * (When this field is present, the schema name field provides the name of the table's schema.) */
620
+ rb_define_const(rb_mPGconstants, "PG_DIAG_TABLE_NAME", INT2FIX(PG_DIAG_TABLE_NAME));
621
+
622
+ /* Result#result_error_field argument constant
623
+ *
624
+ * If the error was associated with a specific table column, the name of the column.
625
+ * (When this field is present, the schema and table name fields identify the table.) */
626
+ rb_define_const(rb_mPGconstants, "PG_DIAG_COLUMN_NAME", INT2FIX(PG_DIAG_COLUMN_NAME));
627
+
628
+ /* Result#result_error_field argument constant
629
+ *
630
+ * If the error was associated with a specific datatype, the name of the datatype.
631
+ * (When this field is present, the schema name field provides the name of the datatype's schema.) */
632
+ rb_define_const(rb_mPGconstants, "PG_DIAG_DATATYPE_NAME", INT2FIX(PG_DIAG_DATATYPE_NAME));
633
+
634
+ /* Result#result_error_field argument constant
635
+ *
636
+ * If the error was associated with a specific constraint, the name of the constraint.
637
+ * The table or domain that the constraint belongs to is reported using the fields listed above.
638
+ * (For this purpose, indexes are treated as constraints, even if they weren't created with constraint syntax.) */
639
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONSTRAINT_NAME", INT2FIX(PG_DIAG_CONSTRAINT_NAME));
640
+ #endif
641
+
642
+ #ifdef HAVE_PQENTERPIPELINEMODE
643
+ /* Connection#pipeline_status constant
644
+ *
645
+ * The libpq connection is in pipeline mode.
646
+ */
647
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ON", INT2FIX(PQ_PIPELINE_ON));
648
+
649
+ /* Connection#pipeline_status constant
650
+ *
651
+ * The libpq connection is not in pipeline mode.
652
+ */
653
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_OFF", INT2FIX(PQ_PIPELINE_OFF));
654
+
655
+ /* Connection#pipeline_status constant
656
+ *
657
+ * The libpq connection is in pipeline mode and an error occurred while processing the current pipeline.
658
+ * The aborted flag is cleared when PQgetResult returns a result of type PGRES_PIPELINE_SYNC.
659
+ */
660
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ABORTED", INT2FIX(PQ_PIPELINE_ABORTED));
661
+ #endif
662
+
663
+ /* Invalid OID constant */
664
+ rb_define_const(rb_mPGconstants, "INVALID_OID", INT2FIX(InvalidOid));
665
+ rb_define_const(rb_mPGconstants, "InvalidOid", INT2FIX(InvalidOid));
666
+
667
+ /* PostgreSQL compiled in default port */
668
+ rb_define_const(rb_mPGconstants, "DEF_PGPORT", INT2FIX(DEF_PGPORT));
669
+
670
+ /* Add the constants to the toplevel namespace */
671
+ rb_include_module( rb_mPG, rb_mPGconstants );
672
+
673
+ /* Initialize the main extension classes */
674
+ init_pg_connection();
675
+ init_pg_result();
676
+ init_pg_errors();
677
+ init_pg_type_map();
678
+ init_pg_type_map_all_strings();
679
+ init_pg_type_map_by_class();
680
+ init_pg_type_map_by_column();
681
+ init_pg_type_map_by_mri_type();
682
+ init_pg_type_map_by_oid();
683
+ init_pg_type_map_in_ruby();
684
+ init_pg_coder();
685
+ init_pg_text_encoder();
686
+ init_pg_text_decoder();
687
+ init_pg_binary_encoder();
688
+ init_pg_binary_decoder();
689
+ init_pg_copycoder();
690
+ init_pg_recordcoder();
691
+ init_pg_tuple();
692
+ }