mirror of
https://github.com/php/pecl-database-pdo_user.git
synced 2026-03-23 22:42:10 +01:00
Add recognition of * wildcard fields and :label :123 placeholders.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -81,8 +81,8 @@
|
||||
#define PU_UNSIGNED 81
|
||||
#define PU_ZEROFILL 82
|
||||
#define PU_TO 83
|
||||
#define PU_ON 84
|
||||
#define PU_DOT 85
|
||||
#define PU_DOT 84
|
||||
#define PU_ON 85
|
||||
#define PU_INNER 86
|
||||
#define PU_JOIN 87
|
||||
#define PU_OUTER 88
|
||||
@@ -91,6 +91,7 @@
|
||||
#define PU_NOT_EQUAL 91
|
||||
#define PU_UNEQUAL 92
|
||||
#define PU_LESSER_EQUAL 93
|
||||
#define PU_DNUM 94
|
||||
#define PU_ASC 95
|
||||
#define PU_DESC 96
|
||||
#define PU_COLON 94
|
||||
#define PU_DNUM 95
|
||||
#define PU_ASC 96
|
||||
#define PU_DESC 97
|
||||
|
||||
@@ -213,6 +213,17 @@ static zval *pusp_do_field(php_pdo_user_sql_token *database, php_pdo_user_sql_to
|
||||
return ret;
|
||||
}
|
||||
|
||||
static zval *pusp_do_placeholder(zval *placeholder)
|
||||
{
|
||||
zval *ret;
|
||||
|
||||
MAKE_STD_ZVAL(ret);
|
||||
array_init(ret);
|
||||
add_assoc_zval(ret, "placeholder", placeholder);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* ---------------- */
|
||||
|
||||
static inline zval *pusp_do_select_statement(zval *fieldlist, zval *tableexpr, zval *modifiers)
|
||||
@@ -481,6 +492,9 @@ fieldlist(R) ::= field(F). { MAKE_STD_ZVAL(R); array_init(R); add_next_index_zva
|
||||
|
||||
%destructor field { zval_ptr_dtor(&$$); }
|
||||
field(R) ::= expr(E). { R = E; }
|
||||
field(R) ::= LABEL(D) DOT LABEL(T) DOT MUL(F). { R = pusp_do_field(&D, &T, &F); }
|
||||
field(R) ::= LABEL(T) DOT MUL(F). { R = pusp_do_field(NULL, &T, &F); }
|
||||
field(R) ::= MUL(F). { R = pusp_do_field(NULL, NULL, &F); }
|
||||
field(R) ::= field(E) AS LABEL(A). { MAKE_STD_ZVAL(R); array_init(R); add_assoc_stringl(R, "type", "alias", sizeof("alias") - 1, 1); add_assoc_zval(R, "field", E); add_assoc_zval(R, "as", pusp_zvalize_token(&A)); }
|
||||
|
||||
%destructor tableexpr { zval_ptr_dtor(&$$); }
|
||||
@@ -564,6 +578,8 @@ expr(R) ::= literal(L). { R = L; }
|
||||
expr(R) ::= LABEL(D) DOT LABEL(T) DOT LABEL(F). { R = pusp_do_field(&D, &T, &F); }
|
||||
expr(R) ::= LABEL(T) DOT LABEL(F). { R = pusp_do_field(NULL, &T, &F); }
|
||||
expr(R) ::= LABEL(F). { R = pusp_do_field(NULL, NULL, &F); }
|
||||
expr(R) ::= COLON LABEL(L). { R = pusp_do_placeholder(pusp_zvalize_token(&L)); }
|
||||
expr(R) ::= COLON intnum(I). { R = pusp_do_placeholder(I); }
|
||||
expr(R) ::= LABEL(F) LPAREN exprlist(A) RPAREN. { R = pusp_do_function(pusp_zvalize_token(&F), A); }
|
||||
expr(R) ::= LPAREN expr(E) RPAREN. { R = E; }
|
||||
expr(R) ::= expr(A) PLUS expr(B). { DO_MATHOP(R,A,"+",B); }
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -152,7 +152,7 @@ int php_pdo_user_sql_get_token(php_pdo_user_sql_tokenizer *t, php_pdo_user_sql_t
|
||||
LNUMS = [0-9];
|
||||
HNUMS = [0-9A-Fa-f];
|
||||
LABELCHAR = [0-9a-zA-Z_\200-\377];
|
||||
LABELSTART = [a-zA-Z_\200-\377];
|
||||
LABELSTART = [a-zA-Z_:\200-\377];
|
||||
ANYNOEOF = [\001-\377];
|
||||
EOF = [\000];
|
||||
|
||||
@@ -264,6 +264,7 @@ int php_pdo_user_sql_get_token(php_pdo_user_sql_tokenizer *t, php_pdo_user_sql_t
|
||||
[=] { RET(PU_EQUALS); }
|
||||
[;] { RET(PU_SEMICOLON); }
|
||||
[.] { RET(PU_DOT); }
|
||||
[:] { RET(PU_COLON); }
|
||||
|
||||
([`] (ESCBT|ESCSEQ|ANYNOEOF\[\\`])* [`]){ RET_UNESC(PU_LABEL); }
|
||||
(["] (ESCQQ|ESCSEQ|ANYNOEOF\[\\"])* ["]){ RET_UNESC(PU_STRING); }
|
||||
|
||||
103
tests/parser.phpt
Normal file
103
tests/parser.phpt
Normal file
@@ -0,0 +1,103 @@
|
||||
--TEST--
|
||||
SQL Parser
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded('pdo_user')) {
|
||||
die('pdo_user not loaded');
|
||||
}
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
$sql = array(
|
||||
'SELECT foo FROM bar',
|
||||
'INSERT INTO bar (foo,baz) values(1,2),(2,3)',
|
||||
'SELECT *,bar.*,db.bar.* FROM bar WHERE `select` = `\x41BC \06123`',
|
||||
);
|
||||
foreach($sql as $s) {
|
||||
var_dump($s);
|
||||
print_r(PDO_User::parseSQL($s));
|
||||
}
|
||||
--EXPECT--
|
||||
string(19) "SELECT foo FROM bar"
|
||||
Array
|
||||
(
|
||||
[type] => statement
|
||||
[statement] => select
|
||||
[fields] => Array
|
||||
(
|
||||
[0] => foo
|
||||
)
|
||||
|
||||
[from] => bar
|
||||
[modifiers] =>
|
||||
[terminating-semicolon] =>
|
||||
)
|
||||
string(43) "INSERT INTO bar (foo,baz) values(1,2),(2,3)"
|
||||
Array
|
||||
(
|
||||
[type] => statement
|
||||
[statement] => insert
|
||||
[table] => bar
|
||||
[fields] => Array
|
||||
(
|
||||
[0] => foo
|
||||
[1] => baz
|
||||
)
|
||||
|
||||
[data] => Array
|
||||
(
|
||||
[0] => Array
|
||||
(
|
||||
[0] => 1
|
||||
[1] => 2
|
||||
)
|
||||
|
||||
[1] => Array
|
||||
(
|
||||
[0] => 2
|
||||
[1] => 3
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[terminating-semicolon] =>
|
||||
)
|
||||
string(65) "SELECT *,bar.*,db.bar.* FROM bar WHERE `select` = `\x41BC \06123`"
|
||||
Array
|
||||
(
|
||||
[type] => statement
|
||||
[statement] => select
|
||||
[fields] => Array
|
||||
(
|
||||
[0] => *
|
||||
[1] => Array
|
||||
(
|
||||
[table] => bar
|
||||
[field] => *
|
||||
)
|
||||
|
||||
[2] => Array
|
||||
(
|
||||
[database] => db
|
||||
[table] => bar
|
||||
[field] => *
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[from] => bar
|
||||
[modifiers] => Array
|
||||
(
|
||||
[where] => Array
|
||||
(
|
||||
[type] => condition
|
||||
[op1] => select
|
||||
[condition] => =
|
||||
[op2] => ABC 123
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[terminating-semicolon] =>
|
||||
)
|
||||
Reference in New Issue
Block a user