class Database

this class provides a unified access interface to SQL databases, whether MySQL, PostgreSQL, SQLite, or JsonSQL

Properties

private Simulator $simulator
private string $databasesDir
private int $id
private string $type
private string $name
private string $label
private string|null $host
private int $port
private string|null $user
private string|null $password
private bool $connected
$link
private array $myformat
private array $pgformat

Methods

void
__construct(Simulator|null $simulator, string|null $databasesDir, int $id, string $type, string $name)

Constructor of class Database

getSimulator()

Returns the Simulator object that uses this database

string|null
getDatabasesDir()

Returns the database directory

int
getId()

Returns the ID of this database

void
setId(int $id)

Sets the ID of this database

string
getType()

Returns the type of this database

void
setType(string $type)

Sets the type of this database

string
getName()

Returns the name of this database

void
setName(string $name)

Sets the name of this database

string
getLabel()

Returns the label of this database

void
setLabel(string $label)

Sets the label of this database

string|null
getHost()

Returns the host of this database

void
setHost(string|null $host)

Sets the host of this database

int
getPort()

Returns the port number of this database

void
setPort(int $port)

Sets the port number of this database

string|null
getUser()

Returns the identifier of the user with access rights to this database

void
setUser(string|null $user)

Sets the identifier of the user with access rights to this database

string|null
getPassword()

Returns the password of the user with access rights to this database

void
setPassword(string|null $password)

Sets the password of the user with access rights to this database

getConnection()

Returns the instance representing a connection to this database

bool
isConnected()

Returns the connection status to the database

bool
getConnected()

Returns the connection status to the database

void
setConnected(bool $connected)

Sets the connection status to the database

bool
connect(bool $withDbName = true)

Composes a DSN (Data Source Name) string, connects to the database and stores an instance representing a connection to a database

array|string|bool
query(string $sql)

Executes an SQL statement, returning an array containing all of the result set rows

prepare(string $sql)

Prepares a statement for execution and returns a statement object.

bool
bindParam(PDOStatement $stmt, string|int $parameter, string $variable, string $type = 'text')

Binds a parameter to the specified variable name.

bool
bindValue(PDOStatement $stmt, string|int $parameter, string $value, string $type = 'text')

Binds a value to a parameter

array|string|bool
execute(PDOStatement $stmt)

Executes a prepared statement

int|bool
exec(string $sql)

Execute an SQL statement and return the number of affected rows

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

Quotes a string for use in a query.

string
lastInsertId()

Returns the ID of the last inserted row or sequence value

string
convertSQLFunctions(string $sql)

Converts internal functions in a SQL statement into appropriate functions to the database driver

Details

at line 190
void __construct(Simulator|null $simulator, string|null $databasesDir, int $id, string $type, string $name)

Constructor of class Database

Parameters

Simulator|null $simulator The Simulator object that uses this database
string|null $databasesDir The "databases" directory, specially for SQLite.
int $id The ID of the database
string $type The type of the database
string $name The name of the database

Return Value

void

at line 205
Simulator getSimulator()

Returns the Simulator object that uses this database

Return Value

Simulator The Simulator object

at line 216
string|null getDatabasesDir()

Returns the database directory

Return Value

string|null The database directory

at line 227
int getId()

Returns the ID of this database

Return Value

int The ID of this database

at line 239
void setId(int $id)

Sets the ID of this database

Parameters

int $id The ID of this database

Return Value

void

at line 250
string getType()

Returns the type of this database

Return Value

string The type of this database

at line 262
void setType(string $type)

Sets the type of this database

Parameters

string $type The type of this database

Return Value

void

at line 273
string getName()

Returns the name of this database

Return Value

string The name of this database

at line 285
void setName(string $name)

Sets the name of this database

Parameters

string $name The name of this database

Return Value

void

at line 296
string getLabel()

Returns the label of this database

Return Value

string The label of this database

at line 308
void setLabel(string $label)

Sets the label of this database

Parameters

string $label The label of this database

Return Value

void

at line 319
string|null getHost()

Returns the host of this database

Return Value

string|null The database host or the relative path in the case of sqlite

at line 331
void setHost(string|null $host)

Sets the host of this database

Parameters

string|null $host The host of this database or the relative path if sqlite

Return Value

void

at line 342
int getPort()

Returns the port number of this database

Return Value

int The port number of this database

at line 354
void setPort(int $port)

Sets the port number of this database

Parameters

int $port The port number of this database

Return Value

void

at line 365
string|null getUser()

Returns the identifier of the user with access rights to this database

Return Value

string|null The identifier of the user

at line 377
void setUser(string|null $user)

Sets the identifier of the user with access rights to this database

Parameters

string|null $user The identifier of the user

Return Value

void

at line 388
string|null getPassword()

Returns the password of the user with access rights to this database

Return Value

string|null The password of the user

at line 400
void setPassword(string|null $password)

Sets the password of the user with access rights to this database

Parameters

string|null $password The password of the user

Return Value

void

at line 411
PDO|JsonSQL getConnection()

Returns the instance representing a connection to this database

Return Value

PDO|JsonSQL The instance

at line 422
bool isConnected()

Returns the connection status to the database

Return Value

bool true if connected, false otherwise

at line 433
bool getConnected()

Returns the connection status to the database

Return Value

bool true if connected, false otherwise

at line 445
void setConnected(bool $connected)

Sets the connection status to the database

Parameters

bool $connected true if connected, false otherwise

Return Value

void

at line 457
bool connect(bool $withDbName = true)

Composes a DSN (Data Source Name) string, connects to the database and stores an instance representing a connection to a database

Parameters

bool $withDbName (default: true) if false the database name is not inserted into the dsn string.

Return Value

bool always true

at line 523
array|string|bool query(string $sql)

Executes an SQL statement, returning an array containing all of the result set rows

Parameters

string $sql The SQL statement to be prepares and executed.

Return Value

array|string|bool the result array of the query on success or false on failure.

at line 549
PDOStatement|Statement prepare(string $sql)

Prepares a statement for execution and returns a statement object.

The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed.

Parameters

string $sql The SQL Statement

Return Value

PDOStatement|Statement The statement object

at line 575
bool bindParam(PDOStatement $stmt, string|int $parameter, string $variable, string $type = 'text')

Binds a parameter to the specified variable name.

Parameters

PDOStatement $stmt The statement object returned by the prepare method.
string|int $parameter Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.
string $variable &$variable Name of the PHP variable to bind to the SQL statement parameter.
string $type (default: 'text') The type of the parameter

Return Value

bool true on success or false on failure.

at line 615
bool bindValue(PDOStatement $stmt, string|int $parameter, string $value, string $type = 'text')

Binds a value to a parameter

Binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement.

Parameters

PDOStatement $stmt The statement object returned by the prepare method.
string|int $parameter The parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.
string $value The value to bind to the parameter.
string $type (default: 'text') The type of the parameter

Return Value

bool true on success or false on failure.

at line 650
array|string|bool execute(PDOStatement $stmt)

Executes a prepared statement

Parameters

PDOStatement $stmt The statement object returned by the prepare method.

Return Value

array|string|bool the result array of the statement on success or false on failure.

at line 675
int|bool exec(string $sql)

Execute an SQL statement and return the number of affected rows

Parameters

string $sql The SQL Statement

Return Value

int|bool the number of rows that were modified or deleted by the SQL statement you issued, 0 if no rows were affected or false on failure

Exceptions

Exception

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

Quotes a string for use in a query.

Places quotes around the input string (if required) and escapes special characters within the input string, using a quoting style appropriate to the underlying driver.

Parameters

string $value The string to be quoted.
int $type The parameter type.

Return Value

string The quoted string or false if the driver does not support quoting in this way.

at line 726
string lastInsertId()

Returns the ID of the last inserted row or sequence value

Return Value

string The ID of the last inserted row

at line 746
private string convertSQLFunctions(string $sql)

Converts internal functions in a SQL statement into appropriate functions to the database driver

Parameters

string $sql The SQL Statement to be converted

Return Value

string The converted SQL statement