1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Wez Furlong <wez@php.net> |
14 | Frank M. Kromann <frank@kromann.info> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_ini.h"
24 #include "ext/standard/info.h"
25 #include "pdo/php_pdo.h"
26 #include "pdo/php_pdo_driver.h"
27 #include "php_pdo_dblib.h"
28 #include "php_pdo_dblib_int.h"
29 #include "zend_exceptions.h"
30
31 /* Cache of the server supported datatypes, initialized in handle_factory */
32 zval* pdo_dblib_datatypes;
33
dblib_fetch_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)34 static int dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
35 {
36 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
37 pdo_dblib_err *einfo = &H->err;
38 pdo_dblib_stmt *S = NULL;
39 char *message;
40 char *msg;
41
42 if (stmt) {
43 S = (pdo_dblib_stmt*)stmt->driver_data;
44 einfo = &S->err;
45 }
46
47 if (einfo->lastmsg) {
48 msg = einfo->lastmsg;
49 } else if (DBLIB_G(err).lastmsg) {
50 msg = DBLIB_G(err).lastmsg;
51 DBLIB_G(err).lastmsg = NULL;
52 } else {
53 msg = einfo->dberrstr;
54 }
55
56 /* don't return anything if there's nothing to return */
57 if (msg == NULL && einfo->dberr == 0 && einfo->oserr == 0 && einfo->severity == 0) {
58 return 0;
59 }
60
61 spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
62 msg, einfo->dberr, einfo->severity, stmt ? stmt->active_query_string : "");
63
64 add_next_index_long(info, einfo->dberr);
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
dblib_handle_closer(pdo_dbh_t * dbh)77 static int dblib_handle_closer(pdo_dbh_t *dbh)
78 {
79 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
80
81 if (H) {
82 pdo_dblib_err_dtor(&H->err);
83 if (H->link) {
84 dbclose(H->link);
85 H->link = NULL;
86 }
87 if (H->login) {
88 dbfreelogin(H->login);
89 H->login = NULL;
90 }
91 pefree(H, dbh->is_persistent);
92 dbh->driver_data = NULL;
93 }
94 return 0;
95 }
96
dblib_handle_preparer(pdo_dbh_t * dbh,const char * sql,size_t sql_len,pdo_stmt_t * stmt,zval * driver_options)97 static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
98 {
99 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
100 pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
101
102 S->H = H;
103 stmt->driver_data = S;
104 stmt->methods = &dblib_stmt_methods;
105 stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
106 S->computed_column_name_count = 0;
107 S->err.sqlstate = stmt->error_code;
108
109 return 1;
110 }
111
dblib_handle_doer(pdo_dbh_t * dbh,const char * sql,size_t sql_len)112 static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
113 {
114 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
115 RETCODE ret, resret;
116
117 dbsetuserdata(H->link, (BYTE*)&H->err);
118
119 if (FAIL == dbcmd(H->link, sql)) {
120 return -1;
121 }
122
123 if (FAIL == dbsqlexec(H->link)) {
124 return -1;
125 }
126
127 resret = dbresults(H->link);
128
129 if (resret == FAIL) {
130 return -1;
131 }
132
133 ret = dbnextrow(H->link);
134 if (ret == FAIL) {
135 return -1;
136 }
137
138 if (dbnumcols(H->link) <= 0) {
139 return DBCOUNT(H->link);
140 }
141
142 /* throw away any rows it might have returned */
143 dbcanquery(H->link);
144
145 return DBCOUNT(H->link);
146 }
147
dblib_handle_quoter(pdo_dbh_t * dbh,const char * unquoted,size_t unquotedlen,char ** quoted,size_t * quotedlen,enum pdo_param_type paramtype)148 static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
149 {
150 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
151 zend_bool use_national_character_set = 0;
152
153 size_t i;
154 char * q;
155 *quotedlen = 0;
156
157 if (H->assume_national_character_set_strings) {
158 use_national_character_set = 1;
159 }
160 if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
161 use_national_character_set = 1;
162 }
163 if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
164 use_national_character_set = 0;
165 }
166
167 /* Detect quoted length, adding extra char for doubled single quotes */
168 for (i = 0; i < unquotedlen; i++) {
169 if (unquoted[i] == '\'') ++*quotedlen;
170 ++*quotedlen;
171 }
172
173 *quotedlen += 2; /* +2 for opening, closing quotes */
174 if (use_national_character_set) {
175 ++*quotedlen; /* N prefix */
176 }
177 q = *quoted = emalloc(*quotedlen + 1); /* Add byte for terminal null */
178 if (use_national_character_set) {
179 *q++ = 'N';
180 }
181 *q++ = '\'';
182
183 for (i = 0; i < unquotedlen; i++) {
184 if (unquoted[i] == '\'') {
185 *q++ = '\'';
186 *q++ = '\'';
187 } else {
188 *q++ = unquoted[i];
189 }
190 }
191 *q++ = '\'';
192
193 *q = 0;
194
195 return 1;
196 }
197
pdo_dblib_transaction_cmd(const char * cmd,pdo_dbh_t * dbh)198 static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
199 {
200 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
201
202 if (FAIL == dbcmd(H->link, cmd)) {
203 return 0;
204 }
205
206 if (FAIL == dbsqlexec(H->link)) {
207 return 0;
208 }
209
210 return 1;
211 }
212
dblib_handle_begin(pdo_dbh_t * dbh)213 static int dblib_handle_begin(pdo_dbh_t *dbh)
214 {
215 return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
216 }
217
dblib_handle_commit(pdo_dbh_t * dbh)218 static int dblib_handle_commit(pdo_dbh_t *dbh)
219 {
220 return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
221 }
222
dblib_handle_rollback(pdo_dbh_t * dbh)223 static int dblib_handle_rollback(pdo_dbh_t *dbh)
224 {
225 return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
226 }
227
dblib_handle_last_id(pdo_dbh_t * dbh,const char * name,size_t * len)228 char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, size_t *len)
229 {
230 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
231
232 RETCODE ret;
233 char *id = NULL;
234
235 /*
236 * Would use scope_identity() but it's not implemented on Sybase
237 */
238
239 if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
240 return NULL;
241 }
242
243 if (FAIL == dbsqlexec(H->link)) {
244 return NULL;
245 }
246
247 ret = dbresults(H->link);
248 if (ret == FAIL || ret == NO_MORE_RESULTS) {
249 dbcancel(H->link);
250 return NULL;
251 }
252
253 ret = dbnextrow(H->link);
254
255 if (ret == FAIL || ret == NO_MORE_ROWS) {
256 dbcancel(H->link);
257 return NULL;
258 }
259
260 if (dbdatlen(H->link, 1) == 0) {
261 dbcancel(H->link);
262 return NULL;
263 }
264
265 id = emalloc(32);
266 *len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
267
268 dbcancel(H->link);
269 return id;
270 }
271
dblib_set_attr(pdo_dbh_t * dbh,zend_long attr,zval * val)272 static int dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
273 {
274 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
275
276 switch(attr) {
277 case PDO_ATTR_DEFAULT_STR_PARAM:
278 H->assume_national_character_set_strings = zval_get_long(val) == PDO_PARAM_STR_NATL ? 1 : 0;
279 return 1;
280 case PDO_ATTR_TIMEOUT:
281 case PDO_DBLIB_ATTR_QUERY_TIMEOUT:
282 return SUCCEED == dbsettime(zval_get_long(val)) ? 1 : 0;
283 case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
284 H->stringify_uniqueidentifier = zval_get_long(val);
285 return 1;
286 case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
287 H->skip_empty_rowsets = zval_is_true(val);
288 return 1;
289 case PDO_DBLIB_ATTR_DATETIME_CONVERT:
290 H->datetime_convert = zval_get_long(val);
291 return 1;
292 default:
293 return 0;
294 }
295 }
296
dblib_get_tds_version(zval * return_value,int tds)297 static void dblib_get_tds_version(zval *return_value, int tds)
298 {
299 switch (tds) {
300 case DBTDS_2_0:
301 ZVAL_STRING(return_value, "2.0");
302 break;
303
304 case DBTDS_3_4:
305 ZVAL_STRING(return_value, "3.4");
306 break;
307
308 case DBTDS_4_0:
309 ZVAL_STRING(return_value, "4.0");
310 break;
311
312 case DBTDS_4_2:
313 ZVAL_STRING(return_value, "4.2");
314 break;
315
316 case DBTDS_4_6:
317 ZVAL_STRING(return_value, "4.6");
318 break;
319
320 case DBTDS_4_9_5:
321 ZVAL_STRING(return_value, "4.9.5");
322 break;
323
324 case DBTDS_5_0:
325 ZVAL_STRING(return_value, "5.0");
326 break;
327
328 #ifdef DBTDS_7_0
329 case DBTDS_7_0:
330 ZVAL_STRING(return_value, "7.0");
331 break;
332 #endif
333
334 #ifdef DBTDS_7_1
335 case DBTDS_7_1:
336 ZVAL_STRING(return_value, "7.1");
337 break;
338 #endif
339
340 #ifdef DBTDS_7_2
341 case DBTDS_7_2:
342 ZVAL_STRING(return_value, "7.2");
343 break;
344 #endif
345
346 #ifdef DBTDS_7_3
347 case DBTDS_7_3:
348 ZVAL_STRING(return_value, "7.3");
349 break;
350 #endif
351
352 #ifdef DBTDS_7_4
353 case DBTDS_7_4:
354 ZVAL_STRING(return_value, "7.4");
355 break;
356 #endif
357
358 default:
359 ZVAL_FALSE(return_value);
360 break;
361 }
362 }
363
dblib_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)364 static int dblib_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
365 {
366 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
367
368 switch (attr) {
369 case PDO_ATTR_DEFAULT_STR_PARAM:
370 ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
371 break;
372
373 case PDO_ATTR_EMULATE_PREPARES:
374 /* this is the only option available, but expose it so common tests and whatever else can introspect */
375 ZVAL_TRUE(return_value);
376 break;
377
378 case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
379 ZVAL_BOOL(return_value, H->stringify_uniqueidentifier);
380 break;
381
382 case PDO_DBLIB_ATTR_VERSION:
383 ZVAL_STRING(return_value, dbversion());
384 break;
385
386 case PDO_DBLIB_ATTR_TDS_VERSION:
387 dblib_get_tds_version(return_value, dbtds(H->link));
388 break;
389
390 case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
391 ZVAL_BOOL(return_value, H->skip_empty_rowsets);
392 break;
393
394 case PDO_DBLIB_ATTR_DATETIME_CONVERT:
395 ZVAL_BOOL(return_value, H->datetime_convert);
396 break;
397
398 default:
399 return 0;
400 }
401
402 return 1;
403 }
404
405 static const struct pdo_dbh_methods dblib_methods = {
406 dblib_handle_closer,
407 dblib_handle_preparer,
408 dblib_handle_doer,
409 dblib_handle_quoter,
410 dblib_handle_begin, /* begin */
411 dblib_handle_commit, /* commit */
412 dblib_handle_rollback, /* rollback */
413 dblib_set_attr, /*set attr */
414 dblib_handle_last_id, /* last insert id */
415 dblib_fetch_error, /* fetch error */
416 dblib_get_attribute, /* get attr */
417 NULL, /* check liveness */
418 NULL, /* get driver methods */
419 NULL, /* request shutdown */
420 NULL /* in transaction */
421 };
422
pdo_dblib_handle_factory(pdo_dbh_t * dbh,zval * driver_options)423 static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
424 {
425 pdo_dblib_db_handle *H;
426 int i, nvars, nvers, ret = 0;
427
428 const pdo_dblib_keyval tdsver[] = {
429 {"4.2",DBVERSION_42}
430 ,{"4.6",DBVERSION_46}
431 ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
432 ,{"6.0",DBVERSION_70}
433 ,{"7.0",DBVERSION_70}
434 #ifdef DBVERSION_71
435 ,{"7.1",DBVERSION_71}
436 #endif
437 #ifdef DBVERSION_72
438 ,{"7.2",DBVERSION_72}
439 ,{"8.0",DBVERSION_72}
440 #endif
441 #ifdef DBVERSION_73
442 ,{"7.3",DBVERSION_73}
443 #endif
444 #ifdef DBVERSION_74
445 ,{"7.4",DBVERSION_74}
446 #endif
447 ,{"10.0",DBVERSION_100}
448 ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
449
450 };
451
452 struct pdo_data_src_parser vars[] = {
453 { "charset", NULL, 0 }
454 ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR, 0 }
455 ,{ "host", "127.0.0.1", 0 }
456 ,{ "dbname", NULL, 0 }
457 ,{ "secure", NULL, 0 } /* DBSETLSECURE */
458 ,{ "version", NULL, 0 } /* DBSETLVERSION */
459 ,{ "user", NULL, 0 }
460 ,{ "password", NULL, 0 }
461 };
462
463 nvars = sizeof(vars)/sizeof(vars[0]);
464 nvers = sizeof(tdsver)/sizeof(tdsver[0]);
465
466 php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
467
468 H = pecalloc(1, sizeof(*H), dbh->is_persistent);
469 H->login = dblogin();
470 H->err.sqlstate = dbh->error_code;
471 H->assume_national_character_set_strings = 0;
472 H->stringify_uniqueidentifier = 0;
473 H->skip_empty_rowsets = 0;
474 H->datetime_convert = 0;
475
476 if (!H->login) {
477 goto cleanup;
478 }
479
480 if (driver_options) {
481 int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
482 int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
483 int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
484
485 if (connect_timeout == -1) {
486 connect_timeout = timeout;
487 }
488 if (query_timeout == -1) {
489 query_timeout = timeout;
490 }
491
492 dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
493 dbsettime(query_timeout); /* Statement Timeout */
494
495 H->assume_national_character_set_strings = pdo_attr_lval(driver_options, PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
496 H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0);
497 H->skip_empty_rowsets = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS, 0);
498 H->datetime_convert = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_DATETIME_CONVERT, 0);
499 }
500
501 DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
502 DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
503
504 if(vars[5].optval) {
505 for(i=0;i<nvers;i++) {
506 if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
507 if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
508 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
509 goto cleanup;
510 }
511 break;
512 }
513 }
514
515 if (i==nvers) {
516 printf("Invalid version '%s'\n", vars[5].optval);
517 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
518 goto cleanup; /* unknown version specified */
519 }
520 }
521
522 if (!dbh->username && vars[6].optval) {
523 dbh->username = pestrdup(vars[6].optval, dbh->is_persistent);
524 }
525
526 if (dbh->username) {
527 if(FAIL == DBSETLUSER(H->login, dbh->username)) {
528 goto cleanup;
529 }
530 }
531
532 if (!dbh->password && vars[7].optval) {
533 dbh->password = pestrdup(vars[7].optval, dbh->is_persistent);
534 }
535
536 if (dbh->password) {
537 if(FAIL == DBSETLPWD(H->login, dbh->password)) {
538 goto cleanup;
539 }
540 }
541
542 #ifndef PHP_DBLIB_IS_MSSQL
543 if (vars[0].optval) {
544 DBSETLCHARSET(H->login, vars[0].optval);
545 }
546 #endif
547
548 DBSETLAPP(H->login, vars[1].optval);
549
550 /* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
551 #ifdef DBSETLDBNAME
552 if (vars[3].optval) {
553 if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
554 }
555 #endif
556
557 H->link = dbopen(H->login, vars[2].optval);
558
559 if (!H->link) {
560 goto cleanup;
561 }
562
563 /*
564 * FreeTDS < 0.92 does not support the DBSETLDBNAME option
565 * Send use database here after login (Will not work with SQL Azure)
566 */
567 #ifndef DBSETLDBNAME
568 if (vars[3].optval) {
569 if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
570 }
571 #endif
572
573 #ifdef PHP_DBLIB_IS_MSSQL
574 /* dblib do not return more than this length from text/image */
575 DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
576 #endif
577
578 /* limit text/image from network */
579 DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
580
581 /* allow double quoted indentifiers */
582 DBSETOPT(H->link, DBQUOTEDIDENT, "1");
583
584 ret = 1;
585 dbh->max_escaped_char_length = 2;
586 dbh->alloc_own_columns = 1;
587
588 cleanup:
589 for (i = 0; i < nvars; i++) {
590 if (vars[i].freeme) {
591 efree(vars[i].optval);
592 }
593 }
594
595 dbh->methods = &dblib_methods;
596 dbh->driver_data = H;
597
598 if (!ret) {
599 zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr,
600 "SQLSTATE[%s] %s (severity %d)",
601 DBLIB_G(err).sqlstate,
602 DBLIB_G(err).dberrstr,
603 DBLIB_G(err).severity);
604 }
605
606 return ret;
607 }
608
609 const pdo_driver_t pdo_dblib_driver = {
610 #ifdef PDO_DBLIB_IS_MSSQL
611 PDO_DRIVER_HEADER(mssql),
612 #elif defined(PHP_WIN32)
613 #define PDO_DBLIB_IS_SYBASE
614 PDO_DRIVER_HEADER(sybase),
615 #else
616 PDO_DRIVER_HEADER(dblib),
617 #endif
618 pdo_dblib_handle_factory
619 };
620