php-src/ext/pdo/pdo_sql_parser.c

770 lines
17 KiB
C
Raw Normal View History

2008-11-14 02:14:50 +08:00
/* Generated by re2c 0.13.6.dev on Thu Nov 13 14:47:06 2008 */
2005-09-11 04:58:57 +08:00
#line 1 "ext/pdo/pdo_sql_parser.re"
2005-02-10 01:06:13 +08:00
/*
2004-05-19 03:30:43 +08:00
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
2004-05-19 03:30:43 +08:00
+----------------------------------------------------------------------+
2006-01-01 20:51:34 +08:00
| This source file is subject to version 3.01 of the PHP license, |
2004-05-19 03:30:43 +08:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 20:51:34 +08:00
| http://www.php.net/license/3_01.txt |
2004-05-19 03:30:43 +08:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: George Schlossnagle <george@omniti.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
2004-05-18 23:38:25 +08:00
#include "php.h"
#include "php_pdo_driver.h"
#include "php_pdo_int.h"
2004-05-18 23:38:25 +08:00
2004-05-18 23:58:17 +08:00
#define PDO_PARSER_TEXT 1
#define PDO_PARSER_BIND 2
#define PDO_PARSER_BIND_POS 3
#define PDO_PARSER_EOI 4
2004-05-18 23:38:25 +08:00
#define RET(i) {s->cur = cursor; return i; }
#define SKIP_ONE(i) {s->cur = s->tok + 1; return 1; }
2004-05-18 23:38:25 +08:00
#define YYCTYPE unsigned char
2004-05-18 23:38:25 +08:00
#define YYCURSOR cursor
#define YYLIMIT cursor
2004-05-18 23:38:25 +08:00
#define YYMARKER s->ptr
#define YYFILL(n)
typedef struct Scanner {
char *ptr, *cur, *tok;
2004-05-18 23:38:25 +08:00
} Scanner;
static int scan(Scanner *s)
{
char *cursor = s->cur;
2005-07-09 04:38:13 +08:00
s->tok = cursor;
#line 55 "ext/pdo/pdo_sql_parser.re"
2004-05-18 23:38:25 +08:00
#line 55 "ext/pdo/pdo_sql_parser.c"
{
YYCTYPE yych;
2007-02-02 08:03:33 +08:00
if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
switch (yych) {
case 0x00: goto yy11;
case '"': goto yy2;
case '\'': goto yy4;
case ':': goto yy5;
case '?': goto yy6;
default: goto yy8;
}
yy2:
yych = *(YYMARKER = ++YYCURSOR);
if (yych >= 0x01) goto yy26;
2004-05-18 23:38:25 +08:00
yy3:
#line 63 "ext/pdo/pdo_sql_parser.re"
{ SKIP_ONE(PDO_PARSER_TEXT); }
#line 75 "ext/pdo/pdo_sql_parser.c"
yy4:
yych = *(YYMARKER = ++YYCURSOR);
if (yych <= 0x00) goto yy3;
goto yy20;
yy5:
yych = *++YYCURSOR;
switch (yych) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '_':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy16;
case ':':
case '?': goto yy13;
default: goto yy3;
}
yy6:
++YYCURSOR;
switch ((yych = *YYCURSOR)) {
case ':':
case '?': goto yy13;
default: goto yy7;
}
yy7:
#line 62 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_BIND_POS); }
#line 160 "ext/pdo/pdo_sql_parser.c"
yy8:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
switch (yych) {
case 0x00:
case '"':
case '\'':
case ':':
case '?': goto yy10;
default: goto yy8;
}
yy10:
#line 64 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_TEXT); }
#line 176 "ext/pdo/pdo_sql_parser.c"
yy11:
++YYCURSOR;
#line 65 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_EOI); }
#line 181 "ext/pdo/pdo_sql_parser.c"
yy13:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
switch (yych) {
case ':':
case '?': goto yy13;
default: goto yy15;
}
yy15:
#line 60 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_TEXT); }
#line 194 "ext/pdo/pdo_sql_parser.c"
yy16:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
switch (yych) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '_':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy16;
default: goto yy18;
}
yy18:
#line 61 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_BIND); }
#line 268 "ext/pdo/pdo_sql_parser.c"
yy19:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
yy20:
switch (yych) {
case 0x00: goto yy21;
case '\'': goto yy23;
case '\\': goto yy22;
default: goto yy19;
}
yy21:
YYCURSOR = YYMARKER;
goto yy3;
yy22:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
if (yych <= 0x00) goto yy21;
goto yy19;
yy23:
++YYCURSOR;
#line 59 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_TEXT); }
#line 293 "ext/pdo/pdo_sql_parser.c"
yy25:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
yy26:
switch (yych) {
case 0x00: goto yy21;
case '"': goto yy28;
case '\\': goto yy27;
default: goto yy25;
2007-02-02 08:03:33 +08:00
}
yy27:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
if (yych <= 0x00) goto yy21;
goto yy25;
yy28:
++YYCURSOR;
#line 58 "ext/pdo/pdo_sql_parser.re"
{ RET(PDO_PARSER_TEXT); }
#line 315 "ext/pdo/pdo_sql_parser.c"
2005-07-19 00:08:37 +08:00
}
#line 66 "ext/pdo/pdo_sql_parser.re"
}
struct placeholder {
char *pos;
int len;
int bindno;
int qlen; /* quoted length of value */
char *quoted; /* quoted value */
int freeq;
struct placeholder *next;
};
PDO_API int pdo_parse_params(pdo_stmt_t *stmt, char *inquery, int inquery_len,
char **outquery, int *outquery_len TSRMLS_DC)
{
Scanner s;
char *ptr, *newbuffer;
int t;
int bindno = 0;
int ret = 0;
int newbuffer_len;
HashTable *params;
struct pdo_bound_param_data *param;
int query_type = PDO_PLACEHOLDER_NONE;
struct placeholder *placeholders = NULL, *placetail = NULL, *plc = NULL;
ptr = *outquery;
s.cur = inquery;
/* phase 1: look for args */
while((t = scan(&s)) != PDO_PARSER_EOI) {
if (t == PDO_PARSER_BIND || t == PDO_PARSER_BIND_POS) {
if (t == PDO_PARSER_BIND) {
int len = s.cur - s.tok;
if ((inquery < (s.cur - len)) && isalnum(*(s.cur - len - 1))) {
continue;
}
query_type |= PDO_PLACEHOLDER_NAMED;
} else {
query_type |= PDO_PLACEHOLDER_POSITIONAL;
}
plc = emalloc(sizeof(*plc));
memset(plc, 0, sizeof(*plc));
plc->next = NULL;
plc->pos = s.tok;
plc->len = s.cur - s.tok;
plc->bindno = bindno++;
if (placetail) {
placetail->next = plc;
} else {
placeholders = plc;
}
placetail = plc;
}
}
if (bindno == 0) {
/* nothing to do; good! */
return 0;
}
/* did the query make sense to me? */
if (query_type == (PDO_PLACEHOLDER_NAMED|PDO_PLACEHOLDER_POSITIONAL)) {
/* they mixed both types; punt */
2005-02-07 08:26:38 +08:00
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "mixed named and positional parameters" TSRMLS_CC);
ret = -1;
goto clean_up;
}
if (stmt->supports_placeholders == query_type && !stmt->named_rewrite_template) {
/* query matches native syntax */
ret = 0;
goto clean_up;
}
if (stmt->named_rewrite_template) {
/* magic/hack.
* We we pretend that the query was positional even if
* it was named so that we fall into the
* named rewrite case below. Not too pretty,
* but it works. */
query_type = PDO_PLACEHOLDER_POSITIONAL;
}
params = stmt->bound_params;
/* Do we have placeholders but no bound params */
2005-02-13 14:30:36 +08:00
if (bindno && !params && stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
2005-02-07 08:26:38 +08:00
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "no parameters were bound" TSRMLS_CC);
ret = -1;
goto clean_up;
}
if (params && bindno != zend_hash_num_elements(params) && stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
/* extra bit of validation for instances when same params are bound more then once */
if (query_type != PDO_PLACEHOLDER_POSITIONAL && bindno > zend_hash_num_elements(params)) {
int ok = 1;
for (plc = placeholders; plc; plc = plc->next) {
if (zend_hash_find(params, plc->pos, plc->len, (void**) &param) == FAILURE) {
ok = 0;
break;
}
}
if (ok) {
goto safe;
}
}
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "number of bound variables does not match number of tokens" TSRMLS_CC);
ret = -1;
goto clean_up;
}
safe:
/* what are we going to do ? */
if (stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
/* query generation */
newbuffer_len = inquery_len;
/* let's quote all the values */
for (plc = placeholders; plc; plc = plc->next) {
if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
ret = zend_hash_index_find(params, plc->bindno, (void**) &param);
} else {
ret = zend_hash_find(params, plc->pos, plc->len, (void**) &param);
}
if (ret == FAILURE) {
/* parameter was not defined */
ret = -1;
2005-02-07 08:26:38 +08:00
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined" TSRMLS_CC);
goto clean_up;
}
if (stmt->dbh->methods->quoter) {
2005-02-07 08:26:38 +08:00
if (param->param_type == PDO_PARAM_LOB && Z_TYPE_P(param->parameter) == IS_RESOURCE) {
php_stream *stm;
php_stream_from_zval_no_verify(stm, &param->parameter);
if (stm) {
size_t len;
char *buf = NULL;
len = php_stream_copy_to_mem(stm, &buf, PHP_STREAM_COPY_ALL, 0);
if (!stmt->dbh->methods->quoter(stmt->dbh, buf, len, &plc->quoted, &plc->qlen,
param->param_type TSRMLS_CC)) {
/* bork */
ret = -1;
strcpy(stmt->error_code, stmt->dbh->error_code);
2006-11-23 22:41:35 +08:00
if (buf) {
efree(buf);
}
2005-02-07 08:26:38 +08:00
goto clean_up;
}
2006-11-23 22:41:35 +08:00
if (buf) {
efree(buf);
}
2005-02-07 08:26:38 +08:00
} else {
pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
ret = -1;
goto clean_up;
}
2005-02-26 23:45:43 +08:00
plc->freeq = 1;
2005-02-07 08:26:38 +08:00
} else {
2005-02-26 23:45:43 +08:00
switch (Z_TYPE_P(param->parameter)) {
case IS_NULL:
plc->quoted = "NULL";
plc->qlen = sizeof("NULL")-1;
plc->freeq = 0;
break;
case IS_LONG:
case IS_DOUBLE:
convert_to_string(param->parameter);
plc->qlen = Z_STRLEN_P(param->parameter);
2005-02-27 00:03:55 +08:00
plc->quoted = Z_STRVAL_P(param->parameter);
2005-02-26 23:45:43 +08:00
plc->freeq = 0;
break;
2005-09-11 04:58:57 +08:00
case IS_BOOL:
convert_to_long(param->parameter);
2005-02-26 23:45:43 +08:00
default:
convert_to_string(param->parameter);
if (!stmt->dbh->methods->quoter(stmt->dbh, Z_STRVAL_P(param->parameter),
Z_STRLEN_P(param->parameter), &plc->quoted, &plc->qlen,
param->param_type TSRMLS_CC)) {
/* bork */
ret = -1;
strcpy(stmt->error_code, stmt->dbh->error_code);
goto clean_up;
}
plc->freeq = 1;
2005-02-07 08:26:38 +08:00
}
}
} else {
plc->quoted = Z_STRVAL_P(param->parameter);
plc->qlen = Z_STRLEN_P(param->parameter);
}
newbuffer_len += plc->qlen;
}
rewrite:
/* allocate output buffer */
newbuffer = emalloc(newbuffer_len + 1);
*outquery = newbuffer;
/* and build the query */
plc = placeholders;
ptr = inquery;
do {
t = plc->pos - ptr;
if (t) {
memcpy(newbuffer, ptr, t);
newbuffer += t;
}
memcpy(newbuffer, plc->quoted, plc->qlen);
newbuffer += plc->qlen;
ptr = plc->pos + plc->len;
plc = plc->next;
} while (plc);
t = (inquery + inquery_len) - ptr;
if (t) {
memcpy(newbuffer, ptr, t);
newbuffer += t;
}
*newbuffer = '\0';
*outquery_len = newbuffer - *outquery;
ret = 1;
goto clean_up;
} else if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
/* rewrite ? to :pdoX */
2008-11-14 02:14:50 +08:00
char *name, *idxbuf;
const char *tmpl = stmt->named_rewrite_template ? stmt->named_rewrite_template : ":pdo%d";
int bind_no = 1;
newbuffer_len = inquery_len;
2005-07-12 11:20:08 +08:00
if (stmt->bound_param_map == NULL) {
ALLOC_HASHTABLE(stmt->bound_param_map);
zend_hash_init(stmt->bound_param_map, 13, NULL, NULL, 0);
}
for (plc = placeholders; plc; plc = plc->next) {
int skip_map = 0;
char *p;
name = estrndup(plc->pos, plc->len);
/* check if bound parameter is already available */
if (!strcmp(name, "?") || zend_hash_find(stmt->bound_param_map, name, plc->len + 1, (void**) &p) == FAILURE) {
2008-11-14 02:14:50 +08:00
spprintf(&idxbuf, 0, tmpl, bind_no++);
} else {
2008-11-14 02:14:50 +08:00
idxbuf = estrdup(p);
skip_map = 1;
}
2008-11-14 02:14:50 +08:00
plc->quoted = idxbuf;
plc->qlen = strlen(plc->quoted);
plc->freeq = 1;
newbuffer_len += plc->qlen;
2005-07-09 01:00:48 +08:00
if (!skip_map && stmt->named_rewrite_template) {
2005-07-09 01:00:48 +08:00
/* create a mapping */
zend_hash_update(stmt->bound_param_map, name, plc->len + 1, idxbuf, plc->qlen + 1, NULL);
}
2005-07-12 11:20:08 +08:00
/* map number to name */
zend_hash_index_update(stmt->bound_param_map, plc->bindno, idxbuf, plc->qlen + 1, NULL);
efree(name);
}
goto rewrite;
} else {
/* rewrite :name to ? */
2005-02-13 14:30:36 +08:00
newbuffer_len = inquery_len;
if (stmt->bound_param_map == NULL) {
ALLOC_HASHTABLE(stmt->bound_param_map);
zend_hash_init(stmt->bound_param_map, 13, NULL, NULL, 0);
}
for (plc = placeholders; plc; plc = plc->next) {
char *name;
name = estrndup(plc->pos, plc->len);
zend_hash_index_update(stmt->bound_param_map, plc->bindno, name, plc->len + 1, NULL);
efree(name);
plc->quoted = "?";
plc->qlen = 1;
}
2005-02-13 14:30:36 +08:00
goto rewrite;
}
clean_up:
while (placeholders) {
plc = placeholders;
placeholders = plc->next;
if (plc->freeq) {
efree(plc->quoted);
}
efree(plc);
}
return ret;
2004-05-18 23:38:25 +08:00
}
#if 0
int old_pdo_parse_params(pdo_stmt_t *stmt, char *inquery, int inquery_len, char **outquery,
2004-05-18 23:58:17 +08:00
int *outquery_len TSRMLS_DC)
2004-05-18 23:38:25 +08:00
{
Scanner s;
char *ptr;
int t;
int bindno = 0;
int newbuffer_len;
2004-05-19 03:30:43 +08:00
int padding;
2004-05-18 23:38:25 +08:00
HashTable *params = stmt->bound_params;
struct pdo_bound_param_data *param;
/* allocate buffer for query with expanded binds, ptr is our writing pointer */
newbuffer_len = inquery_len;
2004-05-19 03:30:43 +08:00
/* calculate the possible padding factor due to quoting */
if(stmt->dbh->max_escaped_char_length) {
padding = stmt->dbh->max_escaped_char_length;
} else {
padding = 3;
}
2004-05-18 23:38:25 +08:00
if(params) {
zend_hash_internal_pointer_reset(params);
while (SUCCESS == zend_hash_get_current_data(params, (void**)&param)) {
if(param->parameter) {
convert_to_string(param->parameter);
/* accomodate a string that needs to be fully quoted
bind placeholders are at least 2 characters, so
the accomodate their own "'s
*/
2004-05-19 03:30:43 +08:00
newbuffer_len += padding * Z_STRLEN_P(param->parameter);
2004-05-18 23:38:25 +08:00
}
zend_hash_move_forward(params);
}
}
*outquery = (char *) emalloc(newbuffer_len + 1);
*outquery_len = 0;
ptr = *outquery;
s.cur = inquery;
2004-05-18 23:58:17 +08:00
while((t = scan(&s)) != PDO_PARSER_EOI) {
if(t == PDO_PARSER_TEXT) {
2004-05-18 23:38:25 +08:00
memcpy(ptr, s.tok, s.cur - s.tok);
ptr += (s.cur - s.tok);
*outquery_len += (s.cur - s.tok);
}
2004-05-18 23:58:17 +08:00
else if(t == PDO_PARSER_BIND) {
2004-05-18 23:38:25 +08:00
if(!params) {
/* error */
efree(*outquery);
2004-05-21 02:44:12 +08:00
*outquery = NULL;
return (int) (s.cur - inquery);
2004-05-18 23:38:25 +08:00
}
/* lookup bind first via hash and then index */
/* stupid keys need to be null-terminated, even though we know their length */
if((SUCCESS == zend_hash_find(params, s.tok, s.cur-s.tok,(void **)&param))
2004-05-18 23:38:25 +08:00
||
(SUCCESS == zend_hash_index_find(params, bindno, (void **)&param)))
{
char *quotedstr;
int quotedstrlen;
/* restore the in-string key, doesn't need null-termination here */
2004-05-18 23:38:25 +08:00
/* currently everything is a string here */
/* quote the bind value if necessary */
if(stmt->dbh->methods->quoter(stmt->dbh, Z_STRVAL_P(param->parameter),
Z_STRLEN_P(param->parameter), &quotedstr, &quotedstrlen TSRMLS_CC))
{
memcpy(ptr, quotedstr, quotedstrlen);
ptr += quotedstrlen;
*outquery_len += quotedstrlen;
efree(quotedstr);
} else {
memcpy(ptr, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter));
ptr += Z_STRLEN_P(param->parameter);
*outquery_len += (Z_STRLEN_P(param->parameter));
}
}
else {
/* error and cleanup */
efree(*outquery);
2004-05-21 02:44:12 +08:00
*outquery = NULL;
return (int) (s.cur - inquery);
}
bindno++;
}
else if(t == PDO_PARSER_BIND_POS) {
if(!params) {
/* error */
efree(*outquery);
2004-05-21 02:44:12 +08:00
*outquery = NULL;
return (int) (s.cur - inquery);
}
/* lookup bind by index */
if(SUCCESS == zend_hash_index_find(params, bindno, (void **)&param))
{
char *quotedstr;
int quotedstrlen;
/* currently everything is a string here */
/* quote the bind value if necessary */
if(stmt->dbh->methods->quoter(stmt->dbh, Z_STRVAL_P(param->parameter),
Z_STRLEN_P(param->parameter), &quotedstr, &quotedstrlen TSRMLS_CC))
{
memcpy(ptr, quotedstr, quotedstrlen);
ptr += quotedstrlen;
*outquery_len += quotedstrlen;
efree(quotedstr);
} else {
memcpy(ptr, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter));
ptr += Z_STRLEN_P(param->parameter);
*outquery_len += (Z_STRLEN_P(param->parameter));
}
}
else {
/* error and cleanup */
efree(*outquery);
2004-05-21 02:44:12 +08:00
*outquery = NULL;
return (int) (s.cur - inquery);
2004-05-18 23:38:25 +08:00
}
bindno++;
}
2004-05-18 23:38:25 +08:00
}
*ptr = '\0';
return 0;
2004-05-18 23:38:25 +08:00
}
#endif
2004-05-18 23:38:25 +08:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
2004-05-19 03:30:43 +08:00
* vim600: noet sw=4 ts=4 fdm=marker ft=c
2004-05-18 23:38:25 +08:00
* vim<600: noet sw=4 ts=4
*/