DataManagement
Database content management.
Table of Contents
- $connector : PDO
- $debug : bool
- $connector : mixed
- $debug : bool
- __construct() : mixed
- The constructor gathers database information and initiate connection.
- beginTransaction() : bool
- Function starting a transaction on the current connector.
- commit() : bool
- Function ending a transaction on the current connector.
- connector() : PDO
- Function returning the PDO object connected to the target database.
- count() : mixed
- Function used to count how many times a result exist.
- customInsert() : array<string|int, mixed>
- Function used to insert a row in a table of the database using a custom SQL request when wanted result is not possible with classic insert() method.
- customSelect() : array<string|int, mixed>
- Function used to retrieve data from the database using a custom SQL request when wanted result is not possible with classic select() method.
- customSQL() : bool
- Function used to send a custom request SQL of any type.
- debug() : self
- Function enabling the debug mode.
- delete() : mixed
- Function used to delete row(s) in a table of the database.
- insert() : mixed
- Function used to insert a row in a table of the database.
- rollback() : bool
- Function ending a transaction with a rollback on the current connector.
- select() : mixed
- Function used to data from one table (or several table with join) :
- sum() : mixed
- Function used to sum all the values of a particular column.
- update() : mixed
- Function used to update row(s) in a table of the database.
- join() : string
- Function creating join clauses in the request.
- where() : string
- Function creating where clauses in the request.
Properties
$connector
public
PDO
$connector
PDO object connected to the target database.
$debug
public
bool
$debug
If set to TRUE, every method forging a SQL request won't execute it but return it instead.
$connector
private
mixed
$connector
$debug
private
bool
$debug
= FALSE
Methods
__construct()
The constructor gathers database information and initiate connection.
public
__construct(string $type, string $server, string $port, string $name, string $username, string $password) : mixed
Parameters
- $type : string
-
Database type, must be 'mysql' OR 'pgsql'.
- $server : string
-
Database server's FQDN or IP address.
- $port : string
-
Database server's port.
- $name : string
-
Database's name.
- $username : string
-
Database authorized user username.
- $password : string
-
Database authorized user password.
Return values
mixed —beginTransaction()
Function starting a transaction on the current connector.
public
beginTransaction() : bool
Return values
bool —Status of the started transaction.
commit()
Function ending a transaction on the current connector.
public
commit() : bool
Return values
bool —Status of the transaction's commit.
connector()
Function returning the PDO object connected to the target database.
public
connector() : PDO
Return values
PDO —PDO object connected to the target database.
count()
Function used to count how many times a result exist.
public
count(string $table, string $column, array<string|int, mixed> $where[, array<string|int, mixed> $join = NULL ]) : mixed
Parameters
- $table : string
-
Table name.
- $column : string
-
Column name.
- $where : array<string|int, mixed>
-
Array with table name as key and array as value with column name and filter value e.g. ['table'=>['columnname'=>'data']]. 'data' has reserved values for nulls and booleans : 'NULL', '!NULL' 'TRUE', 'FALSE'. 'data' can also be an array of values.
- $join : array<string|int, mixed> = NULL
-
= Array with wanted join table name as key and array of needed values as values e.g. ['table' => [type(inner, left, right ...), 'foreignkey', 'primarykey', /from table]].
Return values
mixed —If debug set to TRUE : return forged SQL request, else returns request's status as boolean on fail or int on success.
customInsert()
Function used to insert a row in a table of the database using a custom SQL request when wanted result is not possible with classic insert() method.
public
customInsert(string $sql[, array<string|int, mixed> $data = NULL ]) : array<string|int, mixed>
Parameters
- $sql : string
-
SQL request.
- $data : array<string|int, mixed> = NULL
-
Array of data e.g. ['columnname'=>'data'].
Return values
array<string|int, mixed> —Array with 2 rows : 'raw' => the database's raw response, 'lastInsertId' => the last insert id.
customSelect()
Function used to retrieve data from the database using a custom SQL request when wanted result is not possible with classic select() method.
public
customSelect(string $sql[, array<string|int, mixed> $data = NULL ]) : array<string|int, mixed>
Parameters
- $sql : string
-
SQL request.
- $data : array<string|int, mixed> = NULL
-
Array of data e.g. ['columnname'=>'data'].
Return values
array<string|int, mixed> —Returns the fetchAll results.
customSQL()
Function used to send a custom request SQL of any type.
public
customSQL(string $sql[, array<string|int, mixed> $data = NULL ]) : bool
Parameters
- $sql : string
-
SQL request.
- $data : array<string|int, mixed> = NULL
-
Array of data e.g. ['columnname'=>'data'].
Return values
bool —Request's status.
debug()
Function enabling the debug mode.
public
debug([bool $state = TRUE ]) : self
Parameters
- $state : bool = TRUE
-
Debug mode state, default is TRUE.
Return values
self —Returns itself.
delete()
Function used to delete row(s) in a table of the database.
public
delete(string $table, array<string|int, mixed> $where) : mixed
Parameters
- $table : string
-
Table name.
- $where : array<string|int, mixed>
-
Array of data pointing the row to update e.g. ['columnname'=>'data']. 'data' has reserved values for nulls and booleans : 'NULL', '!NULL' 'TRUE', 'FALSE'. 'data' can also be an array of values.
Return values
mixed —If debug set to TRUE : return forged SQL request, else returns request's status as boolean.
insert()
Function used to insert a row in a table of the database.
public
insert(string $table, array<string|int, mixed> $data) : mixed
Parameters
- $table : string
-
Table name.
- $data : array<string|int, mixed>
-
Array of data e.g. ['columnname'=>'data'].
Return values
mixed —If debug set to TRUE : return forged SQL request, else returns array with 2 rows : 'raw' => the database's raw response, 'lastInsertId' => the last insert id.
rollback()
Function ending a transaction with a rollback on the current connector.
public
rollback() : bool
Return values
bool —Status of the transaction's rollback.
select()
Function used to data from one table (or several table with join) :
public
select(string $table[, array<string|int, mixed> $order = NULL ][, array<string|int, mixed> $join = NULL ][, array<string|int, mixed> $where = NULL ][, int $limit = NULL ][, int $offset = NULL ][, array<string|int, mixed> $columns = ['*'] ]) : mixed
Parameters
- $table : string
-
Table name.
- $order : array<string|int, mixed> = NULL
-
Array of column name and wanted order e.g. ['column' => 'ASC/DESC']. If no value is passed then default value is used : 'ASC'.
- $join : array<string|int, mixed> = NULL
-
Array with wanted join table name as key and array of needed values as values e.g. ['table' => [type(inner, left, right ...), 'foreignkey', 'primarykey', /from table]]. From table argument is optionnal, if not set $table will be used instead.
- $where : array<string|int, mixed> = NULL
-
Array with table name as key and array as value with column name and filter value e.g. ['table'=>['columnname'=>'data']]. 'data' has reserved values for nulls and booleans : 'NULL', '!NULL' 'TRUE', 'FALSE'. 'data' can also be an array of values.
- $limit : int = NULL
-
Number of max rows e.g. 50.
- $offset : int = NULL
-
Offset for returned rows e.g. 100.
- $columns : array<string|int, mixed> = ['*']
-
Array of column name.
Return values
mixed —If debug set to TRUE : return forged SQL request, else returns the fetchAll results.
sum()
Function used to sum all the values of a particular column.
public
sum(string $table, string $column, array<string|int, mixed> $where[, array<string|int, mixed> $join = NULL ]) : mixed
Parameters
- $table : string
-
Table name.
- $column : string
-
Column name.
- $where : array<string|int, mixed>
-
Array with table name as key and array as value with column name and filter value e.g. ['table'=>['columnname'=>'data']]. 'data' has reserved values for nulls and booleans : 'NULL', '!NULL' 'TRUE', 'FALSE'. 'data' can also be an array of values.
- $join : array<string|int, mixed> = NULL
-
= Array with wanted join table name as key and array of needed values as values e.g. ['table' => [type(inner, left, right ...), 'foreignkey', 'primarykey', /from table]].
Return values
mixed —If debug set to TRUE : return forged SQL request, else returns request's status as boolean on fail or int on success.
update()
Function used to update row(s) in a table of the database.
public
update(string $table, array<string|int, mixed> $data, array<string|int, mixed> $where) : mixed
Parameters
- $table : string
-
Table name.
- $data : array<string|int, mixed>
-
Array of data e.g. ['columnname'=>'data'].
- $where : array<string|int, mixed>
-
Array of data pointing the row to update e.g. ['columnname'=>'data']. 'data' has reserved values for nulls and booleans : 'NULL', '!NULL' 'TRUE', 'FALSE'. 'data' can also be an array of values.
Return values
mixed —If debug set to TRUE : return forged SQL request, else returns request's status as boolean.
join()
Function creating join clauses in the request.
private
join(string $table, array<string|int, mixed> $join) : string
Parameters
- $table : string
-
Default table name needed because from table is optionnal.
- $join : array<string|int, mixed>
-
Array with wanted join table name as key and array of needed values as values e.g. ['table' => [type(inner, left, right ...), 'foreignkey', 'primarykey', /from table]].
Return values
string —SQL request with all wanted join clauses.
where()
Function creating where clauses in the request.
private
where(array<string|int, mixed> $where) : string
Parameters
- $where : array<string|int, mixed>
-
Array with table name as key and array of array as value with column name and filter value e.g. ['table'=>[['columnname'=>'data']]]. 'data' has reserved values for nulls and booleans : 'NULL', '!NULL' 'TRUE', 'FALSE'. 'data' can also be an array of values.
Return values
string —SQL request with all wanted where clauses.