ruby-pg 0.7.9.2008.01.08
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/COPYING.txt +340 -0
- data/Contributors +28 -0
- data/GPL +340 -0
- data/LICENSE +58 -0
- data/README +125 -0
- data/ext/compat.c +494 -0
- data/ext/compat.h +148 -0
- data/ext/extconf.rb +52 -0
- data/ext/pg.c +3043 -0
- data/ext/pg.h +13 -0
- metadata +59 -0
data/ext/compat.h
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
|
2
|
+
#ifndef __compat_h
|
3
|
+
#define __compat_h
|
4
|
+
|
5
|
+
#include <stdlib.h>
|
6
|
+
|
7
|
+
#include "ruby.h"
|
8
|
+
#include "rubyio.h"
|
9
|
+
#include "libpq-fe.h"
|
10
|
+
#include "libpq/libpq-fs.h" /* large-object interface */
|
11
|
+
|
12
|
+
|
13
|
+
#ifndef HAVE_PQCONNECTIONUSEDPASSWORD
|
14
|
+
#define POSTGRES_BEFORE_83
|
15
|
+
#endif
|
16
|
+
|
17
|
+
#if RUBY_VERSION_CODE < 180
|
18
|
+
#define rb_check_string_type(x) rb_check_convert_type(x, T_STRING, "String", "to_str")
|
19
|
+
#endif /* RUBY_VERSION_CODE < 180 */
|
20
|
+
|
21
|
+
#ifndef RARRAY_LEN
|
22
|
+
#define RARRAY_LEN(x) RARRAY((x))->len
|
23
|
+
#endif /* RARRAY_LEN */
|
24
|
+
|
25
|
+
#ifndef RSTRING_LEN
|
26
|
+
#define RSTRING_LEN(x) RSTRING((x))->len
|
27
|
+
#endif /* RSTRING_LEN */
|
28
|
+
|
29
|
+
#ifndef RSTRING_PTR
|
30
|
+
#define RSTRING_PTR(x) RSTRING((x))->ptr
|
31
|
+
#endif /* RSTRING_PTR */
|
32
|
+
|
33
|
+
#ifndef StringValuePtr
|
34
|
+
#define StringValuePtr(x) STR2CSTR(x)
|
35
|
+
#endif /* StringValuePtr */
|
36
|
+
|
37
|
+
#ifdef POSTGRES_BEFORE_83
|
38
|
+
#ifndef HAVE_PG_ENCODING_TO_CHAR
|
39
|
+
#define pg_encoding_to_char(x) "SQL_ASCII"
|
40
|
+
#else
|
41
|
+
/* Some versions ofPostgreSQL prior to 8.3 define
|
42
|
+
* pg_encoding_to_char but do not declare it
|
43
|
+
* in a header file, so this declaration will
|
44
|
+
* eliminate an unecessary warning
|
45
|
+
*/
|
46
|
+
extern char* pg_encoding_to_char(int);
|
47
|
+
#endif /* HAVE_PG_ENCODING_TO_CHAR */
|
48
|
+
#endif /* POSTGRES_BEFORE_83 */
|
49
|
+
|
50
|
+
#ifndef PG_DIAG_INTERNAL_POSITION
|
51
|
+
#define PG_DIAG_INTERNAL_POSITION 'p'
|
52
|
+
#endif /* PG_DIAG_INTERNAL_POSITION */
|
53
|
+
|
54
|
+
#ifndef PG_DIAG_INTERNAL_QUERY
|
55
|
+
#define PG_DIAG_INTERNAL_QUERY 'q'
|
56
|
+
#endif /* PG_DIAG_INTERNAL_QUERY */
|
57
|
+
|
58
|
+
#ifndef HAVE_PQFREEMEM
|
59
|
+
#define PQfreemem(ptr) free(ptr)
|
60
|
+
#endif /* HAVE_PQFREEMEM */
|
61
|
+
|
62
|
+
#ifndef HAVE_PQSETCLIENTENCODING
|
63
|
+
int PQsetClientEncoding(PGconn *conn, const char *encoding)
|
64
|
+
#endif /* HAVE_PQSETCLIENTENCODING */
|
65
|
+
|
66
|
+
#ifndef HAVE_PQESCAPESTRING
|
67
|
+
size_t PQescapeString(char *to, const char *from, size_t length);
|
68
|
+
unsigned char * PQescapeBytea(const unsigned char *bintext, size_t binlen, size_t *bytealen);
|
69
|
+
unsigned char * PQunescapeBytea(const unsigned char *strtext, size_t *retbuflen);
|
70
|
+
#endif /* HAVE_PQESCAPESTRING */
|
71
|
+
|
72
|
+
#ifndef HAVE_PQESCAPESTRINGCONN
|
73
|
+
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from,
|
74
|
+
size_t length, int *error);
|
75
|
+
unsigned char *PQescapeByteaConn(PGconn *conn, const unsigned char *from,
|
76
|
+
size_t from_length, size_t *to_length);
|
77
|
+
#endif /* HAVE_PQESCAPESTRINGCONN */
|
78
|
+
|
79
|
+
#ifndef HAVE_PQPREPARE
|
80
|
+
PGresult *PQprepare(PGconn *conn, const char *stmtName, const char *query,
|
81
|
+
int nParams, const Oid *paramTypes);
|
82
|
+
#endif /* HAVE_PQPREPARE */
|
83
|
+
|
84
|
+
#ifndef HAVE_PQDESCRIBEPREPARED
|
85
|
+
PGresult * PQdescribePrepared(PGconn *conn, const char *stmtName);
|
86
|
+
#endif /* HAVE_PQDESCRIBEPREPARED */
|
87
|
+
|
88
|
+
#ifndef HAVE_PQDESCRIBEPORTAL
|
89
|
+
PGresult * PQdescribePortal(PGconn *conn, const char *portalName);
|
90
|
+
#endif /* HAVE_PQDESCRIBEPORTAL */
|
91
|
+
|
92
|
+
#ifndef HAVE_PQCONNECTIONNEEDSPASSWORD
|
93
|
+
int PQconnectionNeedsPassword(PGconn *conn);
|
94
|
+
#endif /* HAVE_PQCONNECTIONNEEDSPASSWORD */
|
95
|
+
|
96
|
+
#ifndef HAVE_PQCONNECTIONUSEDPASSWORD
|
97
|
+
int PQconnectionUsedPassword(PGconn *conn);
|
98
|
+
#endif /* HAVE_PQCONNECTIONUSEDPASSWORD */
|
99
|
+
|
100
|
+
#ifndef HAVE_PQISTHREADSAFE
|
101
|
+
int PQisthreadsafe(void);
|
102
|
+
#endif /* HAVE_PQISTHREADSAFE */
|
103
|
+
|
104
|
+
#ifndef HAVE_LO_TRUNCATE
|
105
|
+
int lo_truncate(PGconn *conn, int fd, size_t len);
|
106
|
+
#endif /* HAVE_LO_TRUNCATE */
|
107
|
+
|
108
|
+
#ifndef HAVE_LO_CREATE
|
109
|
+
Oid lo_create(PGconn *conn, Oid lobjId);
|
110
|
+
#endif /* HAVE_LO_CREATE */
|
111
|
+
|
112
|
+
#ifndef HAVE_PQNPARAMS
|
113
|
+
int PQnparams(const PGresult *res);
|
114
|
+
#endif /* HAVE_PQNPARAMS */
|
115
|
+
|
116
|
+
#ifndef HAVE_PQPARAMTYPE
|
117
|
+
Oid PQparamtype(const PGresult *res, int param_number);
|
118
|
+
#endif /* HAVE_PQPARAMTYPE */
|
119
|
+
|
120
|
+
#ifndef HAVE_PQSERVERVERSION
|
121
|
+
int PQserverVersion(const PGconn* conn);
|
122
|
+
#endif /* HAVE_PQSERVERVERSION */
|
123
|
+
|
124
|
+
#ifndef HAVE_PQEXECPARAMS
|
125
|
+
PGresult *PQexecParams(PGconn *conn, const char *command, int nParams,
|
126
|
+
const Oid *paramTypes, const char * const * paramValues, const int *paramLengths,
|
127
|
+
const int *paramFormats, int resultFormat);
|
128
|
+
PGresult *PQexecParams_compat(PGconn *conn, VALUE command, VALUE values);
|
129
|
+
#endif /* HAVE_PQEXECPARAMS */
|
130
|
+
|
131
|
+
#ifndef HAVE_PQSENDDESCRIBEPREPARED
|
132
|
+
int PQsendDescribePrepared(PGconn *conn, const char *stmtName);
|
133
|
+
#endif /* HAVE_PQSENDDESCRIBEPREPARED */
|
134
|
+
|
135
|
+
#ifndef HAVE_PQSENDDESCRIBEPORTAL
|
136
|
+
int PQsendDescribePortal(PGconn *conn, const char *portalName);
|
137
|
+
#endif /* HAVE_PQSENDDESCRIBEPORTAL */
|
138
|
+
|
139
|
+
#ifndef HAVE_PQSENDPREPARE
|
140
|
+
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query,
|
141
|
+
int nParams, const Oid *paramTypes);
|
142
|
+
#endif /* HAVE_PQSENDPREPARE */
|
143
|
+
|
144
|
+
#ifndef HAVE_PQENCRYPTPASSWORD
|
145
|
+
char *PQencryptPassword(const char *passwd, const char *user);
|
146
|
+
#endif /* HAVE_PQENCRYPTPASSWORD */
|
147
|
+
|
148
|
+
#endif /* __compat_h */
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
if RUBY_VERSION < '1.3'
|
2
|
+
puts 'This library is for ruby-1.3 or higher.'
|
3
|
+
exit 1
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'mkmf'
|
7
|
+
|
8
|
+
def config_value(type)
|
9
|
+
ENV["POSTGRES_#{type.upcase}"] || pg_config(type)
|
10
|
+
end
|
11
|
+
|
12
|
+
def pg_config(type)
|
13
|
+
IO.popen("pg_config --#{type}dir").readline.chomp rescue nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def have_build_env
|
17
|
+
have_library('pq') && have_header('libpq-fe.h') && have_header('libpq/libpq-fs.h')
|
18
|
+
end
|
19
|
+
|
20
|
+
dir_config('pgsql', config_value('include'), config_value('lib'))
|
21
|
+
|
22
|
+
desired_functions = %w(
|
23
|
+
PQsetClientEncoding
|
24
|
+
PQfreemem
|
25
|
+
PQescapeStringConn
|
26
|
+
PQprepare
|
27
|
+
PQescapeString
|
28
|
+
PQexecParams
|
29
|
+
PQconnectionUsedPassword
|
30
|
+
PQconnectionNeedsPassword
|
31
|
+
PQisthreadsafe
|
32
|
+
PQnparams
|
33
|
+
PQparamtype
|
34
|
+
PQsendDescribePrepared
|
35
|
+
PQsendDescribePortal
|
36
|
+
PQsendPrepare
|
37
|
+
PQencryptPassword
|
38
|
+
PQdescribePrepared
|
39
|
+
PQdescribePortal
|
40
|
+
lo_create
|
41
|
+
lo_truncate
|
42
|
+
pg_encoding_to_char
|
43
|
+
)
|
44
|
+
|
45
|
+
if have_build_env
|
46
|
+
desired_functions.each(&method(:have_func))
|
47
|
+
$OBJS = ['pg.o','compat.o']
|
48
|
+
$CFLAGS << ' -Wall -Wmissing-prototypes'
|
49
|
+
create_makefile("pg")
|
50
|
+
else
|
51
|
+
puts 'Could not find PostgreSQL build environment (libraries & headers): Makefile not created'
|
52
|
+
end
|
data/ext/pg.c
ADDED
@@ -0,0 +1,3043 @@
|
|
1
|
+
/************************************************
|
2
|
+
|
3
|
+
pg.c -
|
4
|
+
|
5
|
+
Author: matz
|
6
|
+
created at: Tue May 13 20:07:35 JST 1997
|
7
|
+
|
8
|
+
Author: ematsu
|
9
|
+
modified at: Wed Jan 20 16:41:51 1999
|
10
|
+
|
11
|
+
$Author: jdavis $
|
12
|
+
$Date: 2008-01-08 18:03:23 -0800 (Tue, 08 Jan 2008) $
|
13
|
+
************************************************/
|
14
|
+
|
15
|
+
#include "pg.h"
|
16
|
+
|
17
|
+
#define AssignCheckedStringValue(cstring, rstring) do { \
|
18
|
+
if (!NIL_P(temp = rstring)) { \
|
19
|
+
Check_Type(temp, T_STRING); \
|
20
|
+
cstring = StringValuePtr(temp); \
|
21
|
+
} \
|
22
|
+
} while (0)
|
23
|
+
|
24
|
+
#define rb_define_singleton_alias(klass,new,old) rb_define_alias(rb_singleton_class(klass),new,old)
|
25
|
+
|
26
|
+
static VALUE rb_cPGconn;
|
27
|
+
static VALUE rb_cPGresult;
|
28
|
+
static VALUE rb_ePGError;
|
29
|
+
|
30
|
+
/* The following functions are part of libpq, but not
|
31
|
+
* available from ruby-pg, because they are deprecated,
|
32
|
+
* obsolete, or generally not useful:
|
33
|
+
*
|
34
|
+
* * PQfreemem -- unnecessary: copied to ruby object, then
|
35
|
+
* freed. Ruby object's memory is freed when
|
36
|
+
* it is garbage collected.
|
37
|
+
* * PQbinaryTuples -- better to use PQfformat
|
38
|
+
* * PQprint -- not very useful
|
39
|
+
* * PQsetdb -- not very useful
|
40
|
+
* * PQsetdbLogin -- not very useful
|
41
|
+
* * PQoidStatus -- deprecated, use PQoidValue
|
42
|
+
* * PQrequestCancel -- deprecated, use PQcancel
|
43
|
+
* * PQfn -- use a prepared statement instead
|
44
|
+
* * PQgetline -- deprecated, use PQgetCopyData
|
45
|
+
* * PQgetlineAsync -- deprecated, use PQgetCopyData
|
46
|
+
* * PQputline -- deprecated, use PQputCopyData
|
47
|
+
* * PQputnbytes -- deprecated, use PQputCopyData
|
48
|
+
* * PQendcopy -- deprecated, use PQputCopyEnd
|
49
|
+
*/
|
50
|
+
|
51
|
+
/***************************************************************************
|
52
|
+
* UTILITY FUNCTIONS
|
53
|
+
**************************************************************************/
|
54
|
+
|
55
|
+
static void free_pgconn(PGconn *);
|
56
|
+
static void pgresult_check(VALUE, VALUE);
|
57
|
+
|
58
|
+
static PGconn *get_pgconn(VALUE self);
|
59
|
+
static VALUE pgconn_finish(VALUE self);
|
60
|
+
static VALUE pgresult_clear(VALUE self);
|
61
|
+
static VALUE pgresult_aref(VALUE self, VALUE index);
|
62
|
+
|
63
|
+
/*
|
64
|
+
* Used to quote the values passed in a Hash to PGconn.init
|
65
|
+
* when building the connection string.
|
66
|
+
*/
|
67
|
+
static VALUE
|
68
|
+
pgconn_s_quote_connstr(string)
|
69
|
+
VALUE string;
|
70
|
+
{
|
71
|
+
char *str,*ptr;
|
72
|
+
int i,j=0,len;
|
73
|
+
VALUE result;
|
74
|
+
|
75
|
+
Check_Type(string, T_STRING);
|
76
|
+
|
77
|
+
ptr = RSTRING_PTR(string);
|
78
|
+
len = RSTRING_LEN(string);
|
79
|
+
str = ALLOCA_N(char, len * 2 + 2 + 1);
|
80
|
+
str[j++] = '\'';
|
81
|
+
for(i = 0; i < len; i++) {
|
82
|
+
if(ptr[i] == '\'' || ptr[i] == '\\')
|
83
|
+
str[j++] = '\\';
|
84
|
+
str[j++] = ptr[i];
|
85
|
+
}
|
86
|
+
str[j++] = '\'';
|
87
|
+
result = rb_str_new(str, j);
|
88
|
+
return result;
|
89
|
+
}
|
90
|
+
|
91
|
+
/*
|
92
|
+
* Appends key='hash[key]' to conninfo_rstr
|
93
|
+
*/
|
94
|
+
static void
|
95
|
+
build_key_value_string(hash, conninfo_rstr, key)
|
96
|
+
VALUE hash, conninfo_rstr;
|
97
|
+
char *key;
|
98
|
+
{
|
99
|
+
if(rb_funcall(hash, rb_intern("has_key?"), 1, ID2SYM(rb_intern(key)))) {
|
100
|
+
rb_str_cat2(conninfo_rstr, " ");
|
101
|
+
rb_str_cat2(conninfo_rstr, key);
|
102
|
+
rb_str_cat2(conninfo_rstr, "=");
|
103
|
+
rb_str_concat(conninfo_rstr, pgconn_s_quote_connstr(rb_obj_as_string(
|
104
|
+
rb_hash_aref(hash, ID2SYM(rb_intern(key))))));
|
105
|
+
}
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
|
109
|
+
static void
|
110
|
+
free_pgconn(PGconn *conn)
|
111
|
+
{
|
112
|
+
PQfinish(conn);
|
113
|
+
}
|
114
|
+
|
115
|
+
static void
|
116
|
+
free_pgresult(PGresult *result)
|
117
|
+
{
|
118
|
+
PQclear(result);
|
119
|
+
}
|
120
|
+
|
121
|
+
static PGconn*
|
122
|
+
get_pgconn(VALUE self)
|
123
|
+
{
|
124
|
+
PGconn *conn;
|
125
|
+
Data_Get_Struct(self, PGconn, conn);
|
126
|
+
if (conn == NULL) rb_raise(rb_eStandardError, "not connected");
|
127
|
+
return conn;
|
128
|
+
}
|
129
|
+
|
130
|
+
static PGresult*
|
131
|
+
get_pgresult(VALUE self)
|
132
|
+
{
|
133
|
+
PGresult *result;
|
134
|
+
Data_Get_Struct(self, PGresult, result);
|
135
|
+
if (result == NULL) rb_raise(rb_eStandardError, "result has been cleared");
|
136
|
+
return result;
|
137
|
+
}
|
138
|
+
|
139
|
+
static VALUE
|
140
|
+
new_pgresult(PGresult *result)
|
141
|
+
{
|
142
|
+
return Data_Wrap_Struct(rb_cPGresult, NULL, free_pgresult, result);
|
143
|
+
}
|
144
|
+
|
145
|
+
/*
|
146
|
+
* Raises appropriate exception if PGresult is
|
147
|
+
* in a bad state.
|
148
|
+
*/
|
149
|
+
static void
|
150
|
+
pgresult_check(VALUE rb_pgconn, VALUE rb_pgresult)
|
151
|
+
{
|
152
|
+
VALUE error;
|
153
|
+
PGconn *conn = get_pgconn(rb_pgconn);
|
154
|
+
PGresult *result = get_pgresult(rb_pgresult);
|
155
|
+
|
156
|
+
if(result == NULL)
|
157
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
158
|
+
switch (PQresultStatus(result)) {
|
159
|
+
case PGRES_TUPLES_OK:
|
160
|
+
case PGRES_COPY_OUT:
|
161
|
+
case PGRES_COPY_IN:
|
162
|
+
case PGRES_EMPTY_QUERY:
|
163
|
+
case PGRES_COMMAND_OK:
|
164
|
+
return;
|
165
|
+
case PGRES_BAD_RESPONSE:
|
166
|
+
case PGRES_FATAL_ERROR:
|
167
|
+
case PGRES_NONFATAL_ERROR:
|
168
|
+
error = rb_exc_new2(rb_ePGError, PQresultErrorMessage(result));
|
169
|
+
break;
|
170
|
+
default:
|
171
|
+
error = rb_exc_new2(rb_ePGError,
|
172
|
+
"internal error : unknown result status.");
|
173
|
+
}
|
174
|
+
|
175
|
+
rb_iv_set(error, "@connection", rb_pgconn);
|
176
|
+
rb_iv_set(error, "@result", rb_pgresult);
|
177
|
+
rb_exc_raise(error);
|
178
|
+
return;
|
179
|
+
}
|
180
|
+
|
181
|
+
static VALUE yield_pgresult(VALUE rb_pgresult)
|
182
|
+
{
|
183
|
+
int i;
|
184
|
+
PGresult *result = get_pgresult(rb_pgresult);
|
185
|
+
for(i = 0; i < PQntuples(result); i++) {
|
186
|
+
return rb_yield(pgresult_aref(rb_pgresult, INT2NUM(i)));
|
187
|
+
}
|
188
|
+
return Qnil;
|
189
|
+
}
|
190
|
+
|
191
|
+
/********************************************************************
|
192
|
+
*
|
193
|
+
* Document-class: PGconn
|
194
|
+
*
|
195
|
+
* The class to access PostgreSQL RDBMS, based on the libpq interface,
|
196
|
+
* provides convenient OO methods to interact with PostgreSQL.
|
197
|
+
*
|
198
|
+
* For example, to send query to the database on the localhost:
|
199
|
+
* require 'pg'
|
200
|
+
* conn = PGconn.open(:dbname => 'test')
|
201
|
+
* res = conn.exec('select * from a')
|
202
|
+
*
|
203
|
+
* See the PGresult class for information on working with the results of a query.
|
204
|
+
*
|
205
|
+
*/
|
206
|
+
|
207
|
+
#ifdef HAVE_RB_DEFINE_ALLOC_FUNC
|
208
|
+
static VALUE
|
209
|
+
pgconn_alloc(klass)
|
210
|
+
VALUE klass;
|
211
|
+
{
|
212
|
+
return Data_Wrap_Struct(klass, NULL, free_pgconn, NULL);
|
213
|
+
}
|
214
|
+
#else
|
215
|
+
static VALUE
|
216
|
+
pgconn_s_new(argc, argv, klass)
|
217
|
+
int argc;
|
218
|
+
VALUE *argv;
|
219
|
+
VALUE klass;
|
220
|
+
{
|
221
|
+
VALUE self = rb_obj_alloc(klass);
|
222
|
+
rb_obj_call_init(self, argc, argv);
|
223
|
+
return self;
|
224
|
+
}
|
225
|
+
#endif
|
226
|
+
|
227
|
+
/**************************************************************************
|
228
|
+
* PGconn SINGLETON METHODS
|
229
|
+
**************************************************************************/
|
230
|
+
|
231
|
+
/*
|
232
|
+
* Document-method: new
|
233
|
+
*
|
234
|
+
* call-seq:
|
235
|
+
* PGconn.open(connection_hash) -> PGconn
|
236
|
+
* PGconn.open(connection_string) -> PGconn
|
237
|
+
* PGconn.open(host, port, options, tty, dbname, login, password) -> PGconn
|
238
|
+
*
|
239
|
+
* * +host+ - server hostname
|
240
|
+
* * +hostaddr+ - server address (avoids hostname lookup, overrides +host+)
|
241
|
+
* * +port+ - server port number
|
242
|
+
* * +dbname+ - connecting database name
|
243
|
+
* * +user+ - login user name
|
244
|
+
* * +password+ - login password
|
245
|
+
* * +connect_timeout+ - maximum time to wait for connection to succeed
|
246
|
+
* * +options+ - backend options
|
247
|
+
* * +tty+ - (ignored in newer versions of PostgreSQL)
|
248
|
+
* * +sslmode+ - (disable|allow|prefer|require)
|
249
|
+
* * +krbsrvname+ - kerberos service name
|
250
|
+
* * +gsslib+ - GSS library to use for GSSAPI authentication
|
251
|
+
* * +service+ - service name to use for additional parameters
|
252
|
+
*
|
253
|
+
* _connection_hash_ example: +PGconn.connect(:dbname=>'test', :port=>5432)
|
254
|
+
* _connection_string_ example: +PGconn.connect("dbname=test port=5432")
|
255
|
+
* _connection_hash_ example: +PGconn.connect(nil,5432,nil,nil,'test',nil,nil)
|
256
|
+
*
|
257
|
+
* On failure, it raises a PGError exception.
|
258
|
+
*/
|
259
|
+
|
260
|
+
static VALUE
|
261
|
+
pgconn_init(argc, argv, self)
|
262
|
+
int argc;
|
263
|
+
VALUE *argv;
|
264
|
+
VALUE self;
|
265
|
+
{
|
266
|
+
VALUE args,arg;
|
267
|
+
PGconn *conn = NULL;
|
268
|
+
char *conninfo = NULL;
|
269
|
+
VALUE conninfo_rstr;
|
270
|
+
VALUE error;
|
271
|
+
VALUE temp;
|
272
|
+
char *host, *port, *opt, *tty, *dbname, *login, *pwd;
|
273
|
+
host=port=opt=tty=dbname=login=pwd=NULL;
|
274
|
+
|
275
|
+
rb_scan_args(argc, argv, "0*", &args);
|
276
|
+
if (RARRAY_LEN(args) == 1) {
|
277
|
+
arg = rb_ary_entry(args,0);
|
278
|
+
if(TYPE(arg) == T_HASH) {
|
279
|
+
conninfo_rstr = rb_str_new2("");
|
280
|
+
build_key_value_string(arg, conninfo_rstr, "host");
|
281
|
+
build_key_value_string(arg, conninfo_rstr, "hostaddr");
|
282
|
+
build_key_value_string(arg, conninfo_rstr, "port");
|
283
|
+
build_key_value_string(arg, conninfo_rstr, "dbname");
|
284
|
+
build_key_value_string(arg, conninfo_rstr, "user");
|
285
|
+
build_key_value_string(arg, conninfo_rstr, "password");
|
286
|
+
build_key_value_string(arg, conninfo_rstr, "opt");
|
287
|
+
build_key_value_string(arg, conninfo_rstr, "tty");
|
288
|
+
build_key_value_string(arg, conninfo_rstr, "sslmode");
|
289
|
+
build_key_value_string(arg, conninfo_rstr, "krbsrvname");
|
290
|
+
build_key_value_string(arg, conninfo_rstr, "gsslib");
|
291
|
+
build_key_value_string(arg, conninfo_rstr, "service");
|
292
|
+
conninfo = StringValuePtr(conninfo_rstr);
|
293
|
+
}
|
294
|
+
else if(TYPE(arg) == T_STRING) {
|
295
|
+
conninfo = StringValuePtr(arg);
|
296
|
+
}
|
297
|
+
else {
|
298
|
+
rb_raise(rb_eArgError,
|
299
|
+
"Expecting String or Hash as single argument");
|
300
|
+
}
|
301
|
+
conn = PQconnectdb(conninfo);
|
302
|
+
}
|
303
|
+
else if (RARRAY_LEN(args) == 7) {
|
304
|
+
AssignCheckedStringValue(host, rb_ary_entry(args, 0));
|
305
|
+
AssignCheckedStringValue(port, rb_obj_as_string(rb_ary_entry(args, 1)));
|
306
|
+
AssignCheckedStringValue(opt, rb_ary_entry(args, 2));
|
307
|
+
AssignCheckedStringValue(tty, rb_ary_entry(args, 3));
|
308
|
+
AssignCheckedStringValue(dbname, rb_ary_entry(args, 4));
|
309
|
+
AssignCheckedStringValue(login, rb_ary_entry(args, 5));
|
310
|
+
AssignCheckedStringValue(pwd, rb_ary_entry(args, 6));
|
311
|
+
|
312
|
+
conn = PQsetdbLogin(host, port, opt, tty, dbname, login, pwd);
|
313
|
+
}
|
314
|
+
else {
|
315
|
+
rb_raise(rb_eArgError,
|
316
|
+
"Expected connection info string, hash, or 7 separate arguments.");
|
317
|
+
}
|
318
|
+
|
319
|
+
if (PQstatus(conn) == CONNECTION_BAD) {
|
320
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
321
|
+
rb_iv_set(error, "@connection", self);
|
322
|
+
rb_exc_raise(error);
|
323
|
+
}
|
324
|
+
|
325
|
+
Check_Type(self, T_DATA);
|
326
|
+
DATA_PTR(self) = conn;
|
327
|
+
|
328
|
+
if (rb_block_given_p()) {
|
329
|
+
return rb_ensure(rb_yield, self, pgconn_finish, self);
|
330
|
+
}
|
331
|
+
return self;
|
332
|
+
}
|
333
|
+
|
334
|
+
//TODO PGconn.conndefaults
|
335
|
+
|
336
|
+
|
337
|
+
/*
|
338
|
+
* call-seq:
|
339
|
+
* PGconn.encrypt_password( password, username ) -> String
|
340
|
+
*
|
341
|
+
* This function is intended to be used by client applications that
|
342
|
+
* send commands like: +ALTER USER joe PASSWORD 'pwd'+.
|
343
|
+
* The arguments are the cleartext password, and the SQL name
|
344
|
+
* of the user it is for.
|
345
|
+
*
|
346
|
+
* Return value is the encrypted password.
|
347
|
+
*/
|
348
|
+
static VALUE
|
349
|
+
pgconn_s_encrypt_password(self, password, username)
|
350
|
+
VALUE self, password, username;
|
351
|
+
{
|
352
|
+
char *ret;
|
353
|
+
Check_Type(password, T_STRING);
|
354
|
+
Check_Type(username, T_STRING);
|
355
|
+
ret = PQencryptPassword(StringValuePtr(password),
|
356
|
+
StringValuePtr(username));
|
357
|
+
return rb_tainted_str_new2(ret);
|
358
|
+
}
|
359
|
+
|
360
|
+
/*
|
361
|
+
* call-seq:
|
362
|
+
* PGconn.isthreadsafe() -> Boolean
|
363
|
+
*
|
364
|
+
* Returns +true+ if libpq is thread safe, +false+ otherwise.
|
365
|
+
*/
|
366
|
+
static VALUE
|
367
|
+
pgconn_s_isthreadsafe(self)
|
368
|
+
VALUE self;
|
369
|
+
{
|
370
|
+
return PQisthreadsafe() ? Qtrue : Qfalse;
|
371
|
+
}
|
372
|
+
|
373
|
+
/**************************************************************************
|
374
|
+
* PGconn INSTANCE METHODS
|
375
|
+
**************************************************************************/
|
376
|
+
|
377
|
+
/*
|
378
|
+
* call-seq:
|
379
|
+
* conn.finish()
|
380
|
+
*
|
381
|
+
* Closes the backend connection.
|
382
|
+
*/
|
383
|
+
static VALUE
|
384
|
+
pgconn_finish(self)
|
385
|
+
VALUE self;
|
386
|
+
{
|
387
|
+
PQfinish(get_pgconn(self));
|
388
|
+
DATA_PTR(self) = NULL;
|
389
|
+
return Qnil;
|
390
|
+
}
|
391
|
+
|
392
|
+
/*
|
393
|
+
* call-seq:
|
394
|
+
* conn.reset()
|
395
|
+
*
|
396
|
+
* Resets the backend connection. This method closes the backend connection and tries to re-connect.
|
397
|
+
*/
|
398
|
+
static VALUE
|
399
|
+
pgconn_reset(self)
|
400
|
+
VALUE self;
|
401
|
+
{
|
402
|
+
PQreset(get_pgconn(self));
|
403
|
+
return self;
|
404
|
+
}
|
405
|
+
|
406
|
+
//TODO conn.reset_start
|
407
|
+
|
408
|
+
//TODO conn.reset_poll
|
409
|
+
|
410
|
+
/*
|
411
|
+
* call-seq:
|
412
|
+
* conn.db()
|
413
|
+
*
|
414
|
+
* Returns the connected database name.
|
415
|
+
*/
|
416
|
+
static VALUE
|
417
|
+
pgconn_db(self)
|
418
|
+
VALUE self;
|
419
|
+
{
|
420
|
+
char *db = PQdb(get_pgconn(self));
|
421
|
+
if (!db) return Qnil;
|
422
|
+
return rb_tainted_str_new2(db);
|
423
|
+
}
|
424
|
+
|
425
|
+
/*
|
426
|
+
* call-seq:
|
427
|
+
* conn.user()
|
428
|
+
*
|
429
|
+
* Returns the authenticated user name.
|
430
|
+
*/
|
431
|
+
static VALUE
|
432
|
+
pgconn_user(self)
|
433
|
+
VALUE self;
|
434
|
+
{
|
435
|
+
char *user = PQuser(get_pgconn(self));
|
436
|
+
if (!user) return Qnil;
|
437
|
+
return rb_tainted_str_new2(user);
|
438
|
+
}
|
439
|
+
|
440
|
+
/*
|
441
|
+
* call-seq:
|
442
|
+
* conn.pass()
|
443
|
+
*
|
444
|
+
* Returns the authenticated user name.
|
445
|
+
*/
|
446
|
+
static VALUE
|
447
|
+
pgconn_pass(self)
|
448
|
+
VALUE self;
|
449
|
+
{
|
450
|
+
char *user = PQpass(get_pgconn(self));
|
451
|
+
if (!user) return Qnil;
|
452
|
+
return rb_tainted_str_new2(user);
|
453
|
+
}
|
454
|
+
|
455
|
+
/*
|
456
|
+
* call-seq:
|
457
|
+
* conn.host()
|
458
|
+
*
|
459
|
+
* Returns the connected server name.
|
460
|
+
*/
|
461
|
+
static VALUE
|
462
|
+
pgconn_host(self)
|
463
|
+
VALUE self;
|
464
|
+
{
|
465
|
+
char *host = PQhost(get_pgconn(self));
|
466
|
+
if (!host) return Qnil;
|
467
|
+
return rb_tainted_str_new2(host);
|
468
|
+
}
|
469
|
+
|
470
|
+
/*
|
471
|
+
* call-seq:
|
472
|
+
* conn.port()
|
473
|
+
*
|
474
|
+
* Returns the connected server port number.
|
475
|
+
*/
|
476
|
+
static VALUE
|
477
|
+
pgconn_port(self)
|
478
|
+
VALUE self;
|
479
|
+
{
|
480
|
+
char* port = PQport(get_pgconn(self));
|
481
|
+
return INT2NUM(atol(port));
|
482
|
+
}
|
483
|
+
|
484
|
+
/*
|
485
|
+
* call-seq:
|
486
|
+
* conn.tty()
|
487
|
+
*
|
488
|
+
* Returns the connected pgtty. (Obsolete)
|
489
|
+
*/
|
490
|
+
static VALUE
|
491
|
+
pgconn_tty(self)
|
492
|
+
VALUE self;
|
493
|
+
{
|
494
|
+
char *tty = PQtty(get_pgconn(self));
|
495
|
+
if (!tty) return Qnil;
|
496
|
+
return rb_tainted_str_new2(tty);
|
497
|
+
}
|
498
|
+
|
499
|
+
/*
|
500
|
+
* call-seq:
|
501
|
+
* conn.options()
|
502
|
+
*
|
503
|
+
* Returns backend option string.
|
504
|
+
*/
|
505
|
+
static VALUE
|
506
|
+
pgconn_options(self)
|
507
|
+
VALUE self;
|
508
|
+
{
|
509
|
+
char *options = PQoptions(get_pgconn(self));
|
510
|
+
if (!options) return Qnil;
|
511
|
+
return rb_tainted_str_new2(options);
|
512
|
+
}
|
513
|
+
|
514
|
+
/*
|
515
|
+
* call-seq:
|
516
|
+
* conn.status()
|
517
|
+
*
|
518
|
+
* Returns status of connection : CONNECTION_OK or CONNECTION_BAD
|
519
|
+
*/
|
520
|
+
static VALUE
|
521
|
+
pgconn_status(self)
|
522
|
+
VALUE self;
|
523
|
+
{
|
524
|
+
return INT2NUM(PQstatus(get_pgconn(self)));
|
525
|
+
}
|
526
|
+
|
527
|
+
/*
|
528
|
+
* call-seq:
|
529
|
+
* conn.transaction_status()
|
530
|
+
*
|
531
|
+
* returns one of the following statuses:
|
532
|
+
* PQTRANS_IDLE = 0 (connection idle)
|
533
|
+
* PQTRANS_ACTIVE = 1 (command in progress)
|
534
|
+
* PQTRANS_INTRANS = 2 (idle, within transaction block)
|
535
|
+
* PQTRANS_INERROR = 3 (idle, within failed transaction)
|
536
|
+
* PQTRANS_UNKNOWN = 4 (cannot determine status)
|
537
|
+
*/
|
538
|
+
static VALUE
|
539
|
+
pgconn_transaction_status(self)
|
540
|
+
VALUE self;
|
541
|
+
{
|
542
|
+
return INT2NUM(PQtransactionStatus(get_pgconn(self)));
|
543
|
+
}
|
544
|
+
|
545
|
+
/*
|
546
|
+
* call-seq:
|
547
|
+
* conn.parameter_status( param_name ) -> String
|
548
|
+
*
|
549
|
+
* Returns the setting of parameter _param_name_, where
|
550
|
+
* _param_name_ is one of
|
551
|
+
* * +server_version+
|
552
|
+
* * +server_encoding+
|
553
|
+
* * +client_encoding+
|
554
|
+
* * +is_superuser+
|
555
|
+
* * +session_authorization+
|
556
|
+
* * +DateStyle+
|
557
|
+
* * +TimeZone+
|
558
|
+
* * +integer_datetimes+
|
559
|
+
* * +standard_conforming_strings+
|
560
|
+
*
|
561
|
+
* Returns nil if the value of the parameter is not known.
|
562
|
+
*/
|
563
|
+
static VALUE
|
564
|
+
pgconn_parameter_status(self, param_name)
|
565
|
+
VALUE self, param_name;
|
566
|
+
{
|
567
|
+
const char *ret = PQparameterStatus(get_pgconn(self),
|
568
|
+
StringValuePtr(param_name));
|
569
|
+
if(ret == NULL)
|
570
|
+
return Qnil;
|
571
|
+
else
|
572
|
+
return rb_tainted_str_new2(ret);
|
573
|
+
}
|
574
|
+
|
575
|
+
/*
|
576
|
+
* call-seq:
|
577
|
+
* conn.protocol_version -> Integer
|
578
|
+
*
|
579
|
+
* The 3.0 protocol will normally be used when communicating with PostgreSQL 7.4
|
580
|
+
* or later servers; pre-7.4 servers support only protocol 2.0. (Protocol 1.0 is
|
581
|
+
* obsolete and not supported by libpq.)
|
582
|
+
*/
|
583
|
+
static VALUE
|
584
|
+
pgconn_protocol_version(self)
|
585
|
+
VALUE self;
|
586
|
+
{
|
587
|
+
return INT2NUM(PQprotocolVersion(get_pgconn(self)));
|
588
|
+
}
|
589
|
+
|
590
|
+
/*
|
591
|
+
* call-seq:
|
592
|
+
* conn.server_version -> Integer
|
593
|
+
*
|
594
|
+
* The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. For example, version 7.4.2 will be returned as 70402, and version 8.1 will be returned as 80100 (leading zeroes are not shown). Zero is returned if the connection is bad.
|
595
|
+
*/
|
596
|
+
static VALUE
|
597
|
+
pgconn_server_version(self)
|
598
|
+
VALUE self;
|
599
|
+
{
|
600
|
+
return INT2NUM(PQserverVersion(get_pgconn(self)));
|
601
|
+
}
|
602
|
+
|
603
|
+
/*
|
604
|
+
* call-seq:
|
605
|
+
* conn.error() -> String
|
606
|
+
*
|
607
|
+
* Returns the error message about connection.
|
608
|
+
*/
|
609
|
+
static VALUE
|
610
|
+
pgconn_error_message(self)
|
611
|
+
VALUE self;
|
612
|
+
{
|
613
|
+
char *error = PQerrorMessage(get_pgconn(self));
|
614
|
+
if (!error) return Qnil;
|
615
|
+
return rb_tainted_str_new2(error);
|
616
|
+
}
|
617
|
+
|
618
|
+
//TODO PQsocket
|
619
|
+
/*
|
620
|
+
* call-seq:
|
621
|
+
* conn.socket() -> TCPSocket
|
622
|
+
*
|
623
|
+
* Returns the socket file descriptor of this
|
624
|
+
* connection.
|
625
|
+
*/
|
626
|
+
|
627
|
+
|
628
|
+
/*
|
629
|
+
* call-seq:
|
630
|
+
* conn.backed_pid() -> Fixnum
|
631
|
+
*
|
632
|
+
* Returns the process ID of the backend server
|
633
|
+
* process for this connection.
|
634
|
+
* Note that this is a PID on database server host.
|
635
|
+
*/
|
636
|
+
static VALUE
|
637
|
+
pgconn_backend_pid(self)
|
638
|
+
VALUE self;
|
639
|
+
{
|
640
|
+
return INT2NUM(PQbackendPID(get_pgconn(self)));
|
641
|
+
}
|
642
|
+
|
643
|
+
/*
|
644
|
+
* call-seq:
|
645
|
+
* conn.connection_needs_password() -> Boolean
|
646
|
+
*
|
647
|
+
* Returns +true+ if the authentication method required a
|
648
|
+
* password, but none was available. +false+ otherwise.
|
649
|
+
*/
|
650
|
+
static VALUE
|
651
|
+
pgconn_connection_needs_password(self)
|
652
|
+
VALUE self;
|
653
|
+
{
|
654
|
+
return PQconnectionNeedsPassword(get_pgconn(self)) ? Qtrue : Qfalse;
|
655
|
+
}
|
656
|
+
|
657
|
+
/*
|
658
|
+
* call-seq:
|
659
|
+
* conn.connection_used_password() -> Boolean
|
660
|
+
*
|
661
|
+
* Returns +true+ if the authentication method used
|
662
|
+
* a caller-supplied password, +false+ otherwise.
|
663
|
+
*/
|
664
|
+
static VALUE
|
665
|
+
pgconn_connection_used_password(self)
|
666
|
+
VALUE self;
|
667
|
+
{
|
668
|
+
return PQconnectionUsedPassword(get_pgconn(self)) ? Qtrue : Qfalse;
|
669
|
+
}
|
670
|
+
|
671
|
+
|
672
|
+
//TODO get_ssl
|
673
|
+
|
674
|
+
|
675
|
+
/*
|
676
|
+
* call-seq:
|
677
|
+
* conn.exec(sql [, params, result_format ] ) -> PGresult
|
678
|
+
*
|
679
|
+
* Sends SQL query request specified by _sql_ to PostgreSQL.
|
680
|
+
* Returns a PGresult instance on success.
|
681
|
+
* On failure, it raises a PGError exception.
|
682
|
+
*
|
683
|
+
* +params+ is an optional array of the bind parameters for the SQL query.
|
684
|
+
* Each element of the +params+ array may be either:
|
685
|
+
* a hash of the form:
|
686
|
+
* {:value => String (value of bind parameter)
|
687
|
+
* :type => Fixnum (oid of type of bind parameter)
|
688
|
+
* :format => Fixnum (0 for text, 1 for binary)
|
689
|
+
* }
|
690
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
691
|
+
* { :value => <string value>, :type => 0, :format => 0 }
|
692
|
+
*
|
693
|
+
* PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
|
694
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
695
|
+
* to $1, the 1st element is bound to $2, etc.
|
696
|
+
*
|
697
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
698
|
+
* Instead of specifying type oids, it's recommended to simply add
|
699
|
+
* explicit casts in the query to ensure that the right type is used.
|
700
|
+
*
|
701
|
+
* For example: "SELECT $1::int"
|
702
|
+
*
|
703
|
+
* The optional +result_format+ should be 0 for text results, 1
|
704
|
+
* for binary.
|
705
|
+
*/
|
706
|
+
static VALUE
|
707
|
+
pgconn_exec(argc, argv, self)
|
708
|
+
int argc;
|
709
|
+
VALUE *argv;
|
710
|
+
VALUE self;
|
711
|
+
{
|
712
|
+
PGconn *conn = get_pgconn(self);
|
713
|
+
PGresult *result = NULL;
|
714
|
+
VALUE rb_pgresult;
|
715
|
+
VALUE command, params, in_res_fmt;
|
716
|
+
VALUE param, param_type, param_value, param_format;
|
717
|
+
VALUE param_value_tmp;
|
718
|
+
VALUE sym_type, sym_value, sym_format;
|
719
|
+
int i=0;
|
720
|
+
int nParams;
|
721
|
+
Oid *paramTypes;
|
722
|
+
char ** paramValues;
|
723
|
+
int *paramLengths;
|
724
|
+
int *paramFormats;
|
725
|
+
int resultFormat;
|
726
|
+
|
727
|
+
rb_scan_args(argc, argv, "12", &command, ¶ms, &in_res_fmt);
|
728
|
+
|
729
|
+
Check_Type(command, T_STRING);
|
730
|
+
|
731
|
+
/* If called with no parameters, use PQexec */
|
732
|
+
if(NIL_P(params)) {
|
733
|
+
result = PQexec(conn, StringValuePtr(command));
|
734
|
+
rb_pgresult = new_pgresult(result);
|
735
|
+
pgresult_check(self, rb_pgresult);
|
736
|
+
if (rb_block_given_p()) {
|
737
|
+
return rb_ensure(yield_pgresult, rb_pgresult,
|
738
|
+
pgresult_clear, rb_pgresult);
|
739
|
+
}
|
740
|
+
return rb_pgresult;
|
741
|
+
}
|
742
|
+
|
743
|
+
/* If called with parameters, and optionally result_format,
|
744
|
+
* use PQexecParams
|
745
|
+
*/
|
746
|
+
Check_Type(params, T_ARRAY);
|
747
|
+
|
748
|
+
if(NIL_P(in_res_fmt)) {
|
749
|
+
resultFormat = 0;
|
750
|
+
}
|
751
|
+
else {
|
752
|
+
resultFormat = NUM2INT(in_res_fmt);
|
753
|
+
}
|
754
|
+
|
755
|
+
sym_type = ID2SYM(rb_intern("type"));
|
756
|
+
sym_value = ID2SYM(rb_intern("value"));
|
757
|
+
sym_format = ID2SYM(rb_intern("format"));
|
758
|
+
nParams = RARRAY(params)->len;
|
759
|
+
paramTypes = ALLOC_N(Oid, nParams);
|
760
|
+
paramValues = ALLOC_N(char *, nParams);
|
761
|
+
paramLengths = ALLOC_N(int, nParams);
|
762
|
+
paramFormats = ALLOC_N(int, nParams);
|
763
|
+
for(i = 0; i < nParams; i++) {
|
764
|
+
param = rb_ary_entry(params, i);
|
765
|
+
if (TYPE(param) == T_HASH) {
|
766
|
+
param_type = rb_hash_aref(param, sym_type);
|
767
|
+
param_value_tmp = rb_hash_aref(param, sym_value);
|
768
|
+
if(TYPE(param_value_tmp) == T_STRING)
|
769
|
+
param_value = param_value_tmp;
|
770
|
+
else
|
771
|
+
param_value = rb_funcall(param_value_tmp, rb_intern("to_s"), 0);
|
772
|
+
param_format = rb_hash_aref(param, sym_format);
|
773
|
+
}
|
774
|
+
else {
|
775
|
+
param_type = INT2NUM(0);
|
776
|
+
if(TYPE(param) == T_STRING)
|
777
|
+
param_value = param;
|
778
|
+
else
|
779
|
+
param_value = rb_funcall(param, rb_intern("to_s"), 0);
|
780
|
+
param_format = INT2NUM(0);
|
781
|
+
}
|
782
|
+
Check_Type(param_value, T_STRING);
|
783
|
+
paramTypes[i] = NUM2INT(param_type);
|
784
|
+
paramValues[i] = RSTRING_PTR(param_value);
|
785
|
+
paramLengths[i] = RSTRING_LEN(param_value) + 1;
|
786
|
+
paramFormats[i] = NUM2INT(param_format);
|
787
|
+
}
|
788
|
+
|
789
|
+
result = PQexecParams(conn, StringValuePtr(command), nParams, paramTypes,
|
790
|
+
(const char * const *)paramValues, paramLengths, paramFormats, resultFormat);
|
791
|
+
|
792
|
+
free(paramTypes);
|
793
|
+
free(paramValues);
|
794
|
+
free(paramLengths);
|
795
|
+
free(paramFormats);
|
796
|
+
|
797
|
+
rb_pgresult = new_pgresult(result);
|
798
|
+
pgresult_check(self, rb_pgresult);
|
799
|
+
if (rb_block_given_p()) {
|
800
|
+
return rb_ensure(yield_pgresult, rb_pgresult,
|
801
|
+
pgresult_clear, rb_pgresult);
|
802
|
+
}
|
803
|
+
return rb_pgresult;
|
804
|
+
}
|
805
|
+
|
806
|
+
/*
|
807
|
+
* call-seq:
|
808
|
+
* conn.prepare(stmt_name, sql [, param_types ] ) -> PGresult
|
809
|
+
*
|
810
|
+
* Prepares statement _sql_ with name _name_ to be executed later.
|
811
|
+
* Returns a PGresult instance on success.
|
812
|
+
* On failure, it raises a PGError exception.
|
813
|
+
*
|
814
|
+
* +param_types+ is an optional parameter to specify the Oids of the
|
815
|
+
* types of the parameters.
|
816
|
+
*
|
817
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
818
|
+
* Instead of specifying type oids, it's recommended to simply add
|
819
|
+
* explicit casts in the query to ensure that the right type is used.
|
820
|
+
*
|
821
|
+
* For example: "SELECT $1::int"
|
822
|
+
*
|
823
|
+
* PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
|
824
|
+
* inside the SQL query.
|
825
|
+
*/
|
826
|
+
static VALUE
|
827
|
+
pgconn_prepare(argc, argv, self)
|
828
|
+
int argc;
|
829
|
+
VALUE *argv;
|
830
|
+
VALUE self;
|
831
|
+
{
|
832
|
+
PGconn *conn = get_pgconn(self);
|
833
|
+
PGresult *result = NULL;
|
834
|
+
VALUE rb_pgresult;
|
835
|
+
VALUE name, command, in_paramtypes;
|
836
|
+
VALUE param;
|
837
|
+
int i = 0;
|
838
|
+
int nParams = 0;
|
839
|
+
Oid *paramTypes = NULL;
|
840
|
+
|
841
|
+
rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
|
842
|
+
Check_Type(name, T_STRING);
|
843
|
+
Check_Type(command, T_STRING);
|
844
|
+
|
845
|
+
if(! NIL_P(in_paramtypes)) {
|
846
|
+
Check_Type(in_paramtypes, T_ARRAY);
|
847
|
+
nParams = RARRAY(in_paramtypes)->len;
|
848
|
+
paramTypes = ALLOC_N(Oid, nParams);
|
849
|
+
for(i = 0; i < nParams; i++) {
|
850
|
+
param = rb_ary_entry(in_paramtypes, i);
|
851
|
+
Check_Type(param, T_FIXNUM);
|
852
|
+
paramTypes[i] = NUM2INT(param);
|
853
|
+
}
|
854
|
+
}
|
855
|
+
result = PQprepare(conn, StringValuePtr(name), StringValuePtr(command),
|
856
|
+
nParams, paramTypes);
|
857
|
+
|
858
|
+
free(paramTypes);
|
859
|
+
|
860
|
+
rb_pgresult = new_pgresult(result);
|
861
|
+
pgresult_check(self, rb_pgresult);
|
862
|
+
return rb_pgresult;
|
863
|
+
}
|
864
|
+
|
865
|
+
/*
|
866
|
+
* call-seq:
|
867
|
+
* conn.exec_prepared(statement_name [, params, result_format ] ) -> PGresult
|
868
|
+
*
|
869
|
+
* Execute prepared named statement specified by _statement_name_.
|
870
|
+
* Returns a PGresult instance on success.
|
871
|
+
* On failure, it raises a PGError exception.
|
872
|
+
*
|
873
|
+
* +params+ is an array of the optional bind parameters for the
|
874
|
+
* SQL query. Each element of the +params+ array may be either:
|
875
|
+
* a hash of the form:
|
876
|
+
* {:value => String (value of bind parameter)
|
877
|
+
* :format => Fixnum (0 for text, 1 for binary)
|
878
|
+
* }
|
879
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
880
|
+
* { :value => <string value>, :format => 0 }
|
881
|
+
*
|
882
|
+
* PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
|
883
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
884
|
+
* to $1, the 1st element is bound to $2, etc.
|
885
|
+
*
|
886
|
+
* The optional +result_format+ should be 0 for text results, 1
|
887
|
+
* for binary.
|
888
|
+
*/
|
889
|
+
static VALUE
|
890
|
+
pgconn_exec_prepared(argc, argv, self)
|
891
|
+
int argc;
|
892
|
+
VALUE *argv;
|
893
|
+
VALUE self;
|
894
|
+
{
|
895
|
+
PGconn *conn = get_pgconn(self);
|
896
|
+
PGresult *result = NULL;
|
897
|
+
VALUE rb_pgresult;
|
898
|
+
VALUE name, params, in_res_fmt;
|
899
|
+
VALUE param, param_value, param_format;
|
900
|
+
VALUE param_value_tmp;
|
901
|
+
VALUE sym_value, sym_format;
|
902
|
+
int i = 0;
|
903
|
+
int nParams;
|
904
|
+
char ** paramValues;
|
905
|
+
int *paramLengths;
|
906
|
+
int *paramFormats;
|
907
|
+
int resultFormat;
|
908
|
+
|
909
|
+
|
910
|
+
rb_scan_args(argc, argv, "12", &name, ¶ms, &in_res_fmt);
|
911
|
+
Check_Type(name, T_STRING);
|
912
|
+
|
913
|
+
if(NIL_P(params)) {
|
914
|
+
params = rb_ary_new2(0);
|
915
|
+
resultFormat = 0;
|
916
|
+
}
|
917
|
+
else {
|
918
|
+
Check_Type(params, T_ARRAY);
|
919
|
+
}
|
920
|
+
|
921
|
+
if(NIL_P(in_res_fmt)) {
|
922
|
+
resultFormat = 0;
|
923
|
+
}
|
924
|
+
else {
|
925
|
+
resultFormat = NUM2INT(in_res_fmt);
|
926
|
+
}
|
927
|
+
|
928
|
+
sym_value = ID2SYM(rb_intern("value"));
|
929
|
+
sym_format = ID2SYM(rb_intern("format"));
|
930
|
+
nParams = RARRAY(params)->len;
|
931
|
+
paramValues = ALLOC_N(char *, nParams);
|
932
|
+
paramLengths = ALLOC_N(int, nParams);
|
933
|
+
paramFormats = ALLOC_N(int, nParams);
|
934
|
+
for(i = 0; i < nParams; i++) {
|
935
|
+
param = rb_ary_entry(params, i);
|
936
|
+
if (TYPE(param) == T_HASH) {
|
937
|
+
param_value_tmp = rb_hash_aref(param, sym_value);
|
938
|
+
if(TYPE(param_value_tmp) == T_STRING)
|
939
|
+
param_value = param_value_tmp;
|
940
|
+
else
|
941
|
+
param_value = rb_funcall(param_value_tmp, rb_intern("to_s"), 0);
|
942
|
+
param_format = rb_hash_aref(param, sym_format);
|
943
|
+
}
|
944
|
+
else {
|
945
|
+
if(TYPE(param) == T_STRING)
|
946
|
+
param_value = param;
|
947
|
+
else
|
948
|
+
param_value = rb_funcall(param, rb_intern("to_s"), 0);
|
949
|
+
param_format = INT2NUM(0);
|
950
|
+
}
|
951
|
+
Check_Type(param_value, T_STRING);
|
952
|
+
paramValues[i] = RSTRING_PTR(param_value);
|
953
|
+
paramLengths[i] = RSTRING_LEN(param_value) + 1;
|
954
|
+
paramFormats[i] = NUM2INT(param_format);
|
955
|
+
}
|
956
|
+
|
957
|
+
result = PQexecPrepared(conn, StringValuePtr(name), nParams,
|
958
|
+
(const char * const *)paramValues, paramLengths, paramFormats,
|
959
|
+
resultFormat);
|
960
|
+
|
961
|
+
free(paramValues);
|
962
|
+
free(paramLengths);
|
963
|
+
free(paramFormats);
|
964
|
+
|
965
|
+
rb_pgresult = new_pgresult(result);
|
966
|
+
pgresult_check(self, rb_pgresult);
|
967
|
+
if (rb_block_given_p()) {
|
968
|
+
return rb_ensure(yield_pgresult, rb_pgresult,
|
969
|
+
pgresult_clear, rb_pgresult);
|
970
|
+
}
|
971
|
+
return rb_pgresult;
|
972
|
+
}
|
973
|
+
|
974
|
+
/*
|
975
|
+
* call-seq:
|
976
|
+
* conn.describe_prepared( statement_name ) -> PGresult
|
977
|
+
*
|
978
|
+
* Retrieve information about the prepared statement
|
979
|
+
* _statement_name_.
|
980
|
+
*/
|
981
|
+
static VALUE
|
982
|
+
pgconn_describe_prepared(self, stmt_name)
|
983
|
+
VALUE self, stmt_name;
|
984
|
+
{
|
985
|
+
PGresult *result;
|
986
|
+
VALUE rb_pgresult;
|
987
|
+
PGconn *conn = get_pgconn(self);
|
988
|
+
char *stmt;
|
989
|
+
if(stmt_name == Qnil) {
|
990
|
+
stmt = NULL;
|
991
|
+
}
|
992
|
+
else {
|
993
|
+
Check_Type(stmt_name, T_STRING);
|
994
|
+
stmt = StringValuePtr(stmt_name);
|
995
|
+
}
|
996
|
+
result = PQdescribePrepared(conn, stmt);
|
997
|
+
rb_pgresult = new_pgresult(result);
|
998
|
+
pgresult_check(self, rb_pgresult);
|
999
|
+
return rb_pgresult;
|
1000
|
+
}
|
1001
|
+
|
1002
|
+
|
1003
|
+
/*
|
1004
|
+
* call-seq:
|
1005
|
+
* conn.describe_portal( portal_name ) -> PGresult
|
1006
|
+
*
|
1007
|
+
* Retrieve information about the portal _portal_name_.
|
1008
|
+
*/
|
1009
|
+
static VALUE
|
1010
|
+
pgconn_describe_portal(self, stmt_name)
|
1011
|
+
VALUE self, stmt_name;
|
1012
|
+
{
|
1013
|
+
PGresult *result;
|
1014
|
+
VALUE rb_pgresult;
|
1015
|
+
PGconn *conn = get_pgconn(self);
|
1016
|
+
char *stmt;
|
1017
|
+
if(stmt_name == Qnil) {
|
1018
|
+
stmt = NULL;
|
1019
|
+
}
|
1020
|
+
else {
|
1021
|
+
Check_Type(stmt_name, T_STRING);
|
1022
|
+
stmt = StringValuePtr(stmt_name);
|
1023
|
+
}
|
1024
|
+
result = PQdescribePortal(conn, stmt);
|
1025
|
+
rb_pgresult = new_pgresult(result);
|
1026
|
+
pgresult_check(self, rb_pgresult);
|
1027
|
+
return rb_pgresult;
|
1028
|
+
}
|
1029
|
+
|
1030
|
+
|
1031
|
+
/*
|
1032
|
+
* call-seq:
|
1033
|
+
* conn.make_empty_pgresult( status ) -> PGresult
|
1034
|
+
*
|
1035
|
+
* Constructs and empty PGresult with status _status_.
|
1036
|
+
* _status_ may be one of:
|
1037
|
+
* * +PGRES_EMPTY_QUERY+
|
1038
|
+
* * +PGRES_COMMAND_OK+
|
1039
|
+
* * +PGRES_TUPLES_OK+
|
1040
|
+
* * +PGRES_COPY_OUT+
|
1041
|
+
* * +PGRES_COPY_IN+
|
1042
|
+
* * +PGRES_BAD_RESPONSE+
|
1043
|
+
* * +PGRES_NONFATAL_ERROR+
|
1044
|
+
* * +PGRES_FATAL_ERROR+
|
1045
|
+
*/
|
1046
|
+
static VALUE
|
1047
|
+
pgconn_make_empty_pgresult(VALUE self, VALUE status)
|
1048
|
+
{
|
1049
|
+
PGresult *result;
|
1050
|
+
VALUE rb_pgresult;
|
1051
|
+
PGconn *conn = get_pgconn(self);
|
1052
|
+
result = PQmakeEmptyPGresult(conn, NUM2INT(status));
|
1053
|
+
rb_pgresult = new_pgresult(result);
|
1054
|
+
pgresult_check(self, rb_pgresult);
|
1055
|
+
return rb_pgresult;
|
1056
|
+
}
|
1057
|
+
|
1058
|
+
|
1059
|
+
/*
|
1060
|
+
* call-seq:
|
1061
|
+
* conn.escape_string( str ) -> String
|
1062
|
+
* PGconn.escape_string( str ) -> String # DEPRECATED
|
1063
|
+
*
|
1064
|
+
* Connection instance method for versions of 8.1 and higher of libpq
|
1065
|
+
* uses PQescapeStringConn, which is safer. Avoid calling as a class method,
|
1066
|
+
* the class method uses the deprecated PQescapeString() API function.
|
1067
|
+
*
|
1068
|
+
* Returns a SQL-safe version of the String _str_.
|
1069
|
+
* This is the preferred way to make strings safe for inclusion in
|
1070
|
+
* SQL queries.
|
1071
|
+
*
|
1072
|
+
* Consider using exec_params, which avoids the need for passing values
|
1073
|
+
* inside of SQL commands.
|
1074
|
+
*/
|
1075
|
+
static VALUE
|
1076
|
+
pgconn_s_escape(self, string)
|
1077
|
+
VALUE self;
|
1078
|
+
VALUE string;
|
1079
|
+
{
|
1080
|
+
char *escaped;
|
1081
|
+
int size,error;
|
1082
|
+
VALUE result;
|
1083
|
+
|
1084
|
+
Check_Type(string, T_STRING);
|
1085
|
+
|
1086
|
+
escaped = ALLOCA_N(char, RSTRING_LEN(string) * 2 + 1);
|
1087
|
+
if(CLASS_OF(self) == rb_cPGconn) {
|
1088
|
+
size = PQescapeStringConn(get_pgconn(self), escaped,
|
1089
|
+
RSTRING_PTR(string), RSTRING_LEN(string), &error);
|
1090
|
+
if(error) {
|
1091
|
+
rb_raise(rb_ePGError, PQerrorMessage(get_pgconn(self)));
|
1092
|
+
}
|
1093
|
+
} else {
|
1094
|
+
size = PQescapeString(escaped, RSTRING_PTR(string),
|
1095
|
+
RSTRING_LEN(string));
|
1096
|
+
}
|
1097
|
+
result = rb_str_new(escaped, size);
|
1098
|
+
OBJ_INFECT(result, string);
|
1099
|
+
return result;
|
1100
|
+
}
|
1101
|
+
|
1102
|
+
/*
|
1103
|
+
* call-seq:
|
1104
|
+
* conn.escape_bytea( string ) -> String
|
1105
|
+
* PGconn.escape_bytea( string ) -> String # DEPRECATED
|
1106
|
+
*
|
1107
|
+
* Connection instance method for versions of 8.1 and higher of libpq
|
1108
|
+
* uses PQescapeByteaConn, which is safer. Avoid calling as a class method,
|
1109
|
+
* the class method uses the deprecated PQescapeBytea() API function.
|
1110
|
+
*
|
1111
|
+
* Use the instance method version of this function, it is safer than the
|
1112
|
+
* class method.
|
1113
|
+
*
|
1114
|
+
* Escapes binary data for use within an SQL command with the type +bytea+.
|
1115
|
+
*
|
1116
|
+
* Certain byte values must be escaped (but all byte values may be escaped)
|
1117
|
+
* when used as part of a +bytea+ literal in an SQL statement. In general, to
|
1118
|
+
* escape a byte, it is converted into the three digit octal number equal to
|
1119
|
+
* the octet value, and preceded by two backslashes. The single quote (') and
|
1120
|
+
* backslash (\) characters have special alternative escape sequences.
|
1121
|
+
* #escape_bytea performs this operation, escaping only the minimally required
|
1122
|
+
* bytes.
|
1123
|
+
*
|
1124
|
+
* Consider using exec_params, which avoids the need for passing values inside of
|
1125
|
+
* SQL commands.
|
1126
|
+
*/
|
1127
|
+
static VALUE
|
1128
|
+
pgconn_s_escape_bytea(self, str)
|
1129
|
+
VALUE self;
|
1130
|
+
VALUE str;
|
1131
|
+
{
|
1132
|
+
unsigned char *from, *to;
|
1133
|
+
size_t from_len, to_len;
|
1134
|
+
VALUE ret;
|
1135
|
+
|
1136
|
+
Check_Type(str, T_STRING);
|
1137
|
+
from = (unsigned char*)RSTRING_PTR(str);
|
1138
|
+
from_len = RSTRING_LEN(str);
|
1139
|
+
|
1140
|
+
if(CLASS_OF(self) == rb_cPGconn) {
|
1141
|
+
to = PQescapeByteaConn(get_pgconn(self), from, from_len, &to_len);
|
1142
|
+
} else {
|
1143
|
+
to = PQescapeBytea( from, from_len, &to_len);
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
ret = rb_str_new((char*)to, to_len - 1);
|
1147
|
+
OBJ_INFECT(ret, str);
|
1148
|
+
PQfreemem(to);
|
1149
|
+
return ret;
|
1150
|
+
}
|
1151
|
+
|
1152
|
+
|
1153
|
+
/*
|
1154
|
+
* call-seq:
|
1155
|
+
* PGconn.unescape_bytea( string )
|
1156
|
+
*
|
1157
|
+
* Converts an escaped string representation of binary data into binary data --- the
|
1158
|
+
* reverse of #escape_bytea. This is needed when retrieving +bytea+ data in text format,
|
1159
|
+
* but not when retrieving it in binary format.
|
1160
|
+
*
|
1161
|
+
*/
|
1162
|
+
static VALUE
|
1163
|
+
pgconn_s_unescape_bytea(self, str)
|
1164
|
+
VALUE self, str;
|
1165
|
+
{
|
1166
|
+
unsigned char *from, *to;
|
1167
|
+
size_t to_len;
|
1168
|
+
VALUE ret;
|
1169
|
+
|
1170
|
+
Check_Type(str, T_STRING);
|
1171
|
+
from = (unsigned char*)StringValuePtr(str);
|
1172
|
+
|
1173
|
+
to = PQunescapeBytea(from, &to_len);
|
1174
|
+
|
1175
|
+
ret = rb_str_new((char*)to, to_len);
|
1176
|
+
OBJ_INFECT(ret, str);
|
1177
|
+
PQfreemem(to);
|
1178
|
+
return ret;
|
1179
|
+
}
|
1180
|
+
|
1181
|
+
/*
|
1182
|
+
* call-seq:
|
1183
|
+
* conn.send_query(sql [, params, result_format ] ) -> nil
|
1184
|
+
*
|
1185
|
+
* Sends SQL query request specified by _sql_ to PostgreSQL for
|
1186
|
+
* asynchronous processing, and immediately returns.
|
1187
|
+
* On failure, it raises a PGError exception.
|
1188
|
+
*
|
1189
|
+
* +params+ is an optional array of the bind parameters for the SQL query.
|
1190
|
+
* Each element of the +params+ array may be either:
|
1191
|
+
* a hash of the form:
|
1192
|
+
* {:value => String (value of bind parameter)
|
1193
|
+
* :type => Fixnum (oid of type of bind parameter)
|
1194
|
+
* :format => Fixnum (0 for text, 1 for binary)
|
1195
|
+
* }
|
1196
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
1197
|
+
* { :value => <string value>, :type => 0, :format => 0 }
|
1198
|
+
*
|
1199
|
+
* PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
|
1200
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
1201
|
+
* to $1, the 1st element is bound to $2, etc.
|
1202
|
+
*
|
1203
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
1204
|
+
* Instead of specifying type oids, it's recommended to simply add
|
1205
|
+
* explicit casts in the query to ensure that the right type is used.
|
1206
|
+
*
|
1207
|
+
* For example: "SELECT $1::int"
|
1208
|
+
*
|
1209
|
+
* The optional +result_format+ should be 0 for text results, 1
|
1210
|
+
* for binary.
|
1211
|
+
*/
|
1212
|
+
static VALUE
|
1213
|
+
pgconn_send_query(argc, argv, self)
|
1214
|
+
int argc;
|
1215
|
+
VALUE *argv;
|
1216
|
+
VALUE self;
|
1217
|
+
{
|
1218
|
+
PGconn *conn = get_pgconn(self);
|
1219
|
+
int result;
|
1220
|
+
VALUE command, params, in_res_fmt;
|
1221
|
+
VALUE param, param_type, param_value, param_format;
|
1222
|
+
VALUE param_value_tmp;
|
1223
|
+
VALUE sym_type, sym_value, sym_format;
|
1224
|
+
VALUE error;
|
1225
|
+
int i=0;
|
1226
|
+
int nParams;
|
1227
|
+
Oid *paramTypes;
|
1228
|
+
char ** paramValues;
|
1229
|
+
int *paramLengths;
|
1230
|
+
int *paramFormats;
|
1231
|
+
int resultFormat;
|
1232
|
+
|
1233
|
+
rb_scan_args(argc, argv, "12", &command, ¶ms, &in_res_fmt);
|
1234
|
+
Check_Type(command, T_STRING);
|
1235
|
+
|
1236
|
+
/* If called with no parameters, use PQsendQuery */
|
1237
|
+
if(NIL_P(params)) {
|
1238
|
+
if(PQsendQuery(conn,StringValuePtr(command)) == 0) {
|
1239
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1240
|
+
rb_iv_set(error, "@connection", self);
|
1241
|
+
rb_exc_raise(error);
|
1242
|
+
}
|
1243
|
+
return Qnil;
|
1244
|
+
}
|
1245
|
+
|
1246
|
+
/* If called with parameters, and optionally result_format,
|
1247
|
+
* use PQsendQueryParams
|
1248
|
+
*/
|
1249
|
+
Check_Type(params, T_ARRAY);
|
1250
|
+
|
1251
|
+
if(NIL_P(in_res_fmt)) {
|
1252
|
+
resultFormat = 0;
|
1253
|
+
}
|
1254
|
+
else {
|
1255
|
+
resultFormat = NUM2INT(in_res_fmt);
|
1256
|
+
}
|
1257
|
+
|
1258
|
+
sym_type = ID2SYM(rb_intern("type"));
|
1259
|
+
sym_value = ID2SYM(rb_intern("value"));
|
1260
|
+
sym_format = ID2SYM(rb_intern("format"));
|
1261
|
+
nParams = RARRAY(params)->len;
|
1262
|
+
paramTypes = ALLOC_N(Oid, nParams);
|
1263
|
+
paramValues = ALLOC_N(char *, nParams);
|
1264
|
+
paramLengths = ALLOC_N(int, nParams);
|
1265
|
+
paramFormats = ALLOC_N(int, nParams);
|
1266
|
+
for(i = 0; i < nParams; i++) {
|
1267
|
+
param = rb_ary_entry(params, i);
|
1268
|
+
if (TYPE(param) == T_HASH) {
|
1269
|
+
param_type = rb_hash_aref(param, sym_type);
|
1270
|
+
param_value_tmp = rb_hash_aref(param, sym_value);
|
1271
|
+
if(TYPE(param_value_tmp) == T_STRING)
|
1272
|
+
param_value = param_value_tmp;
|
1273
|
+
else
|
1274
|
+
param_value = rb_funcall(param_value_tmp, rb_intern("to_s"), 0);
|
1275
|
+
param_format = rb_hash_aref(param, sym_format);
|
1276
|
+
}
|
1277
|
+
else {
|
1278
|
+
param_type = INT2NUM(0);
|
1279
|
+
if(TYPE(param) == T_STRING)
|
1280
|
+
param_value = param;
|
1281
|
+
else
|
1282
|
+
param_value = rb_funcall(param, rb_intern("to_s"), 0);
|
1283
|
+
param_format = INT2NUM(0);
|
1284
|
+
}
|
1285
|
+
Check_Type(param_value, T_STRING);
|
1286
|
+
paramTypes[i] = NUM2INT(param_type);
|
1287
|
+
paramValues[i] = RSTRING_PTR(param_value);
|
1288
|
+
paramLengths[i] = RSTRING_LEN(param_value) + 1;
|
1289
|
+
paramFormats[i] = NUM2INT(param_format);
|
1290
|
+
}
|
1291
|
+
|
1292
|
+
result = PQsendQueryParams(conn, StringValuePtr(command), nParams, paramTypes,
|
1293
|
+
(const char * const *)paramValues, paramLengths, paramFormats, resultFormat);
|
1294
|
+
|
1295
|
+
free(paramTypes);
|
1296
|
+
free(paramValues);
|
1297
|
+
free(paramLengths);
|
1298
|
+
free(paramFormats);
|
1299
|
+
|
1300
|
+
if(result == 0) {
|
1301
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1302
|
+
rb_iv_set(error, "@connection", self);
|
1303
|
+
rb_exc_raise(error);
|
1304
|
+
}
|
1305
|
+
return Qnil;
|
1306
|
+
}
|
1307
|
+
|
1308
|
+
/*
|
1309
|
+
* call-seq:
|
1310
|
+
* conn.send_prepare( stmt_name, sql [, param_types ] ) -> nil
|
1311
|
+
*
|
1312
|
+
* Prepares statement _sql_ with name _name_ to be executed later.
|
1313
|
+
* Sends prepare command asynchronously, and returns immediately.
|
1314
|
+
* On failure, it raises a PGError exception.
|
1315
|
+
*
|
1316
|
+
* +param_types+ is an optional parameter to specify the Oids of the
|
1317
|
+
* types of the parameters.
|
1318
|
+
*
|
1319
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
1320
|
+
* Instead of specifying type oids, it's recommended to simply add
|
1321
|
+
* explicit casts in the query to ensure that the right type is used.
|
1322
|
+
*
|
1323
|
+
* For example: "SELECT $1::int"
|
1324
|
+
*
|
1325
|
+
* PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
|
1326
|
+
* inside the SQL query.
|
1327
|
+
*/
|
1328
|
+
static VALUE
|
1329
|
+
pgconn_send_prepare(argc, argv, self)
|
1330
|
+
int argc;
|
1331
|
+
VALUE *argv;
|
1332
|
+
VALUE self;
|
1333
|
+
{
|
1334
|
+
PGconn *conn = get_pgconn(self);
|
1335
|
+
int result;
|
1336
|
+
VALUE name, command, in_paramtypes;
|
1337
|
+
VALUE param;
|
1338
|
+
VALUE error;
|
1339
|
+
int i = 0;
|
1340
|
+
int nParams = 0;
|
1341
|
+
Oid *paramTypes = NULL;
|
1342
|
+
|
1343
|
+
rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
|
1344
|
+
Check_Type(name, T_STRING);
|
1345
|
+
Check_Type(command, T_STRING);
|
1346
|
+
|
1347
|
+
if(! NIL_P(in_paramtypes)) {
|
1348
|
+
Check_Type(in_paramtypes, T_ARRAY);
|
1349
|
+
nParams = RARRAY(in_paramtypes)->len;
|
1350
|
+
paramTypes = ALLOC_N(Oid, nParams);
|
1351
|
+
for(i = 0; i < nParams; i++) {
|
1352
|
+
param = rb_ary_entry(in_paramtypes, i);
|
1353
|
+
Check_Type(param, T_FIXNUM);
|
1354
|
+
paramTypes[i] = NUM2INT(param);
|
1355
|
+
}
|
1356
|
+
}
|
1357
|
+
result = PQsendPrepare(conn, StringValuePtr(name), StringValuePtr(command),
|
1358
|
+
nParams, paramTypes);
|
1359
|
+
|
1360
|
+
free(paramTypes);
|
1361
|
+
|
1362
|
+
if(result == 0) {
|
1363
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1364
|
+
rb_iv_set(error, "@connection", self);
|
1365
|
+
rb_exc_raise(error);
|
1366
|
+
}
|
1367
|
+
return Qnil;
|
1368
|
+
}
|
1369
|
+
|
1370
|
+
/*
|
1371
|
+
* call-seq:
|
1372
|
+
* conn.send_query_prepared( statement_name [, params, result_format ] )
|
1373
|
+
* -> nil
|
1374
|
+
*
|
1375
|
+
* Execute prepared named statement specified by _statement_name_
|
1376
|
+
* asynchronously, and returns immediately.
|
1377
|
+
* On failure, it raises a PGError exception.
|
1378
|
+
*
|
1379
|
+
* +params+ is an array of the optional bind parameters for the
|
1380
|
+
* SQL query. Each element of the +params+ array may be either:
|
1381
|
+
* a hash of the form:
|
1382
|
+
* {:value => String (value of bind parameter)
|
1383
|
+
* :format => Fixnum (0 for text, 1 for binary)
|
1384
|
+
* }
|
1385
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
1386
|
+
* { :value => <string value>, :format => 0 }
|
1387
|
+
*
|
1388
|
+
* PostgreSQL bind parameters are represented as $1, $1, $2, etc.,
|
1389
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
1390
|
+
* to $1, the 1st element is bound to $2, etc.
|
1391
|
+
*
|
1392
|
+
* The optional +result_format+ should be 0 for text results, 1
|
1393
|
+
* for binary.
|
1394
|
+
*/
|
1395
|
+
static VALUE
|
1396
|
+
pgconn_send_query_prepared(argc, argv, self)
|
1397
|
+
int argc;
|
1398
|
+
VALUE *argv;
|
1399
|
+
VALUE self;
|
1400
|
+
{
|
1401
|
+
PGconn *conn = get_pgconn(self);
|
1402
|
+
int result;
|
1403
|
+
VALUE name, params, in_res_fmt;
|
1404
|
+
VALUE param, param_value, param_format;
|
1405
|
+
VALUE param_value_tmp;
|
1406
|
+
VALUE sym_value, sym_format;
|
1407
|
+
VALUE error;
|
1408
|
+
int i = 0;
|
1409
|
+
int nParams;
|
1410
|
+
char ** paramValues;
|
1411
|
+
int *paramLengths;
|
1412
|
+
int *paramFormats;
|
1413
|
+
int resultFormat;
|
1414
|
+
|
1415
|
+
rb_scan_args(argc, argv, "12", &name, ¶ms, &in_res_fmt);
|
1416
|
+
Check_Type(name, T_STRING);
|
1417
|
+
|
1418
|
+
if(NIL_P(params)) {
|
1419
|
+
params = rb_ary_new2(0);
|
1420
|
+
resultFormat = 0;
|
1421
|
+
}
|
1422
|
+
else {
|
1423
|
+
Check_Type(params, T_ARRAY);
|
1424
|
+
}
|
1425
|
+
|
1426
|
+
if(NIL_P(in_res_fmt)) {
|
1427
|
+
resultFormat = 0;
|
1428
|
+
}
|
1429
|
+
else {
|
1430
|
+
resultFormat = NUM2INT(in_res_fmt);
|
1431
|
+
}
|
1432
|
+
|
1433
|
+
sym_value = ID2SYM(rb_intern("value"));
|
1434
|
+
sym_format = ID2SYM(rb_intern("format"));
|
1435
|
+
nParams = RARRAY(params)->len;
|
1436
|
+
paramValues = ALLOC_N(char *, nParams);
|
1437
|
+
paramLengths = ALLOC_N(int, nParams);
|
1438
|
+
paramFormats = ALLOC_N(int, nParams);
|
1439
|
+
for(i = 0; i < nParams; i++) {
|
1440
|
+
param = rb_ary_entry(params, i);
|
1441
|
+
if (TYPE(param) == T_HASH) {
|
1442
|
+
param_value_tmp = rb_hash_aref(param, sym_value);
|
1443
|
+
if(TYPE(param_value_tmp) == T_STRING)
|
1444
|
+
param_value = param_value_tmp;
|
1445
|
+
else
|
1446
|
+
param_value = rb_funcall(param_value_tmp, rb_intern("to_s"), 0);
|
1447
|
+
param_format = rb_hash_aref(param, sym_format);
|
1448
|
+
}
|
1449
|
+
else {
|
1450
|
+
if(TYPE(param) == T_STRING)
|
1451
|
+
param_value = param;
|
1452
|
+
else
|
1453
|
+
param_value = rb_funcall(param, rb_intern("to_s"), 0);
|
1454
|
+
param_format = INT2NUM(0);
|
1455
|
+
}
|
1456
|
+
Check_Type(param_value, T_STRING);
|
1457
|
+
paramValues[i] = RSTRING_PTR(param_value);
|
1458
|
+
paramLengths[i] = RSTRING_LEN(param_value) + 1;
|
1459
|
+
paramFormats[i] = NUM2INT(param_format);
|
1460
|
+
}
|
1461
|
+
|
1462
|
+
result = PQsendQueryPrepared(conn, StringValuePtr(name), nParams,
|
1463
|
+
(const char * const *)paramValues, paramLengths, paramFormats,
|
1464
|
+
resultFormat);
|
1465
|
+
|
1466
|
+
free(paramValues);
|
1467
|
+
free(paramLengths);
|
1468
|
+
free(paramFormats);
|
1469
|
+
|
1470
|
+
if(result == 0) {
|
1471
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1472
|
+
rb_iv_set(error, "@connection", self);
|
1473
|
+
rb_exc_raise(error);
|
1474
|
+
}
|
1475
|
+
return Qnil;
|
1476
|
+
}
|
1477
|
+
|
1478
|
+
/*
|
1479
|
+
* call-seq:
|
1480
|
+
* conn.send_describe_prepared( statement_name ) -> nil
|
1481
|
+
*
|
1482
|
+
* Asynchronously send _command_ to the server. Does not block.
|
1483
|
+
* Use in combination with +conn.get_result+.
|
1484
|
+
*/
|
1485
|
+
static VALUE
|
1486
|
+
pgconn_send_describe_prepared(self, stmt_name)
|
1487
|
+
VALUE self, stmt_name;
|
1488
|
+
{
|
1489
|
+
VALUE error;
|
1490
|
+
PGconn *conn = get_pgconn(self);
|
1491
|
+
/* returns 0 on failure */
|
1492
|
+
if(PQsendDescribePrepared(conn,StringValuePtr(stmt_name)) == 0) {
|
1493
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1494
|
+
rb_iv_set(error, "@connection", self);
|
1495
|
+
rb_exc_raise(error);
|
1496
|
+
}
|
1497
|
+
return Qnil;
|
1498
|
+
}
|
1499
|
+
|
1500
|
+
|
1501
|
+
/*
|
1502
|
+
* call-seq:
|
1503
|
+
* conn.send_describe_portal( portal_name ) -> nil
|
1504
|
+
*
|
1505
|
+
* Asynchronously send _command_ to the server. Does not block.
|
1506
|
+
* Use in combination with +conn.get_result+.
|
1507
|
+
*/
|
1508
|
+
static VALUE
|
1509
|
+
pgconn_send_describe_portal(self, portal)
|
1510
|
+
VALUE self, portal;
|
1511
|
+
{
|
1512
|
+
VALUE error;
|
1513
|
+
PGconn *conn = get_pgconn(self);
|
1514
|
+
/* returns 0 on failure */
|
1515
|
+
if(PQsendDescribePortal(conn,StringValuePtr(portal)) == 0) {
|
1516
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1517
|
+
rb_iv_set(error, "@connection", self);
|
1518
|
+
rb_exc_raise(error);
|
1519
|
+
}
|
1520
|
+
return Qnil;
|
1521
|
+
}
|
1522
|
+
|
1523
|
+
|
1524
|
+
/*
|
1525
|
+
* call-seq:
|
1526
|
+
* conn.get_result() -> PGresult
|
1527
|
+
*
|
1528
|
+
* Asynchronously send _command_ to the server. Does not block.
|
1529
|
+
* Use in combination with +conn.get_result+.
|
1530
|
+
*/
|
1531
|
+
static VALUE
|
1532
|
+
pgconn_get_result(self)
|
1533
|
+
VALUE self;
|
1534
|
+
{
|
1535
|
+
PGresult *result;
|
1536
|
+
VALUE rb_pgresult;
|
1537
|
+
|
1538
|
+
result = PQgetResult(get_pgconn(self));
|
1539
|
+
if(result == NULL)
|
1540
|
+
return Qnil;
|
1541
|
+
|
1542
|
+
rb_pgresult = new_pgresult(result);
|
1543
|
+
pgresult_check(self, rb_pgresult);
|
1544
|
+
if (rb_block_given_p()) {
|
1545
|
+
return rb_ensure(yield_pgresult, rb_pgresult,
|
1546
|
+
pgresult_clear, rb_pgresult);
|
1547
|
+
}
|
1548
|
+
return rb_pgresult;
|
1549
|
+
}
|
1550
|
+
|
1551
|
+
/*
|
1552
|
+
* call-seq:
|
1553
|
+
* conn.consume_input()
|
1554
|
+
*
|
1555
|
+
* If input is available from the server, consume it.
|
1556
|
+
* After calling +consume_input+, you can check +is_busy+
|
1557
|
+
* or *notifies* to see if the state has changed.
|
1558
|
+
*/
|
1559
|
+
static VALUE
|
1560
|
+
pgconn_consume_input(self)
|
1561
|
+
VALUE self;
|
1562
|
+
{
|
1563
|
+
VALUE error;
|
1564
|
+
PGconn *conn = get_pgconn(self);
|
1565
|
+
/* returns 0 on error */
|
1566
|
+
if(PQconsumeInput(conn) == 0) {
|
1567
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1568
|
+
rb_iv_set(error, "@connection", self);
|
1569
|
+
rb_exc_raise(error);
|
1570
|
+
}
|
1571
|
+
return Qnil;
|
1572
|
+
}
|
1573
|
+
|
1574
|
+
/*
|
1575
|
+
* call-seq:
|
1576
|
+
* conn.is_busy() -> Boolean
|
1577
|
+
*
|
1578
|
+
* Returns +true+ if a command is busy, that is, if
|
1579
|
+
* PQgetResult would block. Otherwise returns +false+.
|
1580
|
+
*/
|
1581
|
+
static VALUE
|
1582
|
+
pgconn_is_busy(self)
|
1583
|
+
VALUE self;
|
1584
|
+
{
|
1585
|
+
return PQisBusy(get_pgconn(self)) ? Qtrue : Qfalse;
|
1586
|
+
}
|
1587
|
+
|
1588
|
+
/*
|
1589
|
+
* call-seq:
|
1590
|
+
* conn.setnonblocking() -> Boolean
|
1591
|
+
*
|
1592
|
+
* Returns +true+ if a command is busy, that is, if
|
1593
|
+
* PQgetResult would block. Otherwise returns +false+.
|
1594
|
+
*/
|
1595
|
+
static VALUE
|
1596
|
+
pgconn_setnonblocking(self, state)
|
1597
|
+
VALUE self, state;
|
1598
|
+
{
|
1599
|
+
int arg;
|
1600
|
+
VALUE error;
|
1601
|
+
PGconn *conn = get_pgconn(self);
|
1602
|
+
if(state == Qtrue)
|
1603
|
+
arg = 1;
|
1604
|
+
else if (state == Qfalse)
|
1605
|
+
arg = 0;
|
1606
|
+
else
|
1607
|
+
rb_raise(rb_eArgError, "Boolean value expected");
|
1608
|
+
|
1609
|
+
if(PQsetnonblocking(conn, arg) == -1) {
|
1610
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1611
|
+
rb_iv_set(error, "@connection", self);
|
1612
|
+
rb_exc_raise(error);
|
1613
|
+
}
|
1614
|
+
return Qnil;
|
1615
|
+
}
|
1616
|
+
|
1617
|
+
|
1618
|
+
/*
|
1619
|
+
* call-seq:
|
1620
|
+
* conn.isnonblocking() -> Boolean
|
1621
|
+
*
|
1622
|
+
* Returns +true+ if a command is busy, that is, if
|
1623
|
+
* PQgetResult would block. Otherwise returns +false+.
|
1624
|
+
*/
|
1625
|
+
static VALUE
|
1626
|
+
pgconn_isnonblocking(self)
|
1627
|
+
VALUE self;
|
1628
|
+
{
|
1629
|
+
return PQisnonblocking(get_pgconn(self)) ? Qtrue : Qfalse;
|
1630
|
+
}
|
1631
|
+
|
1632
|
+
/*
|
1633
|
+
* call-seq:
|
1634
|
+
* conn.flush() -> Boolean
|
1635
|
+
*
|
1636
|
+
* Attempts to flush any queued output data to the server.
|
1637
|
+
* Returns +true+ if data is successfully flushed, +false+
|
1638
|
+
* if not (can only return +false+ if connection is
|
1639
|
+
* nonblocking.
|
1640
|
+
* Raises PGError exception if some other failure occurred.
|
1641
|
+
*/
|
1642
|
+
static VALUE
|
1643
|
+
pgconn_flush(self)
|
1644
|
+
VALUE self;
|
1645
|
+
{
|
1646
|
+
PGconn *conn = get_pgconn(self);
|
1647
|
+
int ret;
|
1648
|
+
VALUE error;
|
1649
|
+
ret = PQflush(conn);
|
1650
|
+
if(ret == -1) {
|
1651
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1652
|
+
rb_iv_set(error, "@connection", self);
|
1653
|
+
rb_exc_raise(error);
|
1654
|
+
}
|
1655
|
+
return (ret) ? Qfalse : Qtrue;
|
1656
|
+
}
|
1657
|
+
|
1658
|
+
//TODO get_cancel
|
1659
|
+
|
1660
|
+
//TODO free_cancel
|
1661
|
+
|
1662
|
+
//TODO cancel
|
1663
|
+
|
1664
|
+
/*
|
1665
|
+
* call-seq:
|
1666
|
+
* conn.notifies()
|
1667
|
+
*
|
1668
|
+
* Returns an array of the unprocessed notifiers.
|
1669
|
+
* If there is no unprocessed notifier, it returns +nil+.
|
1670
|
+
*/
|
1671
|
+
static VALUE
|
1672
|
+
pgconn_notifies(self)
|
1673
|
+
VALUE self;
|
1674
|
+
{
|
1675
|
+
PGconn* conn = get_pgconn(self);
|
1676
|
+
PGnotify *notify;
|
1677
|
+
VALUE hash;
|
1678
|
+
VALUE sym_relname, sym_be_pid, sym_extra;
|
1679
|
+
VALUE relname, be_pid, extra;
|
1680
|
+
|
1681
|
+
sym_relname = ID2SYM(rb_intern("relname"));
|
1682
|
+
sym_be_pid = ID2SYM(rb_intern("be_pid"));
|
1683
|
+
sym_extra = ID2SYM(rb_intern("extra"));
|
1684
|
+
|
1685
|
+
notify = PQnotifies(conn);
|
1686
|
+
if (notify == NULL) {
|
1687
|
+
return Qnil;
|
1688
|
+
}
|
1689
|
+
|
1690
|
+
hash = rb_hash_new();
|
1691
|
+
relname = rb_tainted_str_new2(notify->relname);
|
1692
|
+
be_pid = INT2NUM(notify->be_pid);
|
1693
|
+
extra = rb_tainted_str_new2(notify->extra);
|
1694
|
+
|
1695
|
+
rb_hash_aset(hash, sym_relname, relname);
|
1696
|
+
rb_hash_aset(hash, sym_be_pid, be_pid);
|
1697
|
+
rb_hash_aset(hash, sym_extra, extra);
|
1698
|
+
|
1699
|
+
PQfreemem(notify);
|
1700
|
+
return hash;
|
1701
|
+
}
|
1702
|
+
|
1703
|
+
|
1704
|
+
/*
|
1705
|
+
* call-seq:
|
1706
|
+
* conn.put_copy_data( buffer ) -> Boolean
|
1707
|
+
*
|
1708
|
+
* Transmits _buffer_ as copy data to the server.
|
1709
|
+
* Returns true if the data was sent, false if it was
|
1710
|
+
* not sent (false is only possible if the connection
|
1711
|
+
* is in nonblocking mode, and this command would block).
|
1712
|
+
*
|
1713
|
+
* Raises an exception if an error occurs.
|
1714
|
+
*/
|
1715
|
+
static VALUE
|
1716
|
+
pgconn_put_copy_data(self, buffer)
|
1717
|
+
VALUE self, buffer;
|
1718
|
+
{
|
1719
|
+
int ret;
|
1720
|
+
VALUE error;
|
1721
|
+
PGconn *conn = get_pgconn(self);
|
1722
|
+
Check_Type(buffer, T_STRING);
|
1723
|
+
|
1724
|
+
ret = PQputCopyData(conn, RSTRING_PTR(buffer),
|
1725
|
+
RSTRING_LEN(buffer));
|
1726
|
+
if(ret == -1) {
|
1727
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1728
|
+
rb_iv_set(error, "@connection", self);
|
1729
|
+
rb_exc_raise(error);
|
1730
|
+
}
|
1731
|
+
return (ret) ? Qtrue : Qfalse;
|
1732
|
+
}
|
1733
|
+
|
1734
|
+
/*
|
1735
|
+
* call-seq:
|
1736
|
+
* conn.put_copy_end( [ error_message ] ) -> Boolean
|
1737
|
+
*
|
1738
|
+
* Sends end-of-data indication to the server.
|
1739
|
+
*
|
1740
|
+
* _error_message_ is an optional parameter, and if set,
|
1741
|
+
* forces the COPY command to fail with the string
|
1742
|
+
* _error_message_.
|
1743
|
+
*
|
1744
|
+
* Returns true if the end-of-data was sent, false if it was
|
1745
|
+
* not sent (false is only possible if the connection
|
1746
|
+
* is in nonblocking mode, and this command would block).
|
1747
|
+
*/
|
1748
|
+
static VALUE
|
1749
|
+
pgconn_put_copy_end(argc, argv, self)
|
1750
|
+
int argc;
|
1751
|
+
VALUE *argv;
|
1752
|
+
VALUE self;
|
1753
|
+
{
|
1754
|
+
VALUE str;
|
1755
|
+
VALUE error;
|
1756
|
+
int ret;
|
1757
|
+
char *error_message = NULL;
|
1758
|
+
PGconn *conn = get_pgconn(self);
|
1759
|
+
|
1760
|
+
if (rb_scan_args(argc, argv, "01", &str) == 0)
|
1761
|
+
error_message = NULL;
|
1762
|
+
else
|
1763
|
+
error_message = StringValuePtr(str);
|
1764
|
+
|
1765
|
+
ret = PQputCopyEnd(conn, error_message);
|
1766
|
+
if(ret == -1) {
|
1767
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1768
|
+
rb_iv_set(error, "@connection", self);
|
1769
|
+
rb_exc_raise(error);
|
1770
|
+
}
|
1771
|
+
return (ret) ? Qtrue : Qfalse;
|
1772
|
+
}
|
1773
|
+
|
1774
|
+
/*
|
1775
|
+
* call-seq:
|
1776
|
+
* conn.get_copy_data( [ async = false ] ) -> String
|
1777
|
+
*
|
1778
|
+
* Return a string containing one row of data, +nil+
|
1779
|
+
* if the copy is done, or +false+ if the call would
|
1780
|
+
* block (only possible if _async_ is true).
|
1781
|
+
*
|
1782
|
+
*/
|
1783
|
+
static VALUE
|
1784
|
+
pgconn_get_copy_data( argc, argv, self )
|
1785
|
+
int argc;
|
1786
|
+
VALUE *argv;
|
1787
|
+
VALUE self;
|
1788
|
+
{
|
1789
|
+
VALUE async_in;
|
1790
|
+
VALUE error;
|
1791
|
+
int ret;
|
1792
|
+
int async;
|
1793
|
+
char *buffer;
|
1794
|
+
PGconn *conn = get_pgconn(self);
|
1795
|
+
|
1796
|
+
if (rb_scan_args(argc, argv, "01", &async_in) == 0)
|
1797
|
+
async = 0;
|
1798
|
+
else
|
1799
|
+
async = (async_in == Qfalse || async_in == Qnil) ? 0 : 1;
|
1800
|
+
|
1801
|
+
ret = PQgetCopyData(conn, &buffer, async);
|
1802
|
+
if(ret == -2) { // error
|
1803
|
+
error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
|
1804
|
+
rb_iv_set(error, "@connection", self);
|
1805
|
+
rb_exc_raise(error);
|
1806
|
+
}
|
1807
|
+
if(ret == -1) { // No data left
|
1808
|
+
return Qnil;
|
1809
|
+
}
|
1810
|
+
if(ret == 0) { // would block
|
1811
|
+
return Qfalse;
|
1812
|
+
}
|
1813
|
+
return rb_str_new(buffer, ret);
|
1814
|
+
}
|
1815
|
+
|
1816
|
+
/*
|
1817
|
+
* call-seq:
|
1818
|
+
* conn.set_error_verbosity( verbosity ) -> Fixnum
|
1819
|
+
*
|
1820
|
+
* Sets connection's verbosity to _verbosity_ and returns
|
1821
|
+
* the previous setting. Available settings are:
|
1822
|
+
* * PQERRORS_TERSE
|
1823
|
+
* * PQERRORS_DEFAULT
|
1824
|
+
* * PQERRORS_VERBOSE
|
1825
|
+
*/
|
1826
|
+
static VALUE
|
1827
|
+
pgconn_set_error_verbosity(VALUE self, VALUE in_verbosity)
|
1828
|
+
{
|
1829
|
+
PGconn *conn = get_pgconn(self);
|
1830
|
+
PGVerbosity verbosity = NUM2INT(in_verbosity);
|
1831
|
+
return INT2FIX(PQsetErrorVerbosity(conn, verbosity));
|
1832
|
+
}
|
1833
|
+
|
1834
|
+
/*TODO
|
1835
|
+
* call-seq:
|
1836
|
+
* conn.trace( port )
|
1837
|
+
*
|
1838
|
+
* Enables tracing message passing between backend.
|
1839
|
+
* The trace message will be written to the _port_ object,
|
1840
|
+
* which is an instance of the class +File+.
|
1841
|
+
*/
|
1842
|
+
static VALUE
|
1843
|
+
pgconn_trace(self, port)
|
1844
|
+
VALUE self, port;
|
1845
|
+
{
|
1846
|
+
//OpenFile* fp;
|
1847
|
+
|
1848
|
+
Check_Type(port, T_FILE);
|
1849
|
+
//GetOpenFile(port, fp);
|
1850
|
+
|
1851
|
+
//PQtrace(get_pgconn(self), fp->f2?fp->f2:fp->f);
|
1852
|
+
|
1853
|
+
return self;
|
1854
|
+
}
|
1855
|
+
|
1856
|
+
/*
|
1857
|
+
* call-seq:
|
1858
|
+
* conn.untrace()
|
1859
|
+
*
|
1860
|
+
* Disables the message tracing.
|
1861
|
+
*/
|
1862
|
+
static VALUE
|
1863
|
+
pgconn_untrace(self)
|
1864
|
+
VALUE self;
|
1865
|
+
{
|
1866
|
+
PQuntrace(get_pgconn(self));
|
1867
|
+
return self;
|
1868
|
+
}
|
1869
|
+
|
1870
|
+
//TODO set_notice_receiver
|
1871
|
+
|
1872
|
+
//TODO set_notice_processor
|
1873
|
+
|
1874
|
+
/*
|
1875
|
+
* call-seq:
|
1876
|
+
* conn.get_client_encoding() -> String
|
1877
|
+
*
|
1878
|
+
* Returns the client encoding as a String.
|
1879
|
+
*/
|
1880
|
+
static VALUE
|
1881
|
+
pgconn_get_client_encoding(self)
|
1882
|
+
VALUE self;
|
1883
|
+
{
|
1884
|
+
char *encoding = (char *)pg_encoding_to_char(PQclientEncoding(get_pgconn(self)));
|
1885
|
+
return rb_tainted_str_new2(encoding);
|
1886
|
+
}
|
1887
|
+
|
1888
|
+
/*
|
1889
|
+
* call-seq:
|
1890
|
+
* conn.set_client_encoding( encoding )
|
1891
|
+
*
|
1892
|
+
* Sets the client encoding to the _encoding_ String.
|
1893
|
+
*/
|
1894
|
+
static VALUE
|
1895
|
+
pgconn_set_client_encoding(self, str)
|
1896
|
+
VALUE self, str;
|
1897
|
+
{
|
1898
|
+
Check_Type(str, T_STRING);
|
1899
|
+
if ((PQsetClientEncoding(get_pgconn(self), StringValuePtr(str))) == -1){
|
1900
|
+
rb_raise(rb_ePGError, "invalid encoding name: %s",StringValuePtr(str));
|
1901
|
+
}
|
1902
|
+
return Qnil;
|
1903
|
+
}
|
1904
|
+
|
1905
|
+
/*TODO */
|
1906
|
+
static void
|
1907
|
+
notice_proxy(self, message)
|
1908
|
+
VALUE self;
|
1909
|
+
const char *message;
|
1910
|
+
{
|
1911
|
+
VALUE block;
|
1912
|
+
if ((block = rb_iv_get(self, "@on_notice")) != Qnil) {
|
1913
|
+
rb_funcall(block, rb_intern("call"), 1, rb_str_new2(message));
|
1914
|
+
}
|
1915
|
+
}
|
1916
|
+
|
1917
|
+
/*TODO
|
1918
|
+
* call-seq:
|
1919
|
+
* conn.on_notice {|message| ... }
|
1920
|
+
*
|
1921
|
+
* Notice and warning messages generated by the server are not returned
|
1922
|
+
* by the query execution functions, since they do not imply failure of
|
1923
|
+
* the query. Instead they are passed to a notice handling function, and
|
1924
|
+
* execution continues normally after the handler returns. The default
|
1925
|
+
* notice handling function prints the message on <tt>stderr</tt>, but the
|
1926
|
+
* application can override this behavior by supplying its own handling
|
1927
|
+
* function.
|
1928
|
+
*/
|
1929
|
+
static VALUE
|
1930
|
+
pgconn_set_notice_processor(self)
|
1931
|
+
VALUE self;
|
1932
|
+
{
|
1933
|
+
VALUE block = rb_block_proc();
|
1934
|
+
PGconn *conn = get_pgconn(self);
|
1935
|
+
if (PQsetNoticeProcessor(conn, NULL, NULL) != notice_proxy) {
|
1936
|
+
PQsetNoticeProcessor(conn, notice_proxy, (void *) self);
|
1937
|
+
}
|
1938
|
+
rb_iv_set(self, "@on_notice", block);
|
1939
|
+
return self;
|
1940
|
+
}
|
1941
|
+
|
1942
|
+
|
1943
|
+
|
1944
|
+
/**************************************************************************
|
1945
|
+
* LARGE OBJECT SUPPORT
|
1946
|
+
**************************************************************************/
|
1947
|
+
|
1948
|
+
/*
|
1949
|
+
* call-seq:
|
1950
|
+
* conn.lo_creat( [mode] ) -> Fixnum
|
1951
|
+
*
|
1952
|
+
* Creates a large object with mode _mode_. Returns a large object Oid.
|
1953
|
+
* On failure, it raises PGError exception.
|
1954
|
+
*/
|
1955
|
+
static VALUE
|
1956
|
+
pgconn_locreat(argc, argv, self)
|
1957
|
+
int argc;
|
1958
|
+
VALUE *argv;
|
1959
|
+
VALUE self;
|
1960
|
+
{
|
1961
|
+
Oid lo_oid;
|
1962
|
+
int mode;
|
1963
|
+
VALUE nmode;
|
1964
|
+
PGconn *conn = get_pgconn(self);
|
1965
|
+
|
1966
|
+
if (rb_scan_args(argc, argv, "01", &nmode) == 0)
|
1967
|
+
mode = INV_READ;
|
1968
|
+
else
|
1969
|
+
mode = NUM2INT(nmode);
|
1970
|
+
|
1971
|
+
lo_oid = lo_creat(conn, mode);
|
1972
|
+
if (lo_oid == 0)
|
1973
|
+
rb_raise(rb_ePGError, "lo_creat failed");
|
1974
|
+
|
1975
|
+
return INT2FIX(lo_oid);
|
1976
|
+
}
|
1977
|
+
|
1978
|
+
/*
|
1979
|
+
* call-seq:
|
1980
|
+
* conn.lo_create( oid ) -> Fixnum
|
1981
|
+
*
|
1982
|
+
* Creates a large object with oid _oid_. Returns the large object Oid.
|
1983
|
+
* On failure, it raises PGError exception.
|
1984
|
+
*/
|
1985
|
+
static VALUE
|
1986
|
+
pgconn_locreate(self, in_lo_oid)
|
1987
|
+
VALUE self, in_lo_oid;
|
1988
|
+
{
|
1989
|
+
Oid ret, lo_oid;
|
1990
|
+
PGconn *conn = get_pgconn(self);
|
1991
|
+
lo_oid = NUM2INT(in_lo_oid);
|
1992
|
+
|
1993
|
+
ret = lo_create(conn, in_lo_oid);
|
1994
|
+
if (ret == InvalidOid)
|
1995
|
+
rb_raise(rb_ePGError, "lo_create failed");
|
1996
|
+
|
1997
|
+
return INT2FIX(ret);
|
1998
|
+
}
|
1999
|
+
|
2000
|
+
/*
|
2001
|
+
* call-seq:
|
2002
|
+
* conn.lo_import(file) -> Fixnum
|
2003
|
+
*
|
2004
|
+
* Import a file to a large object. Returns a large object Oid.
|
2005
|
+
*
|
2006
|
+
* On failure, it raises a PGError exception.
|
2007
|
+
*/
|
2008
|
+
static VALUE
|
2009
|
+
pgconn_loimport(self, filename)
|
2010
|
+
VALUE self, filename;
|
2011
|
+
{
|
2012
|
+
Oid lo_oid;
|
2013
|
+
|
2014
|
+
PGconn *conn = get_pgconn(self);
|
2015
|
+
|
2016
|
+
Check_Type(filename, T_STRING);
|
2017
|
+
|
2018
|
+
lo_oid = lo_import(conn, StringValuePtr(filename));
|
2019
|
+
if (lo_oid == 0) {
|
2020
|
+
rb_raise(rb_ePGError, PQerrorMessage(conn));
|
2021
|
+
}
|
2022
|
+
return INT2FIX(lo_oid);
|
2023
|
+
}
|
2024
|
+
|
2025
|
+
/*
|
2026
|
+
* call-seq:
|
2027
|
+
* conn.lo_export( oid, file ) -> nil
|
2028
|
+
*
|
2029
|
+
* Saves a large object of _oid_ to a _file_.
|
2030
|
+
*/
|
2031
|
+
static VALUE
|
2032
|
+
pgconn_loexport(self, lo_oid,filename)
|
2033
|
+
VALUE self, lo_oid, filename;
|
2034
|
+
{
|
2035
|
+
PGconn *conn = get_pgconn(self);
|
2036
|
+
int oid;
|
2037
|
+
Check_Type(filename, T_STRING);
|
2038
|
+
|
2039
|
+
oid = NUM2INT(lo_oid);
|
2040
|
+
if (oid < 0) {
|
2041
|
+
rb_raise(rb_ePGError, "invalid large object oid %d",oid);
|
2042
|
+
}
|
2043
|
+
|
2044
|
+
if (lo_export(conn, oid, StringValuePtr(filename)) < 0) {
|
2045
|
+
rb_raise(rb_ePGError, PQerrorMessage(conn));
|
2046
|
+
}
|
2047
|
+
return Qnil;
|
2048
|
+
}
|
2049
|
+
|
2050
|
+
/*
|
2051
|
+
* call-seq:
|
2052
|
+
* conn.lo_open( oid, [mode] ) -> Fixnum
|
2053
|
+
*
|
2054
|
+
* Open a large object of _oid_. Returns a large object descriptor
|
2055
|
+
* instance on success. The _mode_ argument specifies the mode for
|
2056
|
+
* the opened large object,which is either +INV_READ+, or +INV_WRITE+.
|
2057
|
+
*
|
2058
|
+
* If _mode_ is omitted, the default is +INV_READ+.
|
2059
|
+
*/
|
2060
|
+
static VALUE
|
2061
|
+
pgconn_loopen(argc, argv, self)
|
2062
|
+
int argc;
|
2063
|
+
VALUE *argv;
|
2064
|
+
VALUE self;
|
2065
|
+
{
|
2066
|
+
Oid lo_oid;
|
2067
|
+
int fd, mode;
|
2068
|
+
VALUE nmode, selfid;
|
2069
|
+
PGconn *conn = get_pgconn(self);
|
2070
|
+
|
2071
|
+
rb_scan_args(argc, argv, "11", &selfid, &nmode);
|
2072
|
+
lo_oid = NUM2INT(selfid);
|
2073
|
+
if(NIL_P(nmode))
|
2074
|
+
mode = INV_READ;
|
2075
|
+
else
|
2076
|
+
mode = NUM2INT(nmode);
|
2077
|
+
|
2078
|
+
if((fd = lo_open(conn, lo_oid, mode)) < 0) {
|
2079
|
+
rb_raise(rb_ePGError, "can't open large object");
|
2080
|
+
}
|
2081
|
+
return INT2FIX(fd);
|
2082
|
+
}
|
2083
|
+
|
2084
|
+
/*
|
2085
|
+
* call-seq:
|
2086
|
+
* conn.lo_write( lo_desc, buffer ) -> Fixnum
|
2087
|
+
*
|
2088
|
+
* Writes the string _buffer_ to the large object _lo_desc_.
|
2089
|
+
* Returns the number of bytes written.
|
2090
|
+
*/
|
2091
|
+
static VALUE
|
2092
|
+
pgconn_lowrite(self, in_lo_desc, buffer)
|
2093
|
+
VALUE self, buffer;
|
2094
|
+
{
|
2095
|
+
int n;
|
2096
|
+
PGconn *conn = get_pgconn(self);
|
2097
|
+
int fd = NUM2INT(in_lo_desc);
|
2098
|
+
|
2099
|
+
Check_Type(buffer, T_STRING);
|
2100
|
+
|
2101
|
+
if( RSTRING_LEN(buffer) < 0) {
|
2102
|
+
rb_raise(rb_ePGError, "write buffer zero string");
|
2103
|
+
}
|
2104
|
+
if((n = lo_write(conn, fd, StringValuePtr(buffer),
|
2105
|
+
RSTRING_LEN(buffer))) < 0) {
|
2106
|
+
rb_raise(rb_ePGError, "lo_write failed");
|
2107
|
+
}
|
2108
|
+
|
2109
|
+
return INT2FIX(n);
|
2110
|
+
}
|
2111
|
+
|
2112
|
+
/*
|
2113
|
+
* call-seq:
|
2114
|
+
* conn.lo_read( lo_desc, len ) -> String
|
2115
|
+
*
|
2116
|
+
* Attempts to read _len_ bytes from large object _lo_desc_,
|
2117
|
+
* returns resulting data.
|
2118
|
+
*/
|
2119
|
+
static VALUE
|
2120
|
+
pgconn_loread(self, in_lo_desc, in_len)
|
2121
|
+
VALUE self, in_lo_desc, in_len;
|
2122
|
+
{
|
2123
|
+
int ret;
|
2124
|
+
PGconn *conn = get_pgconn(self);
|
2125
|
+
int len = NUM2INT(in_len);
|
2126
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
2127
|
+
VALUE str;
|
2128
|
+
char *buffer;
|
2129
|
+
|
2130
|
+
buffer = malloc(len);
|
2131
|
+
if(buffer == NULL)
|
2132
|
+
rb_raise(rb_eNoMemError, "Malloc failed!");
|
2133
|
+
|
2134
|
+
if (len < 0){
|
2135
|
+
rb_raise(rb_ePGError,"nagative length %d given", len);
|
2136
|
+
}
|
2137
|
+
|
2138
|
+
if((ret = lo_read(conn, lo_desc, buffer, len)) < 0)
|
2139
|
+
rb_raise(rb_ePGError, "lo_read failed");
|
2140
|
+
|
2141
|
+
if(ret == 0) {
|
2142
|
+
free(buffer);
|
2143
|
+
return Qnil;
|
2144
|
+
}
|
2145
|
+
|
2146
|
+
str = rb_tainted_str_new(buffer, len);
|
2147
|
+
free(buffer);
|
2148
|
+
|
2149
|
+
return str;
|
2150
|
+
}
|
2151
|
+
|
2152
|
+
|
2153
|
+
/*
|
2154
|
+
* call-seq
|
2155
|
+
* conn.lo_lseek( lo_desc, offset, whence ) -> Fixnum
|
2156
|
+
*
|
2157
|
+
* Move the large object pointer _lo_desc_ to offset _offset_.
|
2158
|
+
* Valid values for _whence_ are +SEEK_SET+, +SEEK_CUR+, and +SEEK_END+.
|
2159
|
+
* (Or 0, 1, or 2.)
|
2160
|
+
*/
|
2161
|
+
static VALUE
|
2162
|
+
pgconn_lolseek(self, in_lo_desc, offset, whence)
|
2163
|
+
VALUE self, in_lo_desc, offset, whence;
|
2164
|
+
{
|
2165
|
+
PGconn *conn = get_pgconn(self);
|
2166
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
2167
|
+
int ret;
|
2168
|
+
|
2169
|
+
if((ret = lo_lseek(conn, lo_desc, NUM2INT(offset), NUM2INT(whence))) < 0) {
|
2170
|
+
rb_raise(rb_ePGError, "lo_lseek failed");
|
2171
|
+
}
|
2172
|
+
|
2173
|
+
return INT2FIX(ret);
|
2174
|
+
}
|
2175
|
+
|
2176
|
+
/*
|
2177
|
+
* call-seq:
|
2178
|
+
* conn.lo_tell( lo_desc ) -> Fixnum
|
2179
|
+
*
|
2180
|
+
* Returns the current position of the large object _lo_desc_.
|
2181
|
+
*/
|
2182
|
+
static VALUE
|
2183
|
+
pgconn_lotell(self,in_lo_desc)
|
2184
|
+
VALUE self, in_lo_desc;
|
2185
|
+
{
|
2186
|
+
int position;
|
2187
|
+
PGconn *conn = get_pgconn(self);
|
2188
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
2189
|
+
|
2190
|
+
if((position = lo_tell(conn, lo_desc)) < 0)
|
2191
|
+
rb_raise(rb_ePGError,"lo_tell failed");
|
2192
|
+
|
2193
|
+
return INT2FIX(position);
|
2194
|
+
}
|
2195
|
+
|
2196
|
+
/*
|
2197
|
+
* call-seq:
|
2198
|
+
* conn.lo_truncate( lo_desc, len ) -> nil
|
2199
|
+
*
|
2200
|
+
* Truncates the large object _lo_desc_ to size _len_.
|
2201
|
+
*/
|
2202
|
+
static VALUE
|
2203
|
+
pgconn_lotruncate(self, in_lo_desc, in_len)
|
2204
|
+
VALUE self, in_lo_desc, in_len;
|
2205
|
+
{
|
2206
|
+
PGconn *conn = get_pgconn(self);
|
2207
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
2208
|
+
size_t len = NUM2INT(in_len);
|
2209
|
+
|
2210
|
+
if(lo_truncate(conn,lo_desc,len) < 0)
|
2211
|
+
rb_raise(rb_ePGError,"lo_truncate failed");
|
2212
|
+
|
2213
|
+
return Qnil;
|
2214
|
+
}
|
2215
|
+
|
2216
|
+
/*
|
2217
|
+
* call-seq:
|
2218
|
+
* conn.lo_close( lo_desc ) -> nil
|
2219
|
+
*
|
2220
|
+
* Closes the postgres large object of _lo_desc_.
|
2221
|
+
*/
|
2222
|
+
static VALUE
|
2223
|
+
pgconn_loclose(self, in_lo_desc)
|
2224
|
+
VALUE self, in_lo_desc;
|
2225
|
+
{
|
2226
|
+
PGconn *conn = get_pgconn(self);
|
2227
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
2228
|
+
|
2229
|
+
if(lo_unlink(conn,lo_desc) < 0)
|
2230
|
+
rb_raise(rb_ePGError,"lo_close failed");
|
2231
|
+
|
2232
|
+
return Qnil;
|
2233
|
+
}
|
2234
|
+
|
2235
|
+
/*
|
2236
|
+
* call-seq:
|
2237
|
+
* conn.lo_unlink( oid ) -> nil
|
2238
|
+
*
|
2239
|
+
* Unlinks (deletes) the postgres large object of _oid_.
|
2240
|
+
*/
|
2241
|
+
static VALUE
|
2242
|
+
pgconn_lounlink(self, in_oid)
|
2243
|
+
VALUE self, in_oid;
|
2244
|
+
{
|
2245
|
+
PGconn *conn = get_pgconn(self);
|
2246
|
+
int oid = NUM2INT(in_oid);
|
2247
|
+
|
2248
|
+
if (oid < 0)
|
2249
|
+
rb_raise(rb_ePGError, "invalid oid %d",oid);
|
2250
|
+
|
2251
|
+
if(lo_unlink(conn,oid) < 0)
|
2252
|
+
rb_raise(rb_ePGError,"lo_unlink failed");
|
2253
|
+
|
2254
|
+
return Qnil;
|
2255
|
+
}
|
2256
|
+
|
2257
|
+
/********************************************************************
|
2258
|
+
*
|
2259
|
+
* Document-class: PGresult
|
2260
|
+
*
|
2261
|
+
* The class to represent the query result tuples (rows).
|
2262
|
+
* An instance of this class is created as the result of every query.
|
2263
|
+
* You may need to invoke the #clear method of the instance when finished with
|
2264
|
+
* the result for better memory performance.
|
2265
|
+
*/
|
2266
|
+
|
2267
|
+
/**************************************************************************
|
2268
|
+
* PGresult INSTANCE METHODS
|
2269
|
+
**************************************************************************/
|
2270
|
+
|
2271
|
+
/*
|
2272
|
+
* call-seq:
|
2273
|
+
* res.result_status() -> Fixnum
|
2274
|
+
*
|
2275
|
+
* Returns the status of the query. The status value is one of:
|
2276
|
+
* * +PGRES_EMPTY_QUERY+
|
2277
|
+
* * +PGRES_COMMAND_OK+
|
2278
|
+
* * +PGRES_TUPLES_OK+
|
2279
|
+
* * +PGRES_COPY_OUT+
|
2280
|
+
* * +PGRES_COPY_IN+
|
2281
|
+
* * +PGRES_BAD_RESPONSE+
|
2282
|
+
* * +PGRES_NONFATAL_ERROR+
|
2283
|
+
* * +PGRES_FATAL_ERROR+
|
2284
|
+
*/
|
2285
|
+
static VALUE
|
2286
|
+
pgresult_result_status(self)
|
2287
|
+
VALUE self;
|
2288
|
+
{
|
2289
|
+
return INT2FIX(PQresultStatus(get_pgresult(self)));
|
2290
|
+
}
|
2291
|
+
|
2292
|
+
/*
|
2293
|
+
* call-seq:
|
2294
|
+
* res.res_status( status ) -> String
|
2295
|
+
*
|
2296
|
+
* Returns the string representation of status +status+.
|
2297
|
+
*
|
2298
|
+
*/
|
2299
|
+
static VALUE
|
2300
|
+
pgresult_res_status(self,status)
|
2301
|
+
VALUE self,status;
|
2302
|
+
{
|
2303
|
+
return rb_str_new2(PQresStatus(NUM2INT(status)));
|
2304
|
+
}
|
2305
|
+
|
2306
|
+
/*
|
2307
|
+
* call-seq:
|
2308
|
+
* res.result_error_message() -> String
|
2309
|
+
*
|
2310
|
+
* Returns the error message of the command as a string.
|
2311
|
+
*/
|
2312
|
+
static VALUE
|
2313
|
+
pgresult_result_error_message(self)
|
2314
|
+
VALUE self;
|
2315
|
+
{
|
2316
|
+
return rb_str_new2(PQresultErrorMessage(get_pgresult(self)));
|
2317
|
+
}
|
2318
|
+
|
2319
|
+
/*
|
2320
|
+
* call-seq:
|
2321
|
+
* res.result_error_field(fieldcode) -> String
|
2322
|
+
*
|
2323
|
+
* Returns the individual field of an error.
|
2324
|
+
*
|
2325
|
+
* +fieldcode+ is one of:
|
2326
|
+
* * +PG_DIAG_SEVERITY+
|
2327
|
+
* * +PG_DIAG_SQLSTATE+
|
2328
|
+
* * +PG_DIAG_MESSAGE_PRIMARY+
|
2329
|
+
* * +PG_DIAG_MESSAGE_DETAIL+
|
2330
|
+
* * +PG_DIAG_MESSAGE_HINT+
|
2331
|
+
* * +PG_DIAG_STATEMENT_POSITION+
|
2332
|
+
* * +PG_DIAG_INTERNAL_POSITION+
|
2333
|
+
* * +PG_DIAG_INTERNAL_QUERY+
|
2334
|
+
* * +PG_DIAG_CONTEXT+
|
2335
|
+
* * +PG_DIAG_SOURCE_FILE+
|
2336
|
+
* * +PG_DIAG_SOURCE_LINE+
|
2337
|
+
* * +PG_DIAG_SOURCE_FUNCTION+
|
2338
|
+
*/
|
2339
|
+
static VALUE
|
2340
|
+
pgresult_result_error_field(self)
|
2341
|
+
VALUE self;
|
2342
|
+
{
|
2343
|
+
return rb_str_new2(PQresultErrorMessage(get_pgresult(self)));
|
2344
|
+
}
|
2345
|
+
|
2346
|
+
/*
|
2347
|
+
* call-seq:
|
2348
|
+
* res.clear() -> nil
|
2349
|
+
*
|
2350
|
+
* Clears the PGresult object as the result of the query.
|
2351
|
+
*/
|
2352
|
+
static VALUE
|
2353
|
+
pgresult_clear(self)
|
2354
|
+
VALUE self;
|
2355
|
+
{
|
2356
|
+
PQclear(get_pgresult(self));
|
2357
|
+
DATA_PTR(self) = 0;
|
2358
|
+
return Qnil;
|
2359
|
+
}
|
2360
|
+
|
2361
|
+
/*
|
2362
|
+
* call-seq:
|
2363
|
+
* res.ntuples() -> Fixnum
|
2364
|
+
*
|
2365
|
+
* Returns the number of tuples in the query result.
|
2366
|
+
*/
|
2367
|
+
static VALUE
|
2368
|
+
pgresult_ntuples(self)
|
2369
|
+
VALUE self;
|
2370
|
+
{
|
2371
|
+
return INT2FIX(PQntuples(get_pgresult(self)));
|
2372
|
+
}
|
2373
|
+
|
2374
|
+
/*
|
2375
|
+
* call-seq:
|
2376
|
+
* res.nfields() -> Fixnum
|
2377
|
+
*
|
2378
|
+
* Returns the number of columns in the query result.
|
2379
|
+
*/
|
2380
|
+
static VALUE
|
2381
|
+
pgresult_nfields(self)
|
2382
|
+
VALUE self;
|
2383
|
+
{
|
2384
|
+
return INT2NUM(PQnfields(get_pgresult(self)));
|
2385
|
+
}
|
2386
|
+
|
2387
|
+
/*
|
2388
|
+
* call-seq:
|
2389
|
+
* res.fname( index ) -> String
|
2390
|
+
*
|
2391
|
+
* Returns the name of the column corresponding to _index_.
|
2392
|
+
*/
|
2393
|
+
static VALUE
|
2394
|
+
pgresult_fname(self, index)
|
2395
|
+
VALUE self, index;
|
2396
|
+
{
|
2397
|
+
PGresult *result;
|
2398
|
+
int i = NUM2INT(index);
|
2399
|
+
|
2400
|
+
result = get_pgresult(self);
|
2401
|
+
if (i < 0 || i >= PQnfields(result)) {
|
2402
|
+
rb_raise(rb_eArgError,"invalid field number %d", i);
|
2403
|
+
}
|
2404
|
+
return rb_tainted_str_new2(PQfname(result, i));
|
2405
|
+
}
|
2406
|
+
|
2407
|
+
/*
|
2408
|
+
* call-seq:
|
2409
|
+
* res.fnumber( name ) -> Fixnum
|
2410
|
+
*
|
2411
|
+
* Returns the index of the field specified by the string _name_.
|
2412
|
+
*
|
2413
|
+
* Raises an ArgumentError if the specified _name_ isn't one of the field names;
|
2414
|
+
* raises a TypeError if _name_ is not a String.
|
2415
|
+
*/
|
2416
|
+
static VALUE
|
2417
|
+
pgresult_fnumber(self, name)
|
2418
|
+
VALUE self, name;
|
2419
|
+
{
|
2420
|
+
int n;
|
2421
|
+
|
2422
|
+
Check_Type(name, T_STRING);
|
2423
|
+
|
2424
|
+
n = PQfnumber(get_pgresult(self), StringValuePtr(name));
|
2425
|
+
if (n == -1) {
|
2426
|
+
rb_raise(rb_eArgError,"Unknown field: %s", StringValuePtr(name));
|
2427
|
+
}
|
2428
|
+
return INT2FIX(n);
|
2429
|
+
}
|
2430
|
+
|
2431
|
+
/*
|
2432
|
+
* call-seq:
|
2433
|
+
* res.ftable( column_number ) -> Fixnum
|
2434
|
+
*
|
2435
|
+
* Returns the Oid of the table from which the column _column_number_
|
2436
|
+
* was fetched.
|
2437
|
+
*
|
2438
|
+
* Raises ArgumentError if _column_number_ is out of range or if
|
2439
|
+
* the Oid is undefined for that column.
|
2440
|
+
*/
|
2441
|
+
static VALUE
|
2442
|
+
pgresult_ftable(self, column_number)
|
2443
|
+
VALUE self, column_number;
|
2444
|
+
{
|
2445
|
+
Oid n = PQftable(get_pgresult(self), NUM2INT(column_number));
|
2446
|
+
if (n == InvalidOid) {
|
2447
|
+
rb_raise(rb_eArgError,"Oid is undefined for column: %d",
|
2448
|
+
NUM2INT(column_number));
|
2449
|
+
}
|
2450
|
+
return INT2FIX(n);
|
2451
|
+
}
|
2452
|
+
|
2453
|
+
/*
|
2454
|
+
* call-seq:
|
2455
|
+
* res.ftablecol( column_number ) -> Fixnum
|
2456
|
+
*
|
2457
|
+
* Returns the column number (within its table) of the table from
|
2458
|
+
* which the column _column_number_ is made up.
|
2459
|
+
*
|
2460
|
+
* Raises ArgumentError if _column_number_ is out of range or if
|
2461
|
+
* the column number from its table is undefined for that column.
|
2462
|
+
*/
|
2463
|
+
static VALUE
|
2464
|
+
pgresult_ftablecol(self, column_number)
|
2465
|
+
VALUE self, column_number;
|
2466
|
+
{
|
2467
|
+
int n = PQftablecol(get_pgresult(self), NUM2INT(column_number));
|
2468
|
+
if (n == 0) {
|
2469
|
+
rb_raise(rb_eArgError,
|
2470
|
+
"Column number from table is undefined for column: %d",
|
2471
|
+
NUM2INT(column_number));
|
2472
|
+
}
|
2473
|
+
return INT2FIX(n);
|
2474
|
+
}
|
2475
|
+
|
2476
|
+
/*
|
2477
|
+
* call-seq:
|
2478
|
+
* res.fformat( column_number ) -> Fixnum
|
2479
|
+
*
|
2480
|
+
* Returns the format (0 for text, 1 for binary) of column
|
2481
|
+
* _column_number_.
|
2482
|
+
*
|
2483
|
+
* Raises ArgumentError if _column_number_ is out of range.
|
2484
|
+
*/
|
2485
|
+
static VALUE
|
2486
|
+
pgresult_fformat(self, column_number)
|
2487
|
+
VALUE self, column_number;
|
2488
|
+
{
|
2489
|
+
PGresult *result = get_pgresult(self);
|
2490
|
+
int fnumber = NUM2INT(column_number);
|
2491
|
+
if (fnumber >= PQnfields(result)) {
|
2492
|
+
rb_raise(rb_eArgError, "Column number is out of range: %d",
|
2493
|
+
fnumber);
|
2494
|
+
}
|
2495
|
+
return INT2FIX(PQfformat(result, fnumber));
|
2496
|
+
}
|
2497
|
+
|
2498
|
+
/*
|
2499
|
+
* call-seq:
|
2500
|
+
* res.ftype( column_number )
|
2501
|
+
*
|
2502
|
+
* Returns the data type associated with _column_number_.
|
2503
|
+
*
|
2504
|
+
* The integer returned is the internal +OID+ number (in PostgreSQL) of the type.
|
2505
|
+
*/
|
2506
|
+
static VALUE
|
2507
|
+
pgresult_ftype(self, index)
|
2508
|
+
VALUE self, index;
|
2509
|
+
{
|
2510
|
+
PGresult* result = get_pgresult(self);
|
2511
|
+
int i = NUM2INT(index);
|
2512
|
+
if (i < 0 || i >= PQnfields(result)) {
|
2513
|
+
rb_raise(rb_eArgError, "invalid field number %d", i);
|
2514
|
+
}
|
2515
|
+
return INT2NUM(PQftype(result, i));
|
2516
|
+
}
|
2517
|
+
|
2518
|
+
/*
|
2519
|
+
* call-seq:
|
2520
|
+
* res.fmod( column_number )
|
2521
|
+
*
|
2522
|
+
* Returns the type modifier associated with column _column_number_.
|
2523
|
+
*
|
2524
|
+
* Raises ArgumentError if _column_number_ is out of range.
|
2525
|
+
*/
|
2526
|
+
static VALUE
|
2527
|
+
pgresult_fmod(self, column_number)
|
2528
|
+
VALUE self, column_number;
|
2529
|
+
{
|
2530
|
+
PGresult *result = get_pgresult(self);
|
2531
|
+
int fnumber = NUM2INT(column_number);
|
2532
|
+
int modifier;
|
2533
|
+
if (fnumber >= PQnfields(result)) {
|
2534
|
+
rb_raise(rb_eArgError, "Column number is out of range: %d",
|
2535
|
+
fnumber);
|
2536
|
+
}
|
2537
|
+
if((modifier = PQfmod(result,fnumber)) == -1)
|
2538
|
+
rb_raise(rb_eArgError,
|
2539
|
+
"No modifier information available for column: %d",
|
2540
|
+
fnumber);
|
2541
|
+
return INT2NUM(modifier);
|
2542
|
+
}
|
2543
|
+
|
2544
|
+
/*
|
2545
|
+
* call-seq:
|
2546
|
+
* res.fsize( index )
|
2547
|
+
*
|
2548
|
+
* Returns the size of the field type in bytes. Returns <tt>-1</tt> if the field is variable sized.
|
2549
|
+
*
|
2550
|
+
* res = conn.exec("SELECT myInt, myVarChar50 FROM foo")
|
2551
|
+
* res.size(0) => 4
|
2552
|
+
* res.size(1) => -1
|
2553
|
+
*/
|
2554
|
+
static VALUE
|
2555
|
+
pgresult_fsize(self, index)
|
2556
|
+
VALUE self, index;
|
2557
|
+
{
|
2558
|
+
PGresult *result;
|
2559
|
+
int i = NUM2INT(index);
|
2560
|
+
|
2561
|
+
result = get_pgresult(self);
|
2562
|
+
if (i < 0 || i >= PQnfields(result)) {
|
2563
|
+
rb_raise(rb_eArgError,"invalid field number %d", i);
|
2564
|
+
}
|
2565
|
+
return INT2NUM(PQfsize(result, i));
|
2566
|
+
}
|
2567
|
+
|
2568
|
+
/*
|
2569
|
+
* call-seq:
|
2570
|
+
* res.getvalue( tup_num, field_num )
|
2571
|
+
*
|
2572
|
+
* Returns the value in tuple number _tup_num_, field _field_num_.
|
2573
|
+
*/
|
2574
|
+
static VALUE
|
2575
|
+
pgresult_getvalue(self, tup_num, field_num)
|
2576
|
+
VALUE self, tup_num, field_num;
|
2577
|
+
{
|
2578
|
+
PGresult *result;
|
2579
|
+
int i = NUM2INT(tup_num);
|
2580
|
+
int j = NUM2INT(field_num);
|
2581
|
+
|
2582
|
+
result = get_pgresult(self);
|
2583
|
+
if(i < 0 || i >= PQntuples(result)) {
|
2584
|
+
rb_raise(rb_eArgError,"invalid tuple number %d", i);
|
2585
|
+
}
|
2586
|
+
if(j < 0 || j >= PQnfields(result)) {
|
2587
|
+
rb_raise(rb_eArgError,"invalid field number %d", j);
|
2588
|
+
}
|
2589
|
+
return rb_str_new2(PQgetvalue(result, i, j));
|
2590
|
+
}
|
2591
|
+
|
2592
|
+
/*
|
2593
|
+
* call-seq:
|
2594
|
+
* res.getisnull(tuple_position, field_position) -> boolean
|
2595
|
+
*
|
2596
|
+
* Returns +true+ if the specified value is +nil+; +false+ otherwise.
|
2597
|
+
*/
|
2598
|
+
static VALUE
|
2599
|
+
pgresult_getisnull(self, tup_num, field_num)
|
2600
|
+
VALUE self, tup_num, field_num;
|
2601
|
+
{
|
2602
|
+
PGresult *result;
|
2603
|
+
int i = NUM2INT(tup_num);
|
2604
|
+
int j = NUM2INT(field_num);
|
2605
|
+
|
2606
|
+
result = get_pgresult(self);
|
2607
|
+
if (i < 0 || i >= PQntuples(result)) {
|
2608
|
+
rb_raise(rb_eArgError,"invalid tuple number %d", i);
|
2609
|
+
}
|
2610
|
+
if (j < 0 || j >= PQnfields(result)) {
|
2611
|
+
rb_raise(rb_eArgError,"invalid field number %d", j);
|
2612
|
+
}
|
2613
|
+
return PQgetisnull(result, i, j) ? Qtrue : Qfalse;
|
2614
|
+
}
|
2615
|
+
|
2616
|
+
/*
|
2617
|
+
* call-seq:
|
2618
|
+
* res.getlength( tup_num, field_num ) -> Fixnum
|
2619
|
+
*
|
2620
|
+
* Returns the (String) length of the field in bytes.
|
2621
|
+
*
|
2622
|
+
* Equivalent to <tt>res.value(<i>tup_num</i>,<i>field_num</i>).length</tt>.
|
2623
|
+
*/
|
2624
|
+
static VALUE
|
2625
|
+
pgresult_getlength(self, tup_num, field_num)
|
2626
|
+
VALUE self, tup_num, field_num;
|
2627
|
+
{
|
2628
|
+
PGresult *result;
|
2629
|
+
int i = NUM2INT(tup_num);
|
2630
|
+
int j = NUM2INT(field_num);
|
2631
|
+
|
2632
|
+
result = get_pgresult(self);
|
2633
|
+
if (i < 0 || i >= PQntuples(result)) {
|
2634
|
+
rb_raise(rb_eArgError,"invalid tuple number %d", i);
|
2635
|
+
}
|
2636
|
+
if (j < 0 || j >= PQnfields(result)) {
|
2637
|
+
rb_raise(rb_eArgError,"invalid field number %d", j);
|
2638
|
+
}
|
2639
|
+
return INT2FIX(PQgetlength(result, i, j));
|
2640
|
+
}
|
2641
|
+
|
2642
|
+
/*
|
2643
|
+
* call-seq:
|
2644
|
+
* res.nparams() -> Fixnum
|
2645
|
+
*
|
2646
|
+
* Returns the number of parameters of a prepared statement.
|
2647
|
+
* Only useful for the result returned by conn.describePrepared
|
2648
|
+
*/
|
2649
|
+
static VALUE
|
2650
|
+
pgresult_nparams(self)
|
2651
|
+
VALUE self;
|
2652
|
+
{
|
2653
|
+
PGresult *result;
|
2654
|
+
|
2655
|
+
result = get_pgresult(self);
|
2656
|
+
return INT2FIX(PQnparams(result));
|
2657
|
+
}
|
2658
|
+
|
2659
|
+
/*
|
2660
|
+
* call-seq:
|
2661
|
+
* res.paramtype( param_number ) -> Oid
|
2662
|
+
*
|
2663
|
+
* Returns the Oid of the data type of parameter _param_number_.
|
2664
|
+
* Only useful for the result returned by conn.describePrepared
|
2665
|
+
*/
|
2666
|
+
static VALUE
|
2667
|
+
pgresult_paramtype(self, param_number)
|
2668
|
+
VALUE self, param_number;
|
2669
|
+
{
|
2670
|
+
PGresult *result;
|
2671
|
+
|
2672
|
+
result = get_pgresult(self);
|
2673
|
+
return INT2FIX(PQparamtype(result,NUM2INT(param_number)));
|
2674
|
+
}
|
2675
|
+
|
2676
|
+
/*
|
2677
|
+
* call-seq:
|
2678
|
+
* res.cmd_status() -> String
|
2679
|
+
*
|
2680
|
+
* Returns the status string of the last query command.
|
2681
|
+
*/
|
2682
|
+
static VALUE
|
2683
|
+
pgresult_cmd_status(self)
|
2684
|
+
VALUE self;
|
2685
|
+
{
|
2686
|
+
return rb_tainted_str_new2(PQcmdStatus(get_pgresult(self)));
|
2687
|
+
}
|
2688
|
+
|
2689
|
+
/*
|
2690
|
+
* call-seq:
|
2691
|
+
* res.cmd_tuples() -> Fixnum
|
2692
|
+
*
|
2693
|
+
* Returns the number of tuples (rows) affected by the SQL command.
|
2694
|
+
*
|
2695
|
+
* If the SQL command that generated the PGresult was not one of:
|
2696
|
+
* * +INSERT+
|
2697
|
+
* * +UPDATE+
|
2698
|
+
* * +DELETE+
|
2699
|
+
* * +MOVE+
|
2700
|
+
* * +FETCH+
|
2701
|
+
* or if no tuples were affected, <tt>0</tt> is returned.
|
2702
|
+
*/
|
2703
|
+
static VALUE
|
2704
|
+
pgresult_cmd_tuples(self)
|
2705
|
+
VALUE self;
|
2706
|
+
{
|
2707
|
+
long n;
|
2708
|
+
n = strtol(PQcmdTuples(get_pgresult(self)),NULL, 10);
|
2709
|
+
return INT2NUM(n);
|
2710
|
+
}
|
2711
|
+
|
2712
|
+
/*
|
2713
|
+
* call-seq:
|
2714
|
+
* res.oid_value() -> Fixnum
|
2715
|
+
*
|
2716
|
+
* Returns the +oid+ of the inserted row if applicable,
|
2717
|
+
* otherwise +nil+.
|
2718
|
+
*/
|
2719
|
+
static VALUE
|
2720
|
+
pgresult_oid_value(self)
|
2721
|
+
VALUE self;
|
2722
|
+
{
|
2723
|
+
Oid n = PQoidValue(get_pgresult(self));
|
2724
|
+
if (n == InvalidOid)
|
2725
|
+
return Qnil;
|
2726
|
+
else
|
2727
|
+
return INT2FIX(n);
|
2728
|
+
}
|
2729
|
+
|
2730
|
+
/* Utility methods not in libpq */
|
2731
|
+
|
2732
|
+
/*
|
2733
|
+
* call-seq:
|
2734
|
+
* res[ n ] -> Hash
|
2735
|
+
*
|
2736
|
+
* Returns tuple _n_ as a hash.
|
2737
|
+
*/
|
2738
|
+
static VALUE
|
2739
|
+
pgresult_aref(self, index)
|
2740
|
+
VALUE self, index;
|
2741
|
+
{
|
2742
|
+
PGresult *result = get_pgresult(self);
|
2743
|
+
int tuple_num = NUM2INT(index);
|
2744
|
+
int field_num;
|
2745
|
+
VALUE fname,val;
|
2746
|
+
VALUE tuple;
|
2747
|
+
|
2748
|
+
tuple = rb_hash_new();
|
2749
|
+
for(field_num = 0; field_num < PQnfields(result); field_num++) {
|
2750
|
+
fname = rb_str_new2(PQfname(result,field_num));
|
2751
|
+
if(PQgetisnull(result, tuple_num, field_num)) {
|
2752
|
+
rb_hash_aset(tuple, fname, Qnil);
|
2753
|
+
}
|
2754
|
+
else {
|
2755
|
+
val = rb_tainted_str_new2(PQgetvalue(result, tuple_num, field_num));
|
2756
|
+
rb_hash_aset(tuple, fname, val);
|
2757
|
+
}
|
2758
|
+
}
|
2759
|
+
return tuple;
|
2760
|
+
}
|
2761
|
+
|
2762
|
+
/*
|
2763
|
+
* call-seq:
|
2764
|
+
* res.each{ |tuple| ... }
|
2765
|
+
*
|
2766
|
+
* Invokes block for each tuple in the result set.
|
2767
|
+
*/
|
2768
|
+
static VALUE
|
2769
|
+
pgresult_each(self)
|
2770
|
+
VALUE self;
|
2771
|
+
{
|
2772
|
+
PGresult *result = get_pgresult(self);
|
2773
|
+
int tuple_num;
|
2774
|
+
|
2775
|
+
for(tuple_num = 0; tuple_num < PQntuples(result); tuple_num++) {
|
2776
|
+
rb_yield(pgresult_aref(self, INT2NUM(tuple_num)));
|
2777
|
+
}
|
2778
|
+
return self;
|
2779
|
+
}
|
2780
|
+
|
2781
|
+
/*
|
2782
|
+
* call-seq:
|
2783
|
+
* res.fields() -> Array
|
2784
|
+
*
|
2785
|
+
* Returns an array of Strings representing the names of the fields in the result.
|
2786
|
+
*/
|
2787
|
+
static VALUE
|
2788
|
+
pgresult_fields(self)
|
2789
|
+
VALUE self;
|
2790
|
+
{
|
2791
|
+
PGresult *result;
|
2792
|
+
VALUE ary;
|
2793
|
+
int n, i;
|
2794
|
+
|
2795
|
+
result = get_pgresult(self);
|
2796
|
+
n = PQnfields(result);
|
2797
|
+
ary = rb_ary_new2(n);
|
2798
|
+
for (i=0;i<n;i++) {
|
2799
|
+
rb_ary_push(ary, rb_tainted_str_new2(PQfname(result, i)));
|
2800
|
+
}
|
2801
|
+
return ary;
|
2802
|
+
}
|
2803
|
+
|
2804
|
+
/**************************************************************************/
|
2805
|
+
|
2806
|
+
void
|
2807
|
+
Init_pg()
|
2808
|
+
{
|
2809
|
+
rb_ePGError = rb_define_class("PGError", rb_eStandardError);
|
2810
|
+
rb_cPGconn = rb_define_class("PGconn", rb_cObject);
|
2811
|
+
rb_cPGresult = rb_define_class("PGresult", rb_cObject);
|
2812
|
+
|
2813
|
+
|
2814
|
+
/*************************
|
2815
|
+
* PGError
|
2816
|
+
*************************/
|
2817
|
+
rb_define_alias(rb_ePGError, "error", "message");
|
2818
|
+
rb_define_attr(rb_ePGError, "connection", 1, 0);
|
2819
|
+
rb_define_attr(rb_ePGError, "result", 1, 0);
|
2820
|
+
|
2821
|
+
/*************************
|
2822
|
+
* PGconn
|
2823
|
+
*************************/
|
2824
|
+
|
2825
|
+
/****** PGconn CLASS METHODS ******/
|
2826
|
+
#ifdef HAVE_RB_DEFINE_ALLOC_FUNC
|
2827
|
+
rb_define_alloc_func(rb_cPGconn, pgconn_alloc);
|
2828
|
+
#else
|
2829
|
+
rb_define_singleton_method(rb_cPGconn, "new", pgconn_s_new, -1);
|
2830
|
+
#endif
|
2831
|
+
rb_define_singleton_alias(rb_cPGconn, "connect", "new");
|
2832
|
+
rb_define_singleton_alias(rb_cPGconn, "open", "new");
|
2833
|
+
rb_define_singleton_alias(rb_cPGconn, "setdb", "new");
|
2834
|
+
rb_define_singleton_alias(rb_cPGconn, "setdblogin", "new");
|
2835
|
+
rb_define_singleton_alias(rb_cPGconn, "open", "new");
|
2836
|
+
rb_define_singleton_method(rb_cPGconn, "escape_string", pgconn_s_escape, 1);
|
2837
|
+
rb_define_singleton_alias(rb_cPGconn, "escape", "escape_string");
|
2838
|
+
rb_define_singleton_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
|
2839
|
+
rb_define_singleton_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
|
2840
|
+
rb_define_singleton_method(rb_cPGconn, "isthreadsafe", pgconn_s_isthreadsafe, 0);
|
2841
|
+
rb_define_singleton_method(rb_cPGconn, "encrypt_password", pgconn_s_encrypt_password, 0);
|
2842
|
+
|
2843
|
+
/****** PGconn CLASS CONSTANTS: Connection Status ******/
|
2844
|
+
rb_define_const(rb_cPGconn, "CONNECTION_OK", INT2FIX(CONNECTION_OK));
|
2845
|
+
rb_define_const(rb_cPGconn, "CONNECTION_BAD", INT2FIX(CONNECTION_BAD));
|
2846
|
+
|
2847
|
+
/****** PGconn CLASS CONSTANTS: Nonblocking connection status ******/
|
2848
|
+
rb_define_const(rb_cPGconn, "CONNECTION_STARTED", INT2FIX(CONNECTION_STARTED));
|
2849
|
+
rb_define_const(rb_cPGconn, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
|
2850
|
+
rb_define_const(rb_cPGconn, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
|
2851
|
+
rb_define_const(rb_cPGconn, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
|
2852
|
+
rb_define_const(rb_cPGconn, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
|
2853
|
+
rb_define_const(rb_cPGconn, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
|
2854
|
+
|
2855
|
+
/****** PGconn CLASS CONSTANTS: Nonblocking connection polling status ******/
|
2856
|
+
rb_define_const(rb_cPGconn, "PGRES_POLLING_READING", INT2FIX(PGRES_POLLING_READING));
|
2857
|
+
rb_define_const(rb_cPGconn, "PGRES_POLLING_WRITING", INT2FIX(PGRES_POLLING_WRITING));
|
2858
|
+
rb_define_const(rb_cPGconn, "PGRES_POLLING_FAILED", INT2FIX(PGRES_POLLING_FAILED));
|
2859
|
+
rb_define_const(rb_cPGconn, "PGRES_POLLING_OK", INT2FIX(PGRES_POLLING_OK));
|
2860
|
+
|
2861
|
+
/****** PGconn CLASS CONSTANTS: Transaction Status ******/
|
2862
|
+
rb_define_const(rb_cPGconn, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
|
2863
|
+
rb_define_const(rb_cPGconn, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
|
2864
|
+
rb_define_const(rb_cPGconn, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
|
2865
|
+
rb_define_const(rb_cPGconn, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
|
2866
|
+
rb_define_const(rb_cPGconn, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
|
2867
|
+
|
2868
|
+
/****** PGconn CLASS CONSTANTS: Error Verbosity ******/
|
2869
|
+
rb_define_const(rb_cPGconn, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
|
2870
|
+
rb_define_const(rb_cPGconn, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
|
2871
|
+
rb_define_const(rb_cPGconn, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
|
2872
|
+
|
2873
|
+
/****** PGconn CLASS CONSTANTS: Large Objects ******/
|
2874
|
+
rb_define_const(rb_cPGconn, "INV_WRITE", INT2FIX(INV_WRITE));
|
2875
|
+
rb_define_const(rb_cPGconn, "INV_READ", INT2FIX(INV_READ));
|
2876
|
+
rb_define_const(rb_cPGconn, "SEEK_SET", INT2FIX(SEEK_SET));
|
2877
|
+
rb_define_const(rb_cPGconn, "SEEK_CUR", INT2FIX(SEEK_CUR));
|
2878
|
+
rb_define_const(rb_cPGconn, "SEEK_END", INT2FIX(SEEK_END));
|
2879
|
+
|
2880
|
+
/****** PGconn INSTANCE METHODS: Connection Control ******/
|
2881
|
+
rb_define_method(rb_cPGconn, "initialize", pgconn_init, -1);
|
2882
|
+
rb_define_method(rb_cPGconn, "reset", pgconn_reset, 0);
|
2883
|
+
rb_define_method(rb_cPGconn, "finish", pgconn_finish, 0);
|
2884
|
+
|
2885
|
+
/****** PGconn INSTANCE METHODS: Connection Status ******/
|
2886
|
+
rb_define_method(rb_cPGconn, "db", pgconn_db, 0);
|
2887
|
+
rb_define_method(rb_cPGconn, "user", pgconn_user, 0);
|
2888
|
+
rb_define_method(rb_cPGconn, "pass", pgconn_pass, 0);
|
2889
|
+
rb_define_method(rb_cPGconn, "host", pgconn_host, 0);
|
2890
|
+
rb_define_method(rb_cPGconn, "port", pgconn_port, 0);
|
2891
|
+
rb_define_method(rb_cPGconn, "tty", pgconn_tty, 0);
|
2892
|
+
rb_define_method(rb_cPGconn, "options", pgconn_options, 0);
|
2893
|
+
rb_define_method(rb_cPGconn, "status", pgconn_status, 0);
|
2894
|
+
rb_define_method(rb_cPGconn, "transaction_status", pgconn_transaction_status, 0);
|
2895
|
+
rb_define_method(rb_cPGconn, "parameter_status", pgconn_parameter_status, 1);
|
2896
|
+
rb_define_method(rb_cPGconn, "protocol_version", pgconn_protocol_version, 0);
|
2897
|
+
rb_define_method(rb_cPGconn, "server_version", pgconn_server_version, 0);
|
2898
|
+
rb_define_method(rb_cPGconn, "error_message", pgconn_error_message, 0);
|
2899
|
+
//rb_define_method(rb_cPGconn, "socket", pgconn_socket, 0);
|
2900
|
+
rb_define_method(rb_cPGconn, "backend_pid", pgconn_backend_pid, 0);
|
2901
|
+
rb_define_method(rb_cPGconn, "connection_needs_password", pgconn_connection_needs_password, 0);
|
2902
|
+
rb_define_method(rb_cPGconn, "connection_used_password", pgconn_connection_used_password, 0);
|
2903
|
+
//rb_define_method(rb_cPGconn, "getssl", pgconn_getssl, 0);
|
2904
|
+
|
2905
|
+
/****** PGconn INSTANCE METHODS: Command Execution ******/
|
2906
|
+
rb_define_method(rb_cPGconn, "exec", pgconn_exec, -1);
|
2907
|
+
rb_define_method(rb_cPGconn, "prepare", pgconn_prepare, -1);
|
2908
|
+
rb_define_method(rb_cPGconn, "exec_prepared", pgconn_exec_prepared, -1);
|
2909
|
+
rb_define_method(rb_cPGconn, "describe_prepared", pgconn_describe_prepared, 1);
|
2910
|
+
rb_define_method(rb_cPGconn, "describe_portal", pgconn_describe_portal, 1);
|
2911
|
+
rb_define_method(rb_cPGconn, "make_empty_pgresult", pgconn_make_empty_pgresult, 1);
|
2912
|
+
rb_define_method(rb_cPGconn, "escape_string", pgconn_s_escape, 1);
|
2913
|
+
rb_define_alias(rb_cPGconn, "escape", "escape_string");
|
2914
|
+
rb_define_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
|
2915
|
+
rb_define_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
|
2916
|
+
|
2917
|
+
/****** PGconn INSTANCE METHODS: Asynchronous Command Processing ******/
|
2918
|
+
rb_define_method(rb_cPGconn, "send_query", pgconn_send_query, -1);
|
2919
|
+
rb_define_method(rb_cPGconn, "send_prepare", pgconn_send_prepare, -1);
|
2920
|
+
rb_define_method(rb_cPGconn, "send_query_prepared", pgconn_send_query_prepared, -1);
|
2921
|
+
rb_define_method(rb_cPGconn, "send_describe_prepared", pgconn_send_describe_prepared, 1);
|
2922
|
+
rb_define_method(rb_cPGconn, "send_describe_portal", pgconn_send_describe_portal, 1);
|
2923
|
+
rb_define_method(rb_cPGconn, "get_result", pgconn_get_result, 0);
|
2924
|
+
rb_define_method(rb_cPGconn, "consume_input", pgconn_consume_input, 0);
|
2925
|
+
rb_define_method(rb_cPGconn, "is_busy", pgconn_is_busy, 0);
|
2926
|
+
rb_define_method(rb_cPGconn, "setnonblocking", pgconn_setnonblocking, 0);
|
2927
|
+
rb_define_method(rb_cPGconn, "isnonblocking", pgconn_isnonblocking, 0);
|
2928
|
+
rb_define_method(rb_cPGconn, "flush", pgconn_flush, 0);
|
2929
|
+
|
2930
|
+
/****** PGconn INSTANCE METHODS: Cancelling Queries in Progress ******/
|
2931
|
+
//rb_define_method(rb_cPGconn, "get_cancel", pgconn_get_result, 0);
|
2932
|
+
//rb_define_method(rb_cPGconn, "free_cancel", pgconn_get_result, 0);
|
2933
|
+
//rb_define_method(rb_cPGconn, "cancel", pgconn_get_result, 0);
|
2934
|
+
|
2935
|
+
/****** PGconn INSTANCE METHODS: NOTIFY ******/
|
2936
|
+
rb_define_method(rb_cPGconn, "notifies", pgconn_notifies, 0);
|
2937
|
+
|
2938
|
+
/****** PGconn INSTANCE METHODS: COPY ******/
|
2939
|
+
rb_define_method(rb_cPGconn, "put_copy_data", pgconn_put_copy_data, 1);
|
2940
|
+
rb_define_method(rb_cPGconn, "put_copy_end", pgconn_put_copy_end, -1);
|
2941
|
+
rb_define_method(rb_cPGconn, "get_copy_data", pgconn_get_copy_data, -1);
|
2942
|
+
|
2943
|
+
/****** PGconn INSTANCE METHODS: Control Functions ******/
|
2944
|
+
rb_define_method(rb_cPGconn, "set_error_verbosity", pgconn_set_error_verbosity, 1);
|
2945
|
+
rb_define_method(rb_cPGconn, "trace", pgconn_trace, 1);
|
2946
|
+
rb_define_method(rb_cPGconn, "untrace", pgconn_untrace, 0);
|
2947
|
+
|
2948
|
+
/****** PGconn INSTANCE METHODS: Notice Processing ******/
|
2949
|
+
//rb_define_method(rb_cPGconn, "set_notice_receiver", pgconn_set_notice_receiver, 0);
|
2950
|
+
rb_define_method(rb_cPGconn, "set_notice_processor", pgconn_set_notice_processor, 0);
|
2951
|
+
|
2952
|
+
/****** PGconn INSTANCE METHODS: Other TODO ******/
|
2953
|
+
rb_define_method(rb_cPGconn, "get_client_encoding", pgconn_get_client_encoding, 0);
|
2954
|
+
rb_define_method(rb_cPGconn, "set_client_encoding", pgconn_set_client_encoding, 1);
|
2955
|
+
|
2956
|
+
/****** PGconn INSTANCE METHODS: Large Object Support ******/
|
2957
|
+
rb_define_method(rb_cPGconn, "lo_creat", pgconn_locreat, -1);
|
2958
|
+
rb_define_alias(rb_cPGconn, "locreat", "lo_creat");
|
2959
|
+
rb_define_method(rb_cPGconn, "lo_create", pgconn_locreate, 1);
|
2960
|
+
rb_define_alias(rb_cPGconn, "locreate", "lo_create");
|
2961
|
+
rb_define_method(rb_cPGconn, "lo_import", pgconn_loimport, 1);
|
2962
|
+
rb_define_alias(rb_cPGconn, "loimport", "lo_import");
|
2963
|
+
rb_define_method(rb_cPGconn, "lo_export", pgconn_loexport, 2);
|
2964
|
+
rb_define_alias(rb_cPGconn, "loexport", "lo_export");
|
2965
|
+
rb_define_method(rb_cPGconn, "lo_open", pgconn_loopen, -1);
|
2966
|
+
rb_define_alias(rb_cPGconn, "loopen", "lo_open");
|
2967
|
+
rb_define_method(rb_cPGconn, "lo_write",pgconn_lowrite, 2);
|
2968
|
+
rb_define_alias(rb_cPGconn, "lowrite", "lo_write");
|
2969
|
+
rb_define_method(rb_cPGconn, "lo_read",pgconn_loread, 2);
|
2970
|
+
rb_define_alias(rb_cPGconn, "loread", "lo_read");
|
2971
|
+
rb_define_method(rb_cPGconn, "lo_lseek",pgconn_lolseek, 3);
|
2972
|
+
rb_define_alias(rb_cPGconn, "lolseek", "lo_lseek");
|
2973
|
+
rb_define_alias(rb_cPGconn, "lo_seek", "lo_lseek");
|
2974
|
+
rb_define_alias(rb_cPGconn, "loseek", "lo_lseek");
|
2975
|
+
rb_define_method(rb_cPGconn, "lo_tell",pgconn_lotell, 1);
|
2976
|
+
rb_define_alias(rb_cPGconn, "lotell", "lo_tell");
|
2977
|
+
rb_define_method(rb_cPGconn, "lo_truncate", pgconn_lotruncate, 2);
|
2978
|
+
rb_define_alias(rb_cPGconn, "lotruncate", "lo_truncate");
|
2979
|
+
rb_define_method(rb_cPGconn, "lo_close",pgconn_loclose, 1);
|
2980
|
+
rb_define_alias(rb_cPGconn, "loclose", "lo_close");
|
2981
|
+
rb_define_method(rb_cPGconn, "lo_unlink", pgconn_lounlink, 1);
|
2982
|
+
rb_define_alias(rb_cPGconn, "lounlink", "lo_unlink");
|
2983
|
+
|
2984
|
+
/*************************
|
2985
|
+
* PGresult
|
2986
|
+
*************************/
|
2987
|
+
rb_include_module(rb_cPGresult, rb_mEnumerable);
|
2988
|
+
|
2989
|
+
/****** PGresult CONSTANTS: result status ******/
|
2990
|
+
rb_define_const(rb_cPGresult, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
|
2991
|
+
rb_define_const(rb_cPGresult, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
|
2992
|
+
rb_define_const(rb_cPGresult, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
|
2993
|
+
rb_define_const(rb_cPGresult, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
|
2994
|
+
rb_define_const(rb_cPGresult, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
|
2995
|
+
rb_define_const(rb_cPGresult, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
|
2996
|
+
rb_define_const(rb_cPGresult, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
|
2997
|
+
rb_define_const(rb_cPGresult, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
|
2998
|
+
|
2999
|
+
/****** PGresult CONSTANTS: result error field codes ******/
|
3000
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
|
3001
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
|
3002
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
|
3003
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
|
3004
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
|
3005
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
|
3006
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
|
3007
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
|
3008
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
|
3009
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
|
3010
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
|
3011
|
+
rb_define_const(rb_cPGresult, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
|
3012
|
+
|
3013
|
+
/****** PGresult INSTANCE METHODS: libpq ******/
|
3014
|
+
rb_define_method(rb_cPGresult, "result_status", pgresult_result_status, 0);
|
3015
|
+
rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, 1);
|
3016
|
+
rb_define_method(rb_cPGresult, "result_error_message", pgresult_result_error_message, 0);
|
3017
|
+
rb_define_method(rb_cPGresult, "result_error_field", pgresult_result_error_field, 0);
|
3018
|
+
rb_define_method(rb_cPGresult, "clear", pgresult_clear, 0);
|
3019
|
+
rb_define_method(rb_cPGresult, "ntuples", pgresult_ntuples, 0);
|
3020
|
+
rb_define_method(rb_cPGresult, "nfields", pgresult_nfields, 0);
|
3021
|
+
rb_define_method(rb_cPGresult, "fname", pgresult_fname, 1);
|
3022
|
+
rb_define_method(rb_cPGresult, "fnumber", pgresult_fnumber, 1);
|
3023
|
+
rb_define_method(rb_cPGresult, "ftable", pgresult_ftable, 1);
|
3024
|
+
rb_define_method(rb_cPGresult, "ftablecol", pgresult_ftablecol, 1);
|
3025
|
+
rb_define_method(rb_cPGresult, "fformat", pgresult_fformat, 1);
|
3026
|
+
rb_define_method(rb_cPGresult, "ftype", pgresult_ftype, 1);
|
3027
|
+
rb_define_method(rb_cPGresult, "fmod", pgresult_fmod, 1);
|
3028
|
+
rb_define_method(rb_cPGresult, "fsize", pgresult_fsize, 1);
|
3029
|
+
rb_define_method(rb_cPGresult, "getvalue", pgresult_getvalue, 2);
|
3030
|
+
rb_define_method(rb_cPGresult, "getisnull", pgresult_getisnull, 2);
|
3031
|
+
rb_define_method(rb_cPGresult, "getlength", pgresult_getlength, 2);
|
3032
|
+
rb_define_method(rb_cPGresult, "nparams", pgresult_nparams, 0);
|
3033
|
+
rb_define_method(rb_cPGresult, "paramtype", pgresult_paramtype, 0);
|
3034
|
+
rb_define_method(rb_cPGresult, "cmd_status", pgresult_cmd_status, 0);
|
3035
|
+
rb_define_method(rb_cPGresult, "cmd_tuples", pgresult_cmd_tuples, 0);
|
3036
|
+
rb_define_method(rb_cPGresult, "oid_value", pgresult_oid_value, 0);
|
3037
|
+
|
3038
|
+
/****** PGresult INSTANCE METHODS: other ******/
|
3039
|
+
rb_define_method(rb_cPGresult, "[]", pgresult_aref, 1);
|
3040
|
+
rb_define_method(rb_cPGresult, "each", pgresult_each, 0);
|
3041
|
+
rb_define_method(rb_cPGresult, "fields", pgresult_fields, 0);
|
3042
|
+
|
3043
|
+
}
|