~ubuntu-branches/ubuntu/quantal/php5/quantal

« back to all changes in this revision

Viewing changes to ext/pdo_dblib/dblib_stmt.c

  • Committer: Bazaar Package Importer
  • Author(s): Sean Finney
  • Date: 2009-07-01 09:12:10 UTC
  • mto: (0.9.1) (1.1.17 upstream)
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: james.westby@ubuntu.com-20090701091210-go0h6506p62on17r
Tags: upstream-5.3.0
ImportĀ upstreamĀ versionĀ 5.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  +----------------------------------------------------------------------+
 
3
  | PHP Version 5                                                        |
 
4
  +----------------------------------------------------------------------+
 
5
  | Copyright (c) 1997-2009 The PHP Group                                |
 
6
  +----------------------------------------------------------------------+
 
7
  | This source file is subject to version 3.01 of the PHP license,      |
 
8
  | that is bundled with this package in the file LICENSE, and is        |
 
9
  | available through the world-wide-web at the following url:           |
 
10
  | http://www.php.net/license/3_01.txt                                  |
 
11
  | If you did not receive a copy of the PHP license and are unable to   |
 
12
  | obtain it through the world-wide-web, please send a note to          |
 
13
  | license@php.net so we can mail you a copy immediately.               |
 
14
  +----------------------------------------------------------------------+
 
15
  | Author: Wez Furlong <wez@php.net>                                    |
 
16
  |         Frank M. Kromann <frank@kromann.info>                        |
 
17
  +----------------------------------------------------------------------+
 
18
*/
 
19
 
 
20
/* $Id: dblib_stmt.c,v 1.6.2.2.2.4.2.3 2009/03/19 22:16:29 sfox Exp $ */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
# include "config.h"
 
24
#endif
 
25
 
 
26
#include "php.h"
 
27
#include "php_ini.h"
 
28
#include "ext/standard/info.h"
 
29
#include "pdo/php_pdo.h"
 
30
#include "pdo/php_pdo_driver.h"
 
31
#include "php_pdo_dblib.h"
 
32
#include "php_pdo_dblib_int.h"
 
33
#include "zend_exceptions.h"
 
34
 
 
35
static void free_rows(pdo_dblib_stmt *S TSRMLS_DC)
 
36
{
 
37
        int i, j;
 
38
        
 
39
        for (i = 0; i < S->nrows; i++) {
 
40
                for (j = 0; j < S->ncols; j++) {
 
41
                        pdo_dblib_colval *val = &S->rows[i] + j;
 
42
                        if (val->data) {
 
43
                                efree(val->data);
 
44
                                val->data = NULL;
 
45
                        }
 
46
                }
 
47
        }
 
48
        efree(S->rows);
 
49
        S->rows = NULL;
 
50
        S->nrows = 0;
 
51
}
 
52
 
 
53
static int pdo_dblib_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
 
54
{
 
55
        pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
 
56
 
 
57
        if (S->rows) {
 
58
                free_rows(S TSRMLS_CC);
 
59
        }
 
60
        if (S->cols) {
 
61
                efree(S->cols);
 
62
        }
 
63
        efree(S);
 
64
 
 
65
        return 1;
 
66
}
 
67
 
 
68
static int pdo_dblib_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
 
69
{
 
70
        pdo_dbh_t *dbh = stmt->dbh;
 
71
        pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
 
72
        pdo_dblib_db_handle *H = S->H;
 
73
        RETCODE resret, ret;
 
74
        int i, j;
 
75
        int arows;
 
76
        unsigned int size;
 
77
        
 
78
        dbsetuserdata(H->link, &S->err);
 
79
 
 
80
        if (S->rows) {
 
81
                /* clean them up */
 
82
                free_rows(S TSRMLS_CC);
 
83
        }
 
84
 
 
85
        if (FAIL == dbcmd(H->link, stmt->active_query_string)) {
 
86
                return 0;
 
87
        }
 
88
        if (FAIL == dbsqlexec(H->link)) {
 
89
                return 0;
 
90
        }
 
91
        
 
92
        resret = dbresults(H->link);
 
93
        if (resret == FAIL) {
 
94
                return 0;
 
95
        }
 
96
 
 
97
        ret = dbnextrow(H->link);
 
98
 
 
99
        stmt->row_count = DBCOUNT(H->link);
 
100
 
 
101
    if (ret == NO_MORE_ROWS) {
 
102
       return 1;
 
103
    }
 
104
    
 
105
        if (!S->cols) {
 
106
                S->ncols = dbnumcols(H->link);
 
107
 
 
108
                if (S->ncols <= 0) {
 
109
                        return 1;
 
110
                }
 
111
 
 
112
                S->cols = ecalloc(S->ncols, sizeof(pdo_dblib_col));
 
113
                stmt->column_count = S->ncols;
 
114
        
 
115
                for (i = 0, j = 0; i < S->ncols; i++) {
 
116
                        char *tmp = NULL;
 
117
 
 
118
                        S->cols[i].coltype = dbcoltype(H->link, i+1);
 
119
                        S->cols[i].name = (char*)dbcolname(H->link, i+1);
 
120
 
 
121
                        if (!strlen(S->cols[i].name)) {
 
122
                                if (j) {
 
123
                                        spprintf(&tmp, 0, "computed%d", j++);
 
124
                                        strlcpy(S->cols[i].name, tmp, strlen(tmp)+1);
 
125
                                        efree(tmp);
 
126
                                } else {
 
127
                                        S->cols[i].name = "computed";
 
128
                                        j++;
 
129
                                }
 
130
                        }
 
131
 
 
132
                        S->cols[i].source = (char*)dbcolsource(H->link, i+1);
 
133
                        tmp = estrdup(S->cols[i].source ? S->cols[i].source : "");
 
134
                        S->cols[i].source = tmp;
 
135
                        efree(tmp);
 
136
 
 
137
                        S->cols[i].maxlen = dbcollen(H->link, i+1);
 
138
                }
 
139
        }
 
140
 
 
141
        arows = 100;
 
142
        size = S->ncols * sizeof(pdo_dblib_colval);
 
143
        S->rows = safe_emalloc(arows, size, 0);
 
144
 
 
145
        /* let's fetch all the data */
 
146
        do {
 
147
                if (S->nrows >= arows) {
 
148
                        arows *= 2;
 
149
                        S->rows = erealloc(S->rows, arows * size);
 
150
                }
 
151
                for (i = 0; i < S->ncols; i++) {
 
152
                        pdo_dblib_colval *val = &S->rows[S->nrows * S->ncols + i];
 
153
 
 
154
                        if (dbdatlen(H->link, i+1) == 0 && dbdata(H->link, i+1) == NULL) {
 
155
                                val->len = 0;
 
156
                                val->data = NULL;
 
157
                        } else {
 
158
                                switch (S->cols[i].coltype) {
 
159
                                        case SQLCHAR:
 
160
                                        case SQLTEXT:
 
161
                                        case SQLVARBINARY:
 
162
                                        case SQLBINARY:
 
163
                                        case SQLIMAGE:
 
164
                                                val->len = dbdatlen(H->link, i+1);
 
165
                                                val->data = emalloc(val->len + 1);
 
166
                                                memcpy(val->data, dbdata(H->link, i+1), val->len);
 
167
                                                val->data[val->len] = '\0';
 
168
                                                break;
 
169
 
 
170
                                        default:
 
171
                                                if (dbwillconvert(S->cols[i].coltype, SQLCHAR)) {
 
172
                                                        val->len = 32 + (2 * dbdatlen(H->link, i+1));
 
173
                                                        val->data = emalloc(val->len);
 
174
 
 
175
                                                        val->len = dbconvert(NULL, S->cols[i].coltype, dbdata(H->link, i+1),
 
176
                                                                        dbdatlen(H->link, i+1), SQLCHAR, val->data, val->len);
 
177
 
 
178
                                                        if (val->len >= 0) {
 
179
                                                                val->data[val->len] = '\0';
 
180
                                                        }
 
181
                                                } else {
 
182
                                                        val->len = 0;
 
183
                                                        val->data = NULL;
 
184
                                                }
 
185
                                }
 
186
                        }
 
187
                }
 
188
 
 
189
                S->nrows++;
 
190
 
 
191
                ret = dbnextrow(H->link);
 
192
 
 
193
                if (ret == BUF_FULL) {
 
194
                        dbclrbuf(H->link, DBLASTROW(H->link)-1);
 
195
                }
 
196
        } while (ret != FAIL && ret != NO_MORE_ROWS);
 
197
 
 
198
        if (resret != NO_MORE_RESULTS) {
 
199
                /* there are additional result sets available */
 
200
                dbresults(H->link);
 
201
                /* cancel pending rows */
 
202
                dbcanquery(H->link);
 
203
 
 
204
                /* TODO: figure out a sane solution */
 
205
        }
 
206
 
 
207
        S->current = -1;
 
208
                
 
209
        return 1;       
 
210
}
 
211
 
 
212
static int pdo_dblib_stmt_fetch(pdo_stmt_t *stmt,
 
213
        enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
 
214
{
 
215
        pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
 
216
 
 
217
        if (!S->rows) {
 
218
                return 0;
 
219
        }
 
220
        
 
221
        if (++S->current < S->nrows) {
 
222
                return 1;
 
223
        }
 
224
 
 
225
        return 0;
 
226
}
 
227
 
 
228
static int pdo_dblib_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
 
229
{
 
230
        pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
 
231
        struct pdo_column_data *col = &stmt->columns[colno];
 
232
 
 
233
        if (!S->rows) {
 
234
                return 0;
 
235
        }
 
236
 
 
237
        col->maxlen = S->cols[colno].maxlen;    
 
238
        col->namelen = strlen(S->cols[colno].name);     
 
239
        col->name = estrdup(S->cols[colno].name);
 
240
        col->param_type = PDO_PARAM_STR;
 
241
                
 
242
        return 1;
 
243
}
 
244
 
 
245
static int pdo_dblib_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr,
 
246
         unsigned long *len, int *caller_frees TSRMLS_DC)
 
247
{
 
248
        pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
 
249
        pdo_dblib_colval *val = &S->rows[S->current * S->ncols + colno];
 
250
 
 
251
        *ptr = val->data;
 
252
        *len = val->len;
 
253
        return 1;
 
254
}
 
255
 
 
256
static int pdo_dblib_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
 
257
                enum pdo_param_event event_type TSRMLS_DC)
 
258
{
 
259
        return 1;
 
260
}
 
261
 
 
262
static int dblib_dblib_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
 
263
{
 
264
        pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
 
265
 
 
266
        if (S->rows) {
 
267
                free_rows(S TSRMLS_CC);
 
268
                S->rows = NULL;
 
269
        }
 
270
 
 
271
        return 1;
 
272
}
 
273
 
 
274
struct pdo_stmt_methods dblib_stmt_methods = {
 
275
        pdo_dblib_stmt_dtor,
 
276
        pdo_dblib_stmt_execute,
 
277
        pdo_dblib_stmt_fetch,
 
278
        pdo_dblib_stmt_describe,
 
279
        pdo_dblib_stmt_get_col,
 
280
        pdo_dblib_stmt_param_hook,
 
281
        NULL, /* set attr */
 
282
        NULL, /* get attr */
 
283
        NULL, /* meta */
 
284
        NULL, /* nextrow */
 
285
        dblib_dblib_stmt_cursor_closer
 
286
};
 
287