This class contains functions for database driver handling in CONTENIDO.

package Core
subpackage Database

 Methods

Constructor, sets passed options and connects to the DBMS, if not done before.

__construct(array $options) : void

Uses default connection settings, passed $options['connection'] settings will overwrite connection settings for current instance.

Parameters

$options

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

Magic getter function for old class variables.

__get(string $name) : mixed

Parameters

$name

string

name of the variable

Returns

mixed

Returns number of affected rows from last executed query (update, delete)

affectedRows() : int

Returns

intNumber of affected rows

affected_rows()

affected_rows() 

Builds and returns a insert query.

buildInsert(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);

Parameters

$tableName

string

The table name

$fields

array

Assoziative array of fields to insert

Returns

string

Builds and returns a update query.

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);

Parameters

$tableName

string

The table name

$fields

array

Assoziative array of fields to update

$whereClauses

array

Assoziative array of field in where clause. Multiple entries will be concatenated with AND

Returns

string

close()

close() 
deprecated 2012-10-02 This method is deprecated. Use disconnect instead.

Establishes a connection to the database server.

connect() : object | resource | int | null

Returns

objectresourceintnullConnection handler. Return value depends on used driver and is null in case of an error.

copyResultToArray()

copyResultToArray($sTable) 
deprecated [2011-03-03] This method is deprecated. Use toArray() instead.

Parameters

$sTable

Closes the connection and frees the query id.

disconnect() : void

Escape string for using in SQL-Statement.

escape(string $string) : string

Parameters

$string

string

The string to escape

Returns

stringEscaped string

Returns the desired field value from current record set.

f(mixed $name, mixed $default) : mixed

Parameters

$name

mixed

The field name or index position

$default

mixed

The default value to return

Returns

mixedThe value of field

Discard the query result

free() : int

Returns

int

Returns the database driver instance.

getDriver() : \cDbDriverAbstract

Returns the halt behaviour setting.

getHaltBehaviour() : string

Returns

string

Get last inserted id of given table name

getLastInsertedId(string $tableName) : int | null

Parameters

$tableName

string

Returns

intnulllast id of table

Returns collected profile data.

getProfileData() : array
Static

Returns

arrayProfile data array like: - $arr[$i]['time'] (float) Elapsed time to execute the query - $arr[$i]['query'] (string) The query itself

This method returns the current result set as object or null if no result set is left.

getResultObject($className) : object

If optional param $className is set, the result object is an instance of class $className.

Parameters

$className

Returns

object

Returns information about DB server.

getServerInfo() : array | null

The return value depends always on used DBMS.

Returns

arraynullAssoziative array as follows or null: - $arr['description'] (string) Optional, server description - $arr['version'] (string) Optional, server version

Returns names of existing tables.

getTableNames() : array | null

Returns

arraynullIndexed array containing assoziative table data as follows or null: - $info[$i]['table_name'] - $info[$i]['tablespace_name'] - $info[$i]['database']

Error handling

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

Parameters

$message

string

The message to use for error handling

haltmsg()

haltmsg($message) 
deprecated 2012-10-02 This method is deprecated. Use reportHalt instead.

Parameters

$message

Builds and executes a insert query.

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);

Parameters

$tableName

string

The table name

$fields

array

Assoziative array of fields to insert

Returns

bool

Checks if profiling was enabled via configuration.

isProfilingEnabled() : bool

Returns

bool

Loads the database driver and checks its base functionality.

loadDriver() : void

Exceptions

\cDbException

Fetches the next record set from result set

nextRecord() : bool

Returns

bool

next_record()

next_record() 

nf()

nf() 
deprecated 2012-10-02 This method is deprecated. Use numRows instead.

np()

np() 
deprecated 2012-10-02 This method is not longer supported.

Returns the number of fields (columns) from current record set

numFields() : int

Returns

intNumber of fields

Returns the number of rows from last executed select query.

numRows() : int

Returns

intThe number of rows from last select query result

num_fields()

num_fields() 

num_rows()

num_rows() 

p()

p($name) 
deprecated 2012-10-02 This method is not longer supported.

Parameters

$name

Prepares the statement for execution and returns it back.

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));

Parameters

$statement

string

The sql statement to prepare.

Exceptions

\Exception If statement is empty or function is called with less than 2 parameters

Returns

stringThe prepared sql statement

Executes the statement.

query(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));

Parameters

$statement

string

The SQL statement to execute.

Returns

resourceintobjectboolDepends on used database driver, false on error

Logs passed message, basically the last db error to the error log.

reportHalt(string $message) : void

Concatenates a detailed error message and invoke PHP's error_log() method.

Parameters

$message

string

Moves the cursor (position inside current result sets).

seek($pos) : void

Parameters

$pos

server_info()

server_info() 
deprecated 2012-10-02 This method is deprecated. Use getTableNames instead.

Setter for default database configuration, the connection values.

setDefaultConfiguration(array $defaultDbCfg) : void
Static

Parameters

$defaultDbCfg

array

table_names()

table_names() 
deprecated 2012-10-02 This method is deprecated. Use getTableNames instead.

Returns current record set as a associative and/or indexed array.

toArray(string $fetchMode) : array

Parameters

$fetchMode

string

One of cDbDriverHandler::FETCH_* constants

Returns

array

Returns current record set as a object

toObject() : \stdClass

Returns

Builds and executes a update query.

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);

Parameters

$tableName

string

The table name

$fields

array

Assoziative array of fields to update

$whereClauses

array

Assoziative array of field in where clause. Multiple entries will be concatenated with AND

Returns

bool

Adds a entry to the profile data.

_addProfileData(float $timeStart, float $timeEnd, string $statement) : void
Static

Parameters

$timeStart

float

$timeEnd

float

$statement

string

Returns connection from connection cache

_getConnection(mixed $data) : mixed

Parameters

$data

mixed

Connection data array or variable

Returns

mixedEither The connection (object, resource, integer) or null

Prepares the passed statement.

_prepareStatement(string $statement, array $arguments) : string

Parameters

$statement

string

$arguments

array

Returns

string

Prepares a statement with named parameter for execution.

_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));

Parameters

$statement

string

$arguments

array

Arguments array containing the query with named parameter and assoziative entries array

Returns

string

Prepares a statement with parameter for execution.

_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);

Parameters

$statement

string

$arguments

array

Arguments array containing the query with formatting signs and the entries.

Returns

string

Removes connection from cache

_removeConnection(mixed $connection) : void

Parameters

$connection

mixed

The connection to remove in cache

Stores connection in connection cache

_setConnection(mixed $data, mixed $connection) : void

Parameters

$data

mixed

Connection data array

$connection

mixed

The connection to store in cache

 Properties

 

$_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
 

$_metaCache 
array
 

$_profileData : array

 Constants

 

FETCH_ASSOC

FETCH_ASSOC 
 

FETCH_BOTH

FETCH_BOTH 
 

FETCH_NUMERIC

FETCH_NUMERIC 
 

HALT_NO

HALT_NO 
 

HALT_REPORT

HALT_REPORT 
 

HALT_YES

HALT_YES