class DMLParser extends Parser

This class allows you to store and retrieve data from files in JSON format using SQL standard.

  • The data are described by a json schema in compliance with the spécifications of http://json-schema.org
  • This schema can be generated on this site: http://jsonschema.net

  • The API is very similar to PDO

  • The JSON schema is saved in a file whose name is in the form .schema.json

  • The data is saved in a file whose name is in the form .json

Constants

SQL_SELECT_KEYWORD

SQL_FROM_KEYWORD

SQL_WHERE_KEYWORD

SQL_ORDER_BY_KEYWORD

SQL_LIMIT_KEYWORD

SQL_UPDATE_KEYWORD

SQL_CREATE_KEYWORD

SQL_DELETE_KEYWORD

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

protected JsonSQL $jsonsql A pointer on the JsonSQL owner. from Parser
protected Engine $engine A pointer on JSON database engine. from Parser
protected string $sql The sql request. from Parser
private array $synonyms Conversion table of SQL functions in PHP functions
private array $allowed Allowed PHP functions in conditions

Methods

void
__construct(JsonSQL $jsonsql, string $sql)

Constructor of class DMLParser

static stdClass
create(JsonSQL $jsonsql, $sql)

Parser factory.

from Parser
parse()

Parses the sql statement.

from Parser
array
parseSelect(string $sql)

Parses a sql select request according to this BNF syntax :

array
parseSetOperations(string $sql)

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

array
parseInsert(string $sql)

Parses a sql insert into statement according to this BNF syntax :

array
parseUpdate(string $sql)

Parses a sql update statement according to this BNF syntax :

array
parseDelete(string $sql)

Parses a sql delete from statement according to this BNF syntax : DELETE FROM table_name [ WHERE condition ]

array
parseTruncate(string $sql)

Parses a sql truncate table statement according to this BNF syntax :

string
parseExpression(string $expression, string $columns)

Parses and converts a sql expression into a php one

string
parseConditions(string $conditions, string $columns, array $select = array())

Parses and converts sql conditions into a php one

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
replaceSynonyms(string $expr)

Replaces into an expression, the SQL functions by their PHP equivalents

Details

at line 172
void __construct(JsonSQL $jsonsql, string $sql)

Constructor of class DMLParser

Parameters

JsonSQL $jsonsql The JsonSQL instance
string $sql The sql request

Return Value

void

in Parser at line 134
static stdClass create(JsonSQL $jsonsql, $sql)

Parser factory.

Parameters

JsonSQL $jsonsql
$sql

Return Value

stdClass The parsed request

Exceptions

JsonSQLException

in Parser at line 165
stdClass parse()

Parses the sql statement.

Return Value

stdClass The parsed request

Exceptions

JsonSQLException

at line 201
protected array 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

array The parsed request

Exceptions

JsonSQLException

at line 414
protected array 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

array The parsed request

Exceptions

JsonSQLException

at line 468
protected array parseInsert(string $sql)

Parses a sql insert into statement according to this BNF syntax :

INSERT INTO table_name ( [ '(' column_name { ', ' column_name } ')' ] VALUES '(' ( expression | DEFAULT ) {', ' ( expression | DEFAULT ) } ')' {', ' '(' ( expression | DEFAULT ) {', ' ( expression | DEFAULT ) } ')' } | select_statement )

or eBNF syntax :

'INSERT INTO' table_name ( ( '(' column_name ( ', ' column_name ) * ')' ) ? 'VALUES' '(' ( expression | 'DEFAULT' ) ( ', ' ( expression | 'DEFAULT' ) ) * ')' ( ', ' '(' ( expression | 'DEFAULT' ) ( ', ' ( expression | 'DEFAULT' ) ) * ')' ) * | select_statement )

Parameters

string $sql The insert into statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 563
protected array parseUpdate(string $sql)

Parses a sql update statement according to this BNF syntax :

UPDATE table_name SET column_name = expression { ', ' column_name = expression } [ WHERE condition ]

or eBNF syntax :

'UPDATE' table_name 'SET' column_name '=' expression ( ', ' column_name '=' expression ) * ( 'WHERE' condition ) ?

Parameters

string $sql The update statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 614
protected array parseDelete(string $sql)

Parses a sql delete from statement according to this BNF syntax : DELETE FROM table_name [ WHERE condition ]

or eBNF syntax : 'DELETE FROM' table_name ( 'WHERE' condition ) ?

Parameters

string $sql The delete from statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 648
protected array parseTruncate(string $sql)

Parses a sql truncate table statement according to this BNF syntax :

TRUNCATE TABLE table_name { ', ' table_name }

or eBNF syntax :

'TRUNCATE TABLE' table_name ( ', ' table_name ) *

Parameters

string $sql The truncate table statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 670
protected string parseExpression(string $expression, string $columns)

Parses and converts a sql expression into a php one

Parameters

string $expression The expression to parse
string $columns The columns of the request

Return Value

string The parsed expression

at line 711
protected string parseConditions(string $conditions, string $columns, array $select = array())

Parses and converts sql conditions into a php one

Parameters

string $conditions The conditions to parse
string $columns The columns of the request
array $select The select list of the request

Return Value

string The converted conditions

at line 780
void checkSafety(string $expression)

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

Parameters

string $expression The php expression to check

Return Value

void

Exceptions

JsonSQLException

at line 808
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 822
private string replaceSynonyms(string $expr)

Replaces into an expression, the SQL functions by their PHP equivalents

Parameters

string $expr The SQL expression

Return Value

string The new expression with PHP functions.