pg 0.15.0.pre.454-x64-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.gemtest +0 -0
  3. data/BSDL +22 -0
  4. data/ChangeLog +2945 -0
  5. data/Contributors.rdoc +46 -0
  6. data/History.rdoc +205 -0
  7. data/LICENSE +56 -0
  8. data/Manifest.txt +53 -0
  9. data/POSTGRES +23 -0
  10. data/README-OS_X.rdoc +68 -0
  11. data/README-Windows.rdoc +67 -0
  12. data/README.ja.rdoc +14 -0
  13. data/README.rdoc +111 -0
  14. data/Rakefile +156 -0
  15. data/Rakefile.cross +271 -0
  16. data/ext/extconf.rb +91 -0
  17. data/ext/gvl_wrappers.c +13 -0
  18. data/ext/gvl_wrappers.h +185 -0
  19. data/ext/pg.c +525 -0
  20. data/ext/pg.h +126 -0
  21. data/ext/pg_connection.c +3600 -0
  22. data/ext/pg_result.c +939 -0
  23. data/ext/vc/pg.sln +26 -0
  24. data/ext/vc/pg_18/pg.vcproj +216 -0
  25. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  26. data/lib/2.0/pg_ext.so +0 -0
  27. data/lib/pg.rb +52 -0
  28. data/lib/pg/connection.rb +71 -0
  29. data/lib/pg/constants.rb +11 -0
  30. data/lib/pg/exceptions.rb +11 -0
  31. data/lib/pg/result.rb +16 -0
  32. data/sample/array_insert.rb +20 -0
  33. data/sample/async_api.rb +106 -0
  34. data/sample/async_copyto.rb +39 -0
  35. data/sample/async_mixed.rb +56 -0
  36. data/sample/check_conn.rb +21 -0
  37. data/sample/copyfrom.rb +81 -0
  38. data/sample/copyto.rb +19 -0
  39. data/sample/cursor.rb +21 -0
  40. data/sample/disk_usage_report.rb +186 -0
  41. data/sample/issue-119.rb +94 -0
  42. data/sample/losample.rb +69 -0
  43. data/sample/minimal-testcase.rb +17 -0
  44. data/sample/notify_wait.rb +72 -0
  45. data/sample/pg_statistics.rb +294 -0
  46. data/sample/replication_monitor.rb +231 -0
  47. data/sample/test_binary_values.rb +33 -0
  48. data/sample/wal_shipper.rb +434 -0
  49. data/sample/warehouse_partitions.rb +320 -0
  50. data/spec/data/expected_trace.out +26 -0
  51. data/spec/data/random_binary_data +0 -0
  52. data/spec/lib/helpers.rb +279 -0
  53. data/spec/pg/connection_spec.rb +1013 -0
  54. data/spec/pg/result_spec.rb +278 -0
  55. data/spec/pg_spec.rb +31 -0
  56. metadata +275 -0
  57. metadata.gz.sig +0 -0
@@ -0,0 +1,91 @@
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
+ dir_config 'pg'
31
+
32
+ if pgconfig = ( with_config('pg-config') || with_config('pg_config') || find_executable('pg_config') )
33
+ $stderr.puts "Using config values from %s" % [ pgconfig ]
34
+ $CPPFLAGS << " -I%s" % [ `"#{pgconfig}" --includedir`.chomp ]
35
+
36
+ libdir = `"#{pgconfig}" --libdir`.chomp
37
+ $LDFLAGS << " -L%s -Wl,-rpath,%s" % [ libdir, libdir ]
38
+ else
39
+ $stderr.puts "No pg_config... trying anyway. If building fails, please try again with",
40
+ " --with-pg-config=/path/to/pg_config"
41
+ end
42
+
43
+ find_header( 'libpq-fe.h' ) or abort "Can't find the 'libpq-fe.h header"
44
+ find_header( 'libpq/libpq-fs.h' ) or abort "Can't find the 'libpq/libpq-fs.h header"
45
+ find_header( 'pg_config_manual.h' ) or abort "Can't find the 'pg_config_manual.h' header"
46
+
47
+ abort "Can't find the PostgreSQL client library (libpq)" unless
48
+ have_library( 'pq', 'PQconnectdb', ['libpq-fe.h'] ) ||
49
+ have_library( 'libpq', 'PQconnectdb', ['libpq-fe.h'] ) ||
50
+ have_library( 'ms/libpq', 'PQconnectdb', ['libpq-fe.h'] )
51
+
52
+ # optional headers/functions
53
+ have_func 'PQconnectionUsedPassword' or
54
+ abort "Your PostgreSQL is too old. Either install an older version " +
55
+ "of this gem or upgrade your database."
56
+ have_func 'PQisthreadsafe'
57
+ have_func 'PQprepare'
58
+ have_func 'PQexecParams'
59
+ have_func 'PQescapeString'
60
+ have_func 'PQescapeStringConn'
61
+ have_func 'PQescapeLiteral'
62
+ have_func 'PQescapeIdentifier'
63
+ have_func 'PQgetCancel'
64
+ have_func 'lo_create'
65
+ have_func 'pg_encoding_to_char'
66
+ have_func 'pg_char_to_encoding'
67
+ have_func 'PQsetClientEncoding'
68
+ have_func 'PQlibVersion'
69
+ have_func 'PQping'
70
+ have_func 'PQsetSingleRowMode'
71
+
72
+ have_func 'rb_encdb_alias'
73
+ have_func 'rb_enc_alias'
74
+ have_func 'rb_thread_call_without_gvl'
75
+ have_func 'rb_thread_call_with_gvl'
76
+ have_func 'rb_thread_fd_select'
77
+ have_func 'rb_w32_wrap_io_handle'
78
+
79
+ have_const 'PGRES_COPY_BOTH', 'libpq-fe.h'
80
+ have_const 'PGRES_SINGLE_TUPLE', 'libpq-fe.h'
81
+
82
+ $defs.push( "-DHAVE_ST_NOTIFY_EXTRA" ) if
83
+ have_struct_member 'struct pgNotify', 'extra', 'libpq-fe.h'
84
+
85
+ # unistd.h confilicts with ruby/win32.h when cross compiling for win32 and ruby 1.9.1
86
+ have_header 'unistd.h'
87
+ have_header 'ruby/st.h' or have_header 'st.h' or abort "pg currently requires the ruby/st.h header"
88
+
89
+ create_header()
90
+ create_makefile( "pg_ext" )
91
+
@@ -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,185 @@
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_PQexec(param) \
108
+ param(PGconn *, conn)
109
+
110
+ #define FOR_EACH_PARAM_OF_PQexecParams(param) \
111
+ param(PGconn *, conn) \
112
+ param(const char *, command) \
113
+ param(int, nParams) \
114
+ param(const Oid *, paramTypes) \
115
+ param(const char * const *, paramValues) \
116
+ param(const int *, paramLengths) \
117
+ param(const int *, paramFormats)
118
+
119
+ #define FOR_EACH_PARAM_OF_PQexecPrepared(param) \
120
+ param(PGconn *, conn) \
121
+ param(const char *, stmtName) \
122
+ param(int, nParams) \
123
+ param(const char * const *, paramValues) \
124
+ param(const int *, paramLengths) \
125
+ param(const int *, paramFormats)
126
+
127
+ #define FOR_EACH_PARAM_OF_PQprepare(param) \
128
+ param(PGconn *, conn) \
129
+ param(const char *, stmtName) \
130
+ param(const char *, query) \
131
+ param(int, nParams)
132
+
133
+ #define FOR_EACH_PARAM_OF_PQdescribePrepared(param) \
134
+ param(PGconn *, conn)
135
+
136
+ #define FOR_EACH_PARAM_OF_PQdescribePortal(param) \
137
+ param(PGconn *, conn)
138
+
139
+ #define FOR_EACH_PARAM_OF_PQgetResult(param)
140
+
141
+ #define FOR_EACH_PARAM_OF_PQputCopyData(param) \
142
+ param(PGconn *, conn) \
143
+ param(const char *, buffer)
144
+
145
+ #define FOR_EACH_PARAM_OF_PQputCopyEnd(param) \
146
+ param(PGconn *, conn)
147
+
148
+ #define FOR_EACH_PARAM_OF_PQgetCopyData(param) \
149
+ param(PGconn *, conn) \
150
+ param(char **, buffer)
151
+
152
+ /* function( name, void_or_nonvoid, returntype, lastparamtype, lastparamname ) */
153
+ #define FOR_EACH_BLOCKING_FUNCTION(function) \
154
+ function(PQexec, GVL_TYPE_NONVOID, PGresult *, const char *, command) \
155
+ function(PQexecParams, GVL_TYPE_NONVOID, PGresult *, int, resultFormat) \
156
+ function(PQexecPrepared, GVL_TYPE_NONVOID, PGresult *, int, resultFormat) \
157
+ function(PQprepare, GVL_TYPE_NONVOID, PGresult *, const Oid *, paramTypes) \
158
+ function(PQdescribePrepared, GVL_TYPE_NONVOID, PGresult *, const char *, stmtName) \
159
+ function(PQdescribePortal, GVL_TYPE_NONVOID, PGresult *, const char *, portalName) \
160
+ function(PQgetResult, GVL_TYPE_NONVOID, PGresult *, PGconn *, conn) \
161
+ function(PQputCopyData, GVL_TYPE_NONVOID, int, int, nbytes) \
162
+ function(PQputCopyEnd, GVL_TYPE_NONVOID, int, const char *, errormsg) \
163
+ function(PQgetCopyData, GVL_TYPE_NONVOID, int, int, async) \
164
+
165
+ FOR_EACH_BLOCKING_FUNCTION( DEFINE_GVL_STUB_DECL );
166
+
167
+
168
+ /*
169
+ * Definitions of callback functions and their parameters
170
+ */
171
+
172
+ #define FOR_EACH_PARAM_OF_notice_processor_proxy(param) \
173
+ param(void *, arg)
174
+
175
+ #define FOR_EACH_PARAM_OF_notice_receiver_proxy(param) \
176
+ param(void *, arg)
177
+
178
+ /* function( name, void_or_nonvoid, returntype, lastparamtype, lastparamname ) */
179
+ #define FOR_EACH_CALLBACK_FUNCTION(function) \
180
+ function(notice_processor_proxy, GVL_TYPE_VOID, void, const char *, message) \
181
+ function(notice_receiver_proxy, GVL_TYPE_VOID, void, const PGresult *, result) \
182
+
183
+ FOR_EACH_CALLBACK_FUNCTION( DEFINE_GVL_STUB_DECL );
184
+
185
+ #endif /* end __gvl_wrappers_h */
@@ -0,0 +1,525 @@
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_ePGerror;
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
+ #ifdef M17N_SUPPORTED
73
+ /**
74
+ * The mapping from canonical encoding names in PostgreSQL to ones in Ruby.
75
+ */
76
+ const char * const (pg_enc_pg2ruby_mapping[][2]) = {
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
+ {"UTF8", "UTF-8" },
109
+ {"WIN866", "IBM866" },
110
+ {"WIN874", "Windows-874" },
111
+ {"WIN1250", "Windows-1250"},
112
+ {"WIN1251", "Windows-1251"},
113
+ {"WIN1252", "Windows-1252"},
114
+ {"WIN1253", "Windows-1253"},
115
+ {"WIN1254", "Windows-1254"},
116
+ {"WIN1255", "Windows-1255"},
117
+ {"WIN1256", "Windows-1256"},
118
+ {"WIN1257", "Windows-1257"},
119
+ {"WIN1258", "Windows-1258"}
120
+ };
121
+
122
+
123
+ /*
124
+ * A cache of mapping from PostgreSQL's encoding indices to Ruby's rb_encoding*s.
125
+ */
126
+ static struct st_table *enc_pg2ruby;
127
+ static ID s_id_index;
128
+
129
+
130
+ /*
131
+ * Get the index of encoding +val+.
132
+ * :FIXME: Look into replacing this with rb_enc_get_index() since 1.9.1 isn't really
133
+ * used anymore.
134
+ */
135
+ int
136
+ pg_enc_get_index(VALUE val)
137
+ {
138
+ int i = ENCODING_GET_INLINED(val);
139
+ if (i == ENCODING_INLINE_MAX) {
140
+ VALUE iv = rb_ivar_get(val, s_id_index);
141
+ i = NUM2INT(iv);
142
+ }
143
+ return i;
144
+ }
145
+
146
+
147
+ /*
148
+ * Look up the JOHAB encoding, creating it as a dummy encoding if it's not
149
+ * already defined.
150
+ */
151
+ static rb_encoding *
152
+ pg_find_or_create_johab(void)
153
+ {
154
+ static const char * const aliases[] = { "JOHAB", "Windows-1361", "CP1361" };
155
+ int enc_index;
156
+ size_t i;
157
+
158
+ for (i = 0; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
159
+ enc_index = rb_enc_find_index(aliases[i]);
160
+ if (enc_index > 0) return rb_enc_from_index(enc_index);
161
+ }
162
+
163
+ enc_index = rb_define_dummy_encoding(aliases[0]);
164
+ for (i = 1; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
165
+ ENC_ALIAS(aliases[i], aliases[0]);
166
+ }
167
+ return rb_enc_from_index(enc_index);
168
+ }
169
+
170
+ /*
171
+ * Return the given PostgreSQL encoding ID as an rb_encoding.
172
+ *
173
+ * - returns NULL if the client encoding is 'SQL_ASCII'.
174
+ * - returns ASCII-8BIT if the client encoding is unknown.
175
+ */
176
+ rb_encoding *
177
+ pg_get_pg_encoding_as_rb_encoding( int enc_id )
178
+ {
179
+ rb_encoding *enc;
180
+
181
+ /* Use the cached value if it exists */
182
+ if ( st_lookup(enc_pg2ruby, (st_data_t)enc_id, (st_data_t*)&enc) ) {
183
+ return enc;
184
+ }
185
+ else {
186
+ const char *name = pg_encoding_to_char( enc_id );
187
+
188
+ enc = pg_get_pg_encname_as_rb_encoding( name );
189
+ st_insert( enc_pg2ruby, (st_data_t)enc_id, (st_data_t)enc );
190
+
191
+ return enc;
192
+ }
193
+
194
+ }
195
+
196
+ /*
197
+ * Return the given PostgreSQL encoding name as an rb_encoding.
198
+ */
199
+ rb_encoding *
200
+ pg_get_pg_encname_as_rb_encoding( const char *pg_encname )
201
+ {
202
+ size_t i;
203
+
204
+ /* Trying looking it up in the conversion table */
205
+ for ( i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i ) {
206
+ if ( strcmp(pg_encname, pg_enc_pg2ruby_mapping[i][0]) == 0 )
207
+ return rb_enc_find( pg_enc_pg2ruby_mapping[i][1] );
208
+ }
209
+
210
+ /* JOHAB isn't a builtin encoding, so make up a dummy encoding if it's seen */
211
+ if ( strncmp(pg_encname, "JOHAB", 5) == 0 )
212
+ return pg_find_or_create_johab();
213
+
214
+ /* Fallthrough to ASCII-8BIT */
215
+ return rb_ascii8bit_encoding();
216
+ }
217
+
218
+ /*
219
+ * Get the client encoding of the specified connection handle and return it as a rb_encoding.
220
+ */
221
+ rb_encoding *
222
+ pg_conn_enc_get( PGconn *conn )
223
+ {
224
+ int enc_id = PQclientEncoding( conn );
225
+ return pg_get_pg_encoding_as_rb_encoding( enc_id );
226
+ }
227
+
228
+
229
+ /*
230
+ * Returns the given rb_encoding as the equivalent PostgreSQL encoding string.
231
+ */
232
+ const char *
233
+ pg_get_rb_encoding_as_pg_encoding( rb_encoding *enc )
234
+ {
235
+ const char *rb_encname = rb_enc_name( enc );
236
+ const char *encname = NULL;
237
+ size_t i;
238
+
239
+ for (i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i) {
240
+ if (strcmp(rb_encname, pg_enc_pg2ruby_mapping[i][1]) == 0) {
241
+ encname = pg_enc_pg2ruby_mapping[i][0];
242
+ }
243
+ }
244
+
245
+ if ( !encname ) encname = "SQL_ASCII";
246
+
247
+ return encname;
248
+ }
249
+
250
+ #endif /* M17N_SUPPORTED */
251
+
252
+
253
+ /**************************************************************************
254
+ * Module Methods
255
+ **************************************************************************/
256
+
257
+ #ifdef HAVE_PQLIBVERSION
258
+ /*
259
+ * call-seq:
260
+ * PG.library_version -> Integer
261
+ *
262
+ * Get the version of the libpq library in use. The number is formed by
263
+ * converting the major, minor, and revision numbers into two-decimal-
264
+ * digit numbers and appending them together.
265
+ * For example, version 7.4.2 will be returned as 70402, and version
266
+ * 8.1 will be returned as 80100 (leading zeroes are not shown). Zero
267
+ * is returned if the connection is bad.
268
+ */
269
+ static VALUE
270
+ pg_s_library_version(VALUE self)
271
+ {
272
+ UNUSED( self );
273
+ return INT2NUM(PQlibVersion());
274
+ }
275
+ #endif
276
+
277
+
278
+ /*
279
+ * call-seq:
280
+ * PG.isthreadsafe -> Boolean
281
+ * PG.is_threadsafe? -> Boolean
282
+ * PG.threadsafe? -> Boolean
283
+ *
284
+ * Returns +true+ if libpq is thread-safe, +false+ otherwise.
285
+ */
286
+ static VALUE
287
+ pg_s_threadsafe_p(VALUE self)
288
+ {
289
+ UNUSED( self );
290
+ return PQisthreadsafe() ? Qtrue : Qfalse;
291
+ }
292
+
293
+
294
+
295
+ /**************************************************************************
296
+ * Initializer
297
+ **************************************************************************/
298
+
299
+ void
300
+ Init_pg_ext()
301
+ {
302
+ rb_mPG = rb_define_module( "PG" );
303
+ rb_ePGerror = rb_define_class_under( rb_mPG, "Error", rb_eStandardError );
304
+ rb_mPGconstants = rb_define_module_under( rb_mPG, "Constants" );
305
+
306
+ /*************************
307
+ * PG module methods
308
+ *************************/
309
+ #ifdef HAVE_PQLIBVERSION
310
+ rb_define_singleton_method( rb_mPG, "library_version", pg_s_library_version, 0 );
311
+ #endif
312
+ rb_define_singleton_method( rb_mPG, "isthreadsafe", pg_s_threadsafe_p, 0 );
313
+ SINGLETON_ALIAS( rb_mPG, "is_threadsafe?", "isthreadsafe" );
314
+ SINGLETON_ALIAS( rb_mPG, "threadsafe?", "isthreadsafe" );
315
+
316
+ /*************************
317
+ * PG::Error
318
+ *************************/
319
+ rb_define_alias( rb_ePGerror, "error", "message" );
320
+ rb_define_attr( rb_ePGerror, "connection", 1, 0 );
321
+ rb_define_attr( rb_ePGerror, "result", 1, 0 );
322
+
323
+ /****** PG::Connection CLASS CONSTANTS: Connection Status ******/
324
+
325
+ /* Connection succeeded */
326
+ rb_define_const(rb_mPGconstants, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
327
+ /* Connection failed */
328
+ rb_define_const(rb_mPGconstants, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
329
+
330
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection status ******/
331
+
332
+ /* Waiting for connection to be made. */
333
+ rb_define_const(rb_mPGconstants, "CONNECTION_STARTED", INT2FIX(CONNECTION_STARTED));
334
+ /* Connection OK; waiting to send. */
335
+ rb_define_const(rb_mPGconstants, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
336
+ /* Waiting for a response from the server. */
337
+ rb_define_const(rb_mPGconstants, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
338
+ /* Received authentication; waiting for backend start-up to finish. */
339
+ rb_define_const(rb_mPGconstants, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
340
+ /* Negotiating SSL encryption. */
341
+ rb_define_const(rb_mPGconstants, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
342
+ /* Negotiating environment-driven parameter settings. */
343
+ rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
344
+ /* Internal state: connect() needed. */
345
+ rb_define_const(rb_mPGconstants, "CONNECTION_NEEDED", INT2FIX(CONNECTION_NEEDED));
346
+
347
+ /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
348
+
349
+ /* Async connection is waiting to read */
350
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
351
+ /* Async connection is waiting to write */
352
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
353
+ /* Async connection failed or was reset */
354
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
355
+ /* Async connection succeeded */
356
+ rb_define_const(rb_mPGconstants, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
357
+
358
+ /****** PG::Connection CLASS CONSTANTS: Transaction Status ******/
359
+
360
+ /* Transaction is currently idle (#transaction_status) */
361
+ rb_define_const(rb_mPGconstants, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
362
+ /* Transaction is currently active; query has been sent to the server, but not yet completed. (#transaction_status) */
363
+ rb_define_const(rb_mPGconstants, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
364
+ /* Transaction is currently idle, in a valid transaction block (#transaction_status) */
365
+ rb_define_const(rb_mPGconstants, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
366
+ /* Transaction is currently idle, in a failed transaction block (#transaction_status) */
367
+ rb_define_const(rb_mPGconstants, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
368
+ /* Transaction's connection is bad (#transaction_status) */
369
+ rb_define_const(rb_mPGconstants, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
370
+
371
+ /****** PG::Connection CLASS CONSTANTS: Error Verbosity ******/
372
+
373
+ /* Terse error verbosity level (#set_error_verbosity) */
374
+ rb_define_const(rb_mPGconstants, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
375
+ /* Default error verbosity level (#set_error_verbosity) */
376
+ rb_define_const(rb_mPGconstants, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
377
+ /* Verbose error verbosity level (#set_error_verbosity) */
378
+ rb_define_const(rb_mPGconstants, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
379
+
380
+ #ifdef HAVE_PQPING
381
+ /****** PG::Connection CLASS CONSTANTS: Check Server Status ******/
382
+
383
+ /* Server is accepting connections. */
384
+ rb_define_const(rb_mPGconstants, "PQPING_OK", INT2FIX(PQPING_OK));
385
+ /* Server is alive but rejecting connections. */
386
+ rb_define_const(rb_mPGconstants, "PQPING_REJECT", INT2FIX(PQPING_REJECT));
387
+ /* Could not establish connection. */
388
+ rb_define_const(rb_mPGconstants, "PQPING_NO_RESPONSE", INT2FIX(PQPING_NO_RESPONSE));
389
+ /* Connection not attempted (bad params). */
390
+ rb_define_const(rb_mPGconstants, "PQPING_NO_ATTEMPT", INT2FIX(PQPING_NO_ATTEMPT));
391
+ #endif
392
+
393
+ /****** PG::Connection CLASS CONSTANTS: Large Objects ******/
394
+
395
+ /* Flag for #lo_creat, #lo_open -- open for writing */
396
+ rb_define_const(rb_mPGconstants, "INV_WRITE", INT2FIX(INV_WRITE));
397
+ /* Flag for #lo_creat, #lo_open -- open for reading */
398
+ rb_define_const(rb_mPGconstants, "INV_READ", INT2FIX(INV_READ));
399
+ /* Flag for #lo_lseek -- seek from object start */
400
+ rb_define_const(rb_mPGconstants, "SEEK_SET", INT2FIX(SEEK_SET));
401
+ /* Flag for #lo_lseek -- seek from current position */
402
+ rb_define_const(rb_mPGconstants, "SEEK_CUR", INT2FIX(SEEK_CUR));
403
+ /* Flag for #lo_lseek -- seek from object end */
404
+ rb_define_const(rb_mPGconstants, "SEEK_END", INT2FIX(SEEK_END));
405
+
406
+ /****** PG::Result CONSTANTS: result status ******/
407
+
408
+ /* #result_status constant: The string sent to the server was empty. */
409
+ rb_define_const(rb_mPGconstants, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
410
+ /* #result_status constant: Successful completion of a command returning no data. */
411
+ rb_define_const(rb_mPGconstants, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
412
+ /* #result_status constant: Successful completion of a command returning data
413
+ (such as a SELECT or SHOW). */
414
+ rb_define_const(rb_mPGconstants, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
415
+ /* #result_status constant: Copy Out (from server) data transfer started. */
416
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
417
+ /* #result_status constant: Copy In (to server) data transfer started. */
418
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
419
+ /* #result_status constant: The server’s response was not understood. */
420
+ rb_define_const(rb_mPGconstants, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
421
+ /* #result_status constant: A nonfatal error (a notice or warning) occurred. */
422
+ rb_define_const(rb_mPGconstants, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
423
+ /* #result_status constant: A fatal error occurred. */
424
+ rb_define_const(rb_mPGconstants, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
425
+ /* #result_status constant: Copy In/Out data transfer in progress. */
426
+ #ifdef HAVE_CONST_PGRES_COPY_BOTH
427
+ rb_define_const(rb_mPGconstants, "PGRES_COPY_BOTH", INT2FIX(PGRES_COPY_BOTH));
428
+ #endif
429
+ /* #result_status constant: Single tuple from larger resultset. */
430
+ #ifdef HAVE_CONST_PGRES_SINGLE_TUPLE
431
+ rb_define_const(rb_mPGconstants, "PGRES_SINGLE_TUPLE", INT2FIX(PGRES_SINGLE_TUPLE));
432
+ #endif
433
+
434
+ /****** Result CONSTANTS: result error field codes ******/
435
+
436
+ /* #result_error_field argument constant: The severity; the field contents
437
+ * are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE,
438
+ * DEBUG, INFO, or LOG (in a notice message), or a localized translation
439
+ * of one of these. Always present.
440
+ */
441
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
442
+
443
+ /* #result_error_field argument constant: The SQLSTATE code for the
444
+ * error. The SQLSTATE code identies the type of error that has occurred;
445
+ * it can be used by front-end applications to perform specic operations
446
+ * (such as er- ror handling) in response to a particular database
447
+ * error. For a list of the possible SQLSTATE codes, see Appendix A.
448
+ * This eld is not localizable, and is always present.
449
+ */
450
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
451
+
452
+ /* #result_error_field argument constant: The primary human-readable
453
+ * error message (typically one line). Always present. */
454
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
455
+
456
+ /* #result_error_field argument constant: Detail: an optional secondary
457
+ * error message carrying more detail about the problem. Might run to
458
+ * multiple lines.
459
+ */
460
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
461
+
462
+ /* #result_error_field argument constant: Hint: an optional suggestion
463
+ * what to do about the problem. This is intended to differ from detail
464
+ * in that it offers advice (potentially inappropriate) rather than
465
+ * hard facts. Might run to multiple lines.
466
+ */
467
+
468
+ rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
469
+ /* #result_error_field argument constant: A string containing a decimal
470
+ * integer indicating an error cursor position as an index into the
471
+ * original statement string. The rst character has index 1, and
472
+ * positions are measured in characters not bytes.
473
+ */
474
+
475
+ rb_define_const(rb_mPGconstants, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
476
+ /* #result_error_field argument constant: This is dened the same as
477
+ * the PG_DIAG_STATEMENT_POSITION eld, but it is used when the cursor
478
+ * position refers to an internally generated command rather than the
479
+ * one submitted by the client. The PG_DIAG_INTERNAL_QUERY eld will
480
+ * always appear when this eld appears.
481
+ */
482
+
483
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
484
+ /* #result_error_field argument constant: The text of a failed
485
+ * internally-generated command. This could be, for example, a SQL
486
+ * query issued by a PL/pgSQL function.
487
+ */
488
+
489
+ rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
490
+ /* #result_error_field argument constant: An indication of the context
491
+ * in which the error occurred. Presently this includes a call stack
492
+ * traceback of active procedural language functions and internally-generated
493
+ * queries. The trace is one entry per line, most recent rst.
494
+ */
495
+
496
+ rb_define_const(rb_mPGconstants, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
497
+ /* #result_error_field argument constant: The le name of the source-code
498
+ * location where the error was reported. */
499
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
500
+
501
+ /* #result_error_field argument constant: The line number of the
502
+ * source-code location where the error was reported. */
503
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
504
+
505
+ /* #result_error_field argument constant: The name of the source-code
506
+ * function reporting the error. */
507
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
508
+
509
+ /* Invalid OID constant */
510
+ rb_define_const(rb_mPGconstants, "INVALID_OID", INT2FIX(InvalidOid));
511
+ rb_define_const(rb_mPGconstants, "InvalidOid", INT2FIX(InvalidOid));
512
+
513
+ /* Add the constants to the toplevel namespace */
514
+ rb_include_module( rb_mPG, rb_mPGconstants );
515
+
516
+ #ifdef M17N_SUPPORTED
517
+ enc_pg2ruby = st_init_numtable();
518
+ s_id_index = rb_intern("@encoding");
519
+ #endif
520
+
521
+ /* Initialize the main extension classes */
522
+ init_pg_connection();
523
+ init_pg_result();
524
+ }
525
+