DataBase
Generic class of access to the database, either MySQL or PostgreSQL.
Table of Contents
CHANNEL | 'database' | |
---|---|---|
$engine | Link to the database engine selected in the configuration. | DataBaseEngine |
$link | The link with de database. | resource |
$miniLog | Manage the log of all controllers, models and database. | MiniLog |
$tables | List of tables in the database. | array |
__construct() | DataBase constructor and prepare the class to use it. | mixed |
beginTransaction() | Start a transaction in the database. | bool |
close() | Disconnect from the database. | bool |
commit() | Record the statements executed in the database. | bool |
connect() | Connect to the database. | bool |
connected() | Returns True if it is connected to the database. | bool |
escapeColumn() | Escape the quotes from the column name. | string |
escapeString() | Escape the quotes from the text string. | string |
exec() | Execute SQL statements on the database (inserts, updates or deletes). | bool |
getColumns() | Returns an array with the columns of a given table. | array |
getConstraints() | Returns an array with the constraints of a table. | array |
getEngine() | Return the database engine used | DataBaseEngine |
getIndexes() | Returns an array with the indices of a given table. | array |
getOperator() | Gets the operator for the database engine | string |
getTables() | Returns an array with the names of the tables in the database. | array |
inTransaction() | Indicates if there is an open transaction. | bool |
lastval() | Returns the last ID assigned when doing an INSERT in the database. | int|bool |
rollback() | Undo the statements executed in the database. | bool |
select() | Execute a SQL statement of type select, and return an array with the results, or an empty array in case of failure. | array |
selectLimit() | Execute a SQL statement of type select, but with pagination, and return an array with the results or an empty array in case of failure. | array |
tableExists() | Returns True if the table exists, False otherwise. | bool |
updateSequence() | mixed | |
var2str() | Transforms a variable into a valid text string to be used in a SQL query. | string |
version() | Returns the used database engine and the version. | string |
Constants
CHANNEL
mixed
$CHANNEL
= 'database'
Properties
$engine
Link to the database engine selected in the configuration.
private
static DataBaseEngine
$engine
$link
The link with de database.
private
static resource
$link
$miniLog
Manage the log of all controllers, models and database.
private
static MiniLog
$miniLog
$tables
List of tables in the database.
private
static array
$tables
= []
Methods
__construct()
DataBase constructor and prepare the class to use it.
public
__construct(
)
: mixed
Return values
mixedbeginTransaction()
Start a transaction in the database.
public
beginTransaction(
)
: bool
Return values
boolclose()
Disconnect from the database.
public
close(
)
: bool
Return values
boolcommit()
Record the statements executed in the database.
public
commit(
)
: bool
Return values
boolconnect()
Connect to the database.
public
connect(
)
: bool
Return values
boolconnected()
Returns True if it is connected to the database.
public
connected(
)
: bool
Return values
boolescapeColumn()
Escape the quotes from the column name.
public
escapeColumn(
$name :
string
)
: string
Parameters
- $name : string
Return values
stringescapeString()
Escape the quotes from the text string.
public
escapeString(
$str :
string
)
: string
Parameters
- $str : string
Return values
stringexec()
Execute SQL statements on the database (inserts, updates or deletes).
public
exec(
$sql :
string
)
: bool
To make selects, it is better to use select () or selectLimit (). If there is no open transaction, one starts, queries are executed If the transaction has opened it in the call, it closes it confirming or discarding according to whether it has gone well or has given an error
Parameters
- $sql : string
Return values
boolgetColumns()
Returns an array with the columns of a given table.
public
getColumns(
$tableName :
string
)
: array
Parameters
- $tableName : string
Return values
arraygetConstraints()
Returns an array with the constraints of a table.
public
getConstraints(
$tableName :
string
[, $extended :
bool
= false ]
)
: array
Parameters
- $tableName : string
- $extended : bool = false
Return values
arraygetEngine()
Return the database engine used
public
getEngine(
)
: DataBaseEngine
Return values
DataBaseEnginegetIndexes()
Returns an array with the indices of a given table.
public
getIndexes(
$tableName :
string
)
: array
Parameters
- $tableName : string
Return values
arraygetOperator()
Gets the operator for the database engine
public
getOperator(
$operator :
string
)
: string
Parameters
- $operator : string
Return values
stringgetTables()
Returns an array with the names of the tables in the database.
public
getTables(
)
: array
Return values
arrayinTransaction()
Indicates if there is an open transaction.
public
inTransaction(
)
: bool
Return values
boollastval()
Returns the last ID assigned when doing an INSERT in the database.
public
lastval(
)
: int|bool
Return values
int|boolrollback()
Undo the statements executed in the database.
public
rollback(
)
: bool
Return values
boolselect()
Execute a SQL statement of type select, and return an array with the results, or an empty array in case of failure.
public
select(
$sql :
string
)
: array
Parameters
- $sql : string
Return values
arrayselectLimit()
Execute a SQL statement of type select, but with pagination, and return an array with the results or an empty array in case of failure.
public
selectLimit(
$sql :
string
[, $limit :
int
= FS_ITEM_LIMIT ]
, $offset :
int
)
: array
Limit is the number of items you want to return. Offset is the result number from which you want it to start.
Parameters
- $sql : string
- $limit : int = FS_ITEM_LIMIT
- $offset : int
Return values
arraytableExists()
Returns True if the table exists, False otherwise.
public
tableExists(
$tableName :
string
[, $list :
array
= [] ]
)
: bool
Parameters
- $tableName : string
- $list : array = []
Return values
boolupdateSequence()
public
updateSequence(
$tableName :
string
, $fields :
array
)
: mixed
Parameters
- $tableName : string
- $fields : array
Return values
mixedvar2str()
Transforms a variable into a valid text string to be used in a SQL query.
public
var2str(
$val :
mixed
)
: string
Parameters
- $val : mixed
Return values
stringversion()
Returns the used database engine and the version.
public
version(
)
: string