class JsonSQL

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

static private array $_instance List of class instances.
private Engine $engine The JsonSQL engine attached to this instance

Methods

__construct()

Represents a connection between PHP and a json database in memory.

static JsonSQL
open(string $name, boolean $create = false)

Open a json database

static JsonSQL
create(string $name)

Creates a json database then open it

getEngine()

Returns the JsonSQL engine attached to this instance

object
schema()

Returns a pointer to the json schema object

table(string $name)

Returns a ArrayIterator on the rows of the table $name

prepare(string $sql)

Prepares a statement for execution and returns a statement object

query(string $sql)

Executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a Statement object.

int
exec(string $sql)

Executes an SQL statement in a single function call, returning the number of rows affected by the statement.

string
quote(string $string, int $type = \PDO::PARAM_STR)

Quotes a string for use in a query.

string
lastInsertId()

Returns the ID of the last inserted row.

bool
beginTransaction()

Initiates a transaction.

bool
commit()

Commits a transaction.

bool
rollBack()

Rolls back the current transaction, as initiated by beginTransaction().

bool
inTransaction()

Checks if inside a transaction.

void
insert(string $table, array $row)

Appends a row to a table

void
delete(string $table, int $index)

Deletes a table row

void
replace(string $table, int $index, object $row)

Replaces a table row by another

void
createTable(string $table, object $columns, array $required, array $foreignkeys, bool $ifnotexists = false)

Creates a table in the database

void
truncate(string $table)

Deletes all rows from a table

void
dropTable(string $table, bool $ifexists = false)

Drops a table

void
renameTable(string $table, string $newname)

Renames a table

void
addColumn(string $table, string $column, object $columnDef, array $required = array())

Adds a column in a table of the database

void
renameColumn(string $table, string $column, string $newname)

Renames a column

void
dropColumn(string $table, string $column, bool $ifexists = false)

Drops a column

void
setColumnType(string $table, string $column, string $type, string $format = '', string $datatype = '')

Changes the type of a column

void
setNotNull(string $table, string $column, bool $allownull = false)

Changes whether a column is marked to allow null values or to reject null values

void
setDefault(string $table, string $column, string|bool $default = false)

Set or remove the default value for a column.

void
setPrimaryKey(string $table, string $column, bool $remove = false)

Set or remove primary key for a column.

void
setAutoincrement(string $table, string $column, bool $remove = false)

Set or remove autoincrement for a column.

void
setTableTitle(string $table, string|bool $title = false)

Set or remove the title of a table.

void
setTableDescription(string $table, string|bool $description = false)

Set or remove the description of a table.

void
setColumnTitle(string $table, string $column, string|bool $title = false)

Set or remove the title of a column.

void
setColumnDescription(string $table, string $column, string|bool $description = false)

Set or remove the description of a column.

void
save()

Saves the current database and/or its schema on the file system

Details

at line 72
private __construct()

Represents a connection between PHP and a json database in memory.

Exceptions

JsonSQLException

at line 86
static JsonSQL open(string $name, boolean $create = false)

Open a json database

Parameters

string $name The name of json database (without the file extension)
boolean $create if true, creates the database if it doesn't exists

Return Value

JsonSQL The JsonSQL instance

See also

JsonSQL::create

at line 104
static JsonSQL create(string $name)

Creates a json database then open it

Parameters

string $name The name of json database (without the file extension)

Return Value

JsonSQL The JsonSQL instance

Exceptions

JsonSQLException

at line 118
Engine getEngine()

Returns the JsonSQL engine attached to this instance

Return Value

Engine The JsonSQL engine

at line 128
object schema()

Returns a pointer to the json schema object

Return Value

object The json schema object

at line 139
ArrayIterator table(string $name)

Returns a ArrayIterator on the rows of the table $name

Parameters

string $name The table name

Return Value

ArrayIterator The ArrayIterator

at line 150
Statement prepare(string $sql)

Prepares a statement for execution and returns a statement object

Parameters

string $sql a valid SQL statement

Return Value

Statement a Statement instance

at line 168
Statement query(string $sql)

Executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a Statement object.

Parameters

string $sql a valid SQL statement to prepare and execute.

Return Value

Statement a Statement instance

at line 184
int exec(string $sql)

Executes an SQL statement in a single function call, returning the number of rows affected by the statement.

Parameters

string $sql The valid SQL statement to prepare and execute.

Return Value

int The number of rows that were modified or deleted by the SQL statement

at line 197
string quote(string $string, int $type = \PDO::PARAM_STR)

Quotes a string for use in a query.

Parameters

string $string The string to be quoted.
int $type provides a PDO data type hint (default : PDO::PARAM_STR).

Return Value

string a quoted string that is safe to pass into an SQL statement

at line 207
string lastInsertId()

Returns the ID of the last inserted row.

Return Value

string a string representing the row ID of the last row that was inserted into the database

at line 217
bool beginTransaction()

Initiates a transaction.

Return Value

bool always true

at line 227
bool commit()

Commits a transaction.

Return Value

bool always true

at line 237
bool rollBack()

Rolls back the current transaction, as initiated by beginTransaction().

Return Value

bool always true

at line 247
bool inTransaction()

Checks if inside a transaction.

Return Value

bool true if a transaction is currently active, and false if not.

at line 260
void insert(string $table, array $row)

Appends a row to a table

Parameters

string $table The table name
array $row The row to append

Return Value

void

Exceptions

JsonSQLException

at line 272
void delete(string $table, int $index)

Deletes a table row

Parameters

string $table The table name
int $index The position of the row in the table

Return Value

void

at line 286
void replace(string $table, int $index, object $row)

Replaces a table row by another

Parameters

string $table The table name
int $index The position of the row in the table
object $row The new row

Return Value

void

Exceptions

JsonSQLException

at line 302
void createTable(string $table, object $columns, array $required, array $foreignkeys, bool $ifnotexists = false)

Creates a table in the database

Parameters

string $table The table name
object $columns The columns definition
array $required The list of required columns
array $foreignkeys The list of foreign keys definition
bool $ifnotexists if true, don't throw an error if the table already exists

Return Value

void

Exceptions

JsonSQLException

at line 314
void truncate(string $table)

Deletes all rows from a table

Parameters

string $table The table name

Return Value

void

Exceptions

JsonSQLException

at line 327
void dropTable(string $table, bool $ifexists = false)

Drops a table

Parameters

string $table The table name
bool $ifexists if true, don't throw an error if the table doesn't exists

Return Value

void

Exceptions

JsonSQLException

at line 340
void renameTable(string $table, string $newname)

Renames a table

Parameters

string $table The table name
string $newname The new name of the table

Return Value

void

Exceptions

JsonSQLException

at line 355
void addColumn(string $table, string $column, object $columnDef, array $required = array())

Adds a column in a table of the database

Parameters

string $table The table name
string $column The name of the new column
object $columnDef The column definition
array $required an array with the column name if required

Return Value

void

Exceptions

JsonSQLException

at line 369
void renameColumn(string $table, string $column, string $newname)

Renames a column

Parameters

string $table The table name
string $column The actual column name in the table
string $newname The new name of the column

Return Value

void

Exceptions

JsonSQLException

at line 383
void dropColumn(string $table, string $column, bool $ifexists = false)

Drops a column

Parameters

string $table The table name
string $column The actual column name to drop in the table
bool $ifexists if true, don't throw an error if the table or the column doesn't exists

Return Value

void

Exceptions

JsonSQLException

at line 399
void setColumnType(string $table, string $column, string $type, string $format = '', string $datatype = '')

Changes the type of a column

Parameters

string $table The table name
string $column The actual column name
string $type The type of the column
string $format The format of the column
string $datatype The datatype of the column

Return Value

void

Exceptions

JsonSQLException

at line 413
void setNotNull(string $table, string $column, bool $allownull = false)

Changes whether a column is marked to allow null values or to reject null values

Parameters

string $table The table name
string $column The actual column name
bool $allownull if true, the column allow null value

Return Value

void

Exceptions

JsonSQLException

at line 427
void setDefault(string $table, string $column, string|bool $default = false)

Set or remove the default value for a column.

Parameters

string $table The table name
string $column The actual column name
string|bool $default The default value. If false, remove the default

Return Value

void

Exceptions

JsonSQLException

at line 441
void setPrimaryKey(string $table, string $column, bool $remove = false)

Set or remove primary key for a column.

Parameters

string $table The table name
string $column The actual column name
bool $remove if true, remove the primary key

Return Value

void

Exceptions

JsonSQLException

at line 455
void setAutoincrement(string $table, string $column, bool $remove = false)

Set or remove autoincrement for a column.

Parameters

string $table The table name
string $column The actual column name
bool $remove if true, remove the primary key

Return Value

void

Exceptions

JsonSQLException

at line 468
void setTableTitle(string $table, string|bool $title = false)

Set or remove the title of a table.

Parameters

string $table The table name
string|bool $title The title content. If false, remove the title

Return Value

void

Exceptions

JsonSQLException

at line 481
void setTableDescription(string $table, string|bool $description = false)

Set or remove the description of a table.

Parameters

string $table The table name
string|bool $description The description content. If false, remove the description

Return Value

void

Exceptions

JsonSQLException

at line 495
void setColumnTitle(string $table, string $column, string|bool $title = false)

Set or remove the title of a column.

Parameters

string $table The table name
string $column The actual column name
string|bool $title The title content. If false, remove the title

Return Value

void

Exceptions

JsonSQLException

at line 509
void setColumnDescription(string $table, string $column, string|bool $description = false)

Set or remove the description of a column.

Parameters

string $table The table name
string $column The actual column name
string|bool $description The description content. If false, remove the description

Return Value

void

Exceptions

JsonSQLException

at line 520
void save()

Saves the current database and/or its schema on the file system

Return Value

void

Exceptions

JsonSQLException