class DDLParser 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

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 $datatypes Conversion table of SQL data types in JSON data types

Methods

void
__construct(JsonSQL $jsonsql, string $sql)

Constructor of class DDLParser

static stdClass
create(JsonSQL $jsonsql, $sql)

Parser factory.

from Parser
parse()

Parses the sql statement.

from Parser
array
parseCreate(string $sql)

Parses a sql create table statement according to this two BNF syntax :

void
fillTableField(string $table, string $field, array $scolumns)

Copy the definitions of columns

array
parseAlter(string $sql)

Parses a sql alter table statement according to this two BNF syntax :

array
parseDropTable(string $sql)

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

string
encodeLiteral(string $text)

Encode text between quote with base64

string
decodeLiteral($text, $withQuotes = false)

Decode text encoded with base64

Details

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

Constructor of class DDLParser

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 141
protected array parseCreate(string $sql)

Parses a sql create table statement according to this two BNF syntax :

CREATE [ LOCAL | GLOBAL ] TABLE [ IF NOT EXISTS ] table_name ( column_name datatype [ CONSTRAINT constraint_name] [ NOT NULL|NULLABLE ] [ DEFAULT default ] [ PRIMARY KEY ] [ AUTOINCREMENT|AUTO_INCREMENT|SERIAL ] { ', ' column_name datatype [ CONSTRAINT constraint_name] [ NOT NULL|NULLABLE ] [ DEFAULT default ] [ PRIMARY KEY ] [ AUTOINCREMENT|AUTO_INCREMENT|SERIAL ] } { ', ' FOREIGN KEY (column_name { ', ' column_name} ) REFERENCES table_name (column_name { ', ' column_name} ) } [ ', ' PRIMARY KEY (column_name { ', ' column_name} ) ] )

or

CREATE [ LOCAL | GLOBAL ] TABLE [ IF NOT EXISTS ] table_name [ (column_name, { ', ' column_name }) ] AS select_statement [ WITH [ NO ] DATA ]

or eBNF syntax :

('CREATE' ( 'LOCAL' | 'GLOBAL' ) ? 'TABLE' ( 'IF NOT EXISTS' ) ? table_name '(' column_name datatype ( 'CONSTRAINT' constraint_name ) ? ( 'NOT NULL' | 'NULLABLE' ) ? ( 'DEFAULT' default ) ? ( 'PRIMARY KEY' ) ? ( 'AUTOINCREMENT' | 'AUTO_INCREMENT' | 'SERIAL' ) ? ( ', ' column_name datatype ( 'CONSTRAINT' constraint_name ) ? ( 'NOT NULL' | 'NULLABLE' ) ? ( 'DEFAULT' default ) ? ( 'PRIMARY KEY' ) ? ( 'AUTOINCREMENT' | 'AUTO_INCREMENT' | 'SERIAL' ) ? ) * ( ', ' 'FOREIGN KEY' '(' column_name ( ', ' column_name ) * ')' 'REFERENCES' table_name '(' column_name ( ', ' column_name ) * ')' ) * ( ', ' 'PRIMARY KEY' '(' column_name ( ', ' column_name ) * ')' ) ? ')' | 'CREATE' ( 'LOCAL' | 'GLOBAL' ) ? 'TABLE' ( 'IF NOT EXISTS' ) ? table_name ( '(' column_name ( ', ' column_name ) * ')' ) ? 'AS' select_statement ( 'WITH' ( 'NO' ) ? 'DATA' ) ? )

Parameters

string $sql The create table statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 352
private void fillTableField(string $table, string $field, array $scolumns)

Copy the definitions of columns

Parameters

string $table The name of the table
string $field The name of the field
array $scolumns &$scolumns The columns of the table

Return Value

void

at line 402
protected array parseAlter(string $sql)

Parses a sql alter table statement according to this two BNF syntax :

ALTER TABLE table_name [ RENAME TO new_table_name | RENAME COLUMN column_name TO new_column_name | DROP [ COLUMN ] [IF EXISTS] column_name | DROP COMMENT | MODIFY COMMENT comment | MODIFY [ COLUMN ] column_name [ SET TYPE datatype | [ SET | REMOVE ] NOT NULL | [ SET DEFAULT default ] | REMOVE DEFAULT | [ SET | REMOVE ] PRIMARY KEY | [ SET | REMOVE ] [ AUTOINCREMENT|AUTO_INCREMENT|SERIAL ] | [ SET COMMENT comment ] | REMOVE COMMENT ] | [ SET TITLE title ] | REMOVE TITLE ] | ADD [ COLUMN ] column_name datatype [ NOT NULL|NULLABLE ] [ DEFAULT default ] [ PRIMARY KEY ] [ AUTOINCREMENT|AUTO_INCREMENT|SERIAL ] [ COMMENT comment ] ]

or eBNF syntax :

'ALTER' 'TABLE' table_name ( 'RENAME TO' new_table_name | 'RENAME COLUMN' column_name 'TO' new_column_name | 'DROP' 'COLUMN' ? 'IF EXISTS' ? column_name | 'DROP' 'COMMENT' | 'MODIFY' 'COMMENT' comment | 'MODIFY' 'COLUMN' ? column_name ( 'SET TYPE' datatype | ( ('SET' | 'REMOVE' ) 'NOT NULL' ) | ( 'SET DEFAULT' default ) | 'REMOVE DEFAULT' | ( 'SET' | 'REMOVE' ) 'PRIMARY KEY' | ( 'SET' | 'REMOVE' ) ('AUTOINCREMENT'|'AUTO_INCREMENT'|'SERIAL') | ( 'SET TITLE' title ) | 'REMOVE TITLE') | 'ADD' 'COLUMN' ? column_name datatype 'NOT NULL' ? ( 'DEFAULT' default ) ? ( 'PRIMARY KEY' ) ? ( 'AUTOINCREMENT' | 'AUTO_INCREMENT' | 'SERIAL' ) ? ( 'TITLE' title ) ? ( 'COMMENT' comment ) ? )

Parameters

string $sql The create alter statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 679
protected array parseDropTable(string $sql)

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

DROP TABLE [ IF EXISTS ] table_name { ', ' table_name }

or eBNF syntax :

'DROP TABLE' ( 'IF EXISTS' ) ? table_name ( ', ' table_name ) *

Parameters

string $sql The drop table statement

Return Value

array The parsed request

Exceptions

JsonSQLException

at line 702
private string encodeLiteral(string $text)

Encode text between quote with base64

Parameters

string $text The text to encode

Return Value

string The encoded text.

at line 728
private string decodeLiteral($text, $withQuotes = false)

Decode text encoded with base64

Parameters

$text
$withQuotes

Return Value

string The decoded text.