This class contains functions for database driver handling in CONTENIDO.
package | Core |
---|---|
subpackage | Database |
__construct(array $options) : void
Uses default connection settings, passed $options['connection'] settings will overwrite connection settings for current instance.
array
Assoziative options as follows: - $options['haltBehavior'] (string) Optional, halt behavior on occured errors - $options['haltMsgPrefix'] (string) Optional, Text to prepend to the halt message - $options['enableProfiling'] (bool) Optional, flag to enable profiling - $options['connection'] (array) Optional, assoziative connection settings - $options['connection']['host'] (string) Hostname or ip - $options['connection']['database'] (string) Database name - $options['connection']['user'] (string) User name - $options['connection']['password'] (string) User password
__get(string $name) : mixed
string
name of the variable
mixed
affectedRows() : int
int
Number of affected rowsbuildInsert(string $tableName, array $fields) : string
String values in passed fields parameter will be escaped automatically.
Example:
$db = cRegistry::getDb(); $fields = array( 'idcode' => $idcode, 'idcatart' => $idcatart, 'idlang' => $lang, 'idclient' => $client, 'code' => "... code n' fun ...", ); $statement = $db->buildInsert($cfg['tab']['code'], $fields); $db->query($statement);
string
The table name
array
Assoziative array of fields to insert
string
buildUpdate(string $tableName, array $fields, array $whereClauses) : string
String values in passed aFields and aWhere parameter will be escaped automatically.
Example:
$db = cRegistry::getDb(); $fields = array('code' => "... some new code n' fun ..."); $whereClauses = array('idcode' => 123); $statement = $db->buildUpdate($cfg['tab']['code'], $fields, $whereClauses); $db->query($statement);
string
The table name
array
Assoziative array of fields to update
array
Assoziative array of field in where clause. Multiple entries will be concatenated with AND
string
close()
deprecated | 2012-10-02 This method is deprecated. Use disconnect instead. |
---|
connect() : object | resource | int | null
object
resource
int
null
Connection handler. Return value depends on
used driver and is null in case of an error.copyResultToArray($sTable)
deprecated | [2011-03-03] This method is deprecated. Use toArray() instead. |
---|
disconnect() : void
escape(string $string) : string
string
The string to escape
string
Escaped stringf(mixed $name, mixed $default) : mixed
mixed
The field name or index position
mixed
The default value to return
mixed
The value of fieldfree() : int
int
getHaltBehaviour() : string
string
getLastInsertedId(string $tableName) : int | null
string
int
null
last id of tablegetMetaData(string $tableName, bool $full) : array
string
The table to get metadata or empty string to retrieve metadata of all tables
bool
Flag to load full metadata
array
Depends on used database and on parameter $fullgetProfileData() : array
array
Profile data array like:
- $arr[$i]['time'] (float) Elapsed time to execute the query
- $arr[$i]['query'] (string) The query itselfgetResultObject($className) : object
If optional param $className is set, the result object is an instance of class $className.
object
getServerInfo() : array | null
The return value depends always on used DBMS.
array
null
Assoziative array as follows or null:
- $arr['description'] (string) Optional, server description
- $arr['version'] (string) Optional, server versiongetTableNames() : array | null
array
null
Indexed array containing assoziative table data as
follows or null:
- $info[$i]['table_name']
- $info[$i]['tablespace_name']
- $info[$i]['database']halt(string $message) : void
Error handler function, delegates passed message to the function reportHalt() if property $this->_haltBehaviour is not set to self::HALT_REPORT.
Terminates further script execution if $this->_haltBehaviour is set to self::HALT_YES
string
The message to use for error handling
haltmsg($message)
deprecated | 2012-10-02 This method is deprecated. Use reportHalt instead. |
---|
insert(string $tableName, array $fields) : bool
String values in passed aFields parameter will be escaped automatically.
Example:
$db = cRegistry::getDb(); $fields = array( 'idcatart' => $idcatart, 'idlang' => $lang, 'idclient' => $client, 'code' => "... code n' fun ...", ); $result = $db->insert($cfg['tab']['code'], $fields);
string
The table name
array
Assoziative array of fields to insert
bool
isProfilingEnabled() : bool
bool
loadDriver() : void
\cDbException |
---|
nextRecord() : bool
bool
nf()
deprecated | 2012-10-02 This method is deprecated. Use numRows instead. |
---|
np()
deprecated | 2012-10-02 This method is not longer supported. |
---|
numFields() : int
int
Number of fieldsnumRows() : int
int
The number of rows from last select query resultp($name)
deprecated | 2012-10-02 This method is not longer supported. |
---|
prepare(string $statement) : string
Accepts multiple parameter, where the first parameter should be the query and any additional parameter should be the values to replace in format definitions. As an alternative the second parameter cound be also a indexed array with values to replace in format definitions.
Other option is to call this function with the statement containing named parameter and the second parameter as a assoziative array with key/value pairs to set in statement.
Examples:
// multiple parameter $sql = $obj->prepare('SELECT * FROM `%s` WHERE id = %d', 'tablename', 123); // 2 parameter where the first is the statement with formatting signs and the second the entries array $sql = $obj->prepare('SELECT * FROM `%s` WHERE id = %d', array('tablename', 123)); // 2 parameter where the first is the statement with named parameter and the second the assoziative entries array $sql = $obj->prepare('SELECT * FROM `:mytab` WHERE id = :myid', array('mytab' => 'tablename', 'myid' => 123));
string
The sql statement to prepare.
\Exception |
If statement is empty or function is called with less than 2 parameters |
---|
string
The prepared sql statementquery(string $statement) : resource | int | object | bool
If called with one parameter, it executes the statement directly.
Accepts multiple parameter, where the first parameter should be the query and any additional parameter should be the values to replace in format definitions. As an alternative the second parameter cound be also a indexed array with values to replace in format definitions.
Other option is to call this function with the statement containing named parameter and the second parameter as a assoziative array with key/value pairs to set in statement.
Examples:
// call with one parameter $obj->query('SELECT * FROM `tablename` WHERE id = 123'); // call with multiple parameter $obj->query('SELECT * FROM `%s` WHERE id = %d', 'tablename', 123); // 2 parameter where the first is the statement with formatting signs and the second the entries array $obj->query('SELECT * FROM `%s` WHERE id = %d', array('tablename', 123)); // 2 parameter where the first is the statement with named parameter and the second the assoziative entries array $obj->query('SELECT * FROM `:mytab` WHERE id = :myid', array('mytab' => 'tablename', 'myid' => 123));
string
The SQL statement to execute.
resource
int
object
bool
Depends on used database driver, false on errorreportHalt(string $message) : void
Concatenates a detailed error message and invoke PHP's error_log() method.
string
seek($pos) : void
server_info()
deprecated | 2012-10-02 This method is deprecated. Use getTableNames instead. |
---|
setDefaultConfiguration(array $defaultDbCfg) : void
array
table_names()
deprecated | 2012-10-02 This method is deprecated. Use getTableNames instead. |
---|
toArray(string $fetchMode) : array
string
One of cDbDriverHandler::FETCH_* constants
array
update(string $tableName, array $fields, array $whereClauses) : bool
String values in passed fields and whereClauses parameter will be escaped automatically.
Example:
$db = cRegistry::getDb(); $fields = array('code' => "... some new code n' fun ..."); $whereClauses = array('idcode' => 123); $result = $db->update($cfg['tab']['code'], $fields, $whereClauses);
string
The table name
array
Assoziative array of fields to update
array
Assoziative array of field in where clause. Multiple entries will be concatenated with AND
bool
_addProfileData(float $timeStart, float $timeEnd, string $statement) : void
float
float
string
_getConnection(mixed $data) : mixed
mixed
Connection data array or variable
mixed
Either The connection (object, resource, integer) or null_prepareStatement(string $statement, array $arguments) : string
string
array
string
_prepareStatementA(string $statement, array $arguments) : string
Examples:
// named parameter and assoziative entries array $sql = $obj->_prepareStatementA('SELECT * FROM `:mytab` WHERE id = :myid', array('mytab' => 'tablename', 'myid' => 123)); $sql = $obj->_prepareStatementA('SELECT * FROM `:mytab` WHERE id = :myid AND user = :myuser', array('mytab' => 'tablename', 'myid' => 123, 'myuser' => 3));
string
array
Arguments array containing the query with named parameter and assoziative entries array
string
_prepareStatementF(string $statement, array $arguments) : string
Examples:
$obj->_prepareStatementF('SELECT * FROM `%s` WHERE id = %d', 'tablename', 123); $obj->_prepareStatementF('SELECT * FROM `%s` WHERE id = %d AND user = %d', 'tablename', 123, 3);
string
array
Arguments array containing the query with formatting signs and the entries.
string
_removeConnection(mixed $connection) : void
mixed
The connection to remove in cache
_setConnection(mixed $data, mixed $connection) : void
mixed
Connection data array
mixed
The connection to store in cache
$_connectionCache
array |
---|
$_dbCfg : array
$_defaultDbCfg : array
$_driver : \cDbDriverAbstract
$_driverType : string
$_haltBehaviour : string
Feasible values are - "yes" (halt with message) - "no" (ignore errors quietly) - "report" (ignore errror, but spit a warning)
$_haltMsgPrefix : string
$_profileData : array
FETCH_ASSOC
FETCH_BOTH
FETCH_NUMERIC
HALT_NO
HALT_REPORT
HALT_YES