forked from bucardo/dbdpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbdimp.h
283 lines (202 loc) · 11 KB
/
dbdimp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright (c) 2000-2016 Greg Sabino Mullane and others: see the Changes file
Portions Copyright (c) 1997-2000 Edmund Mergl
Portions Copyright (c) 1994-1997 Tim Bunce
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.
*/
/* Define drh implementor data structure */
struct imp_drh_st {
dbih_drc_t com; /* MUST be first element in structure */
};
/* Define dbh implementor data structure */
struct imp_dbh_st {
dbih_dbc_t com; /* MUST be first element in structure */
int pg_protocol; /* value of PQprotocolVersion, usually 3 (could also be 0) */
int pg_server_version; /* server version e.g. 80100 */
int pid_number; /* prefixed before prepare_number */
int prepare_number; /* internal prepared statement name modifier */
int copystate; /* 0=none PGRES_COPY_IN PGRES_COPY_OUT */
int pg_errorlevel; /* PQsetErrorVerbosity. Set by user, defaults to 1 */
int server_prepare; /* do we want to use PQexecPrepared? 0=no 1=yes 2=smart. Can be changed by user */
int switch_prepared; /* how many executes until we switch to PQexecPrepared */
int async_status; /* 0=no async 1=async started -1=async has been cancelled */
imp_sth_t *async_sth; /* current async statement handle */
AV *savepoints; /* list of savepoints */
PGconn *conn; /* connection structure */
char *sqlstate; /* from the last result */
bool pg_bool_tf; /* do bools return 't'/'f'? Set by user, default is 0 */
bool prepare_now; /* force immediate prepares, even with placeholders. Set by user, default is 0 */
bool done_begin; /* have we done a begin? (e.g. are we in a transaction?) */
bool dollaronly; /* only consider $1, $2 ... as valid placeholders */
bool nocolons; /* do not consider :1, :2 ... as valid placeholders */
bool ph_escaped; /* allow backslash to escape placeholders */
bool expand_array; /* transform arrays from the db into Perl arrays? Default is 1 */
bool txn_read_only; /* are we in read-only mode? Set with $dbh->{ReadOnly} */
int pg_enable_utf8; /* legacy utf8 flag: force utf8 flag on or off, regardless of client_encoding */
bool pg_utf8_flag; /* are we currently flipping the utf8 flag on? */
bool client_encoding_utf8; /* is the client_encoding utf8 last we checked? */
};
/* The placeholder structure. Used as array elements in the ph_array_t structure */
struct ph_st {
char *fooname; /* name if using :foo style */
char *value; /* the literal passed-in value, may be binary */
STRLEN valuelen; /* length of the value */
char *quoted; /* quoted version of the value, for PQexec only */
STRLEN quotedlen; /* length of the quoted value */
bool referenced; /* used for PREPARE AS construction */
bool defaultval; /* is it using a generic 'default' value? */
bool iscurrent; /* do we want to use a literal CURRENT_TIMESTAMP? */
bool isdefault; /* are we passing a literal 'DEFAULT'? */
bool isinout; /* is this a bind_param_inout value? */
SV *inout; /* what variable we are updating via inout magic */
sql_type_info_t* bind_type; /* type information for this placeholder */
};
typedef struct ph_st ph_t;
/* The array container for the above segment structure */
struct ph_array_st {
int length; /* length of the array */
int elements; /* num of elements in the array */
ph_t *array; /* the array of placeholders */
};
typedef struct ph_array_st ph_array_t;
/* Each statement is broken up into segments */
struct seg_st {
char *segment; /* non-placeholder string segment */
int placeholder; /* which placeholder this points to, 0=none */
};
typedef struct seg_st seg_t;
struct seg_array_st {
int length;
int elements;
seg_t *array;
};
typedef struct seg_array_st seg_array_t;
typedef enum
{
PLACEHOLDER_NONE,
PLACEHOLDER_QUESTIONMARK,
PLACEHOLDER_DOLLAR,
PLACEHOLDER_COLON
} PGPlaceholderType;
#define PLACEHOLDER_TYPE_COUNT (PLACEHOLDER_COLON + 1)
/* Define sth implementor data structure */
struct imp_sth_st {
dbih_stc_t com; /* MUST be first element in structure */
int server_prepare; /* inherited from dbh. 3 states: 0=no 1=yes 2=smart */
int switch_prepared; /* inherited from dbh */
int number_iterations; /* how many times has the statement been executed? Used by switch_prepared */
PGPlaceholderType placeholder_type; /* which style is being used 1=? 2=$1 3=:foo */
int numsegs; /* how many segments this statement has */
int numphs; /* how many placeholders this statement has */
int numbound; /* how many placeholders were explicitly bound by the client, not us */
int cur_tuple; /* current tuple being fetched */
int rows; /* number of affected rows */
int async_flag; /* async? 0=no 1=async 2=cancel 4=wait */
int async_status; /* 0=no async 1=async started -1=async has been cancelled */
STRLEN totalsize; /* total string length of the statement (with no placeholders)*/
const char ** PQvals; /* List of values to pass to PQ* */
int * PQlens; /* List of lengths to pass to PQ* */
int * PQfmts; /* List of formats to pass to PQ* */
Oid * PQoids; /* List of types to pass to PQ* */
char *prepare_name; /* name of the prepared query; NULL if not prepared */
char *firstword; /* first word of the statement */
PGresult *result; /* result structure from the executed query */
sql_type_info_t **type_info; /* type of each column in result */
seg_array_t seg_ary; /* array of segments */
ph_array_t ph_ary; /* array of placeholders */
bool prepare_now; /* prepare this statement right away, even if it has placeholders */
bool prepared_by_us; /* false if {prepare_name} set directly */
bool onetime; /* this statement is guaranteed not to be run again - so don't use SSP */
bool direct; /* allow bypassing of the statement parsing */
bool is_dml; /* is this SELECT/INSERT/UPDATE/DELETE? */
bool has_binary; /* does it have one or more binary placeholders? */
bool has_default; /* does it have one or more 'DEFAULT' values? */
bool has_current; /* does it have one or more 'DEFAULT' values? */
bool dollaronly; /* Only use $1 as placeholders, allow all else */
bool nocolons; /* do not consider :1, :2 ... as valid placeholders */
bool use_inout; /* Any placeholders using inout? */
bool all_bound; /* Have all placeholders been bound? */
};
/* Avoid name clashes by assigning DBI funcs to a pg_ name. */
/* In order of appearance in dbdimp.c */
#define dbd_init pg_init
extern void dbd_init (dbistate_t *dbistate);
#define dbd_db_login6 pg_db_login6
int dbd_db_login6 (SV * dbh, imp_dbh_t * imp_dbh, char * dbname, char * uid, char * pwd, SV *attr);
#define dbd_db_ping pg_db_ping
int dbd_db_ping(SV *dbh);
#define dbd_db_commit pg_db_commit
int dbd_db_commit (SV * dbh, imp_dbh_t * imp_dbh);
#define dbd_db_rollback pg_db_rollback
int dbd_db_rollback (SV * dbh, imp_dbh_t * imp_dbh);
#define dbd_db_disconnect pg_db_disconnect
int dbd_db_disconnect (SV * dbh, imp_dbh_t * imp_dbh);
#define dbd_db_destroy pg_db_destroy
void dbd_db_destroy (SV * dbh, imp_dbh_t * imp_dbh);
#define dbd_db_FETCH_attrib pg_db_FETCH_attrib
SV * dbd_db_FETCH_attrib (SV * dbh, imp_dbh_t * imp_dbh, SV * keysv);
#define dbd_db_STORE_attrib pg_db_STORE_attrib
int dbd_db_STORE_attrib (SV * dbh, imp_dbh_t * imp_dbh, SV * keysv, SV * valuesv);
#define dbd_st_FETCH_attrib pg_st_FETCH_attrib
SV * dbd_st_FETCH_attrib (SV * sth, imp_sth_t * imp_sth, SV * keysv);
#define dbd_st_STORE_attrib pg_st_STORE_attrib
int dbd_st_STORE_attrib (SV * sth, imp_sth_t * imp_sth, SV * keysv, SV * valuesv);
#define dbd_discon_all pg_discon_all
int dbd_discon_all (SV * drh, imp_drh_t * imp_drh);
#define dbd_st_prepare_sv pg_st_prepare_sv
int dbd_st_prepare_sv (SV * sth, imp_sth_t * imp_sth, SV * statement_sv, SV * attribs);
#define dbd_bind_ph pg_bind_ph
int dbd_bind_ph (SV * sth, imp_sth_t * imp_sth, SV * ph_name, SV * newvalue, IV sql_type, SV * attribs, int is_inout, IV maxlen);
#define dbd_st_execute pg_st_execute
int dbd_st_execute (SV * sth, imp_sth_t * imp_sth);
#define dbd_st_fetch pg_st_fetch
AV * dbd_st_fetch (SV * sth, imp_sth_t * imp_sth);
#define dbd_st_rows pg_st_rows
int dbd_st_rows (SV * sth, imp_sth_t * imp_sth);
#define dbd_st_finish pg_st_finish
int dbd_st_finish (SV * sth, imp_sth_t * imp_sth);
#define dbd_st_cancel pg_st_cancel
int dbd_st_cancel (SV * sth, imp_sth_t * imp_sth);
#define dbd_st_destroy pg_st_destroy
void dbd_st_destroy (SV * sth, imp_sth_t * imp_sth);
#define dbd_st_blob_read pg_st_blob_read
int dbd_st_blob_read (SV * sth, imp_sth_t * imp_sth, int lobjId, long offset, long len, SV * destrv, long destoffset);
/*
Everything else should map back to the DBI version, or be handled by Pg.pm
TODO: Explicitly map out each one.
*/
/* Custom PG functions, in order they appear in dbdimp.c */
int pg_db_getfd (imp_dbh_t * imp_dbh);
SV * pg_db_pg_notifies (SV *dbh, imp_dbh_t *imp_dbh);
SV * pg_rightgraded_sv(pTHX_ SV *input, bool utf8);
SV * pg_stringify_array(SV * input, const char * array_delim, int server_version, bool utf8);
int pg_quickexec (SV *dbh, const char *sql, const int asyncflag);
int pg_db_putline (SV *dbh, SV *svbuf);
int pg_db_getline (SV *dbh, SV * svbuf, int length);
int pg_db_getcopydata (SV *dbh, SV * dataline, int async);
int pg_db_putcopydata (SV *dbh, SV * dataline);
int pg_db_putcopyend (SV * dbh);
int pg_db_endcopy (SV * dbh);
void pg_db_pg_server_trace (SV *dbh, FILE *fh);
void pg_db_pg_server_untrace (SV *dbh);
int pg_db_savepoint (SV *dbh, imp_dbh_t *imp_dbh, char * savepoint);
int pg_db_rollback_to (SV *dbh, imp_dbh_t *imp_dbh, const char * savepoint);
int pg_db_release (SV *dbh, imp_dbh_t *imp_dbh, char * savepoint);
unsigned int pg_db_lo_creat (SV *dbh, int mode);
int pg_db_lo_open (SV *dbh, unsigned int lobjId, int mode);
int pg_db_lo_close (SV *dbh, int fd);
int pg_db_lo_read (SV *dbh, int fd, char *buf, size_t len);
int pg_db_lo_write (SV *dbh, int fd, char *buf, size_t len);
int pg_db_lo_lseek (SV *dbh, int fd, int offset, int whence);
int pg_db_lo_tell (SV *dbh, int fd);
int pg_db_lo_truncate (SV *dbh, int fd, size_t len);
int pg_db_lo_unlink (SV *dbh, unsigned int lobjId);
unsigned int pg_db_lo_import (SV *dbh, char *filename);
unsigned int pg_db_lo_import_with_oid (SV *dbh, char *filename, unsigned int lobjId);
int pg_db_lo_export (SV *dbh, unsigned int lobjId, char *filename);
int pg_db_result (SV *h, imp_dbh_t *imp_dbh);
int pg_db_ready(SV *h, imp_dbh_t *imp_dbh);
int pg_db_cancel (SV *h, imp_dbh_t *imp_dbh);
int pg_db_cancel_sth (SV *sth, imp_sth_t *imp_sth);
/* end of dbdimp.h */