1/*
2  +----------------------------------------------------------------------+
3  | PHP Version 5                                                        |
4  +----------------------------------------------------------------------+
5  | Copyright (c) 1997-2014 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$ */
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/* Cache of the server supported datatypes, initialized in handle_factory */
36zval* pdo_dblib_datatypes;
37
38static int dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC)
39{
40    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
41    pdo_dblib_err *einfo = &H->err;
42    pdo_dblib_stmt *S = NULL;
43    char *message;
44    char *msg;
45
46    if (stmt) {
47        S = (pdo_dblib_stmt*)stmt->driver_data;
48        einfo = &S->err;
49    }
50
51    if (einfo->dberr == SYBESMSG && einfo->lastmsg) {
52        msg = einfo->lastmsg;
53    } else if (einfo->dberr == SYBESMSG && DBLIB_G(err).lastmsg) {
54        msg = DBLIB_G(err).lastmsg;
55        DBLIB_G(err).lastmsg = NULL;
56    } else {
57        msg = einfo->dberrstr;
58    }
59
60    spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
61        msg, einfo->dberr, einfo->severity, stmt ? stmt->active_query_string : "");
62
63    add_next_index_long(info, einfo->dberr);
64    // TODO: avoid reallocation ???
65    add_next_index_string(info, message);
66    efree(message);
67    add_next_index_long(info, einfo->oserr);
68    add_next_index_long(info, einfo->severity);
69    if (einfo->oserrstr) {
70        add_next_index_string(info, einfo->oserrstr);
71    }
72
73    return 1;
74}
75
76
77static int dblib_handle_closer(pdo_dbh_t *dbh TSRMLS_DC)
78{
79    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
80
81    if (H) {
82        if (H->link) {
83            dbclose(H->link);
84            H->link = NULL;
85        }
86        if (H->login) {
87            dbfreelogin(H->login);
88            H->login = NULL;
89        }
90        pefree(H, dbh->is_persistent);
91        dbh->driver_data = NULL;
92    }
93    return 0;
94}
95
96static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, zend_long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
97{
98    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
99    pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
100
101    S->H = H;
102    stmt->driver_data = S;
103    stmt->methods = &dblib_stmt_methods;
104    stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
105    S->err.sqlstate = stmt->error_code;
106
107    return 1;
108}
109
110static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, zend_long sql_len TSRMLS_DC)
111{
112    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
113    RETCODE ret, resret;
114
115    dbsetuserdata(H->link, (BYTE*)&H->err);
116
117    if (FAIL == dbcmd(H->link, sql)) {
118        return -1;
119    }
120
121    if (FAIL == dbsqlexec(H->link)) {
122        return -1;
123    }
124
125    resret = dbresults(H->link);
126
127    if (resret == FAIL) {
128        return -1;
129    }
130
131    ret = dbnextrow(H->link);
132    if (ret == FAIL) {
133        return -1;
134    }
135
136    if (dbnumcols(H->link) <= 0) {
137        return DBCOUNT(H->link);
138    }
139
140    /* throw away any rows it might have returned */
141    dbcanquery(H->link);
142
143    return DBCOUNT(H->link);
144}
145
146static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC)
147{
148    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
149    char *q;
150    int l = 1;
151
152    *quoted = q = safe_emalloc(2, unquotedlen, 3);
153    *q++ = '\'';
154
155    while (unquotedlen--) {
156        if (*unquoted == '\'') {
157            *q++ = '\'';
158            *q++ = '\'';
159            l += 2;
160        } else {
161            *q++ = *unquoted;
162            ++l;
163        }
164        unquoted++;
165    }
166
167    *q++ = '\'';
168    *q++ = '\0';
169    *quotedlen = l+1;
170
171    return 1;
172}
173
174static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC)
175{
176    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
177    RETCODE ret;
178
179    if (FAIL == dbcmd(H->link, cmd)) {
180        return 0;
181    }
182
183    if (FAIL == dbsqlexec(H->link)) {
184        return 0;
185    }
186
187    return 1;
188}
189
190static int dblib_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
191{
192    return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh TSRMLS_CC);
193}
194
195static int dblib_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
196{
197    return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh TSRMLS_CC);
198}
199
200static int dblib_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
201{
202    return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh TSRMLS_CC);
203}
204
205char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, unsigned int *len TSRMLS_DC)
206{
207    pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
208
209    RETCODE ret;
210    char *id = NULL;
211
212    /*
213     * Would use scope_identity() but it's not implemented on Sybase
214     */
215
216    if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
217        return NULL;
218    }
219
220    if (FAIL == dbsqlexec(H->link)) {
221        return NULL;
222    }
223
224    ret = dbresults(H->link);
225    if (ret == FAIL || ret == NO_MORE_RESULTS) {
226        dbcancel(H->link);
227        return NULL;
228    }
229
230    ret = dbnextrow(H->link);
231
232    if (ret == FAIL || ret == NO_MORE_ROWS) {
233        dbcancel(H->link);
234        return NULL;
235    }
236
237    if (dbdatlen(H->link, 1) == 0) {
238        dbcancel(H->link);
239        return NULL;
240    }
241
242    id = emalloc(32);
243    *len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, id, (DBINT)-1);
244
245    dbcancel(H->link);
246    return id;
247}
248
249static struct pdo_dbh_methods dblib_methods = {
250    dblib_handle_closer,
251    dblib_handle_preparer,
252    dblib_handle_doer,
253    dblib_handle_quoter,
254    dblib_handle_begin, /* begin */
255    dblib_handle_commit, /* commit */
256    dblib_handle_rollback, /* rollback */
257    NULL, /*set attr */
258    dblib_handle_last_id, /* last insert id */
259    dblib_fetch_error, /* fetch error */
260    NULL, /* get attr */
261    NULL, /* check liveness */
262    NULL, /* get driver methods */
263    NULL, /* request shutdown */
264    NULL  /* in transaction */
265};
266
267static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC)
268{
269    pdo_dblib_db_handle *H;
270    int i, nvars, nvers, ret = 0;
271    int *val;
272
273    const pdo_dblib_keyval tdsver[] = {
274         {"4.2",DBVERSION_42}
275        ,{"4.6",DBVERSION_46}
276        ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
277        ,{"6.0",DBVERSION_70}
278        ,{"7.0",DBVERSION_70}
279#ifdef DBVERSION_71
280        ,{"7.1",DBVERSION_71}
281#endif
282#ifdef DBVERSION_72
283        ,{"7.2",DBVERSION_72}
284        ,{"8.0",DBVERSION_72}
285#endif
286        ,{"10.0",DBVERSION_100}
287        ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
288
289    };
290
291    nvers = sizeof(tdsver)/sizeof(tdsver[0]);
292
293    struct pdo_data_src_parser vars[] = {
294        { "charset",    NULL,   0 }
295        ,{ "appname",   "PHP " PDO_DBLIB_FLAVOUR,   0 }
296        ,{ "host",      "127.0.0.1", 0 }
297        ,{ "dbname",    NULL,   0 }
298        ,{ "secure",    NULL,   0 } /* DBSETLSECURE */
299        ,{ "version",   NULL,   0 } /* DBSETLVERSION */
300    };
301
302    nvars = sizeof(vars)/sizeof(vars[0]);
303
304    php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
305
306    H = pecalloc(1, sizeof(*H), dbh->is_persistent);
307    H->login = dblogin();
308    H->err.sqlstate = dbh->error_code;
309
310    if (!H->login) {
311        goto cleanup;
312    }
313
314    DBERRHANDLE(H->login, (EHANDLEFUNC) error_handler);
315    DBMSGHANDLE(H->login, (MHANDLEFUNC) msg_handler);
316
317    if(vars[5].optval) {
318        for(i=0;i<nvers;i++) {
319            if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
320                if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
321                    pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string." TSRMLS_CC);
322                    goto cleanup;
323                }
324                break;
325            }
326        }
327
328        if (i==nvers) {
329            printf("Invalid version '%s'\n", vars[5].optval);
330            pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string." TSRMLS_CC);
331            goto cleanup; /* unknown version specified */
332        }
333    }
334
335    if (dbh->username) {
336        if(FAIL == DBSETLUSER(H->login, dbh->username)) {
337            goto cleanup;
338        }
339    }
340
341    if (dbh->password) {
342        if(FAIL == DBSETLPWD(H->login, dbh->password)) {
343            goto cleanup;
344        }
345    }
346
347#if !PHP_DBLIB_IS_MSSQL
348    if (vars[0].optval) {
349        DBSETLCHARSET(H->login, vars[0].optval);
350    }
351#endif
352
353    DBSETLAPP(H->login, vars[1].optval);
354
355/* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
356#ifdef DBSETLDBNAME
357    if (vars[3].optval) {
358        if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
359    }
360#endif
361
362    H->link = dbopen(H->login, vars[2].optval);
363
364    if (!H->link) {
365        goto cleanup;
366    }
367
368/*
369 * FreeTDS < 0.92 does not support the DBSETLDBNAME option
370 * Send use database here after login (Will not work with SQL Azure)
371 */
372#ifndef DBSETLDBNAME
373    if (vars[3].optval) {
374        if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
375    }
376#endif
377
378#if PHP_DBLIB_IS_MSSQL
379    /* dblib do not return more than this length from text/image */
380    DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
381#endif
382
383    /* limit text/image from network */
384    DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
385
386    /* allow double quoted indentifiers */
387    DBSETOPT(H->link, DBQUOTEDIDENT, "1");
388
389    ret = 1;
390    dbh->max_escaped_char_length = 2;
391    dbh->alloc_own_columns = 1;
392
393cleanup:
394    for (i = 0; i < nvars; i++) {
395        if (vars[i].freeme) {
396            efree(vars[i].optval);
397        }
398    }
399
400    dbh->methods = &dblib_methods;
401    dbh->driver_data = H;
402
403    if (!ret) {
404        zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr TSRMLS_CC,
405            "SQLSTATE[%s] %s (severity %d)",
406            DBLIB_G(err).sqlstate,
407            DBLIB_G(err).dberrstr,
408            DBLIB_G(err).severity);
409    }
410
411    return ret;
412}
413
414pdo_driver_t pdo_dblib_driver = {
415#if PDO_DBLIB_IS_MSSQL
416    PDO_DRIVER_HEADER(mssql),
417#elif defined(PHP_WIN32)
418#define PDO_DBLIB_IS_SYBASE
419    PDO_DRIVER_HEADER(sybase),
420#else
421    PDO_DRIVER_HEADER(dblib),
422#endif
423    pdo_dblib_handle_factory
424};
425
426