PHP Класс 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}.
С версии: 3.0
Автор: Qiang Xue ([email protected])
Наследование: extends Prado\TComponent
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__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 )

Защищенные методы

Метод Описание
close ( ) Closes the currently active DB connection.
open ( ) Opens DB connection if it is currently not
setConnectionCharset ( ) * Set the database connection charset.

Описание методов

__construct() публичный Метод

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
См. также: http://www.php.net/manual/en/function.PDO-construct.php
public __construct ( $dsn = '', $username = '', $password = '', $charset = '' )

__sleep() публичный Метод

Close the connection when serializing.
public __sleep ( )

beginTransaction() публичный Метод

Starts a transaction.
public beginTransaction ( ) : TDbTransaction
Результат TDbTransaction the transaction initiated

close() защищенный Метод

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

createCommand() публичный Метод

Creates a command for execution.
public createCommand ( $sql ) : TDbCommand
Результат TDbCommand the DB command

getActive() публичный Метод

public getActive ( ) : boolean
Результат boolean whether the DB connection is established

getAttribute() публичный Метод

Obtains a specific DB connection attribute information.
См. также: http://www.php.net/manual/en/function.PDO-getAttribute.php
public getAttribute ( $name ) : mixed
Результат mixed the corresponding attribute information

getAutoCommit() публичный Метод

public getAutoCommit ( ) : boolean
Результат boolean whether creating or updating a DB record will be automatically committed. Some DBMS (such as sqlite) may not support this feature.

getAvailableDrivers() публичный статический Метод

См. также: http://www.php.net/manual/en/function.PDO-getAvailableDrivers.php
public static getAvailableDrivers ( ) : array
Результат array list of available PDO drivers

getCharset() публичный Метод

public getCharset ( ) : string
Результат string the charset used for database connection. Defaults to emtpy string.

getClientVersion() публичный Метод

public getClientVersion ( ) : string
Результат string the version information of the DB driver

getColumnCase() публичный Метод

public getColumnCase ( ) : TDbColumnCaseMode
Результат TDbColumnCaseMode the case of the column names

getConnectionStatus() публичный Метод

public getConnectionStatus ( ) : string
Результат string the status of the connection Some DBMS (such as sqlite) may not support this feature.

getConnectionString() публичный Метод

public getConnectionString ( ) : string
Результат string The Data Source Name, or DSN, contains the information required to connect to the database.

getCurrentTransaction() публичный Метод

public getCurrentTransaction ( ) : TDbTransaction
Результат TDbTransaction the currently active transaction. Null if no active transaction.

getDbMetaData() публичный Метод

public getDbMetaData ( ) : TDbMetaData
Результат Prado\Data\Common\TDbMetaData

getDriverName() публичный Метод

public getDriverName ( ) : string
Результат string name of the DB driver

getLastInsertID() публичный Метод

Returns the ID of the last inserted row or sequence value.
См. также: http://www.php.net/manual/en/function.PDO-lastInsertId.php
public getLastInsertID ( $sequenceName = '' ) : string
Результат string the row ID of the last row inserted, or the last value retrieved from the sequence object

getNullConversion() публичный Метод

public getNullConversion ( ) : TDbNullConversionMode
Результат TDbNullConversionMode how the null and empty strings are converted

getPassword() публичный Метод

public getPassword ( ) : string
Результат string the password for establishing DB connection. Defaults to empty string.

getPdoInstance() публичный Метод

public getPdoInstance ( ) : PDO
Результат PDO the PDO instance, null if the connection is not established yet

getPersistent() публичный Метод

public getPersistent ( ) : boolean
Результат boolean whether the connection is persistent or not Some DBMS (such as sqlite) may not support this feature.

getPrefetch() публичный Метод

public getPrefetch ( ) : boolean
Результат boolean whether the connection performs data prefetching

getServerInfo() публичный Метод

public getServerInfo ( ) : string
Результат string the information of DBMS server

getServerVersion() публичный Метод

public getServerVersion ( ) : string
Результат string the version information of DBMS server

getTimeout() публичный Метод

public getTimeout ( ) : integer
Результат integer timeout settings for the connection

getTransactionClass() публичный Метод

С версии: 3.1.7
public getTransactionClass ( ) : string
Результат string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. Defaults to 'System.Data.TDbTransaction'.

getUsername() публичный Метод

public getUsername ( ) : string
Результат string the username for establishing DB connection. Defaults to empty string.

open() защищенный Метод

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

quoteColumnAlias() публичный Метод

Quotes a column alias for use in a query.
public quoteColumnAlias ( string $name ) : string
$name string column name
Результат string the properly quoted column alias

quoteColumnName() публичный Метод

Quotes a column name for use in a query.
public quoteColumnName ( string $name ) : string
$name string column name
Результат string the properly quoted column name

quoteString() публичный Метод

Quotes a string for use in a query.
См. также: http://www.php.net/manual/en/function.PDO-quote.php
public quoteString ( $str ) : string
Результат string the properly quoted string

quoteTableName() публичный Метод

Quotes a table name for use in a query.
public quoteTableName ( string $name ) : string
$name string table name
Результат string the properly quoted table name

setActive() публичный Метод

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

setAttribute() публичный Метод

Sets an attribute on the database connection.
См. также: http://www.php.net/manual/en/function.PDO-setAttribute.php
public setAttribute ( $name, $value )

setAutoCommit() публичный Метод

public setAutoCommit ( $value )

setCharset() публичный Метод

public setCharset ( $value )

setColumnCase() публичный Метод

public setColumnCase ( $value )

setConnectionCharset() защищенный Метод

Only MySql databases are supported for now.
С версии: 3.1.2
protected setConnectionCharset ( )

setConnectionString() публичный Метод

См. также: http://www.php.net/manual/en/function.PDO-construct.php
public setConnectionString ( $value )

setNullConversion() публичный Метод

public setNullConversion ( $value )

setPassword() публичный Метод

public setPassword ( $value )

setPersistent() публичный Метод

public setPersistent ( $value )

setTransactionClass() публичный Метод

С версии: 3.1.7
public setTransactionClass ( $value )

setUsername() публичный Метод

public setUsername ( $value )