-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path4d_statement.c
403 lines (377 loc) · 11.5 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
/*
+----------------------------------------------------------------------+
| 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 = 0;/*namelen;*/
cols[i].namelen = namelen;
cols[i].name = estrndup(name, namelen);
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;
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:
ZVAL_STRING(return_value, S->charset, 1);
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;
zval *flags;
char *str;
FOURD_TYPE type;
if (!S->result) {
return FAILURE;
}
if (colno >= stmt->column_count) {
/* error invalid column */
return FAILURE;
}
array_init(return_value);
MAKE_STD_ZVAL(flags);
array_init(flags);
switch(type=fourd_get_column_type(S->result,colno))
{
case VK_BLOB:
case VK_IMAGE:
add_next_index_string(flags, "blob", 1);
break;
}
str=pestrdup(stringFromType(type),1);
if (str) {
add_assoc_string(return_value, "native_type", str, 1);
}
add_assoc_zval(return_value, "flags", flags);
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);
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");
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
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:
{
FOURD_LONG val=Z_LVAL_P(param->parameter);
fourd_bind_param(S->state,param->paramno,VK_LONG, &val);
}
return 1;
case PDO_PARAM_LOB:
if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
php_stream *stm;
php_stream_from_zval_no_verify(stm, ¶m->parameter);
if (stm) {
SEPARATE_ZVAL(¶m->parameter);
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);
} else {
pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
return 0;
}
{
FOURD_BLOB str;
int len=0;
str.length=Z_STRLEN_P(param->parameter);
str.data=Z_STRVAL_P(param->parameter);
fourd_bind_param(S->state,param->paramno,VK_BLOB, &str);
}
return 1;
}
/* fall through */
case PDO_PARAM_STR:
default:
switch (Z_TYPE_P(param->parameter)) {
case IS_NULL:
fourd_bind_param(S->state,param->paramno,VK_STRING, NULL);
return 1;
case IS_LONG:
{
FOURD_LONG val=Z_LVAL_P(param->parameter);
fourd_bind_param(S->state,param->paramno,VK_LONG, &val);
}
return 1;
case IS_DOUBLE:
fourd_bind_param(S->state,param->paramno,VK_REAL, &Z_DVAL_P(param->parameter));
return 1;
case IS_STRING:
{
FOURD_STRING str;
int 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) */
char* val=php_mb_convert_encoding(Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
str.length=len/2;
str.data=val;
fourd_bind_param(S->state,param->paramno,VK_STRING, &str);
}
return 1;
default:
{
FOURD_STRING str;
int len=0;
char* val=NULL;
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);
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,
};