forked from eeeewwqq/pecl-pdo-4d
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4d_driver.c
400 lines (341 loc) · 11 KB
/
4d_driver.c
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
/*
+----------------------------------------------------------------------+
| PECL :: PDO_4D |
+----------------------------------------------------------------------+
| Copyright (c) 2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
| Contributed by: 4D <[email protected]>, http://www.4d.com |
| Alter Way, http://www.alterway.fr |
| Authors: Stephane Planquart <[email protected]> |
| Alexandre Morgaut <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $ Id: $ */
//#if HAVE_PDO_4D
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "pdo/php_pdo.h"
#include "pdo/php_pdo_driver.h"
#include "php_pdo_4d.h"
#include <zend_exceptions.h>
#include "php_pdo_4d_int.h"
int _pdo_4d_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line) /* {{{ */
{
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
pdo_error_type *pdo_err;
pdo_4d_error_info *einfo;
pdo_4d_stmt *S = NULL;
if (stmt) {
S = (pdo_4d_stmt*)stmt->driver_data;
pdo_err = &stmt->error_code;
einfo = &S->einfo;
} else {
pdo_err = &dbh->error_code;
einfo = &H->einfo;
}
//pdo_err = &dbh->error_code;
//einfo = &H->einfo;
einfo->errcode = fourd_errno(H->server);
einfo->file = file;
einfo->line = line;
/* free memory of last error message */
if (einfo->errmsg) {
pefree(einfo->errmsg, dbh->is_persistent);
einfo->errmsg = NULL;
}
strlcpy(*pdo_err, fourd_sqlstate(H->server), 6);
/* if error_code is not null => get error message */
/* printf("einfo->errcode:%d\n",einfo->errcode); */
if (einfo->errcode) {
/* printf("copy errmsg\n"); */
einfo->errmsg = pestrdup(fourd_error(H->server), dbh->is_persistent);
}
else {
strlcpy(*pdo_err, PDO_ERR_NONE, 6);
return 0;
}
if (!dbh->methods) {
zend_throw_exception_ex(php_pdo_get_exception(), 0, "SQLSTATE[%s] [%d] %s",
*pdo_err, einfo->errcode, einfo->errmsg);
}
return einfo->errcode;
}
static void pdo_4d_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info )
{
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
pdo_4d_error_info *einfo = &H->einfo;
if (stmt) {
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
einfo = &S->einfo;
} else {
einfo = &H->einfo;
}
if (einfo->errcode) {
add_next_index_long(info, einfo->errcode);
add_next_index_string(info, einfo->errmsg);
}
return;
}
static void fourd_handle_closer(pdo_dbh_t *dbh ) /* {{{ */
{
if (!dbh || !dbh->driver_data) {
return;
}
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
if (H) {
if (H->server) {
fourd_close(H->server);
fourd_free(H->server);
H->server = NULL;
}
if (H->einfo.errmsg) {
pefree(H->einfo.errmsg, dbh->is_persistent);
H->einfo.errmsg = NULL;
}
pefree(H, dbh->is_persistent);
dbh->driver_data = NULL;
}
return;
}
static long fourd_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
{
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
if (fourd_exec(H->server, ZSTR_VAL(sql))) {
pdo_4d_error(dbh);
return -1;
} else {
FOURD_LONG8 c = fourd_affected_rows(H->server);
if (c == (FOURD_LONG8) -1) {
pdo_4d_error(dbh);
return (H->einfo.errcode ? -1 : 0);
} else {
return c;
}
}
}
static bool fourd_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
pdo_4d_stmt *S = ecalloc(1, sizeof(pdo_4d_stmt));
zend_string *nsql = NULL;
int ret;
S->H = H;
stmt->driver_data = S;
stmt->methods = &fourd_stmt_methods;
S->charset = H->charset;
/* prepare statement */
stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
ret = pdo_parse_params(stmt, sql, &nsql);
if (ret == 1) {
/* query was rewritten */
sql = nsql;
} else if (ret == -1) {
/* failed to parse */
strcpy(dbh->error_code, dbh->error_code);
return false;
}
if ((S->state = fourd_prepare_statement(H->server, ZSTR_VAL(sql))) == NULL) {
if (nsql) {
zend_string_release(nsql);
}
pdo_4d_error(dbh);
/*printf("Error sur prepare (%d):%s\n",fourd_errno(cnx),fourd_error(cnx));*/
}
if (nsql) {
zend_string_release(nsql);
}
return true;
/* end of prepare statement */
end:
stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
return 1;
}
static bool pdo_4d_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
{
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
switch (attr) {
case PDO_FOURD_ATTR_CHARSET:
H->charset = pestrdup(Z_STRVAL_P(val), dbh->is_persistent);
return true;
case PDO_FOURD_ATTR_PREFERRED_IMAGE_TYPES:
fourd_set_preferred_image_types(H->server, Z_STRVAL_P(val));
return true;
default:
return false;
}
}
static bool fourd_handle_begin(pdo_dbh_t *dbh )
{
return 0 <= fourd_handle_doer(dbh, zend_string_init("BEGIN", sizeof("BEGIN")-1, 0));
}
static bool fourd_handle_commit(pdo_dbh_t *dbh )
{
return 0 <= fourd_handle_doer(dbh, zend_string_init("COMMIT", sizeof("COMMIT")-1, 0));
}
static bool fourd_handle_rollback(pdo_dbh_t *dbh )
{
return 0 <= fourd_handle_doer(dbh, zend_string_init("ROLLBACK", sizeof("ROLLBACK")-1, 0));
}
static int pdo_4d_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value)
{
pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
switch (attr) {
case PDO_FOURD_ATTR_CHARSET:
ZVAL_STRING(return_value, H->charset);
break;
case PDO_FOURD_ATTR_PREFERRED_IMAGE_TYPES:
ZVAL_STRING(return_value, fourd_get_preferred_image_types(H->server));
break;
default:
return 0;
}
return 1;
}
static zend_string* fourd_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
{
int qcount = 0;
char *c;
zend_string *temp_str, *quoted;
if (!ZSTR_LEN(unquoted)) {
quoted = zend_string_init("''", 2, 0);
return quoted;
}
/* count single quotes */
for (c = (char *)ZSTR_VAL(unquoted); (c = strchr(c, '\'')); qcount++, c++)
; /* empty loop */
int quotedlen = ZSTR_LEN(unquoted) + qcount + 2;
quoted = zend_string_alloc(quotedlen, 0);
c = ZSTR_VAL(quoted);
*c++ = '\'';
/* foreach (chunk that ends in a quote) */
char *l = (char *)ZSTR_VAL(unquoted), *r;
while ((r = strchr(l, '\''))) {
temp_str = zend_string_init(l, r - l + 1, 0);
memcpy(c, ZSTR_VAL(temp_str), ZSTR_LEN(temp_str));
c += ZSTR_LEN(temp_str);
*c++ = '\''; /* add second quote */
zend_string_release(temp_str);
l = r + 1;
}
/* Copy remainder and add enclosing quote */
temp_str = zend_string_init(l, ZSTR_LEN(unquoted) - (l - ZSTR_VAL(unquoted)), 0);
memcpy(c, ZSTR_VAL(temp_str), ZSTR_LEN(temp_str));
zend_string_release(temp_str);
ZSTR_VAL(quoted)[quotedlen - 1] = '\'';
ZSTR_VAL(quoted)[quotedlen] = '\0';
return quoted;
}
/* }}} */
static struct pdo_dbh_methods fourd_methods = {
fourd_handle_closer, //mysql_handle_closer,
fourd_handle_preparer, //mysql_handle_preparer,
fourd_handle_doer, //mysql_handle_doer,
fourd_handle_quoter, //mysql_handle_quoter,
fourd_handle_begin, //mysql_handle_begin,
fourd_handle_commit, //mysql_handle_commit,
fourd_handle_rollback, //mysql_handle_rollback,
pdo_4d_set_attribute, //pdo_mysql_set_attribute,
NULL, //pdo_mysql_last_insert_id,
pdo_4d_fetch_error_func, //pdo_mysql_fetch_error_func,
pdo_4d_get_attribute, //pdo_mysql_get_attribute,
NULL //pdo_mysql_check_liveness
};
static int pdo_4d_handle_factory(pdo_dbh_t *dbh, zval *driver_options )
{
pdo_4d_db_handle *H;
char *host = NULL;
unsigned int port = 19812;
char *dbname = NULL;
char *charset = NULL;
char *user=NULL;
char *pwd=NULL;
int timeout=0;
char *preferred_image_types;
struct pdo_data_src_parser vars[] = {
{ "host", "localhost", 0 },
{ "port", "19812", 0 },
{ "dbname", "", 0 },
{ "charset", "UTF-16LE", 0 },
{ "user", "", 0 },
{ "password", "", 0 },
};
php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars,6);
H=pecalloc(1, sizeof(pdo_4d_db_handle), dbh->is_persistent);
H->einfo.errcode = 0;
H->einfo.errmsg = NULL;
if((H->server=fourd_init())==NULL)
{
pdo_4d_error(dbh);
//think to code free method
dbh->methods = &fourd_methods;
return 0;
}
/*set timeout */
timeout=INI_INT("pdo_4d.timeout");
if(timeout<=0) {
timeout=30;/*30 is timeout by default */
}
fourd_timeout(H->server,timeout);
/*set prefered image types */
preferred_image_types=INI_STR("pdo_4d.preferred_image_types");
if(preferred_image_types!=NULL && strlen(preferred_image_types)){
fourd_set_preferred_image_types(H->server,preferred_image_types);
}
dbh->driver_data = H;
H->max_buffer_size = 1024*1024;
H->buffered = H->emulate_prepare = 1; //review this one
/* get user password from php.ini if is null */
if(dbh->username==NULL) {
user=vars[4].optval;
}else{
user=dbh->username;
}
if(dbh->password==NULL){
pwd=vars[5].optval;
}else{
pwd=dbh->password;
}
//parce connection attribut
host = vars[0].optval;
if(vars[1].optval) {
port = atoi(vars[1].optval);
}
dbname = vars[2].optval;
charset = vars[3].optval;
H->charset=charset;
//connection
if (fourd_connect(H->server, host, user, pwd, dbname, port)) // Returns 1 for an error
{
pdo_4d_error(dbh);
dbh->methods = &fourd_methods;
return 0;
}
H->attached = 1;
dbh->alloc_own_columns = 1;
dbh->max_escaped_char_length = 2;
dbh->methods = &fourd_methods;
return 1;
}
pdo_driver_t pdo_4d_driver = {
PDO_DRIVER_HEADER(4D),
pdo_4d_handle_factory
};
//#endif /* HAVE_PDO_4D */