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
|
diff -up php5.3-201105121030/ext/pdo_dblib/dblib_stmt.c.50755 php5.3-201105121030/ext/pdo_dblib/dblib_stmt.c
--- php5.3-201105121030/ext/pdo_dblib/dblib_stmt.c.50755 2011-03-17 14:35:50.000000000 +0100
+++ php5.3-201105121030/ext/pdo_dblib/dblib_stmt.c 2011-05-13 18:56:02.329918378 +0200
@@ -33,234 +33,105 @@
#include "php_pdo_dblib_int.h"
#include "zend_exceptions.h"
-static void free_rows(pdo_dblib_stmt *S TSRMLS_DC)
+static int dblib_dblib_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
{
- int i, j;
+ pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
+ pdo_dblib_db_handle *H = S->H;
+
+ /* Cancel any pending results */
+ dbcancel(H->link);
+
+ efree(stmt->columns);
+ stmt->columns = NULL;
- for (i = 0; i < S->nrows; i++) {
- for (j = 0; j < S->ncols; j++) {
- pdo_dblib_colval *val = &S->rows[i*S->ncols] + j;
- if (val->data) {
- efree(val->data);
- val->data = NULL;
- }
- }
- }
- efree(S->rows);
- S->rows = NULL;
- S->nrows = 0;
+ return 1;
}
static int pdo_dblib_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
- if (S->rows) {
- free_rows(S TSRMLS_CC);
- }
- if (S->cols) {
- efree(S->cols);
- }
- efree(S);
+ dblib_dblib_stmt_cursor_closer(stmt TSRMLS_CC);
+ efree(S);
+
return 1;
}
-static int pdo_dblib_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
+static int pdo_dblib_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
pdo_dblib_db_handle *H = S->H;
- RETCODE resret, ret;
- int i, j;
- int arows;
- unsigned int size;
-
- dbsetuserdata(H->link, &S->err);
-
- if (S->rows) {
- /* clean them up */
- free_rows(S TSRMLS_CC);
+ RETCODE ret;
+
+ ret = dbresults(H->link);
+
+ if (ret == FAIL || ret == NO_MORE_RESULTS) {
+ return 0;
}
+
+ stmt->row_count = DBCOUNT(H->link);
+ stmt->column_count = dbnumcols(H->link);
+
+ return 1;
+}
+static int pdo_dblib_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
+{
+ pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
+ pdo_dblib_db_handle *H = S->H;
+ RETCODE ret;
+
+ dbsetuserdata(H->link, (BYTE*) &S->err);
+
if (FAIL == dbcmd(H->link, stmt->active_query_string)) {
return 0;
}
+
if (FAIL == dbsqlexec(H->link)) {
return 0;
}
- resret = dbresults(H->link);
- if (resret == FAIL) {
+ ret = pdo_dblib_stmt_next_rowset(stmt TSRMLS_CC);
+
+ if (ret == 0) {
return 0;
}
-
- ret = dbnextrow(H->link);
-
+
stmt->row_count = DBCOUNT(H->link);
-
- if (ret == NO_MORE_ROWS) {
- return 1;
- }
-
- if (!S->cols) {
- S->ncols = dbnumcols(H->link);
-
- if (S->ncols <= 0) {
- return 1;
- }
-
- S->cols = ecalloc(S->ncols, sizeof(pdo_dblib_col));
- stmt->column_count = S->ncols;
+ stmt->column_count = dbnumcols(H->link);
- for (i = 0, j = 0; i < S->ncols; i++) {
- char *tmp = NULL;
-
- S->cols[i].coltype = dbcoltype(H->link, i+1);
- S->cols[i].name = (char*)dbcolname(H->link, i+1);
-
- if (!strlen(S->cols[i].name)) {
- if (j) {
- spprintf(&tmp, 0, "computed%d", j++);
- strlcpy(S->cols[i].name, tmp, strlen(tmp)+1);
- efree(tmp);
- } else {
- S->cols[i].name = "computed";
- j++;
- }
- }
-
- S->cols[i].source = (char*)dbcolsource(H->link, i+1);
- tmp = estrdup(S->cols[i].source ? S->cols[i].source : "");
- S->cols[i].source = tmp;
- efree(tmp);
-
- S->cols[i].maxlen = dbcollen(H->link, i+1);
- }
- }
-
- arows = 100;
- size = S->ncols * sizeof(pdo_dblib_colval);
- S->rows = safe_emalloc(arows, size, 0);
-
- /* let's fetch all the data */
- do {
- if (S->nrows >= arows) {
- arows *= 2;
- S->rows = erealloc(S->rows, arows * size);
- }
- for (i = 0; i < S->ncols; i++) {
- pdo_dblib_colval *val = &S->rows[S->nrows * S->ncols + i];
-
- if (dbdatlen(H->link, i+1) == 0 && dbdata(H->link, i+1) == NULL) {
- val->len = 0;
- val->data = NULL;
- } else {
- switch (S->cols[i].coltype) {
- case SQLCHAR:
- case SQLTEXT:
- case SQLVARBINARY:
- case SQLBINARY:
- case SQLIMAGE:
- val->len = dbdatlen(H->link, i+1);
- val->data = emalloc(val->len + 1);
- memcpy(val->data, dbdata(H->link, i+1), val->len);
- val->data[val->len] = '\0';
- break;
- case SQLMONEY:
- case SQLMONEY4:
- case SQLMONEYN: {
- DBFLT8 money_value;
- dbconvert(NULL, S->cols[i].coltype, dbdata(H->link, i+1), dbdatlen(H->link, i+1), SQLFLT8, (LPBYTE)&money_value, 8);
- val->len = spprintf(&val->data, 0, "%.4f", money_value);
- }
- break;
-#ifdef SQLUNIQUE
- case SQLUNIQUE: {
-#else
- case 36: { /* FreeTDS hack, also used by ext/mssql */
-#endif
- val->len = 36+1;
- val->data = emalloc(val->len + 1);
-
- /* uniqueidentifier is a 16-byte binary number, convert to 32 char hex string */
-#ifdef SQLUNIQUE
- val->len = dbconvert(NULL, SQLUNIQUE, dbdata(H->link, i+1), dbdatlen(H->link, i+1), SQLCHAR, val->data, val->len);
-#else
- val->len = dbconvert(NULL, 36, dbdata(H->link, i+1), dbdatlen(H->link, i+1), SQLCHAR, val->data, val->len);
-#endif
- php_strtoupper(val->data, val->len);
- break;
- }
- default:
- if (dbwillconvert(S->cols[i].coltype, SQLCHAR)) {
- val->len = 32 + (2 * dbdatlen(H->link, i+1));
- val->data = emalloc(val->len);
-
- val->len = dbconvert(NULL, S->cols[i].coltype, dbdata(H->link, i+1),
- dbdatlen(H->link, i+1), SQLCHAR, val->data, val->len);
-
- if (val->len >= 0) {
- val->data[val->len] = '\0';
- }
- } else {
- val->len = 0;
- val->data = NULL;
- }
- }
- }
- }
-
- S->nrows++;
-
- ret = dbnextrow(H->link);
-
- if (ret == BUF_FULL) {
- dbclrbuf(H->link, DBLASTROW(H->link)-1);
- }
- } while (ret != FAIL && ret != NO_MORE_ROWS);
-
- if (resret != NO_MORE_RESULTS) {
- /* there are additional result sets available */
- dbresults(H->link);
- /* cancel pending rows */
- dbcanquery(H->link);
-
- /* TODO: figure out a sane solution */
- }
-
- S->current = -1;
-
- return 1;
+ return 1;
}
static int pdo_dblib_stmt_fetch(pdo_stmt_t *stmt,
enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
{
+
+ RETCODE ret;
+
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
-
- if (!S->rows) {
+ pdo_dblib_db_handle *H = S->H;
+
+ ret = dbnextrow(H->link);
+
+ if (ret == FAIL || ret == NO_MORE_ROWS) {
return 0;
}
- if (++S->current < S->nrows) {
- return 1;
- }
-
- return 0;
+ return 1;
}
static int pdo_dblib_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
{
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
+ pdo_dblib_db_handle *H = S->H;
+
struct pdo_column_data *col = &stmt->columns[colno];
-
- if (!S->rows) {
- return 0;
- }
-
- col->maxlen = S->cols[colno].maxlen;
- col->namelen = strlen(S->cols[colno].name);
- col->name = estrdup(S->cols[colno].name);
+
+ col->name = (char*)dbcolname(H->link, colno+1);
+ col->maxlen = dbcollen(H->link, colno+1);
+ col->namelen = strlen(col->name);
col->param_type = PDO_PARAM_STR;
return 1;
@@ -269,11 +140,76 @@ static int pdo_dblib_stmt_describe(pdo_s
static int pdo_dblib_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr,
unsigned long *len, int *caller_frees TSRMLS_DC)
{
+
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
- pdo_dblib_colval *val = &S->rows[S->current * S->ncols + colno];
+ pdo_dblib_db_handle *H = S->H;
+
+ int coltype;
+ unsigned int tmp_len;
+ char *tmp_ptr = NULL;
+
+ coltype = dbcoltype(H->link, colno+1);
+
+ *len = dbdatlen(H->link, colno+1);
+ *ptr = dbdata(H->link, colno+1);
+
+ if (*len == 0 && *ptr == NULL) {
+ return 1;
+ }
+
+ switch (coltype) {
+ case SQLCHAR:
+ case SQLTEXT:
+ case SQLVARBINARY:
+ case SQLBINARY:
+ case SQLIMAGE:
+ case SQLVARCHAR:
+ tmp_ptr = emalloc(*len + 1);
+ memcpy(tmp_ptr, *ptr, *len);
+ tmp_ptr[*len] = '\0';
+ *ptr = tmp_ptr;
+ break;
+ case SQLMONEY:
+ case SQLMONEY4:
+ case SQLMONEYN: {
+ DBFLT8 money_value;
+ dbconvert(NULL, coltype, *ptr, *len, SQLFLT8, (LPBYTE)&money_value, 8);
+ *len = spprintf(&tmp_ptr, 0, "%.4f", money_value);
+ *ptr = tmp_ptr;
+ break;
+ }
+#ifdef SQLUNIQUE
+ case SQLUNIQUE: {
+#else
+ case 36: { /* FreeTDS hack, also used by ext/mssql */
+#endif
+ *len = 36+1;
+ tmp_ptr = emalloc(*len + 1);
+
+ /* uniqueidentifier is a 16-byte binary number, convert to 32 char hex string */
+#ifdef SQLUNIQUE
+ *len = dbconvert(NULL, SQLUNIQUE, ptr, *len, SQLCHAR, tmp_ptr, *len);
+#else
+ *len = dbconvert(NULL, 36, ptr, *len, SQLCHAR, tmp_ptr, *len);
+#endif
+ php_strtoupper(tmp_ptr, *len);
+ *ptr = tmp_ptr;
+ break;
+ }
+ default:
+ if (dbwillconvert(coltype, SQLCHAR)) {
+ tmp_len = 32 + (2 * (*len));
+ tmp_ptr = emalloc(tmp_len);
+ *len = dbconvert(NULL, coltype, *ptr, *len, SQLCHAR, tmp_ptr, -1);
+ *ptr = tmp_ptr;
+ } else {
+ *len = 0;
+ *ptr = NULL;
+ }
+ }
+
+ *caller_frees = 1;
- *ptr = val->data;
- *len = val->len;
return 1;
}
@@ -283,17 +219,6 @@ static int pdo_dblib_stmt_param_hook(pdo
return 1;
}
-static int dblib_dblib_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
-{
- pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
-
- if (S->rows) {
- free_rows(S TSRMLS_CC);
- S->rows = NULL;
- }
-
- return 1;
-}
struct pdo_stmt_methods dblib_stmt_methods = {
pdo_dblib_stmt_dtor,
@@ -305,7 +230,7 @@ struct pdo_stmt_methods dblib_stmt_metho
NULL, /* set attr */
NULL, /* get attr */
NULL, /* meta */
- NULL, /* nextrow */
+ pdo_dblib_stmt_next_rowset, /* nextrow */
dblib_dblib_stmt_cursor_closer
};
diff -up php5.3-201105121030/ext/pdo_dblib/php_pdo_dblib_int.h.50755 php5.3-201105121030/ext/pdo_dblib/php_pdo_dblib_int.h
--- php5.3-201105121030/ext/pdo_dblib/php_pdo_dblib_int.h.50755 2011-01-01 04:37:16.000000000 +0100
+++ php5.3-201105121030/ext/pdo_dblib/php_pdo_dblib_int.h 2011-05-13 18:49:33.325783259 +0200
@@ -114,28 +114,7 @@ typedef struct {
} pdo_dblib_db_handle;
typedef struct {
- int coltype;
- char *name;
- int maxlen;
- char *source;
-} pdo_dblib_col;
-
-typedef struct {
- unsigned long len;
- char *data;
-} pdo_dblib_colval;
-
-typedef struct {
pdo_dblib_db_handle *H;
-
- int ncols;
- pdo_dblib_col *cols;
-
- pdo_dblib_colval *rows;
- int nrows;
-
- int current;
-
pdo_dblib_err err;
} pdo_dblib_stmt;
|