Engine
class Engine
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
Properties
private JsonSQL|null | $jsonsql | A pointer on the JsonSQL owner. | |
private string|null | $name | Name of the JSON database managed by that engine | |
private object|null | $json | The committed content of the JSON database managed by that engine | |
static private bool | $compact | Indicates whether to save the json file in a compact form or not | |
private bool | $transaction | true if a transaction is currently active, and false if not. | |
private object|null | $backup | Content being updated during a transaction | |
private bool | $modified | true if data has been modified, and false if not. | |
private bool | $schemaModified | true if the database schema has been modified, and false if not. | |
private string|false | $lastInsertId | Stores the ID of the last inserted row. | |
private object | $db | A pointer on the content of the JSON database managed by that engine. |
Methods
Class destructor
Returns the pointer on the content of the JSON database
Open a json database
Creates a json database then open it
Returns a pointer to the json schema object
returns a ArrayIterator on the rows of the table $name
Quotes a string for use in a query.
Returns the ID of the last inserted row.
Initiates a transaction.
Ends a transaction.
Commits a transaction.
Commits any modification
Commits any modification of the schema
Rolls back the current transaction, as initiated by beginTransaction().
Checks if inside a transaction.
Loads a json database into memory.
Loads a parsed request from memory cache.
Checks if the table already contains a record with the provided keys
Appends a row to a table
Deletes a table row
Creates a table in the database
Deletes all rows from a table
Drops a table
Renames a table
Renames a column
Drops a column
Changes the type of a column
Changes whether a column is marked to allow null values or to reject null values
Set or remove the default value for a column.
Set or remove primary key for a column.
Set or remove autoincrement for a column.
Set or remove the title of a table.
Set or remove the description of a table.
Set or remove the title of a column.
Set or remove the description of a column.
Saves the current database and/or its schema on the file system
Converts a string value according to its json data type
Tokenizes a list of comma separated internal properties and returns an object with these properties.
Details
at line 140
void
__construct(JsonSQL $jsonsql)
Constructor of class Engine
at line 150
__destruct()
Class destructor
commit all changes if a transaction is currently active
at line 161
object
getDb()
Returns the pointer on the content of the JSON database
at line 172
open(string $name, bool $create = false)
Open a json database
at line 207
create(string $name, bool $compact = false)
Creates a json database then open it
at line 232
object
schema()
Returns a pointer to the json schema object
at line 243
ArrayIterator
table(string $name)
returns a ArrayIterator on the rows of the table $name
at line 255
string
quote(string $string, int $type = \PDO::PARAM_STR)
Quotes a string for use in a query.
at line 275
string|false
lastInsertId()
Returns the ID of the last inserted row.
at line 285
bool
beginTransaction()
Initiates a transaction.
at line 300
protected void
endTransaction()
Ends a transaction.
at line 314
bool
commit()
Commits a transaction.
at line 329
bool
notifyModification()
Commits any modification
at line 340
bool
notifySchemaModification()
Commits any modification of the schema
at line 351
bool
rollBack()
Rolls back the current transaction, as initiated by beginTransaction().
at line 366
bool
inTransaction()
Checks if inside a transaction.
at line 380
private object
loadJsonFromCache(string $path)
Loads a json database into memory.
If APCu is enabled and the database is stored in memory then return it. otherwise, the database is loaded from the filesystem and decoded.
at line 420
object
loadRequestFromCache(string $sql)
Loads a parsed request from memory cache.
If APCu is enabled and the request is stored in memory then return it. otherwise, the request is parsed. only select statement are stored into memory cache.
at line 444
protected void
checkDuplicateKeys(string $table, array $keys, int $exclude = -1)
Checks if the table already contains a record with the provided keys
at line 472
void
insert(string $table, array $row)
Appends a row to a table
at line 543
void
delete(string $table, int $index)
Deletes a table row
at line 559
void
replace(string $table, int $index, stdClass $row)
Replaces a table row by another
at line 612
void
createTable(string $table, stdClass $columns, array $required, array $foreignkeys, bool $ifnotexists = false)
Creates a table in the database
at line 624
void
truncate(string $table)
Deletes all rows from a table
at line 643
void
dropTable(string $table, bool $ifexists = false)
Drops a table
at line 656
void
renameTable(string $table, string $newname)
Renames a table
at line 671
void
addColumn(string $table, string $column, stdClass $columnDef, array $required = array())
Adds a column in a table of the database
at line 685
void
renameColumn(string $table, string $column, string $newname)
Renames a column
at line 699
void
dropColumn(string $table, string $column, bool $ifexists = false)
Drops a column
at line 715
void
setColumnType(string $table, string $column, string $type, string $format = '', string $datatype = '')
Changes the type of a column
at line 729
void
setNotNull(string $table, string $column, bool $allownull = false)
Changes whether a column is marked to allow null values or to reject null values
at line 743
void
setDefault(string $table, string $column, string|bool $default = false)
Set or remove the default value for a column.
at line 757
void
setPrimaryKey(string $table, string $column, bool $remove = false)
Set or remove primary key for a column.
at line 771
void
setAutoincrement(string $table, string $column, bool $remove = false)
Set or remove autoincrement for a column.
at line 784
void
setTableTitle(string $table, string|bool $title = false)
Set or remove the title of a table.
at line 797
void
setTableDescription(string $table, string|bool $description = false)
Set or remove the description of a table.
at line 811
void
setColumnTitle(string $table, string $column, string|bool $title = false)
Set or remove the title of a column.
at line 825
void
setColumnDescription(string $table, string $column, string|bool $description = false)
Set or remove the description of a column.
at line 836
void
save()
Saves the current database and/or its schema on the file system
at line 865
string|float|bool|int|null
normalizeValue(string $type, string $value)
Converts a string value according to its json data type
at line 891
object
properties(string $arg)
Tokenizes a list of comma separated internal properties and returns an object with these properties.
Internal properties are stored into the title property of the column definition in the database schema. Actually, only 'primarykey' and 'autoincrement' are used.