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_OFFSET_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 Parser
create(JsonSQL $jsonsql, $sql)

Parser factory.

from Parser
object
parse()

Parses the sql statement.

from Parser
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 :

object
parseInsert(string $sql)

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

object
parseUpdate(string $sql)

Parses a sql update statement according to this BNF syntax :

object
parseDelete(string $sql)

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

object
parseCreate(string $sql)

function parseCreate

object
parseAlter(string $sql)

function parseAlter

object
parseTruncate(string $sql)

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

object
parseDropTable(string $sql)

function parseDropTable

string
parseExpression(string $expression, array $columns)

Parses and converts a sql expression into a php one

string
parseConditions(string $conditions, array $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 138
static Parser create(JsonSQL $jsonsql, $sql)

Parser factory.

Parameters

JsonSQL $jsonsql
$sql

Return Value

Parser The parsed request

Exceptions

JsonSQLException

in Parser at line 169
object parse()

Parses the sql statement.

Return Value

object The parsed request

Exceptions

JsonSQLException

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

JsonSQLException

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

JsonSQLException

at line 468
protected object 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

object The parsed request

Exceptions

JsonSQLException

at line 564
protected object 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

object The parsed request

Exceptions

JsonSQLException

at line 615
protected object 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

object The parsed request

Exceptions

JsonSQLException

at line 843
protected object parseCreate(string $sql)

function parseCreate

Parameters

string $sql The create table statement

Return Value

object The parsed request

Exceptions

JsonSQLException

at line 856
protected object parseAlter(string $sql)

function parseAlter

Parameters

string $sql The create alter statement

Return Value

object The parsed request

Exceptions

JsonSQLException

at line 649
protected object 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

object The parsed request

Exceptions

JsonSQLException

at line 869
protected object parseDropTable(string $sql)

function parseDropTable

Parameters

string $sql The drop table statement

Return Value

object The parsed request

Exceptions

JsonSQLException

at line 671
protected string parseExpression(string $expression, array $columns)

Parses and converts a sql expression into a php one

Parameters

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

Return Value

string The parsed expression

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

Parses and converts sql conditions into a php one

Parameters

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

Return Value

string The converted conditions

at line 781
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 809
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 823
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.