forked from benddailey/pecl-pdo-4d
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4d_statement.c
517 lines (465 loc) · 14.4 KB
/
4d_statement.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
+----------------------------------------------------------------------+
| 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: $ */
#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 "php_pdo_4d_int.h"
static int pdo_4d_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_4d_stmt *S;
pdo_4d_db_handle *H;
FOURD_LONG8 row_count;
S = (pdo_4d_stmt*)stmt->driver_data;
H = S->H;
if (S->result) {
fourd_free_result(S->result);
S->result = NULL;
}
if(S->state==NULL) { /* if statement has not prepared */
if ((S->result=fourd_query(H->server, stmt->active_query_string)) ==NULL) {
pdo_4d_error_stmt(stmt);
return 0;
}
}
else { /* if statement has prepared */
if ((S->result=fourd_exec_statement(S->state)) ==NULL) {
pdo_4d_error_stmt(stmt);
return 0;
}
}
if ((row_count = fourd_affected_rows(H->server)) == (FOURD_LONG8)-1) {
stmt->row_count = fourd_num_rows(S->result);
stmt->column_count = fourd_num_columns(S->result);
}
else {
/* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
stmt->row_count = row_count;
}
return 1;
}
static int pdo_4d_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
struct pdo_column_data *cols = stmt->columns;
unsigned int i;
if (!S->result) {
return 0;
}
if (colno >= stmt->column_count) {
/* error invalid column */
return 0;
}
/* fetch all on demand, this seems easiest
** if we've been here before bail out
*/
if (cols[0].name) {
return 1;
}
for (i=0; i < stmt->column_count; i++) {
int namelen;
const char *name=fourd_get_column_name(S->result,i);
namelen = strlen(name);
cols[i].precision = 0;
cols[i].maxlen = namelen;/*namelen;*/
#if PHP_VERSION_ID >= 70000
cols[i].name = zend_string_init(name, namelen, 0);
#else
cols[i].namelen = namelen;
cols[i].name = estrndup(name, namelen);
#endif
cols[i].param_type = PDO_PARAM_STR;
//cols[i].param_type = PDO_PARAM_STR;
//if(i==1) cols[i].param_type = PDO_PARAM_LOB;
}
return 1;
}
static int pdo_4d_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
if (!S->result) {
return 0;
}
if (colno >= stmt->column_count) {
/* error invalid column */
return 0;
}
fourd_field_to_string(S->result,colno,ptr,len);
switch(fourd_get_column_type(S->result,colno))
{
case VK_STRING:
/* convert into desired charset */
//*ptr=php_mb_convert_encoding(*ptr, *len,S->charset,FOURD_CHARSET_SERVEUR,len TSRMLS_CC);
break;
case VK_BLOB:
case VK_IMAGE:
/*use emalloc for memory allocation and free memory allocate by malloc */
{
char *lptr=NULL;
FOURD_BLOB *b=NULL;
b=fourd_field(S->result,colno);
if(b!=NULL){
free(*ptr); /* fourd_get_column_type return "" for blob or image type resource*/
*ptr=emalloc(b->length);
*len=b->length;
memcpy(*ptr,b->data,b->length);
}
else {
*ptr=NULL;
*len=0;
}
}
break;
default:
/* convert into desired charset from "ISO-8859-1" for not VK_STRING,VK_BLOB or VK_IMAGE data */
//*ptr=php_mb_convert_encoding(*ptr, *len,S->charset,"ISO-8859-1",len TSRMLS_CC);
break;
}
return 1;
}
static int pdo_4d_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
size_t len;
//char* test = php_mb_convert_encoding("TEST", 4, "UTF-8", "UTF-16LE", len);
if (!S->result) {
strcpy(stmt->error_code, "HY000");
return 0;
}
if (!fourd_next_row(S->result)) {
if (fourd_errno(S->H->server)>0) {
pdo_4d_error_stmt(stmt);
}
return 0;
}
//S->current_lengths = fourd_fetch_lengths(S->result);
return 1;
}
static int pdo_4d_stmt_set_attribute(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
pdo_4d_db_handle *H = S->H;
switch (attr) {
case PDO_FOURD_ATTR_CHARSET:
S->charset = pestrdup(Z_STRVAL_P(val),1);
return 1;
default:
return 0;
}
}
static int pdo_4d_stmt_get_attribute(pdo_stmt_t *stmt, long attr, zval *return_value TSRMLS_DC)
{
//pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
switch (attr) {
case PDO_FOURD_ATTR_CHARSET:
#if PHP_VERSION_ID >= 70000
ZVAL_STRING(return_value, S->charset);
#else
ZVAL_STRING(return_value, S->charset, 1);
#endif
break;
default:
return 0;
}
return 1;
}
static int pdo_4d_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
//MYSQL_FIELD *F;
#if PHP_VERSION_ID >= 70000
zval flags;
#else
zval *flags;
#endif
char *str;
FOURD_TYPE type;
if (!S->result) {
return FAILURE;
}
if (colno >= stmt->column_count) {
/* error invalid column */
return FAILURE;
}
array_init(return_value);
#if PHP_VERSION_ID < 70000
MAKE_STD_ZVAL(flags);
array_init(flags);
#else
array_init(&flags);
#endif
switch(type=fourd_get_column_type(S->result,colno))
{
case VK_BLOB:
case VK_IMAGE:
#if PHP_VERSION_ID >= 70000
add_next_index_string(&flags, "blob");
#else
add_next_index_string(flags, "blob", 1);
#endif
break;
}
str=pestrdup(stringFromType(type),1);
if (str) {
#if PHP_VERSION_ID >= 70000
add_assoc_string(return_value, "native_type", str);
#else
add_assoc_string(return_value, "native_type", str, 1);
#endif
}
#if PHP_VERSION_ID >= 70000
add_assoc_zval(return_value, "flags", &flags);
#else
add_assoc_zval(return_value, "flags", flags);
#endif
return SUCCESS;
}
static int pdo_4d_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
if (S->result) {
fourd_close_statement(S->result);
fourd_free_result(S->result);
S->result = NULL;
}
return 1;
}
static int pdo_4d_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
if (S->result) {
/* free the resource */
fourd_close_statement(S->result);
fourd_free_result(S->result);
S->result = NULL;
}
if (S->einfo.errmsg) {
pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
S->einfo.errmsg = NULL;
}
efree(S);
return 1;
}
static int pdo_4d_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
enum pdo_param_event event_type TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
if(S->state == NULL) { /* it's not a prepared statement */
return 1;
}
switch (event_type) {
case PDO_PARAM_EVT_NORMALIZE:
/* if (param->name) {
if (param->name[0] == '$') {
param->paramno = atoi(param->name + 1);
} else {
char *nameptr;
if (stmt->bound_param_map && SUCCESS == zend_hash_find(stmt->bound_param_map,
param->name, param->namelen + 1, (void**)&nameptr)) {
param->paramno = atoi(nameptr + 1) - 1;
} else {
fprintf(stderr,"Error HY093\n");
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
return 0;
}
}
}*/
/*printf("bind param:%d\n",param->paramno);*/
if (param->paramno < 0 ) {
strcpy(stmt->error_code, "HY093");
//pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
#if PHP_VERSION_ID >= 70000
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name->val TSRMLS_CC);
#else
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
#endif
return 0;
}
return 1;
break;
case PDO_PARAM_EVT_EXEC_PRE:
/* printf("bind_param: %d,%d\n",PDO_PARAM_TYPE(param->param_type),Z_TYPE_P(param->parameter)); */
if (param->paramno < 0 ) {
strcpy(stmt->error_code, "HY093");
#if PHP_VERSION_ID >= 70000
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name->val TSRMLS_CC);
#else
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
#endif
return 0;
}
if (param->is_param) {
switch (PDO_PARAM_TYPE(param->param_type)) {
case PDO_PARAM_STMT:
return 0;
case PDO_PARAM_NULL:
fourd_bind_param(S->state,param->paramno,VK_STRING, NULL);
return 1;
case PDO_PARAM_INT:
{
#if PHP_VERSION_ID >= 70000
FOURD_LONG val=Z_LVAL(param->parameter);
#else
FOURD_LONG val=Z_LVAL_P(param->parameter);
#endif
fourd_bind_param(S->state,param->paramno,VK_LONG, &val);
}
return 1;
case PDO_PARAM_LOB:
#if PHP_VERSION_ID >= 70000
if (Z_TYPE(param->parameter) == IS_RESOURCE) {
#else
if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
#endif
php_stream *stm;
php_stream_from_zval_no_verify(stm, ¶m->parameter);
if (stm) {
SEPARATE_ZVAL(¶m->parameter);
#if PHP_VERSION_ID >= 70000
param->parameter.u1.v.type = IS_STRING;
Z_STRLEN(param->parameter) = php_stream_copy_to_mem(stm,
PHP_STREAM_COPY_ALL, 0)->len;
#else
Z_TYPE_P(param->parameter) = IS_STRING;
Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
&Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
#endif
} else {
pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
return 0;
}
{
FOURD_BLOB str;
#if PHP_VERSION_ID >= 70000
str.length=Z_STRLEN(param->parameter);
str.data=Z_STRVAL(param->parameter);
#else
str.length=Z_STRLEN_P(param->parameter);
str.data=Z_STRVAL_P(param->parameter);
#endif
fourd_bind_param(S->state,param->paramno,VK_BLOB, &str);
}
return 1;
}
/* fall through */
case PDO_PARAM_STR:
default:
#if PHP_VERSION_ID >= 70000
switch (Z_TYPE(param->parameter)) {
#else
switch (Z_TYPE_P(param->parameter)) {
#endif
case IS_NULL:
fourd_bind_param(S->state,param->paramno,VK_STRING, NULL);
return 1;
case IS_LONG:
{
#if PHP_VERSION_ID >= 70000
FOURD_LONG val=Z_LVAL(param->parameter);
#else
FOURD_LONG val=Z_LVAL_P(param->parameter);
#endif
fourd_bind_param(S->state,param->paramno,VK_LONG, &val);
}
return 1;
case IS_DOUBLE:
#if PHP_VERSION_ID >= 70000
fourd_bind_param(S->state,param->paramno,VK_REAL, &Z_DVAL(param->parameter));
#else
fourd_bind_param(S->state,param->paramno,VK_REAL, &Z_DVAL_P(param->parameter));
#endif
return 1;
case IS_STRING:
{
FOURD_STRING str;
size_t len=0;
/* MBSTRING_API char * php_mb_convert_encoding(char *input, size_t length, char *_to_encoding, char *_from_encodings, size_t *output_len TSRMLS_DC) */
#if PHP_VERSION_ID >= 70000
//char* val=php_mb_convert_encoding(Z_STRVAL(param->parameter), Z_STRLEN(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
char* val = Z_STRVAL(param->parameter);
len = Z_STRLEN(param->parameter);
#else
char* val=php_mb_convert_encoding(Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
#endif
str.length=len/2;
str.data=val;
fourd_bind_param(S->state,param->paramno,VK_STRING, &str);
}
return 1;
default:
{
FOURD_STRING str;
size_t len=0;
char* val;
size_t test_length = 0;
char* test_value;
#if PHP_VERSION_ID >= 70000
convert_to_string(¶m->parameter);
test_value = Z_STRVAL(param->parameter);
test_length = Z_STRLEN(param->parameter);
val = test_value;
len = Z_STRLEN(param->parameter);
//val = php_mb_convert_encoding(test_value, test_length,FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
//PHPWRITE(Z_STRVAL(param->parameter), Z_STRLEN(param->parameter));
#else
convert_to_string(param->parameter);
val=php_mb_convert_encoding(Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
#endif
str.length = len/2;
str.data = val;
fourd_bind_param(S->state,param->paramno,VK_STRING, &str);
}
return 1;
}
}
}
break;
default:
;
}
return 1;
}
struct pdo_stmt_methods fourd_stmt_methods = {
pdo_4d_stmt_dtor,
pdo_4d_stmt_execute,
pdo_4d_stmt_fetch,
pdo_4d_stmt_describe,
pdo_4d_stmt_get_col,
pdo_4d_stmt_param_hook,
pdo_4d_stmt_set_attribute,
pdo_4d_stmt_get_attribute,
pdo_4d_stmt_col_meta,
NULL,
pdo_4d_stmt_cursor_closer,
};