php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
dblib_driver.c
Go to the documentation of this file.
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 | https://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 "ext/pdo/php_pdo.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 */
33
34static void dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
35{
37 pdo_dblib_err *einfo = &H->err;
39 char *message;
40 char *msg;
41
42 if (stmt) {
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;
59 }
60
61 spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
62 msg, einfo->dberr, einfo->severity, stmt ? ZSTR_VAL(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
74
75static void dblib_handle_closer(pdo_dbh_t *dbh)
76{
78
79 if (H) {
80 pdo_dblib_err_dtor(&H->err);
81 if (H->link) {
82 dbclose(H->link);
83 H->link = NULL;
84 }
85 if (H->login) {
86 dbfreelogin(H->login);
87 H->login = NULL;
88 }
89 pefree(H, dbh->is_persistent);
90 dbh->driver_data = NULL;
91 }
92}
93
94static bool dblib_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
95{
97 pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
98
99 S->H = H;
100 stmt->driver_data = S;
101 stmt->methods = &dblib_stmt_methods;
102 stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
103 S->computed_column_name_count = 0;
104 S->err.sqlstate = stmt->error_code;
105
106 return true;
107}
108
109static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
110{
112 RETCODE ret, resret;
113
114 dbsetuserdata(H->link, (BYTE*)&H->err);
115
116 if (FAIL == dbcmd(H->link, ZSTR_VAL(sql))) {
117 return -1;
118 }
119
120 if (FAIL == dbsqlexec(H->link)) {
121 return -1;
122 }
123
124 resret = dbresults(H->link);
125
126 if (resret == FAIL) {
127 return -1;
128 }
129
130 ret = dbnextrow(H->link);
131 if (ret == FAIL) {
132 return -1;
133 }
134
135 if (dbnumcols(H->link) <= 0) {
136 return DBCOUNT(H->link);
137 }
138
139 /* throw away any rows it might have returned */
140 dbcanquery(H->link);
141
142 return DBCOUNT(H->link);
143}
144
145static zend_string* dblib_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
146{
148 bool use_national_character_set = 0;
149 size_t i;
150 char *q;
151 size_t quotedlen = 0, extralen = 0;
152 zend_string *quoted_str;
153
154 if (H->assume_national_character_set_strings) {
155 use_national_character_set = 1;
156 }
157 if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
158 use_national_character_set = 1;
159 }
160 if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
161 use_national_character_set = 0;
162 }
163
164 /* Detect quoted length, adding extra char for doubled single quotes */
165 for (i = 0; i < ZSTR_LEN(unquoted); i++) {
166 if (ZSTR_VAL(unquoted)[i] == '\'') ++extralen;
167 ++quotedlen;
168 }
169
170 quotedlen += 2; /* +2 for opening, closing quotes */
171 if (use_national_character_set) {
172 ++quotedlen; /* N prefix */
173 }
174
175 if (UNEXPECTED(quotedlen > ZSTR_MAX_LEN - extralen)) {
176 return NULL;
177 }
178
179 quotedlen += extralen;
180 quoted_str = zend_string_alloc(quotedlen, 0);
181 q = ZSTR_VAL(quoted_str);
182 if (use_national_character_set) {
183 *q++ = 'N';
184 }
185 *q++ = '\'';
186
187 for (i = 0; i < ZSTR_LEN(unquoted); i++) {
188 if (ZSTR_VAL(unquoted)[i] == '\'') {
189 *q++ = '\'';
190 *q++ = '\'';
191 } else {
192 *q++ = ZSTR_VAL(unquoted)[i];
193 }
194 }
195 *q++ = '\'';
196 *q = '\0';
197
198 return quoted_str;
199}
200
201static bool pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
202{
204
205 if (FAIL == dbcmd(H->link, cmd)) {
206 return false;
207 }
208
209 if (FAIL == dbsqlexec(H->link)) {
210 return false;
211 }
212
213 return true;
214}
215
216static bool dblib_handle_begin(pdo_dbh_t *dbh)
217{
218 return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
219}
220
221static bool dblib_handle_commit(pdo_dbh_t *dbh)
222{
223 return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
224}
225
226static bool dblib_handle_rollback(pdo_dbh_t *dbh)
227{
228 return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
229}
230
232{
234
235 RETCODE ret;
236 char *id = NULL;
237 size_t len;
238 zend_string *ret_id;
239
240 /*
241 * Would use scope_identity() but it's not implemented on Sybase
242 */
243
244 if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
245 return NULL;
246 }
247
248 if (FAIL == dbsqlexec(H->link)) {
249 return NULL;
250 }
251
252 ret = dbresults(H->link);
253 if (ret == FAIL || ret == NO_MORE_RESULTS) {
254 dbcancel(H->link);
255 return NULL;
256 }
257
258 ret = dbnextrow(H->link);
259
260 if (ret == FAIL || ret == NO_MORE_ROWS) {
261 dbcancel(H->link);
262 return NULL;
263 }
264
265 if (dbdatlen(H->link, 1) == 0) {
266 dbcancel(H->link);
267 return NULL;
268 }
269
270 id = emalloc(32);
271 len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
272 dbcancel(H->link);
273
274 ret_id = zend_string_init(id, len, 0);
275 efree(id);
276 return ret_id;
277}
278
279static bool dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
280{
282 zend_long lval;
283 bool bval;
284
285 switch(attr) {
287 if (!pdo_get_long_param(&lval, val)) {
288 return false;
289 }
290 H->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL ? 1 : 0;
291 return true;
292 case PDO_ATTR_TIMEOUT:
294 if (!pdo_get_long_param(&lval, val)) {
295 return false;
296 }
297 return SUCCEED == dbsettime(lval);
299 if (!pdo_get_bool_param(&bval, val)) {
300 return false;
301 }
302 H->stringify_uniqueidentifier = bval;
303 return true;
305 if (!pdo_get_bool_param(&bval, val)) {
306 return false;
307 }
308 H->skip_empty_rowsets = bval;
309 return true;
311 if (!pdo_get_bool_param(&bval, val)) {
312 return false;
313 }
314 H->datetime_convert = bval;
315 return true;
316 default:
317 return false;
318 }
319}
320
321static void dblib_get_tds_version(zval *return_value, int tds)
322{
323 switch (tds) {
324 case DBTDS_2_0:
326 break;
327
328 case DBTDS_3_4:
330 break;
331
332 case DBTDS_4_0:
334 break;
335
336 case DBTDS_4_2:
338 break;
339
340 case DBTDS_4_6:
342 break;
343
344 case DBTDS_4_9_5:
345 ZVAL_STRING(return_value, "4.9.5");
346 break;
347
348 case DBTDS_5_0:
350 break;
351
352#ifdef DBTDS_7_0
353 case DBTDS_7_0:
355 break;
356#endif
357
358#ifdef DBTDS_7_1
359 case DBTDS_7_1:
361 break;
362#endif
363
364#ifdef DBTDS_7_2
365 case DBTDS_7_2:
367 break;
368#endif
369
370#ifdef DBTDS_7_3
371 case DBTDS_7_3:
373 break;
374#endif
375
376#ifdef DBTDS_7_4
377 case DBTDS_7_4:
379 break;
380#endif
381
382 default:
384 break;
385 }
386}
387
388static int dblib_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
389{
391
392 switch (attr) {
394 ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
395 break;
396
398 /* this is the only option available, but expose it so common tests and whatever else can introspect */
400 break;
401
403 ZVAL_BOOL(return_value, H->stringify_uniqueidentifier);
404 break;
405
407 ZVAL_STRING(return_value, dbversion());
408 break;
409
411 dblib_get_tds_version(return_value, dbtds(H->link));
412 break;
413
415 ZVAL_BOOL(return_value, H->skip_empty_rowsets);
416 break;
417
419 ZVAL_BOOL(return_value, H->datetime_convert);
420 break;
421
422 default:
423 return 0;
424 }
425
426 return 1;
427}
428
429static const struct pdo_dbh_methods dblib_methods = {
430 dblib_handle_closer,
431 dblib_handle_preparer,
432 dblib_handle_doer,
433 dblib_handle_quoter,
434 dblib_handle_begin, /* begin */
435 dblib_handle_commit, /* commit */
436 dblib_handle_rollback, /* rollback */
437 dblib_set_attr, /*set attr */
438 dblib_handle_last_id, /* last insert id */
439 dblib_fetch_error, /* fetch error */
440 dblib_get_attribute, /* get attr */
441 NULL, /* check liveness */
442 NULL, /* get driver methods */
443 NULL, /* request shutdown */
444 NULL, /* in transaction, use PDO's internal tracking mechanism */
445 NULL, /* get gc */
446 NULL /* scanner */
447};
448
449static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
450{
452 int i, nvars, nvers, ret = 0;
453
454 const pdo_dblib_keyval tdsver[] = {
455 {"4.2",DBVERSION_42}
456 ,{"4.6",DBVERSION_46}
457 ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
458 ,{"6.0",DBVERSION_70}
459 ,{"7.0",DBVERSION_70}
460#ifdef DBVERSION_71
461 ,{"7.1",DBVERSION_71}
462#endif
463#ifdef DBVERSION_72
464 ,{"7.2",DBVERSION_72}
465 ,{"8.0",DBVERSION_72}
466#endif
467#ifdef DBVERSION_73
468 ,{"7.3",DBVERSION_73}
469#endif
470#ifdef DBVERSION_74
471 ,{"7.4",DBVERSION_74}
472#endif
473 ,{"10.0",DBVERSION_100}
474 ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
475
476 };
477
478 struct pdo_data_src_parser vars[] = {
479 { "charset", NULL, 0 }
480 ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR, 0 }
481 ,{ "host", "127.0.0.1", 0 }
482 ,{ "dbname", NULL, 0 }
483 ,{ "secure", NULL, 0 } /* DBSETLSECURE */
484 ,{ "version", NULL, 0 } /* DBSETLVERSION */
485 ,{ "user", NULL, 0 }
486 ,{ "password", NULL, 0 }
487 };
488
489 nvars = sizeof(vars)/sizeof(vars[0]);
490 nvers = sizeof(tdsver)/sizeof(tdsver[0]);
491
493
494 H = pecalloc(1, sizeof(*H), dbh->is_persistent);
495 H->login = dblogin();
496 H->err.sqlstate = dbh->error_code;
497 H->assume_national_character_set_strings = 0;
498 H->stringify_uniqueidentifier = 0;
499 H->skip_empty_rowsets = 0;
500 H->datetime_convert = 0;
501
502 if (!H->login) {
503 goto cleanup;
504 }
505
506 if (driver_options) {
507 int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
508 int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
509 int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
510
511 if (connect_timeout == -1) {
512 connect_timeout = timeout;
513 }
514 if (query_timeout == -1) {
515 query_timeout = timeout;
516 }
517
518 dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
519 dbsettime(query_timeout); /* Statement Timeout */
520
521 H->assume_national_character_set_strings = pdo_attr_lval(driver_options, PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
522 H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0);
523 H->skip_empty_rowsets = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS, 0);
524 H->datetime_convert = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_DATETIME_CONVERT, 0);
525 }
526
527 DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
528 DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
529
530 if(vars[5].optval) {
531 for(i=0;i<nvers;i++) {
532 if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
533 if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
534 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
535 goto cleanup;
536 }
537 break;
538 }
539 }
540
541 if (i==nvers) {
542 printf("Invalid version '%s'\n", vars[5].optval);
543 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
544 goto cleanup; /* unknown version specified */
545 }
546 }
547
548 if (!dbh->username && vars[6].optval) {
549 dbh->username = pestrdup(vars[6].optval, dbh->is_persistent);
550 }
551
552 if (dbh->username) {
553 if(FAIL == DBSETLUSER(H->login, dbh->username)) {
554 goto cleanup;
555 }
556 }
557
558 if (!dbh->password && vars[7].optval) {
559 dbh->password = pestrdup(vars[7].optval, dbh->is_persistent);
560 }
561
562 if (dbh->password) {
563 if(FAIL == DBSETLPWD(H->login, dbh->password)) {
564 goto cleanup;
565 }
566 }
567
568#ifndef PHP_DBLIB_IS_MSSQL
569 if (vars[0].optval) {
570 DBSETLCHARSET(H->login, vars[0].optval);
571 }
572#endif
573
574 DBSETLAPP(H->login, vars[1].optval);
575
576/* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
577#ifdef DBSETLDBNAME
578 if (vars[3].optval) {
579 if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
580 }
581#endif
582
583 H->link = dbopen(H->login, vars[2].optval);
584
585 if (!H->link) {
586 goto cleanup;
587 }
588
589/*
590 * FreeTDS < 0.92 does not support the DBSETLDBNAME option
591 * Send use database here after login (Will not work with SQL Azure)
592 */
593#ifndef DBSETLDBNAME
594 if (vars[3].optval) {
595 if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
596 }
597#endif
598
599#ifdef PHP_DBLIB_IS_MSSQL
600 /* dblib do not return more than this length from text/image */
601 DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
602#endif
603
604 /* limit text/image from network */
605 DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
606
607 /* allow double quoted indentifiers */
608 DBSETOPT(H->link, DBQUOTEDIDENT, "1");
609
610 ret = 1;
612 dbh->alloc_own_columns = 1;
613
614cleanup:
615 for (i = 0; i < nvars; i++) {
616 if (vars[i].freeme) {
617 efree(vars[i].optval);
618 }
619 }
620
621 dbh->methods = &dblib_methods;
622 dbh->driver_data = H;
623
624 if (!ret) {
626 "SQLSTATE[%s] %s (severity %d)",
628 DBLIB_G(err).dberrstr,
629 DBLIB_G(err).severity);
630 }
631
632 return ret;
633}
634
636#ifdef PDO_DBLIB_IS_MSSQL
637 PDO_DRIVER_HEADER(mssql),
638#elif defined(PHP_WIN32)
639#define PDO_DBLIB_IS_SYBASE
640 PDO_DRIVER_HEADER(sybase),
641#else
642 PDO_DRIVER_HEADER(dblib),
643#endif
644 pdo_dblib_handle_factory
645};
size_t len
Definition apprentice.c:174
printf(string $format, mixed ... $values)
const pdo_driver_t pdo_dblib_driver
zval * pdo_dblib_datatypes
zend_string * dblib_handle_last_id(pdo_dbh_t *dbh, const zend_string *name)
const struct pdo_stmt_methods dblib_stmt_methods
Definition dblib_stmt.c:507
char * err
Definition ffi.c:3029
new_type attr
Definition ffi.c:4364
zval * val
Definition ffi.c:4262
#define NULL
Definition gdcache.h:45
#define S(s, l, r)
Definition hash_gost.c:121
#define H(x, y, z)
Definition md5.c:146
PDO_API int php_pdo_parse_data_source(const char *data_source, zend_ulong data_source_len, struct pdo_data_src_parser *parsed, int nparams)
Definition pdo.c:157
PDO_API zend_class_entry * php_pdo_get_exception(void)
Definition pdo.c:62
PDO_API bool pdo_get_long_param(zend_long *lval, zval *value)
Definition pdo_dbh.c:774
void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlstate, const char *supp)
Definition pdo_dbh.c:68
PDO_API bool pdo_get_bool_param(bool *bval, zval *value)
Definition pdo_dbh.c:792
int pdo_dblib_error_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
Definition pdo_dblib.c:81
void pdo_dblib_err_dtor(pdo_dblib_err *err)
Definition pdo_dblib.c:147
int pdo_dblib_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
Definition pdo_dblib.c:126
#define DBERRHANDLE(a, b)
#define DBMSGHANDLE(a, b)
#define DBLIB_G(v)
#define DBSETOPT(a, b, c)
#define SQLCHAR
@ PDO_DBLIB_ATTR_VERSION
@ PDO_DBLIB_ATTR_QUERY_TIMEOUT
@ PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS
@ PDO_DBLIB_ATTR_DATETIME_CONVERT
@ PDO_DBLIB_ATTR_TDS_VERSION
@ PDO_DBLIB_ATTR_CONNECTION_TIMEOUT
@ PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER
#define dbfreelogin
char sqlstate[6]
#define PDO_DBLIB_FLAVOUR
struct _pdo_dbh_t pdo_dbh_t
#define PDO_DRIVER_HEADER(name)
struct _pdo_stmt_t pdo_stmt_t
pdo_param_type
@ PDO_PARAM_STR_NATL
@ PDO_PARAM_STR_CHAR
@ PDO_PLACEHOLDER_NONE
@ PDO_ATTR_EMULATE_PREPARES
@ PDO_ATTR_DEFAULT_STR_PARAM
@ PDO_ATTR_TIMEOUT
unsigned char key[REFLECTION_KEY_LEN]
char * msg
Definition phpdbg.h:289
#define spprintf
Definition spprintf.h:29
#define FAIL(...)
const char * data_source
unsigned max_escaped_char_length
unsigned is_persistent
size_t data_source_len
void * driver_data
pdo_error_type error_code
unsigned alloc_own_columns
const struct pdo_dbh_methods * methods
zend_string * active_query_string
ZEND_API zend_result add_next_index_long(zval *arg, zend_long n)
Definition zend_API.c:2132
ZEND_API zend_result add_next_index_string(zval *arg, const char *str)
Definition zend_API.c:2186
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define pestrdup(s, persistent)
Definition zend_alloc.h:206
#define ecalloc(nmemb, size)
Definition zend_alloc.h:158
#define efree(ptr)
Definition zend_alloc.h:155
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pecalloc(nmemb, size, persistent)
Definition zend_alloc.h:200
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strcmp(string $string1, string $string2)
ZEND_API ZEND_COLD zend_object * zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format,...)
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
#define UNEXPECTED(condition)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_MAX_LEN
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define ZVAL_FALSE(z)
#define ZVAL_TRUE(z)
#define ZVAL_LONG(z, l)
#define ZVAL_BOOL(z, b)
zval * return_value
zend_string * name
zval * ret