pg_jruby 0.14.1.rc2-java → 0.17.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -0
  4. data/BSDL +22 -0
  5. data/ChangeLog +0 -0
  6. data/Contributors.rdoc +45 -0
  7. data/History.rdoc +270 -0
  8. data/LICENSE +56 -0
  9. data/Manifest.txt +51 -5
  10. data/POSTGRES +23 -0
  11. data/README-OS_X.rdoc +68 -0
  12. data/README-Windows.rdoc +67 -0
  13. data/README.ja.rdoc +14 -0
  14. data/README.rdoc +52 -94
  15. data/Rakefile +181 -73
  16. data/Rakefile.cross +273 -0
  17. data/ext/errorcodes.def +931 -0
  18. data/ext/errorcodes.rb +99 -0
  19. data/ext/errorcodes.txt +463 -0
  20. data/ext/extconf.rb +97 -0
  21. data/ext/gvl_wrappers.c +13 -0
  22. data/ext/gvl_wrappers.h +253 -0
  23. data/ext/pg.c +545 -0
  24. data/ext/pg.h +156 -0
  25. data/ext/pg_connection.c +3643 -0
  26. data/ext/pg_errors.c +89 -0
  27. data/ext/pg_result.c +920 -0
  28. data/ext/vc/pg.sln +26 -0
  29. data/ext/vc/pg_18/pg.vcproj +216 -0
  30. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  31. data/lib/pg.rb +12 -18
  32. data/lib/pg/connection.rb +117 -10
  33. data/lib/pg/exceptions.rb +2 -8
  34. data/lib/pg/result.rb +6 -1
  35. data/lib/pg_ext.jar +0 -0
  36. data/sample/array_insert.rb +20 -0
  37. data/sample/async_api.rb +106 -0
  38. data/sample/async_copyto.rb +39 -0
  39. data/sample/async_mixed.rb +56 -0
  40. data/sample/check_conn.rb +21 -0
  41. data/sample/copyfrom.rb +81 -0
  42. data/sample/copyto.rb +19 -0
  43. data/sample/cursor.rb +21 -0
  44. data/sample/disk_usage_report.rb +186 -0
  45. data/sample/issue-119.rb +94 -0
  46. data/sample/losample.rb +69 -0
  47. data/sample/minimal-testcase.rb +17 -0
  48. data/sample/notify_wait.rb +72 -0
  49. data/sample/pg_statistics.rb +294 -0
  50. data/sample/replication_monitor.rb +231 -0
  51. data/sample/test_binary_values.rb +33 -0
  52. data/sample/wal_shipper.rb +434 -0
  53. data/sample/warehouse_partitions.rb +320 -0
  54. data/spec/data/expected_trace.out +26 -0
  55. data/spec/data/random_binary_data +0 -0
  56. data/spec/lib/helpers.rb +350 -0
  57. data/spec/pg/connection_spec.rb +1276 -0
  58. data/spec/pg/result_spec.rb +345 -0
  59. data/spec/pg_spec.rb +44 -0
  60. metadata +136 -90
  61. metadata.gz.sig +0 -0
  62. data/CHANGELOG.rdoc +0 -7
  63. data/bin/pg +0 -3
@@ -0,0 +1,97 @@
1
+ require 'pp'
2
+ require 'mkmf'
3
+
4
+
5
+ if ENV['MAINTAINER_MODE']
6
+ $stderr.puts "Maintainer mode enabled."
7
+ $CFLAGS <<
8
+ ' -Wall' <<
9
+ ' -ggdb' <<
10
+ ' -DDEBUG' <<
11
+ ' -pedantic'
12
+ end
13
+
14
+ if pgdir = with_config( 'pg' )
15
+ ENV['PATH'] = "#{pgdir}/bin" + File::PATH_SEPARATOR + ENV['PATH']
16
+ end
17
+
18
+ if ENV['CROSS_COMPILING']
19
+ $LDFLAGS << " -L#{CONFIG['libdir']}"
20
+
21
+ # Link against all required libraries for static build, if they are available
22
+ have_library( 'crypt32', 'CertOpenStore' ) && append_library( $libs, 'crypt32' )
23
+ have_library( 'gdi32', 'CreateDC' ) && append_library( $libs, 'gdi32' )
24
+ have_library( 'secur32' ) && append_library( $libs, 'secur32' )
25
+ have_library( 'ws2_32', 'WSASocket') && append_library( $libs, 'ws2_32' )
26
+ have_library( 'crypto', 'BIO_new' ) && append_library( $libs, 'crypto' )
27
+ have_library( 'ssl', 'SSL_new' ) && append_library( $libs, 'ssl' )
28
+ end
29
+
30
+ if pgconfig = ( with_config('pg-config') || with_config('pg_config') || find_executable('pg_config') )
31
+ $stderr.puts "Using config values from %s" % [ pgconfig ]
32
+ incdir = `"#{pgconfig}" --includedir`.chomp
33
+ libdir = `"#{pgconfig}" --libdir`.chomp
34
+ dir_config 'pg', incdir, libdir
35
+
36
+ # Try to use runtime path linker option, even if RbConfig doesn't know about it.
37
+ # The rpath option is usually set implicit by dir_config(), but so far not
38
+ # on MacOS-X.
39
+ if RbConfig::CONFIG["RPATHFLAG"].to_s.empty? && try_link('int main() {return 0;}', " -Wl,-rpath,#{libdir}")
40
+ $LDFLAGS << " -Wl,-rpath,#{libdir}"
41
+ end
42
+ else
43
+ $stderr.puts "No pg_config... trying anyway. If building fails, please try again with",
44
+ " --with-pg-config=/path/to/pg_config"
45
+ dir_config 'pg'
46
+ end
47
+
48
+ find_header( 'libpq-fe.h' ) or abort "Can't find the 'libpq-fe.h header"
49
+ find_header( 'libpq/libpq-fs.h' ) or abort "Can't find the 'libpq/libpq-fs.h header"
50
+ find_header( 'pg_config_manual.h' ) or abort "Can't find the 'pg_config_manual.h' header"
51
+
52
+ abort "Can't find the PostgreSQL client library (libpq)" unless
53
+ have_library( 'pq', 'PQconnectdb', ['libpq-fe.h'] ) ||
54
+ have_library( 'libpq', 'PQconnectdb', ['libpq-fe.h'] ) ||
55
+ have_library( 'ms/libpq', 'PQconnectdb', ['libpq-fe.h'] )
56
+
57
+ # optional headers/functions
58
+ have_func 'PQconnectionUsedPassword' or
59
+ abort "Your PostgreSQL is too old. Either install an older version " +
60
+ "of this gem or upgrade your database."
61
+ have_func 'PQisthreadsafe'
62
+ have_func 'PQprepare'
63
+ have_func 'PQexecParams'
64
+ have_func 'PQescapeString'
65
+ have_func 'PQescapeStringConn'
66
+ have_func 'PQescapeLiteral'
67
+ have_func 'PQescapeIdentifier'
68
+ have_func 'PQgetCancel'
69
+ have_func 'lo_create'
70
+ have_func 'pg_encoding_to_char'
71
+ have_func 'pg_char_to_encoding'
72
+ have_func 'PQsetClientEncoding'
73
+ have_func 'PQlibVersion'
74
+ have_func 'PQping'
75
+ have_func 'PQsetSingleRowMode'
76
+
77
+ have_func 'rb_encdb_alias'
78
+ have_func 'rb_enc_alias'
79
+ have_func 'rb_thread_call_without_gvl'
80
+ have_func 'rb_thread_call_with_gvl'
81
+ have_func 'rb_thread_fd_select'
82
+ have_func 'rb_w32_wrap_io_handle'
83
+
84
+ have_const 'PGRES_COPY_BOTH', 'libpq-fe.h'
85
+ have_const 'PGRES_SINGLE_TUPLE', 'libpq-fe.h'
86
+ have_const 'PG_DIAG_TABLE_NAME', 'libpq-fe.h'
87
+
88
+ $defs.push( "-DHAVE_ST_NOTIFY_EXTRA" ) if
89
+ have_struct_member 'struct pgNotify', 'extra', 'libpq-fe.h'
90
+
91
+ # unistd.h confilicts with ruby/win32.h when cross compiling for win32 and ruby 1.9.1
92
+ have_header 'unistd.h'
93
+ have_header 'ruby/st.h' or have_header 'st.h' or abort "pg currently requires the ruby/st.h header"
94
+
95
+ create_header()
96
+ create_makefile( "pg_ext" )
97
+
@@ -0,0 +1,13 @@
1
+ /*
2
+ * gvl_wrappers.c - Wrapper functions for locking/unlocking the Ruby GVL
3
+ *
4
+ */
5
+
6
+ #include "pg.h"
7
+
8
+ FOR_EACH_BLOCKING_FUNCTION( DEFINE_GVL_WRAPPER_STRUCT );
9
+ FOR_EACH_BLOCKING_FUNCTION( DEFINE_GVL_SKELETON );
10
+ FOR_EACH_BLOCKING_FUNCTION( DEFINE_GVL_STUB );
11
+ FOR_EACH_CALLBACK_FUNCTION( DEFINE_GVL_WRAPPER_STRUCT );
12
+ FOR_EACH_CALLBACK_FUNCTION( DEFINE_GVLCB_SKELETON );
13
+ FOR_EACH_CALLBACK_FUNCTION( DEFINE_GVLCB_STUB );
@@ -0,0 +1,253 @@
1
+ /*
2
+ * gvl_wrappers.h - Wrapper functions for locking/unlocking the Ruby GVL
3
+ *
4
+ * These are some obscure preprocessor directives that allow to generate
5
+ * drop-in replacement wrapper functions in a declarative manner.
6
+ * These wrapper functions ensure that ruby's GVL is released on each
7
+ * function call and reacquired at the end of the call or in callbacks.
8
+ * This way blocking functions calls don't block concurrent ruby threads.
9
+ *
10
+ * The wrapper of each function is prefixed by "gvl_".
11
+ *
12
+ * Use "gcc -E" to retrieve the generated code.
13
+ */
14
+
15
+ #ifndef __gvl_wrappers_h
16
+ #define __gvl_wrappers_h
17
+
18
+ #if defined(HAVE_RB_THREAD_CALL_WITH_GVL)
19
+ extern void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1);
20
+ #endif
21
+
22
+ #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
23
+ extern void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
24
+ rb_unblock_function_t *ubf, void *data2);
25
+ #endif
26
+
27
+ #define DEFINE_PARAM_LIST1(type, name) \
28
+ name,
29
+
30
+ #define DEFINE_PARAM_LIST2(type, name) \
31
+ p->params.name,
32
+
33
+ #define DEFINE_PARAM_LIST3(type, name) \
34
+ type name,
35
+
36
+ #define DEFINE_PARAM_DECL(type, name) \
37
+ type name;
38
+
39
+ #define DEFINE_GVL_WRAPPER_STRUCT(name, when_non_void, rettype, lastparamtype, lastparamname) \
40
+ struct gvl_wrapper_##name##_params { \
41
+ struct { \
42
+ FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_DECL) \
43
+ lastparamtype lastparamname; \
44
+ } params; \
45
+ when_non_void( rettype retval; ) \
46
+ };
47
+
48
+ #define DEFINE_GVL_SKELETON(name, when_non_void, rettype, lastparamtype, lastparamname) \
49
+ static void * gvl_##name##_skeleton( void *data ){ \
50
+ struct gvl_wrapper_##name##_params *p = (struct gvl_wrapper_##name##_params*)data; \
51
+ when_non_void( p->retval = ) \
52
+ name( FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST2) p->params.lastparamname ); \
53
+ return NULL; \
54
+ }
55
+
56
+ #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
57
+ #define DEFINE_GVL_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
58
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
59
+ struct gvl_wrapper_##name##_params params = { \
60
+ {FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST1) lastparamname}, when_non_void((rettype)0) \
61
+ }; \
62
+ rb_thread_call_without_gvl(gvl_##name##_skeleton, &params, RUBY_UBF_IO, 0); \
63
+ when_non_void( return params.retval; ) \
64
+ }
65
+ #else
66
+ #define DEFINE_GVL_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
67
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
68
+ return name( FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST1) lastparamname ); \
69
+ }
70
+ #endif
71
+
72
+ #define DEFINE_GVL_STUB_DECL(name, when_non_void, rettype, lastparamtype, lastparamname) \
73
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname);
74
+
75
+ #define DEFINE_GVLCB_SKELETON(name, when_non_void, rettype, lastparamtype, lastparamname) \
76
+ static void * gvl_##name##_skeleton( void *data ){ \
77
+ struct gvl_wrapper_##name##_params *p = (struct gvl_wrapper_##name##_params*)data; \
78
+ when_non_void( p->retval = ) \
79
+ name( FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST2) p->params.lastparamname ); \
80
+ return NULL; \
81
+ }
82
+
83
+ #if defined(HAVE_RB_THREAD_CALL_WITH_GVL)
84
+ #define DEFINE_GVLCB_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
85
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
86
+ struct gvl_wrapper_##name##_params params = { \
87
+ {FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST1) lastparamname}, when_non_void((rettype)0) \
88
+ }; \
89
+ rb_thread_call_with_gvl(gvl_##name##_skeleton, &params); \
90
+ when_non_void( return params.retval; ) \
91
+ }
92
+ #else
93
+ #define DEFINE_GVLCB_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
94
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
95
+ return name( FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST1) lastparamname ); \
96
+ }
97
+ #endif
98
+
99
+ #define GVL_TYPE_VOID(string)
100
+ #define GVL_TYPE_NONVOID(string) string
101
+
102
+
103
+ /*
104
+ * Definitions of blocking functions and their parameters
105
+ */
106
+
107
+ #define FOR_EACH_PARAM_OF_PQconnectdb(param)
108
+
109
+ #define FOR_EACH_PARAM_OF_PQconnectStart(param)
110
+
111
+ #define FOR_EACH_PARAM_OF_PQconnectPoll(param)
112
+
113
+ #define FOR_EACH_PARAM_OF_PQreset(param)
114
+
115
+ #define FOR_EACH_PARAM_OF_PQresetStart(param)
116
+
117
+ #define FOR_EACH_PARAM_OF_PQresetPoll(param)
118
+
119
+ #define FOR_EACH_PARAM_OF_PQexec(param) \
120
+ param(PGconn *, conn)
121
+
122
+ #define FOR_EACH_PARAM_OF_PQexecParams(param) \
123
+ param(PGconn *, conn) \
124
+ param(const char *, command) \
125
+ param(int, nParams) \
126
+ param(const Oid *, paramTypes) \
127
+ param(const char * const *, paramValues) \
128
+ param(const int *, paramLengths) \
129
+ param(const int *, paramFormats)
130
+
131
+ #define FOR_EACH_PARAM_OF_PQexecPrepared(param) \
132
+ param(PGconn *, conn) \
133
+ param(const char *, stmtName) \
134
+ param(int, nParams) \
135
+ param(const char * const *, paramValues) \
136
+ param(const int *, paramLengths) \
137
+ param(const int *, paramFormats)
138
+
139
+ #define FOR_EACH_PARAM_OF_PQprepare(param) \
140
+ param(PGconn *, conn) \
141
+ param(const char *, stmtName) \
142
+ param(const char *, query) \
143
+ param(int, nParams)
144
+
145
+ #define FOR_EACH_PARAM_OF_PQdescribePrepared(param) \
146
+ param(PGconn *, conn)
147
+
148
+ #define FOR_EACH_PARAM_OF_PQdescribePortal(param) \
149
+ param(PGconn *, conn)
150
+
151
+ #define FOR_EACH_PARAM_OF_PQgetResult(param)
152
+
153
+ #define FOR_EACH_PARAM_OF_PQputCopyData(param) \
154
+ param(PGconn *, conn) \
155
+ param(const char *, buffer)
156
+
157
+ #define FOR_EACH_PARAM_OF_PQputCopyEnd(param) \
158
+ param(PGconn *, conn)
159
+
160
+ #define FOR_EACH_PARAM_OF_PQgetCopyData(param) \
161
+ param(PGconn *, conn) \
162
+ param(char **, buffer)
163
+
164
+ #define FOR_EACH_PARAM_OF_PQnotifies(param)
165
+
166
+ #define FOR_EACH_PARAM_OF_PQsendQuery(param) \
167
+ param(PGconn *, conn)
168
+
169
+ #define FOR_EACH_PARAM_OF_PQsendQueryParams(param) \
170
+ param(PGconn *, conn) \
171
+ param(const char *, command) \
172
+ param(int, nParams) \
173
+ param(const Oid *, paramTypes) \
174
+ param(const char *const *, paramValues) \
175
+ param(const int *, paramLengths) \
176
+ param(const int *, paramFormats)
177
+
178
+ #define FOR_EACH_PARAM_OF_PQsendPrepare(param) \
179
+ param(PGconn *, conn) \
180
+ param(const char *, stmtName) \
181
+ param(const char *, query) \
182
+ param(int, nParams)
183
+
184
+ #define FOR_EACH_PARAM_OF_PQsendQueryPrepared(param) \
185
+ param(PGconn *, conn) \
186
+ param(const char *, stmtName) \
187
+ param(int, nParams) \
188
+ param(const char *const *, paramValues) \
189
+ param(const int *, paramLengths) \
190
+ param(const int *, paramFormats)
191
+
192
+ #define FOR_EACH_PARAM_OF_PQsendDescribePrepared(param) \
193
+ param(PGconn *, conn)
194
+
195
+ #define FOR_EACH_PARAM_OF_PQsendDescribePortal(param) \
196
+ param(PGconn *, conn)
197
+
198
+ #define FOR_EACH_PARAM_OF_PQisBusy(param)
199
+
200
+ #define FOR_EACH_PARAM_OF_PQcancel(param) \
201
+ param(PGcancel *, cancel) \
202
+ param(char *, errbuf)
203
+
204
+ /* function( name, void_or_nonvoid, returntype, lastparamtype, lastparamname ) */
205
+ #define FOR_EACH_BLOCKING_FUNCTION(function) \
206
+ function(PQconnectdb, GVL_TYPE_NONVOID, PGconn *, const char *, conninfo) \
207
+ function(PQconnectStart, GVL_TYPE_NONVOID, PGconn *, const char *, conninfo) \
208
+ function(PQconnectPoll, GVL_TYPE_NONVOID, PostgresPollingStatusType, PGconn *, conn) \
209
+ function(PQreset, GVL_TYPE_VOID, void, PGconn *, conn) \
210
+ function(PQresetStart, GVL_TYPE_NONVOID, int, PGconn *, conn) \
211
+ function(PQresetPoll, GVL_TYPE_NONVOID, PostgresPollingStatusType, PGconn *, conn) \
212
+ function(PQexec, GVL_TYPE_NONVOID, PGresult *, const char *, command) \
213
+ function(PQexecParams, GVL_TYPE_NONVOID, PGresult *, int, resultFormat) \
214
+ function(PQexecPrepared, GVL_TYPE_NONVOID, PGresult *, int, resultFormat) \
215
+ function(PQprepare, GVL_TYPE_NONVOID, PGresult *, const Oid *, paramTypes) \
216
+ function(PQdescribePrepared, GVL_TYPE_NONVOID, PGresult *, const char *, stmtName) \
217
+ function(PQdescribePortal, GVL_TYPE_NONVOID, PGresult *, const char *, portalName) \
218
+ function(PQgetResult, GVL_TYPE_NONVOID, PGresult *, PGconn *, conn) \
219
+ function(PQputCopyData, GVL_TYPE_NONVOID, int, int, nbytes) \
220
+ function(PQputCopyEnd, GVL_TYPE_NONVOID, int, const char *, errormsg) \
221
+ function(PQgetCopyData, GVL_TYPE_NONVOID, int, int, async) \
222
+ function(PQnotifies, GVL_TYPE_NONVOID, PGnotify *, PGconn *, conn) \
223
+ function(PQsendQuery, GVL_TYPE_NONVOID, int, const char *, query) \
224
+ function(PQsendQueryParams, GVL_TYPE_NONVOID, int, int, resultFormat) \
225
+ function(PQsendPrepare, GVL_TYPE_NONVOID, int, const Oid *, paramTypes) \
226
+ function(PQsendQueryPrepared, GVL_TYPE_NONVOID, int, int, resultFormat) \
227
+ function(PQsendDescribePrepared, GVL_TYPE_NONVOID, int, const char *, stmt) \
228
+ function(PQsendDescribePortal, GVL_TYPE_NONVOID, int, const char *, portal) \
229
+ function(PQisBusy, GVL_TYPE_NONVOID, int, PGconn *, conn) \
230
+ function(PQcancel, GVL_TYPE_NONVOID, int, int, errbufsize);
231
+
232
+
233
+ FOR_EACH_BLOCKING_FUNCTION( DEFINE_GVL_STUB_DECL );
234
+
235
+
236
+ /*
237
+ * Definitions of callback functions and their parameters
238
+ */
239
+
240
+ #define FOR_EACH_PARAM_OF_notice_processor_proxy(param) \
241
+ param(void *, arg)
242
+
243
+ #define FOR_EACH_PARAM_OF_notice_receiver_proxy(param) \
244
+ param(void *, arg)
245
+
246
+ /* function( name, void_or_nonvoid, returntype, lastparamtype, lastparamname ) */
247
+ #define FOR_EACH_CALLBACK_FUNCTION(function) \
248
+ function(notice_processor_proxy, GVL_TYPE_VOID, void, const char *, message) \
249
+ function(notice_receiver_proxy, GVL_TYPE_VOID, void, const PGresult *, result) \
250
+
251
+ FOR_EACH_CALLBACK_FUNCTION( DEFINE_GVL_STUB_DECL );
252
+
253
+ #endif /* end __gvl_wrappers_h */
@@ -0,0 +1,545 @@
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
+ * - Dave Lee
11
+ * - Eiji Matsumoto <usagi@ruby.club.or.jp>
12
+ * - Yukihiro Matsumoto <matz@ruby-lang.org>
13
+ * - Noboru Saitou <noborus@netlab.jp>
14
+ *
15
+ * See Contributors.rdoc for the many additional fine people that have contributed
16
+ * to this library over the years.
17
+ *
18
+ * Copyright (c) 1997-2012 by the authors.
19
+ *
20
+ * You may redistribute this software under the same terms as Ruby itself; see
21
+ * http://www.ruby-lang.org/en/LICENSE.txt or the LICENSE file in the source
22
+ * for details.
23
+ *
24
+ * Portions of the code are from the PostgreSQL project, and are distributed
25
+ * under the terms of the PostgreSQL license, included in the file "POSTGRES".
26
+ *
27
+ * Portions copyright LAIKA, Inc.
28
+ *
29
+ *
30
+ * The following functions are part of libpq, but not available from ruby-pg,
31
+ * because they are deprecated, obsolete, or generally not useful:
32
+ *
33
+ * - PQfreemem -- unnecessary: copied to ruby object, then freed. Ruby object's
34
+ * memory is freed when it is garbage collected.
35
+ * - PQbinaryTuples -- better to use PQfformat
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
+ VALUE rb_mPG;
51
+ VALUE rb_mPGconstants;
52
+
53
+
54
+ /*
55
+ * Document-class: PG::Error
56
+ *
57
+ * This is the exception class raised when an error is returned from
58
+ * a libpq API call.
59
+ *
60
+ * The attributes +connection+ and +result+ are set to the connection
61
+ * object and result set object, respectively.
62
+ *
63
+ * If the connection object or result set object is not available from
64
+ * the context in which the error was encountered, it is +nil+.
65
+ */
66
+
67
+ /*
68
+ * M17n functions
69
+ */
70
+
71
+ #ifdef M17N_SUPPORTED
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
+ {"BIG5", "Big5" },
77
+ {"EUC_CN", "GB2312" },
78
+ {"EUC_JP", "EUC-JP" },
79
+ {"EUC_JIS_2004", "EUC-JP" },
80
+ {"EUC_KR", "EUC-KR" },
81
+ {"EUC_TW", "EUC-TW" },
82
+ {"GB18030", "GB18030" },
83
+ {"GBK", "GBK" },
84
+ {"ISO_8859_5", "ISO-8859-5" },
85
+ {"ISO_8859_6", "ISO-8859-6" },
86
+ {"ISO_8859_7", "ISO-8859-7" },
87
+ {"ISO_8859_8", "ISO-8859-8" },
88
+ /* {"JOHAB", "JOHAB" }, dummy */
89
+ {"KOI8", "KOI8-R" },
90
+ {"KOI8R", "KOI8-R" },
91
+ {"KOI8U", "KOI8-U" },
92
+ {"LATIN1", "ISO-8859-1" },
93
+ {"LATIN2", "ISO-8859-2" },
94
+ {"LATIN3", "ISO-8859-3" },
95
+ {"LATIN4", "ISO-8859-4" },
96
+ {"LATIN5", "ISO-8859-9" },
97
+ {"LATIN6", "ISO-8859-10" },
98
+ {"LATIN7", "ISO-8859-13" },
99
+ {"LATIN8", "ISO-8859-14" },
100
+ {"LATIN9", "ISO-8859-15" },
101
+ {"LATIN10", "ISO-8859-16" },
102
+ {"MULE_INTERNAL", "Emacs-Mule" },
103
+ {"SJIS", "Windows-31J" },
104
+ {"SHIFT_JIS_2004","Windows-31J" },
105
+ /* {"SQL_ASCII", NULL }, special case*/
106
+ {"UHC", "CP949" },
107
+ {"UTF8", "UTF-8" },
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
+ * A cache of mapping from PostgreSQL's encoding indices to Ruby's rb_encoding*s.
124
+ */
125
+ static struct st_table *enc_pg2ruby;
126
+ static ID s_id_index;
127
+
128
+
129
+ /*
130
+ * Get the index of encoding +val+.
131
+ * :FIXME: Look into replacing this with rb_enc_get_index() since 1.9.1 isn't really
132
+ * used anymore.
133
+ */
134
+ int
135
+ pg_enc_get_index(VALUE val)
136
+ {
137
+ int i = ENCODING_GET_INLINED(val);
138
+ if (i == ENCODING_INLINE_MAX) {
139
+ VALUE iv = rb_ivar_get(val, s_id_index);
140
+ i = NUM2INT(iv);
141
+ }
142
+ return i;
143
+ }
144
+
145
+
146
+ /*
147
+ * Look up the JOHAB encoding, creating it as a dummy encoding if it's not
148
+ * already defined.
149
+ */
150
+ static rb_encoding *
151
+ pg_find_or_create_johab(void)
152
+ {
153
+ static const char * const aliases[] = { "JOHAB", "Windows-1361", "CP1361" };
154
+ int enc_index;
155
+ size_t i;
156
+
157
+ for (i = 0; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
158
+ enc_index = rb_enc_find_index(aliases[i]);
159
+ if (enc_index > 0) return rb_enc_from_index(enc_index);
160
+ }
161
+
162
+ enc_index = rb_define_dummy_encoding(aliases[0]);
163
+ for (i = 1; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
164
+ ENC_ALIAS(aliases[i], aliases[0]);
165
+ }
166
+ return rb_enc_from_index(enc_index);
167
+ }
168
+
169
+ /*
170
+ * Return the given PostgreSQL encoding ID as an rb_encoding.
171
+ *
172
+ * - returns NULL if the client encoding is 'SQL_ASCII'.
173
+ * - returns ASCII-8BIT if the client encoding is unknown.
174
+ */
175
+ rb_encoding *
176
+ pg_get_pg_encoding_as_rb_encoding( int enc_id )
177
+ {
178
+ rb_encoding *enc;
179
+
180
+ /* Use the cached value if it exists */
181
+ if ( st_lookup(enc_pg2ruby, (st_data_t)enc_id, (st_data_t*)&enc) ) {
182
+ return enc;
183
+ }
184
+ else {
185
+ const char *name = pg_encoding_to_char( enc_id );
186
+
187
+ enc = pg_get_pg_encname_as_rb_encoding( name );
188
+ st_insert( enc_pg2ruby, (st_data_t)enc_id, (st_data_t)enc );
189
+
190
+ return enc;
191
+ }
192
+
193
+ }
194
+
195
+ /*
196
+ * Return the given PostgreSQL encoding name as an rb_encoding.
197
+ */
198
+ rb_encoding *
199
+ pg_get_pg_encname_as_rb_encoding( const char *pg_encname )
200
+ {
201
+ size_t i;
202
+
203
+ /* Trying looking it up in the conversion table */
204
+ for ( i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i ) {
205
+ if ( strcmp(pg_encname, pg_enc_pg2ruby_mapping[i][0]) == 0 )
206
+ return rb_enc_find( pg_enc_pg2ruby_mapping[i][1] );
207
+ }
208
+
209
+ /* JOHAB isn't a builtin encoding, so make up a dummy encoding if it's seen */
210
+ if ( strncmp(pg_encname, "JOHAB", 5) == 0 )
211
+ return pg_find_or_create_johab();
212
+
213
+ /* Fallthrough to ASCII-8BIT */
214
+ return rb_ascii8bit_encoding();
215
+ }
216
+
217
+ /*
218
+ * Get the client encoding of the specified connection handle and return it as a rb_encoding.
219
+ */
220
+ rb_encoding *
221
+ pg_conn_enc_get( PGconn *conn )
222
+ {
223
+ int enc_id = PQclientEncoding( conn );
224
+ return pg_get_pg_encoding_as_rb_encoding( enc_id );
225
+ }
226
+
227
+
228
+ /*
229
+ * Returns the given rb_encoding as the equivalent PostgreSQL encoding string.
230
+ */
231
+ const char *
232
+ pg_get_rb_encoding_as_pg_encoding( rb_encoding *enc )
233
+ {
234
+ const char *rb_encname = rb_enc_name( enc );
235
+ const char *encname = NULL;
236
+ size_t i;
237
+
238
+ for (i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i) {
239
+ if (strcmp(rb_encname, pg_enc_pg2ruby_mapping[i][1]) == 0) {
240
+ encname = pg_enc_pg2ruby_mapping[i][0];
241
+ }
242
+ }
243
+
244
+ if ( !encname ) encname = "SQL_ASCII";
245
+
246
+ return encname;
247
+ }
248
+
249
+ #endif /* M17N_SUPPORTED */
250
+
251
+
252
+ /**************************************************************************
253
+ * Module Methods
254
+ **************************************************************************/
255
+
256
+ #ifdef HAVE_PQLIBVERSION
257
+ /*
258
+ * call-seq:
259
+ * PG.library_version -> Integer
260
+ *
261
+ * Get the version of the libpq library in use. The number is formed by
262
+ * converting the major, minor, and revision numbers into two-decimal-
263
+ * digit numbers and appending them together.
264
+ * For example, version 7.4.2 will be returned as 70402, and version
265
+ * 8.1 will be returned as 80100 (leading zeroes are not shown). Zero
266
+ * is returned if the connection is bad.
267
+ */
268
+ static VALUE
269
+ pg_s_library_version(VALUE self)
270
+ {
271
+ UNUSED( self );
272
+ return INT2NUM(PQlibVersion());
273
+ }
274
+ #endif
275
+
276
+
277
+ /*
278
+ * call-seq:
279
+ * PG.isthreadsafe -> Boolean
280
+ * PG.is_threadsafe? -> Boolean
281
+ * PG.threadsafe? -> Boolean
282
+ *
283
+ * Returns +true+ if libpq is thread-safe, +false+ otherwise.
284
+ */
285
+ static VALUE
286
+ pg_s_threadsafe_p(VALUE self)
287
+ {
288
+ UNUSED( self );
289
+ return PQisthreadsafe() ? Qtrue : Qfalse;
290
+ }
291
+
292
+
293
+
294
+ /**************************************************************************
295
+ * Initializer
296
+ **************************************************************************/
297
+
298
+ void
299
+ Init_pg_ext()
300
+ {
301
+ rb_mPG = rb_define_module( "PG" );
302
+ rb_mPGconstants = rb_define_module_under( rb_mPG, "Constants" );
303
+
304
+ /*************************
305
+ * PG module methods
306
+ *************************/
307
+ #ifdef HAVE_PQLIBVERSION
308
+ rb_define_singleton_method( rb_mPG, "library_version", pg_s_library_version, 0 );
309
+ #endif
310
+ rb_define_singleton_method( rb_mPG, "isthreadsafe", pg_s_threadsafe_p, 0 );
311
+ SINGLETON_ALIAS( rb_mPG, "is_threadsafe?", "isthreadsafe" );
312
+ SINGLETON_ALIAS( rb_mPG, "threadsafe?", "isthreadsafe" );
313
+
314
+ /****** PG::Connection CLASS CONSTANTS: Connection Status ******/
315
+
316
+ /* Connection succeeded */
317
+ rb_define_const(rb_mPGconstants, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
318
+ /* Connection failed */
319
+ rb_define_const(rb_mPGconstants, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
320
+
321
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection status ******/
322
+
323
+ /* Waiting for connection to be made. */
324
+ rb_define_const(rb_mPGconstants, "CONNECTION_STARTED", INT2FIX(CONNECTION_STARTED));
325
+ /* Connection OK; waiting to send. */
326
+ rb_define_const(rb_mPGconstants, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
327
+ /* Waiting for a response from the server. */
328
+ rb_define_const(rb_mPGconstants, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
329
+ /* Received authentication; waiting for backend start-up to finish. */
330
+ rb_define_const(rb_mPGconstants, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
331
+ /* Negotiating SSL encryption. */
332
+ rb_define_const(rb_mPGconstants, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
333
+ /* Negotiating environment-driven parameter settings. */
334
+ rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
335
+ /* Internal state: connect() needed. */
336
+ rb_define_const(rb_mPGconstants, "CONNECTION_NEEDED", INT2FIX(CONNECTION_NEEDED));
337
+
338
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
339
+
340
+ /* Async connection is waiting to read */
341
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
342
+ /* Async connection is waiting to write */
343
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
344
+ /* Async connection failed or was reset */
345
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
346
+ /* Async connection succeeded */
347
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
348
+
349
+ /****** PG::Connection CLASS CONSTANTS: Transaction Status ******/
350
+
351
+ /* Transaction is currently idle (#transaction_status) */
352
+ rb_define_const(rb_mPGconstants, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
353
+ /* Transaction is currently active; query has been sent to the server, but not yet completed. (#transaction_status) */
354
+ rb_define_const(rb_mPGconstants, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
355
+ /* Transaction is currently idle, in a valid transaction block (#transaction_status) */
356
+ rb_define_const(rb_mPGconstants, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
357
+ /* Transaction is currently idle, in a failed transaction block (#transaction_status) */
358
+ rb_define_const(rb_mPGconstants, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
359
+ /* Transaction's connection is bad (#transaction_status) */
360
+ rb_define_const(rb_mPGconstants, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
361
+
362
+ /****** PG::Connection CLASS CONSTANTS: Error Verbosity ******/
363
+
364
+ /* Terse error verbosity level (#set_error_verbosity) */
365
+ rb_define_const(rb_mPGconstants, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
366
+ /* Default error verbosity level (#set_error_verbosity) */
367
+ rb_define_const(rb_mPGconstants, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
368
+ /* Verbose error verbosity level (#set_error_verbosity) */
369
+ rb_define_const(rb_mPGconstants, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
370
+
371
+ #ifdef HAVE_PQPING
372
+ /****** PG::Connection CLASS CONSTANTS: Check Server Status ******/
373
+
374
+ /* Server is accepting connections. */
375
+ rb_define_const(rb_mPGconstants, "PQPING_OK", INT2FIX(PQPING_OK));
376
+ /* Server is alive but rejecting connections. */
377
+ rb_define_const(rb_mPGconstants, "PQPING_REJECT", INT2FIX(PQPING_REJECT));
378
+ /* Could not establish connection. */
379
+ rb_define_const(rb_mPGconstants, "PQPING_NO_RESPONSE", INT2FIX(PQPING_NO_RESPONSE));
380
+ /* Connection not attempted (bad params). */
381
+ rb_define_const(rb_mPGconstants, "PQPING_NO_ATTEMPT", INT2FIX(PQPING_NO_ATTEMPT));
382
+ #endif
383
+
384
+ /****** PG::Connection CLASS CONSTANTS: Large Objects ******/
385
+
386
+ /* Flag for #lo_creat, #lo_open -- open for writing */
387
+ rb_define_const(rb_mPGconstants, "INV_WRITE", INT2FIX(INV_WRITE));
388
+ /* Flag for #lo_creat, #lo_open -- open for reading */
389
+ rb_define_const(rb_mPGconstants, "INV_READ", INT2FIX(INV_READ));
390
+ /* Flag for #lo_lseek -- seek from object start */
391
+ rb_define_const(rb_mPGconstants, "SEEK_SET", INT2FIX(SEEK_SET));
392
+ /* Flag for #lo_lseek -- seek from current position */
393
+ rb_define_const(rb_mPGconstants, "SEEK_CUR", INT2FIX(SEEK_CUR));
394
+ /* Flag for #lo_lseek -- seek from object end */
395
+ rb_define_const(rb_mPGconstants, "SEEK_END", INT2FIX(SEEK_END));
396
+
397
+ /****** PG::Result CONSTANTS: result status ******/
398
+
399
+ /* #result_status constant: The string sent to the server was empty. */
400
+ rb_define_const(rb_mPGconstants, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
401
+ /* #result_status constant: Successful completion of a command returning no data. */
402
+ rb_define_const(rb_mPGconstants, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
403
+ /* #result_status constant: Successful completion of a command returning data
404
+ (such as a SELECT or SHOW). */
405
+ rb_define_const(rb_mPGconstants, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
406
+ /* #result_status constant: Copy Out (from server) data transfer started. */
407
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
408
+ /* #result_status constant: Copy In (to server) data transfer started. */
409
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
410
+ /* #result_status constant: The server’s response was not understood. */
411
+ rb_define_const(rb_mPGconstants, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
412
+ /* #result_status constant: A nonfatal error (a notice or warning) occurred. */
413
+ rb_define_const(rb_mPGconstants, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
414
+ /* #result_status constant: A fatal error occurred. */
415
+ rb_define_const(rb_mPGconstants, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
416
+ /* #result_status constant: Copy In/Out data transfer in progress. */
417
+ #ifdef HAVE_CONST_PGRES_COPY_BOTH
418
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_BOTH", INT2FIX(PGRES_COPY_BOTH));
419
+ #endif
420
+ /* #result_status constant: Single tuple from larger resultset. */
421
+ #ifdef HAVE_CONST_PGRES_SINGLE_TUPLE
422
+ rb_define_const(rb_mPGconstants, "PGRES_SINGLE_TUPLE", INT2FIX(PGRES_SINGLE_TUPLE));
423
+ #endif
424
+
425
+ /****** Result CONSTANTS: result error field codes ******/
426
+
427
+ /* #result_error_field argument constant: The severity; the field contents
428
+ * are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE,
429
+ * DEBUG, INFO, or LOG (in a notice message), or a localized translation
430
+ * of one of these. Always present.
431
+ */
432
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
433
+
434
+ /* #result_error_field argument constant: The SQLSTATE code for the
435
+ * error. The SQLSTATE code identies the type of error that has occurred;
436
+ * it can be used by front-end applications to perform specic operations
437
+ * (such as er- ror handling) in response to a particular database
438
+ * error. For a list of the possible SQLSTATE codes, see Appendix A.
439
+ * This eld is not localizable, and is always present.
440
+ */
441
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
442
+
443
+ /* #result_error_field argument constant: The primary human-readable
444
+ * error message (typically one line). Always present. */
445
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
446
+
447
+ /* #result_error_field argument constant: Detail: an optional secondary
448
+ * error message carrying more detail about the problem. Might run to
449
+ * multiple lines.
450
+ */
451
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
452
+
453
+ /* #result_error_field argument constant: Hint: an optional suggestion
454
+ * what to do about the problem. This is intended to differ from detail
455
+ * in that it offers advice (potentially inappropriate) rather than
456
+ * hard facts. Might run to multiple lines.
457
+ */
458
+
459
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
460
+ /* #result_error_field argument constant: A string containing a decimal
461
+ * integer indicating an error cursor position as an index into the
462
+ * original statement string. The rst character has index 1, and
463
+ * positions are measured in characters not bytes.
464
+ */
465
+
466
+ rb_define_const(rb_mPGconstants, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
467
+ /* #result_error_field argument constant: This is dened the same as
468
+ * the PG_DIAG_STATEMENT_POSITION eld, but it is used when the cursor
469
+ * position refers to an internally generated command rather than the
470
+ * one submitted by the client. The PG_DIAG_INTERNAL_QUERY eld will
471
+ * always appear when this eld appears.
472
+ */
473
+
474
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
475
+ /* #result_error_field argument constant: The text of a failed
476
+ * internally-generated command. This could be, for example, a SQL
477
+ * query issued by a PL/pgSQL function.
478
+ */
479
+
480
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
481
+ /* #result_error_field argument constant: An indication of the context
482
+ * in which the error occurred. Presently this includes a call stack
483
+ * traceback of active procedural language functions and internally-generated
484
+ * queries. The trace is one entry per line, most recent rst.
485
+ */
486
+
487
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
488
+ /* #result_error_field argument constant: The le name of the source-code
489
+ * location where the error was reported. */
490
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
491
+
492
+ /* #result_error_field argument constant: The line number of the
493
+ * source-code location where the error was reported. */
494
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
495
+
496
+ /* #result_error_field argument constant: The name of the source-code
497
+ * function reporting the error. */
498
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
499
+
500
+ #ifdef HAVE_CONST_PG_DIAG_TABLE_NAME
501
+ /* #result_error_field argument constant: If the error was associated with a
502
+ * specific database object, the name of the schema containing that object, if any. */
503
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SCHEMA_NAME", INT2FIX(PG_DIAG_SCHEMA_NAME));
504
+
505
+ /* #result_error_field argument constant: If the error was associated with a
506
+ *specific table, the name of the table. (When this field is present, the schema name
507
+ * field provides the name of the table's schema.) */
508
+ rb_define_const(rb_mPGconstants, "PG_DIAG_TABLE_NAME", INT2FIX(PG_DIAG_TABLE_NAME));
509
+
510
+ /* #result_error_field argument constant: If the error was associated with a
511
+ * specific table column, the name of the column. (When this field is present, the
512
+ * schema and table name fields identify the table.) */
513
+ rb_define_const(rb_mPGconstants, "PG_DIAG_COLUMN_NAME", INT2FIX(PG_DIAG_COLUMN_NAME));
514
+
515
+ /* #result_error_field argument constant: If the error was associated with a
516
+ * specific datatype, the name of the datatype. (When this field is present, the
517
+ * schema name field provides the name of the datatype's schema.) */
518
+ rb_define_const(rb_mPGconstants, "PG_DIAG_DATATYPE_NAME", INT2FIX(PG_DIAG_DATATYPE_NAME));
519
+
520
+ /* #result_error_field argument constant: If the error was associated with a
521
+ * specific constraint, the name of the constraint. The table or domain that the
522
+ * constraint belongs to is reported using the fields listed above. (For this
523
+ * purpose, indexes are treated as constraints, even if they weren't created with
524
+ * constraint syntax.) */
525
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONSTRAINT_NAME", INT2FIX(PG_DIAG_CONSTRAINT_NAME));
526
+ #endif
527
+
528
+ /* Invalid OID constant */
529
+ rb_define_const(rb_mPGconstants, "INVALID_OID", INT2FIX(InvalidOid));
530
+ rb_define_const(rb_mPGconstants, "InvalidOid", INT2FIX(InvalidOid));
531
+
532
+ /* Add the constants to the toplevel namespace */
533
+ rb_include_module( rb_mPG, rb_mPGconstants );
534
+
535
+ #ifdef M17N_SUPPORTED
536
+ enc_pg2ruby = st_init_numtable();
537
+ s_id_index = rb_intern("@encoding");
538
+ #endif
539
+
540
+ /* Initialize the main extension classes */
541
+ init_pg_connection();
542
+ init_pg_result();
543
+ init_pg_errors();
544
+ }
545
+