PHP Class Prado\Data\TDbConnection

TDbConnection represents a connection to a database. TDbConnection works together with {@link TDbCommand}, {@link TDbDataReader} and {@link TDbTransaction} to provide data access to various DBMS in a common set of APIs. They are a thin wrapper of the {@link http://www.php.net/manual/en/ref.pdo.php PDO} PHP extension. To establish a connection, set {@link setActive Active} to true after specifying {@link setConnectionString ConnectionString}, {@link setUsername Username} and {@link setPassword Password}. Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) using the {@link setCharset Charset} property. The value of this property is database dependant. e.g. for mysql, you can use 'latin1' for cp1252 West European, 'utf8' for unicode, ... The following example shows how to create a TDbConnection instance and establish the actual connection: $connection=new TDbConnection($dsn,$username,$password); $connection->Active=true; After the DB connection is established, one can execute an SQL statement like the following: $command=$connection->createCommand($sqlStatement); $command->execute(); // a non-query SQL statement execution or execute an SQL query and fetch the result set $reader=$command->query(); each $row is an array representing a row of data foreach($reader as $row) ... One can do prepared SQL execution and bind parameters to the prepared SQL: $command=$connection->createCommand($sqlStatement); $command->bindParameter($name1,$value1); $command->bindParameter($name2,$value2); $command->execute(); To use transaction, do like the following: $transaction=$connection->beginTransaction(); try { $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); .... other SQL executions $transaction->commit(); } catch(Exception $e) { $transaction->rollBack(); } TDbConnection provides a set of methods to support setting and querying of certain DBMS attributes, such as {@link getNullConversion NullConversion}.
Since: 3.0
Author: Qiang Xue ([email protected])
Inheritance: extends Prado\TComponent
Show file Open project: pradosoft/prado Class Usage Examples

Public Methods

Method Description
__construct ( $dsn = '', $username = '', $password = '', $charset = '' ) Constructor.
__sleep ( ) Close the connection when serializing.
beginTransaction ( ) : TDbTransaction Starts a transaction.
createCommand ( $sql ) : TDbCommand Creates a command for execution.
getActive ( ) : boolean
getAttribute ( $name ) : mixed Obtains a specific DB connection attribute information.
getAutoCommit ( ) : boolean
getAvailableDrivers ( ) : array
getCharset ( ) : string
getClientVersion ( ) : string
getColumnCase ( ) : TDbColumnCaseMode
getConnectionStatus ( ) : string
getConnectionString ( ) : string
getCurrentTransaction ( ) : TDbTransaction
getDbMetaData ( ) : TDbMetaData
getDriverName ( ) : string
getLastInsertID ( $sequenceName = '' ) : string Returns the ID of the last inserted row or sequence value.
getNullConversion ( ) : TDbNullConversionMode
getPassword ( ) : string
getPdoInstance ( ) : PDO
getPersistent ( ) : boolean
getPrefetch ( ) : boolean
getServerInfo ( ) : string
getServerVersion ( ) : string
getTimeout ( ) : integer
getTransactionClass ( ) : string
getUsername ( ) : string
quoteColumnAlias ( string $name ) : string Quotes a column alias for use in a query.
quoteColumnName ( string $name ) : string Quotes a column name for use in a query.
quoteString ( $str ) : string Quotes a string for use in a query.
quoteTableName ( string $name ) : string Quotes a table name for use in a query.
setActive ( $value ) Open or close the DB connection.
setAttribute ( $name, $value ) Sets an attribute on the database connection.
setAutoCommit ( $value )
setCharset ( $value )
setColumnCase ( $value )
setConnectionString ( $value )
setNullConversion ( $value )
setPassword ( $value )
setPersistent ( $value )
setTransactionClass ( $value )
setUsername ( $value )

Protected Methods

Method Description
close ( ) Closes the currently active DB connection.
open ( ) Opens DB connection if it is currently not
setConnectionCharset ( ) * Set the database connection charset.

Method Details

__construct() public method

Note, the DB connection is not established when this connection instance is created. Set {@link setActive Active} property to true to establish the connection. Since 3.1.2, you can set the charset for MySql connection
See also: http://www.php.net/manual/en/function.PDO-construct.php
public __construct ( $dsn = '', $username = '', $password = '', $charset = '' )

__sleep() public method

Close the connection when serializing.
public __sleep ( )

beginTransaction() public method

Starts a transaction.
public beginTransaction ( ) : TDbTransaction
return TDbTransaction the transaction initiated

close() protected method

It does nothing if the connection is already closed.
protected close ( )

createCommand() public method

Creates a command for execution.
public createCommand ( $sql ) : TDbCommand
return TDbCommand the DB command

getActive() public method

public getActive ( ) : boolean
return boolean whether the DB connection is established

getAttribute() public method

Obtains a specific DB connection attribute information.
See also: http://www.php.net/manual/en/function.PDO-getAttribute.php
public getAttribute ( $name ) : mixed
return mixed the corresponding attribute information

getAutoCommit() public method

public getAutoCommit ( ) : boolean
return boolean whether creating or updating a DB record will be automatically committed. Some DBMS (such as sqlite) may not support this feature.

getAvailableDrivers() public static method

See also: http://www.php.net/manual/en/function.PDO-getAvailableDrivers.php
public static getAvailableDrivers ( ) : array
return array list of available PDO drivers

getCharset() public method

public getCharset ( ) : string
return string the charset used for database connection. Defaults to emtpy string.

getClientVersion() public method

public getClientVersion ( ) : string
return string the version information of the DB driver

getColumnCase() public method

public getColumnCase ( ) : TDbColumnCaseMode
return TDbColumnCaseMode the case of the column names

getConnectionStatus() public method

public getConnectionStatus ( ) : string
return string the status of the connection Some DBMS (such as sqlite) may not support this feature.

getConnectionString() public method

public getConnectionString ( ) : string
return string The Data Source Name, or DSN, contains the information required to connect to the database.

getCurrentTransaction() public method

public getCurrentTransaction ( ) : TDbTransaction
return TDbTransaction the currently active transaction. Null if no active transaction.

getDbMetaData() public method

public getDbMetaData ( ) : TDbMetaData
return Prado\Data\Common\TDbMetaData

getDriverName() public method

public getDriverName ( ) : string
return string name of the DB driver

getLastInsertID() public method

Returns the ID of the last inserted row or sequence value.
See also: http://www.php.net/manual/en/function.PDO-lastInsertId.php
public getLastInsertID ( $sequenceName = '' ) : string
return string the row ID of the last row inserted, or the last value retrieved from the sequence object

getNullConversion() public method

public getNullConversion ( ) : TDbNullConversionMode
return TDbNullConversionMode how the null and empty strings are converted

getPassword() public method

public getPassword ( ) : string
return string the password for establishing DB connection. Defaults to empty string.

getPdoInstance() public method

public getPdoInstance ( ) : PDO
return PDO the PDO instance, null if the connection is not established yet

getPersistent() public method

public getPersistent ( ) : boolean
return boolean whether the connection is persistent or not Some DBMS (such as sqlite) may not support this feature.

getPrefetch() public method

public getPrefetch ( ) : boolean
return boolean whether the connection performs data prefetching

getServerInfo() public method

public getServerInfo ( ) : string
return string the information of DBMS server

getServerVersion() public method

public getServerVersion ( ) : string
return string the version information of DBMS server

getTimeout() public method

public getTimeout ( ) : integer
return integer timeout settings for the connection

getTransactionClass() public method

Since: 3.1.7
public getTransactionClass ( ) : string
return string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. Defaults to 'System.Data.TDbTransaction'.

getUsername() public method

public getUsername ( ) : string
return string the username for establishing DB connection. Defaults to empty string.

open() protected method

Opens DB connection if it is currently not
protected open ( )

quoteColumnAlias() public method

Quotes a column alias for use in a query.
public quoteColumnAlias ( string $name ) : string
$name string column name
return string the properly quoted column alias

quoteColumnName() public method

Quotes a column name for use in a query.
public quoteColumnName ( string $name ) : string
$name string column name
return string the properly quoted column name

quoteString() public method

Quotes a string for use in a query.
See also: http://www.php.net/manual/en/function.PDO-quote.php
public quoteString ( $str ) : string
return string the properly quoted string

quoteTableName() public method

Quotes a table name for use in a query.
public quoteTableName ( string $name ) : string
$name string table name
return string the properly quoted table name

setActive() public method

Open or close the DB connection.
public setActive ( $value )

setAttribute() public method

Sets an attribute on the database connection.
See also: http://www.php.net/manual/en/function.PDO-setAttribute.php
public setAttribute ( $name, $value )

setAutoCommit() public method

public setAutoCommit ( $value )

setCharset() public method

public setCharset ( $value )

setColumnCase() public method

public setColumnCase ( $value )

setConnectionCharset() protected method

Only MySql databases are supported for now.
Since: 3.1.2
protected setConnectionCharset ( )

setConnectionString() public method

See also: http://www.php.net/manual/en/function.PDO-construct.php
public setConnectionString ( $value )

setNullConversion() public method

public setNullConversion ( $value )

setPassword() public method

public setPassword ( $value )

setPersistent() public method

public setPersistent ( $value )

setTransactionClass() public method

Since: 3.1.7
public setTransactionClass ( $value )

setUsername() public method

public setUsername ( $value )