-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbdimp.cpp
447 lines (342 loc) · 9.61 KB
/
dbdimp.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* (C) Copyright NuoDB, Inc. 2012-2018
* Redistribution and use permitted under the terms of the 3-clause BSD license.
*/
#include "dbdimp.h"
DBISTATE_DECLARE;
void dbd_init(dbistate_t* dbistate)
{
DBISTATE_INIT; /* Initialize the DBI macros */
DBIS = dbistate;
}
int dbd_db_login6_sv(SV *dbh, imp_dbh_t *imp_dbh, SV *dbname, SV *uid, SV *pwd, SV *attr)
{
SV* sv;
HV* hv;
char* key;
STRLEN len;
I32 cnt, i;
// Retrieve the private attributes that are stashed in imp_dbh by
// DBI::NuoDB::connect
sv = DBIc_IMP_DATA(imp_dbh);
if (!sv || !SvROK(sv)) {
return FALSE;
}
hv = (HV*) SvRV(sv);
if (SvTYPE(hv) != SVt_PVHV)
return FALSE;
NuoDB::Connection *conn = NuoDB_createConnection();
if (!conn)
return FALSE;
NuoDB::Properties *properties = conn->allocProperties();
if (SvOK(uid))
properties->putValue("user", SvPV_nolen(uid));
if (SvOK(pwd))
properties->putValue("password", SvPV_nolen(pwd));
cnt = hv_iterinit(hv);
for (i = 1; i <= cnt; i++) {
sv = hv_iternextsv(hv, &key, (I32*) &len);
properties->putValue(key, SvPV(sv, len));
}
try {
conn->openDatabase(SvPV_nolen(dbname), properties);
imp_dbh->conn = conn;
DBIc_ACTIVE_on(imp_dbh);
DBIc_IMPSET_on(imp_dbh);
} catch (NuoDB::SQLException& xcp) {
do_error(dbh, xcp.getSqlcode(), xcp.getText());
conn->close();
imp_dbh->conn = NULL;
return FALSE;
}
return TRUE;
}
int dbd_st_prepare_sv(SV *sth, imp_sth_t *imp_sth, SV *statement, SV *attribs)
{
D_imp_dbh_from_sth;
if (!imp_dbh->conn) {
do_error(sth, -1, "Connection is not available.");
return FALSE;
}
char *sql = SvPV_nolen(statement);
try {
imp_sth->pstmt = imp_dbh->conn->prepareStatement(sql);
DBIc_IMPSET_on(imp_sth);
NuoDB::ParameterMetaData* md = imp_sth->pstmt->getParameterMetaData();
DBIc_NUM_PARAMS(imp_sth) = md->getParameterCount();
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
int dbd_st_execute(SV* sth, imp_sth_t* imp_sth)
{
D_imp_dbh_from_sth;
if (!imp_dbh->conn) {
do_error(sth, -1, "Connection is not available.");
return FALSE;
}
if (!imp_sth->pstmt) {
do_error(sth, -1, "Statement was not prepared.");
return FALSE;
}
try {
DBIc_ACTIVE_off(imp_sth);
imp_sth->rs = NULL;
if (imp_sth->pstmt->execute()) {
imp_sth->rs = imp_sth->pstmt->getResultSet();
} else {
imp_sth->rs = imp_sth->pstmt->getGeneratedKeys();
}
if (!imp_sth->rs)
return FALSE;
NuoDB::ResultSetMetaData *md = imp_sth->rs->getMetaData();
DBIc_NUM_FIELDS(imp_sth) = md->getColumnCount();
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
AV* dbd_st_fetch(SV *sth, imp_sth_t* imp_sth)
{
AV* av;
int i;
D_imp_dbh_from_sth;
if (!imp_dbh->conn) {
do_error(sth, -1, "Connection is not available.");
return Nullav;
}
NuoDB::ResultSet *rs = imp_sth->rs;
if (!rs) {
return Nullav;
}
int numFields = DBIc_NUM_FIELDS(imp_sth);
try {
if (!rs->next()) {
DBIc_ACTIVE_off(imp_sth);
return Nullav;
}
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return Nullav;
}
av = DBIc_DBISTATE(imp_sth)->get_fbav(imp_sth);
for (i = 0; i < numFields; i++) {
SV *sv = AvARRAY(av)[i];
const char * str = rs->getString(i + 1);
if (rs->wasNull()) {
(void) SvOK_off(sv);
} else {
sv_setpvn(sv, str, strlen(str));
sv_utf8_decode(sv);
}
}
return av;
}
void dbd_st_destroy(SV *sth, imp_sth_t *imp_sth)
{
D_imp_dbh_from_sth;
try {
if (imp_dbh->conn && imp_sth->rs)
imp_sth->rs->close();
if (imp_dbh->conn && imp_sth->pstmt)
imp_sth->pstmt->close();
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
}
DBIc_IMPSET_off(imp_sth);
}
int dbd_st_finish(SV* sth, imp_sth_t* imp_sth)
{
return TRUE;
}
const char * dbd_st_analyze(SV *sth)
{
D_imp_sth(sth);
if (!imp_sth->pstmt) {
do_error(sth, -1, "Statement was not prepared.");
return NULL;
}
// 2 = RemPreparedStatement::AnalyzeTree but RemPreparedStatement.h depends on Platform/LinkedList.h , so can not be included
try {
return imp_sth->pstmt->analyze(2);
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return NULL;
}
}
int dbd_db_commit(SV* dbh, imp_dbh_t* imp_dbh)
{
if (!imp_dbh->conn) {
do_error(dbh, -1, "Connection is not available.");
return FALSE;
}
try {
imp_dbh->conn->commit();
} catch (NuoDB::SQLException& xcp) {
do_error(dbh, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
int dbd_db_rollback(SV* dbh, imp_dbh_t* imp_dbh)
{
if (!imp_dbh->conn) {
do_error(dbh, -1, "Connection is not available.");
return FALSE;
}
try {
imp_dbh->conn->rollback();
} catch (NuoDB::SQLException& xcp) {
do_error(dbh, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
SV* dbd_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv)
{
STRLEN kl;
char *key = SvPV(keysv, kl);
if (kl==10 && strEQ(key, "AutoCommit")) {
return sv_2mortal(boolSV(DBIc_has(imp_dbh, DBIcf_AutoCommit)));
} else {
return Nullsv;
}
}
int dbd_db_STORE_attrib(SV* dbh, imp_dbh_t* imp_dbh, SV* keysv, SV* valuesv)
{
STRLEN kl;
char *key = SvPV(keysv, kl);
bool bool_value = SvTRUE(valuesv);
if (kl==10 && strEQ(key, "AutoCommit")) {
if (!imp_dbh->conn) {
do_error(dbh, -1, "Connection is not available.");
// We need to set valuesv to -900 in order to prevent
// a croak() in DBI.xs:2168
sv_setiv(valuesv, -900);
return FALSE;
}
try {
imp_dbh->conn->setAutoCommit(bool_value);
DBIc_set(imp_dbh, DBIcf_AutoCommit, bool_value);
} catch (NuoDB::SQLException& xcp) {
do_error(dbh, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
} else {
return FALSE;
}
return TRUE;
}
SV* dbd_st_FETCH_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv)
{
return Nullsv;
}
int dbd_st_STORE_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv, SV *valuesv)
{
return FALSE;
}
int dbd_st_blob_read(SV *sth, imp_sth_t *imp_sth, int field, long offset, long len, SV *destrv, long destoffset)
{
return FALSE;
}
int dbd_db_disconnect(SV* dbh, imp_dbh_t* imp_dbh)
{
if (!imp_dbh->conn)
return FALSE;
try {
imp_dbh->conn->close();
imp_dbh->conn = NULL;
} catch (NuoDB::SQLException& xcp) {
do_error(dbh, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
int dbd_bind_ph(SV *sth, imp_sth_t *imp_sth, SV *param, SV *value, IV sql_type, SV *attribs, int is_inout, IV maxlen)
{
STRLEN value_len;
if (is_inout)
croak("Can't bind ``lvalue'' mode.");
if (!imp_sth)
return FALSE;
if (!imp_sth->pstmt)
return FALSE;
char * value_str = SvPV(value, value_len);
try {
if (memchr(value_str, 0, value_len)) {
//NuoDB::Blob* blob = imp_dbh->conn->createBlob();
//blob->setBytes(value_len, (const unsigned char*) value_str);
//imp_sth->pstmt->setBlob(SvIV(param), blob);
//blob->release();
imp_sth->pstmt->setBytes(SvIV(param), value_len, value_str);
} else {
imp_sth->pstmt->setString(SvIV(param), value_str);
}
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
int dbd_st_add_batch(SV *sth)
{
D_imp_sth(sth);
if (!imp_sth)
return FALSE;
if (!imp_sth->pstmt)
return FALSE;
try {
imp_sth->pstmt->addBatch();
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
int dbd_st_execute_batch(SV *sth)
{
D_imp_sth(sth);
if (!imp_sth)
return FALSE;
if (!imp_sth->pstmt)
return FALSE;
try {
imp_sth->pstmt->executeBatch();
} catch (NuoDB::SQLException& xcp) {
do_error(sth, xcp.getSqlcode(), xcp.getText());
return FALSE;
}
return TRUE;
}
void dbd_db_destroy(SV* dbh, imp_dbh_t* imp_dbh)
{
imp_dbh->conn = NULL;
DBIc_IMPSET_off(imp_dbh);
}
const char * dbd_db_version(SV *dbh)
{
D_imp_dbh(dbh);
if (!imp_dbh->conn) {
do_error(dbh, -1, "Database not connected.");
return NULL;
}
NuoDB::DatabaseMetaData *metaData = imp_dbh->conn->getMetaData();
try {
return metaData->getDatabaseProductVersion();
} catch (NuoDB::SQLException& xcp) {
do_error(dbh, xcp.getSqlcode(), xcp.getText());
return NULL;
}
return NULL;
}
void do_error(SV* h, int rc, const char* what)
{
D_imp_xxh(h);
sv_setiv(DBIc_ERR(imp_xxh), (IV)rc);
SV *errstr = DBIc_ERRSTR(imp_xxh);
SvUTF8_on(errstr);
sv_setpv(errstr, what);
sv_utf8_decode(errstr);
}