class SQLSelectTokenizer

This class allows the tokenization of an SQL select request.

Constants

CROSS_JOIN

Types of JOINS : CROSS_JOIN: Returns the Cartesian product of the sets of rows from the joined tables

INNER_JOIN

INNER_JOIN: Returns all rows when there is at least one match in BOTH tables

LEFT_JOIN

LEFT_JOIN: Return all rows from the left table, and the matched rows from the right table

RIGHT_JOIN

RIGHT_JOIN: Return all rows from the right table, and the matched rows from the left table

Properties

private array $allowed Allowed PHP functions in conditions
private array|null $tables

Methods

void
__construct(array $tables = null)

Constructor of class SQLSelectTokenizer

void
setTables(array $tables)

Sets the list of tables of the SQL select

void
checkSafety(string $expression)

Verifies that an php expression is sufficiently secured before being evaluated by the eval function

bool
isExpression(string $string)

Checks if a string contains an expression.

string
parseExpression(string $expression)

Parses and converts a sql expression into a php one

string
parseConditions(string $conditions)

Parses and converts sql conditions into a php one

void
addTokenInCondition(stdClass $condition, Token $token)

Inserts a token into a condition

void
resetCondition(stdClass $condition)

Resets a condition

int
insertCondition(array $conditions, stdClass $condition)

Inserts a condition into a condition array if it does not already exist and returns its position in the array.

parseWhere(string $where)

Parses a where clause

object
parseSelect(string $sql)

Parses a sql select request according to this BNF syntax :

object
parseSetOperations(string $sql)

Parses a sql compound select request containing set operations according to this BNF syntax :

Details

at line 193
void __construct(array $tables = null)

Constructor of class SQLSelectTokenizer

Parameters

array $tables (default: null) The list of tables of the SQL select

Return Value

void

at line 205
void setTables(array $tables)

Sets the list of tables of the SQL select

Parameters

array $tables The list of tables of the SQL select

Return Value

void

at line 218
void checkSafety(string $expression)

Verifies that an php expression is sufficiently secured before being evaluated by the eval function

Parameters

string $expression php expression to check

Return Value

void

Exceptions

SQLSelectTokenizerException

at line 246
protected bool isExpression(string $string)

Checks if a string contains an expression.

Parameters

string $string The string to check

Return Value

bool true if the string contains an expression, and false if not.

at line 260
protected string parseExpression(string $expression)

Parses and converts a sql expression into a php one

Parameters

string $expression The expression to parse

Return Value

string The parsed expression

at line 279
protected string parseConditions(string $conditions)

Parses and converts sql conditions into a php one

Parameters

string $conditions The conditions to parse

Return Value

string The converted conditions

at line 299
protected void addTokenInCondition(stdClass $condition, Token $token)

Inserts a token into a condition

Parameters

stdClass $condition &$condition The target condition
Token $token The token to be inserted

Return Value

void

at line 325
protected void resetCondition(stdClass $condition)

Resets a condition

Parameters

stdClass $condition &$condition The condition to reset

Return Value

void

at line 343
protected int insertCondition(array $conditions, stdClass $condition)

Inserts a condition into a condition array if it does not already exist and returns its position in the array.

Parameters

array $conditions &$conditions The target array of conditions
stdClass $condition The condition to be inserted

Return Value

int The position of the condition in the array.

at line 367
protected stdClass parseWhere(string $where)

Parses a where clause

Parameters

string $where The where clause

Return Value

stdClass The parsed where clause

at line 463
object parseSelect(string $sql)

Parses a sql select request according to this BNF syntax :

SELECT [ ALL | DISTINCT ] ( expression [ AS alias ] | * | table_name.* ) {',' ( expression [ AS alias ] | * | table_name.* ) } FROM table_name [ AS alias ] {',' table_name [ AS alias ]} { [ CROSS | INNER | LEFT [OUTER] | RIGHT [OUTER] ] JOIN table_name [ AS alias ] [ ON condition ] } [ WHERE condition ] [ GROUP BY expression {',' expression} ] [ HAVING condition ] [ ORDER BY expression [ ASC | DESC ] {',' expression [ ASC | DESC ]} ] [LIMIT {[offset,] row_count | row_count OFFSET offset}]

or eBNF syntax :

'SELECT' ( 'ALL' | 'DISTINCT' ) ? ( expression ( 'AS' ? alias ) ? | '' | table_name '.' ) ( ',' ( expression ( 'AS' ? alias ) ? | '' | table_name '.' ) ) * 'FROM' table_name ( 'AS' ? alias ) ? ( ',' table_name ( 'AS' ? alias ) ? ) * ( ( ( 'CROSS' | 'INNER' | 'LEFT' ( 'OUTER' ) ? | 'RIGHT' ( 'OUTER' ) ? ) ? 'JOIN' table_name ( 'AS' ? alias ) ? ( 'ON' condition ) ? ) ) * ( 'WHERE' condition ) ? ( 'GROUP BY' expression ( ',' expression ) * ( 'HAVING' condition ) ? ) ? ( 'ORDER BY' expression ( 'ASC' | 'DESC' ) ? ( ',' expression ( 'ASC' | 'DESC' ) ? ) * ) ? ( 'LIMIT' ( ( offset ',' ) ? row_count | row_count 'OFFSET' offset ) ) ?

Parameters

string $sql The select statement

Return Value

object The parsed request

Exceptions

SQLSelectTokenizerException

at line 694
object parseSetOperations(string $sql)

Parses a sql compound select request containing set operations according to this BNF syntax :

select_statement [ UNION | UNION ALL | INTERSECT | EXCEPT | MINUS ] select_statement { select_statement [ UNION | UNION ALL | INTERSECT | EXCEPT | MINUS ] select_statement }

or eBNF syntax :

select_statement (( 'UNION' | 'UNION ALL' | 'INTERSECT' | 'EXCEPT' | 'MINUS' ) select_statement) *

if the statement contains no set operator, just parses the select request

Parameters

string $sql The select statement

Return Value

object The parsed request

Exceptions

SQLSelectTokenizerException