2007-01-07 17:52:16 +03:00
< ? php
/*
* $Id $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
* SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
* LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL . For more information , see
2008-01-23 01:52:53 +03:00
* < http :// www . phpdoctrine . org >.
2007-01-07 17:52:16 +03:00
*/
2008-05-06 17:41:22 +04:00
2008-05-30 16:09:24 +04:00
#namespace Doctrine::DBAL::Connections;
2008-07-27 23:38:56 +04:00
#use Doctrine::Common::Configuration;
2008-08-01 22:46:14 +04:00
#use Doctrine::Common::EventManager;
#use Doctrine::DBAL::Exceptions::ConnectionException;
2008-07-27 23:38:56 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-08-02 21:41:37 +04:00
* A thin wrapper on top of the PDO class .
2007-05-27 22:56:04 +04:00
*
* 1. Event listeners
* An easy to use , pluggable eventlistener architecture . Aspects such as
* logging , query profiling and caching can be easily implemented through
* the use of these listeners
*
* 2. Lazy - connecting
* Creating an instance of Doctrine_Connection does not connect
* to database . Connecting to database is only invoked when actually needed
2008-01-29 12:59:37 +03:00
* ( for example when query () is being called )
2007-05-27 22:56:04 +04:00
*
* 3. Convenience methods
2007-06-15 01:50:12 +04:00
* Doctrine_Connection provides many convenience methods such as fetchAll (), fetchOne () etc .
2007-05-27 22:56:04 +04:00
*
2007-01-07 17:52:16 +03:00
* @ license http :// www . opensource . org / licenses / lgpl - license . php LGPL
* @ since 1.0
* @ version $Revision $
* @ author Konsta Vesterinen < kvesteri @ cc . hut . fi >
* @ author Lukas Smith < smith @ pooteeweet . org > ( MDB2 library )
2008-02-13 13:53:07 +03:00
* @ author Roman Borschel < roman @ code - factory . org >
2008-06-05 23:01:58 +04:00
* @ todo
* 1 ) REPLICATION SUPPORT
* Replication support should be tackled at this layer ( DBAL ) .
* There can be options that look like :
* 'slaves' => array (
* 'slave1' => array (
* user , pass etc .
* ),
* 'slave2' => array (
* user , pass etc .
* )),
* 'slaveConnectionResolver' => new MySlaveConnectionResolver (),
* 'masters' => array ( ... ),
* 'masterConnectionResolver' => new MyMasterConnectionResolver ()
*
2008-07-27 23:38:56 +04:00
* Doctrine :: DBAL could ship with a simple standard broker that uses a primitive
2008-08-01 22:46:14 +04:00
* round - robin approach to distribution . User can provide its own brokers .
2007-01-07 17:52:16 +03:00
*/
2008-08-01 22:46:14 +04:00
abstract class Doctrine_Connection
2007-01-07 17:52:16 +03:00
{
/**
2008-02-04 00:29:57 +03:00
* The PDO database handle .
*
2008-07-04 20:32:19 +04:00
* @ var PDO
2007-01-07 17:52:16 +03:00
*/
2008-07-04 20:32:19 +04:00
protected $_pdo ;
2008-05-06 17:41:22 +04:00
2008-06-05 23:01:58 +04:00
/**
* The Configuration .
*
2008-07-27 23:38:56 +04:00
* @ var Doctrine :: Common :: Configuration
2008-06-05 23:01:58 +04:00
*/
protected $_config ;
/**
* The EventManager .
*
2008-07-27 23:38:56 +04:00
* @ var Doctrine :: Commom :: EventManager
2008-06-05 23:01:58 +04:00
*/
protected $_eventManager ;
2008-01-23 00:42:17 +03:00
/**
* Name of the connection
*
* @ var string $_name
*/
protected $_name ;
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-02-04 00:29:57 +03:00
* The name of this connection driver .
*
* @ var string $driverName
2007-01-07 17:52:16 +03:00
*/
2008-07-04 20:32:19 +04:00
protected $_driverName ;
2008-05-06 17:41:22 +04:00
2007-06-12 23:39:03 +04:00
/**
2008-02-04 00:29:57 +03:00
* Whether or not a connection has been established .
*
2008-08-01 22:46:14 +04:00
* @ var boolean
2007-06-12 23:39:03 +04:00
*/
2008-07-04 20:32:19 +04:00
protected $_isConnected = false ;
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-07-27 23:38:56 +04:00
* Boolean flag that indicates whether identifiers should get quoted .
2008-02-04 00:29:57 +03:00
*
2008-07-27 23:38:56 +04:00
* @ var boolean
2007-01-07 17:52:16 +03:00
*/
2008-07-27 23:38:56 +04:00
protected $_quoteIdentifiers ;
2008-05-06 17:41:22 +04:00
/**
2008-08-02 21:41:37 +04:00
* @ var array
2008-05-06 17:41:22 +04:00
*/
2008-08-02 21:41:37 +04:00
protected $_serverInfo = array ();
2008-05-06 17:41:22 +04:00
/**
2008-06-05 23:01:58 +04:00
* The parameters used during creation of the Connection .
2008-07-21 00:13:24 +04:00
*
* @ var array
2008-05-06 17:41:22 +04:00
*/
2008-06-05 23:01:58 +04:00
protected $_params = array ();
2008-05-06 17:41:22 +04:00
/**
* List of all available drivers .
*
2008-07-27 23:38:56 +04:00
* @ var array $availableDrivers
* @ todo Move elsewhere .
2008-05-06 17:41:22 +04:00
*/
2008-07-04 20:32:19 +04:00
private static $_availableDrivers = array (
2008-05-06 17:41:22 +04:00
'Mysql' , 'Pgsql' , 'Oracle' , 'Informix' , 'Mssql' , 'Sqlite' , 'Firebird'
);
/**
* The query count . Represents the number of executed database queries by the connection .
*
* @ var integer
*/
2008-07-04 20:32:19 +04:00
protected $_queryCount = 0 ;
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-07-27 23:38:56 +04:00
* The DatabasePlatform object that provides information about the
* database platform used by the connection .
2007-01-07 17:52:16 +03:00
*
2008-07-27 23:38:56 +04:00
* @ var Doctrine :: DBAL :: Platforms :: DatabasePlatform
2007-01-07 17:52:16 +03:00
*/
2008-08-01 22:46:14 +04:00
protected $_platform ;
/**
2008-08-02 21:41:37 +04:00
* The transaction object .
2008-08-01 22:46:14 +04:00
*
* @ var Doctrine :: DBAL :: Transactions :: Transaction
*/
protected $_transaction ;
2008-08-02 21:41:37 +04:00
/**
* The sequence manager .
*
* @ var Doctrine :: DBAL :: Sequencing :: SequenceManager
*/
protected $_sequenceManager ;
2008-08-09 13:45:28 +04:00
/**
* The schema manager .
*
* @ var Doctrine :: DBAL :: Schema :: SchemaManager
*/
protected $_schemaManager ;
2007-01-07 17:52:16 +03:00
/**
2008-05-30 16:09:24 +04:00
* Constructor .
2008-07-27 23:38:56 +04:00
* Creates a new Connection .
2007-01-07 17:52:16 +03:00
*
2008-07-04 20:32:19 +04:00
* @ param array $params The connection parameters .
2007-01-07 17:52:16 +03:00
*/
2008-06-05 23:01:58 +04:00
public function __construct ( array $params )
2007-01-07 17:52:16 +03:00
{
2008-06-05 23:01:58 +04:00
if ( isset ( $params [ 'pdo' ])) {
2008-07-04 20:32:19 +04:00
$this -> _pdo = $params [ 'pdo' ];
$this -> _isConnected = true ;
2007-06-12 23:39:03 +04:00
}
2008-06-05 23:01:58 +04:00
$this -> _params = $params ;
}
/**
* Sets the Configuration used by the Connection .
*
2008-07-27 23:38:56 +04:00
* @ param Doctrine :: Common :: Configuration $config
2008-06-05 23:01:58 +04:00
*/
public function setConfiguration ( Doctrine_Configuration $config )
{
$this -> _config = $config ;
}
/**
* Gets the Configuration used by the Connection .
*
* @ return Configuration
*/
public function getConfiguration ()
{
if ( ! $this -> _config ) {
$this -> _config = new Doctrine_Configuration ();
}
return $this -> _config ;
}
/**
* Sets the EventManager used by the Connection .
*
2008-07-27 23:38:56 +04:00
* @ param Doctrine :: Common :: EventManager $eventManager
2008-06-05 23:01:58 +04:00
*/
public function setEventManager ( Doctrine_EventManager $eventManager )
{
$this -> _eventManager = $eventManager ;
}
/**
* Gets the EventManager used by the Connection .
*
2008-07-27 23:38:56 +04:00
* @ return Doctrine :: Common :: EventManager
2008-06-05 23:01:58 +04:00
*/
public function getEventManager ()
{
if ( ! $this -> _eventManager ) {
$this -> _eventManager = new Doctrine_EventManager ();
}
return $this -> _eventManager ;
2007-01-07 17:52:16 +03:00
}
2008-05-06 17:41:22 +04:00
2008-07-27 23:38:56 +04:00
/**
2008-08-01 22:46:14 +04:00
* Gets the DatabasePlatform for the connection .
2008-07-27 23:38:56 +04:00
*
2008-08-01 22:46:14 +04:00
* @ return Doctrine :: DBAL :: Platforms :: DatabasePlatform
2008-07-27 23:38:56 +04:00
*/
2008-08-01 22:46:14 +04:00
public function getDatabasePlatform ()
2008-06-15 19:56:28 +04:00
{
2008-08-01 22:46:14 +04:00
throw new Doctrine_Connection_Exception ( " No DatabasePlatform available "
. " for connection " . get_class ( $this ));
2008-06-15 19:56:28 +04:00
}
2007-06-12 23:39:03 +04:00
/**
2008-07-27 23:38:56 +04:00
* Returns an array of available PDO drivers
* @ todo Move elsewhere .
2007-06-12 23:39:03 +04:00
*/
public static function getAvailableDrivers ()
{
return PDO :: getAvailableDrivers ();
}
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
* returns the name of this driver
*
* @ return string the name of this driver
*/
public function getName ()
2008-01-23 00:42:17 +03:00
{
return $this -> _name ;
}
2008-05-06 17:41:22 +04:00
2008-02-15 21:42:06 +03:00
/**
* Sets the name of the connection
*
* @ param string $name
* @ return void
*/
2008-01-23 00:42:17 +03:00
public function setName ( $name )
{
$this -> _name = $name ;
}
2008-01-29 12:59:37 +03:00
2008-02-15 21:42:06 +03:00
/**
* Gets the name of the instance driver
*
* @ return void
*/
2008-01-23 00:42:17 +03:00
public function getDriverName ()
2007-01-07 17:52:16 +03:00
{
2008-07-04 20:32:19 +04:00
return $this -> _driverName ;
2007-01-07 17:52:16 +03:00
}
2008-05-06 17:41:22 +04:00
2008-07-04 20:32:19 +04:00
/**
* Gets the PDO handle used by the connection .
*
* @ return PDO
*/
public function getPdo ()
{
$this -> connect ();
return $this -> _pdo ;
2007-01-07 17:52:16 +03:00
}
2008-05-06 17:41:22 +04:00
2007-06-12 03:25:46 +04:00
/**
2008-02-04 00:29:57 +03:00
* Establishes the connection with the database .
2007-06-12 03:25:46 +04:00
*
* @ return boolean
*/
public function connect ()
{
2008-07-04 20:32:19 +04:00
if ( $this -> _isConnected ) {
2007-06-12 03:25:46 +04:00
return false ;
}
2008-05-24 22:18:37 +04:00
//$event = new Doctrine_Event($this, Doctrine_Event::CONN_CONNECT);
//$this->getListener()->preConnect($event);
2007-06-12 03:25:46 +04:00
2008-07-04 20:32:19 +04:00
// TODO: the extension_loaded check can happen earlier, maybe in the factory
2008-07-21 00:13:24 +04:00
if ( ! extension_loaded ( 'pdo' )) {
2008-05-18 00:04:56 +04:00
throw new Doctrine_Connection_Exception ( " Couldn't locate driver named " . $e [ 0 ]);
2007-06-12 03:25:46 +04:00
}
2008-07-21 00:13:24 +04:00
$driverOptions = isset ( $this -> _params [ 'driverOptions' ]) ?
$this -> _params [ 'driverOptions' ] : array ();
$user = isset ( $this -> _params [ 'user' ]) ?
$this -> _params [ 'user' ] : null ;
$password = isset ( $this -> _params [ 'password' ]) ?
$this -> _params [ 'password' ] : null ;
$this -> _pdo = new PDO (
$this -> _constructPdoDsn (),
$user ,
$password ,
$driverOptions
);
$this -> _pdo -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
$this -> _pdo -> setAttribute ( PDO :: ATTR_CASE , PDO :: CASE_LOWER );
2007-06-12 03:25:46 +04:00
2007-06-12 23:39:03 +04:00
// attach the pending attributes to adapter
2008-07-04 20:32:19 +04:00
/* foreach ( $this -> pendingAttributes as $attr => $value ) {
2007-06-12 03:25:46 +04:00
// some drivers don't support setting this so we just skip it
2007-09-03 18:57:18 +04:00
if ( $attr == Doctrine :: ATTR_DRIVER_NAME ) {
2007-06-12 03:25:46 +04:00
continue ;
}
2008-07-04 20:32:19 +04:00
$this -> _pdo -> setAttribute ( $attr , $value );
} */
2007-06-12 03:25:46 +04:00
2008-07-04 20:32:19 +04:00
$this -> _isConnected = true ;
2007-06-12 03:25:46 +04:00
2008-05-24 22:18:37 +04:00
//$this->getListener()->postConnect($event);
2007-06-12 03:25:46 +04:00
return true ;
}
2008-02-04 00:29:57 +03:00
2008-06-05 23:01:58 +04:00
/**
* Constructs the PDO DSN for use in the PDO constructor .
* Concrete connection implementations override this implementation to
* create the proper DSN .
*
* @ return string
* @ todo make abstract , implement in subclasses .
2008-08-02 21:41:37 +04:00
* @ todo throw different exception ?
2008-06-05 23:01:58 +04:00
*/
protected function _constructPdoDsn ()
{
2008-07-04 20:32:19 +04:00
throw Doctrine_Exception :: notImplemented ( '_constructPdoDsn' , get_class ( $this ));
2008-06-05 23:01:58 +04:00
}
2007-01-07 17:52:16 +03:00
/**
* Execute a SQL REPLACE query . A REPLACE query is identical to a INSERT
* query , except that if there is already a row in the table with the same
* key field values , the REPLACE query just updates its values instead of
* inserting a new row .
*
* The REPLACE type of query does not make part of the SQL standards . Since
2008-08-02 21:41:37 +04:00
* practically only MySQL and SQLite implement it natively , this type of
* query is emulated through this method for other DBMS using standard types
2007-01-07 17:52:16 +03:00
* of queries inside a transaction to assure the atomicity of the operation .
*
* @ param string name of the table on which the REPLACE query will
* be executed .
*
* @ param array an associative array that describes the fields and the
* values that will be inserted or updated in the specified table . The
* indexes of the array are the names of all the fields of the table .
*
* The values of the array are values to be assigned to the specified field .
*
* @ param array $keys an array containing all key fields ( primary key fields
* or unique index fields ) for this table
*
* the uniqueness of a row will be determined according to
* the provided key fields
*
* this method will fail if no key fields are specified
*
* @ throws Doctrine_Connection_Exception if this driver doesn ' t support replace
* @ throws Doctrine_Connection_Exception if some of the key values was null
* @ throws Doctrine_Connection_Exception if there were no key fields
* @ throws PDOException if something fails at PDO level
2008-02-04 00:29:57 +03:00
* @ return integer number of rows affected
2007-01-07 17:52:16 +03:00
*/
2008-02-12 15:31:28 +03:00
public function replace ( $tableName , array $data , array $keys )
2007-01-07 17:52:16 +03:00
{
if ( empty ( $keys )) {
throw new Doctrine_Connection_Exception ( 'Not specified which fields are keys' );
}
$condition = $values = array ();
2008-02-12 15:31:28 +03:00
foreach ( $data as $columnName => $value ) {
$values [ $columnName ] = $value ;
2007-01-07 17:52:16 +03:00
2008-02-12 15:31:28 +03:00
if ( in_array ( $columnName , $keys )) {
2007-01-07 17:52:16 +03:00
if ( $value === null )
2008-02-12 15:31:28 +03:00
throw new Doctrine_Connection_Exception ( 'key value ' . $columnName . ' may not be null' );
2007-01-07 17:52:16 +03:00
2008-02-12 15:31:28 +03:00
$condition [] = $columnName . ' = ?' ;
2007-01-07 17:52:16 +03:00
$conditionValues [] = $value ;
}
}
2008-02-12 15:31:28 +03:00
$query = 'DELETE FROM ' . $this -> quoteIdentifier ( $tableName )
2007-11-18 19:06:37 +03:00
. ' WHERE ' . implode ( ' AND ' , $condition );
$affectedRows = $this -> exec ( $query , $conditionValues );
2007-01-07 17:52:16 +03:00
$this -> insert ( $table , $values );
$affectedRows ++ ;
return $affectedRows ;
}
2007-10-21 10:23:59 +04:00
2007-11-10 13:37:10 +03:00
/**
2008-02-12 15:31:28 +03:00
* Deletes table row ( s ) matching the specified identifier .
2007-11-10 13:37:10 +03:00
*
* @ throws Doctrine_Connection_Exception if something went wrong at the database level
* @ param string $table The table to delete data from
2008-01-12 22:49:11 +03:00
* @ param array $identifier An associateve array containing identifier fieldname - value pairs .
2007-11-10 13:37:10 +03:00
* @ return integer The number of affected rows
*/
2008-02-12 15:31:28 +03:00
public function delete ( $tableName , array $identifier )
2007-11-10 13:37:10 +03:00
{
2008-01-12 22:49:11 +03:00
$criteria = array ();
2007-11-10 13:37:10 +03:00
foreach ( array_keys ( $identifier ) as $id ) {
2008-02-27 05:17:42 +03:00
$criteria [] = $this -> quoteIdentifier ( $id ) . ' = ?' ;
2007-11-10 13:37:10 +03:00
}
$query = 'DELETE FROM '
2008-02-12 15:31:28 +03:00
. $this -> quoteIdentifier ( $tableName )
2008-01-12 22:49:11 +03:00
. ' WHERE ' . implode ( ' AND ' , $criteria );
2007-11-10 13:37:10 +03:00
2007-11-10 16:21:40 +03:00
return $this -> exec ( $query , array_values ( $identifier ));
2007-11-10 13:37:10 +03:00
}
2007-11-10 03:57:13 +03:00
/**
* Updates table row ( s ) with specified data
*
* @ throws Doctrine_Connection_Exception if something went wrong at the database level
* @ param string $table The table to insert data into
* @ param array $values An associateve array containing column - value pairs .
* @ return mixed boolean false if empty value array was given ,
* otherwise returns the number of affected rows
*/
2008-02-12 15:31:28 +03:00
public function update ( $tableName , array $data , array $identifier )
2007-11-10 03:57:13 +03:00
{
2008-02-12 15:31:28 +03:00
if ( empty ( $data )) {
2007-11-10 03:57:13 +03:00
return false ;
}
$set = array ();
2008-02-12 15:31:28 +03:00
foreach ( $data as $columnName => $value ) {
2007-11-10 03:57:13 +03:00
if ( $value instanceof Doctrine_Expression ) {
2008-02-27 05:17:42 +03:00
$set [] = $this -> quoteIdentifier ( $columnName ) . ' = ' . $value -> getSql ();
2008-02-12 15:31:28 +03:00
unset ( $data [ $columnName ]);
2007-11-10 03:57:13 +03:00
} else {
2008-02-27 05:17:42 +03:00
$set [] = $this -> quoteIdentifier ( $columnName ) . ' = ?' ;
2007-11-10 03:57:13 +03:00
}
}
2008-02-12 15:31:28 +03:00
$params = array_merge ( array_values ( $data ), array_values ( $identifier ));
2007-11-10 03:57:13 +03:00
2008-02-12 15:31:28 +03:00
$sql = 'UPDATE ' . $this -> quoteIdentifier ( $tableName )
2007-11-10 03:57:13 +03:00
. ' SET ' . implode ( ', ' , $set )
2008-02-12 15:31:28 +03:00
. ' WHERE ' . implode ( ' = ? AND ' , array_keys ( $identifier ))
2007-11-10 03:57:13 +03:00
. ' = ?' ;
return $this -> exec ( $sql , $params );
}
2007-01-07 17:52:16 +03:00
/**
* Inserts a table row with specified data .
*
* @ param string $table The table to insert data into .
2008-01-12 22:49:11 +03:00
* @ param array $fields An associateve array containing fieldname - value pairs .
2007-11-10 13:37:10 +03:00
* @ return mixed boolean false if empty value array was given ,
* otherwise returns the number of affected rows
2007-01-07 17:52:16 +03:00
*/
2008-02-12 15:31:28 +03:00
public function insert ( $tableName , array $data )
2008-01-05 22:55:56 +03:00
{
2008-02-12 15:31:28 +03:00
if ( empty ( $data )) {
2007-01-07 17:52:16 +03:00
return false ;
}
2007-08-25 04:34:25 +04:00
// column names are specified as array keys
$cols = array ();
2008-08-02 21:41:37 +04:00
// the query VALUES will contain either expressions (eg 'NOW()') or ?
2007-07-01 15:27:45 +04:00
$a = array ();
2008-02-12 15:31:28 +03:00
foreach ( $data as $columnName => $value ) {
$cols [] = $this -> quoteIdentifier ( $columnName );
2007-07-01 15:27:45 +04:00
if ( $value instanceof Doctrine_Expression ) {
2007-08-25 04:34:25 +04:00
$a [] = $value -> getSql ();
2008-02-12 15:31:28 +03:00
unset ( $data [ $columnName ]);
2007-07-01 15:27:45 +04:00
} else {
2007-08-25 04:34:25 +04:00
$a [] = '?' ;
2007-07-01 15:27:45 +04:00
}
}
2008-01-29 12:59:37 +03:00
$query = 'INSERT INTO ' . $this -> quoteIdentifier ( $tableName )
2007-08-25 04:34:25 +04:00
. ' (' . implode ( ', ' , $cols ) . ') '
. 'VALUES (' ;
2007-07-01 15:27:45 +04:00
$query .= implode ( ', ' , $a ) . ')' ;
2008-02-28 18:30:55 +03:00
2008-02-12 15:31:28 +03:00
return $this -> exec ( $query , array_values ( $data ));
2008-01-29 12:59:37 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* Set the charset on the current connection
*
* @ param string charset
*/
public function setCharset ( $charset )
{
2008-01-12 22:49:11 +03:00
return true ;
2007-05-27 22:56:04 +04:00
}
2007-10-21 10:23:59 +04:00
2007-05-27 22:56:04 +04:00
/**
2008-01-12 22:49:11 +03:00
* Quote a string so it can be safely used as a table or column name .
2007-05-27 22:56:04 +04:00
*
* Delimiting style depends on which database driver is being used .
*
* NOTE : just because you CAN use delimited identifiers doesn ' t mean
* you SHOULD use them . In general , they end up causing way more
* problems than they solve .
*
* Portability is broken by using the following characters inside
* delimited identifiers :
* + backtick ( < kbd > ` </ kbd > ) -- due to MySQL
* + double quote ( < kbd > " </kbd>) -- due to Oracle
* + brackets ( < kbd > [ </ kbd > or < kbd > ] </ kbd > ) -- due to Access
*
* Delimited identifiers are known to generally work correctly under
* the following drivers :
* + mssql
* + mysql
* + mysqli
* + oci8
* + pgsql
* + sqlite
*
* InterBase doesn ' t seem to be able to use delimited identifiers
* via PHP 4. They work fine under PHP 5.
*
* @ param string $str identifier name to be quoted
* @ param bool $checkOption check the 'quote_identifier' option
*
* @ return string quoted identifier string
*/
public function quoteIdentifier ( $str , $checkOption = true )
{
2008-07-27 23:38:56 +04:00
if ( is_null ( $this -> _quoteIdentifiers )) {
$this -> _quoteIdentifiers = $this -> _config -> get ( 'quoteIdentifiers' );
}
if ( ! $this -> _quoteIdentifiers ) {
return $str ;
}
2007-09-03 18:57:18 +04:00
// quick fix for the identifiers that contain a dot
2007-07-13 02:31:16 +04:00
if ( strpos ( $str , '.' )) {
$e = explode ( '.' , $str );
2008-08-02 21:41:37 +04:00
return $this -> quoteIdentifier ( $e [ 0 ])
. '.'
2008-07-27 23:38:56 +04:00
. $this -> quoteIdentifier ( $e [ 1 ]);
2007-07-13 02:31:16 +04:00
}
2008-07-27 23:38:56 +04:00
2008-08-02 21:41:37 +04:00
$c = $this -> _platform -> getIdentifierQuoteCharacter ();
$str = str_replace ( $c , $c . $c , $str );
return $c . $str . $c ;
2007-05-27 22:56:04 +04:00
}
2007-10-21 10:23:59 +04:00
2007-05-27 22:56:04 +04:00
/**
2008-07-27 23:38:56 +04:00
* Some drivers need the boolean values to be converted into integers
* when using DQL API .
2007-05-27 22:56:04 +04:00
*
* This method takes care of that conversion
*
* @ param array $item
* @ return void
*/
public function convertBooleans ( $item )
{
2008-07-27 23:38:56 +04:00
if ( is_array ( $item )) {
foreach ( $item as $k => $value ) {
if ( is_bool ( $value )) {
$item [ $k ] = ( int ) $value ;
}
}
} else {
if ( is_bool ( $item )) {
$item = ( int ) $item ;
}
}
return $item ;
2007-05-27 22:56:04 +04:00
}
2007-10-21 10:23:59 +04:00
2007-05-27 22:56:04 +04:00
/**
2008-08-02 21:41:37 +04:00
* Quotes given input parameter .
2007-05-27 22:56:04 +04:00
*
2008-08-02 21:41:37 +04:00
* @ param mixed $input Parameter to be quoted .
* @ param string $type Type of the parameter .
* @ return string The quoted parameter .
2007-05-27 22:56:04 +04:00
*/
public function quote ( $input , $type = null )
{
2008-07-04 20:32:19 +04:00
return $this -> _pdo -> quote ( $input , $type );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-20 14:44:41 +03:00
/**
* Set the date / time format for the current connection
*
* @ param string time format
*
* @ return void
*/
public function setDateFormat ( $format = null )
{
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchAll
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ return array
*/
2008-01-29 12:59:37 +03:00
public function fetchAll ( $statement , array $params = array ())
2007-01-25 01:50:49 +03:00
{
2008-07-27 23:38:56 +04:00
return $this -> execute ( $statement , $params ) -> fetchAll ( PDO :: FETCH_ASSOC );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchOne
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ param int $colnum 0 - indexed column number to retrieve
* @ return mixed
*/
2008-01-29 12:59:37 +03:00
public function fetchOne ( $statement , array $params = array (), $colnum = 0 )
2007-01-25 01:50:49 +03:00
{
2007-01-07 17:52:16 +03:00
return $this -> execute ( $statement , $params ) -> fetchColumn ( $colnum );
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchRow
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ return array
*/
2008-01-29 12:59:37 +03:00
public function fetchRow ( $statement , array $params = array ())
2007-01-25 01:50:49 +03:00
{
2008-07-27 23:38:56 +04:00
return $this -> execute ( $statement , $params ) -> fetch ( PDO :: FETCH_ASSOC );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchArray
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ return array
*/
2008-01-29 12:59:37 +03:00
public function fetchArray ( $statement , array $params = array ())
2007-01-25 01:50:49 +03:00
{
2008-07-27 23:38:56 +04:00
return $this -> execute ( $statement , $params ) -> fetch ( PDO :: FETCH_NUM );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchColumn
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ param int $colnum 0 - indexed column number to retrieve
* @ return array
*/
2008-01-29 12:59:37 +03:00
public function fetchColumn ( $statement , array $params = array (), $colnum = 0 )
2007-01-25 01:50:49 +03:00
{
2008-07-27 23:38:56 +04:00
return $this -> execute ( $statement , $params ) -> fetchAll ( PDO :: FETCH_COLUMN , $colnum );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchAssoc
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ return array
*/
2008-01-29 12:59:37 +03:00
public function fetchAssoc ( $statement , array $params = array ())
2007-01-25 01:50:49 +03:00
{
2008-07-27 23:38:56 +04:00
return $this -> execute ( $statement , $params ) -> fetchAll ( PDO :: FETCH_ASSOC );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
* fetchBoth
*
* @ param string $statement sql query to be executed
* @ param array $params prepared statement params
* @ return array
*/
2008-01-29 12:59:37 +03:00
public function fetchBoth ( $statement , array $params = array ())
2007-01-25 01:50:49 +03:00
{
2008-07-27 23:38:56 +04:00
return $this -> execute ( $statement , $params ) -> fetchAll ( PDO :: FETCH_BOTH );
2007-01-07 17:52:16 +03:00
}
2008-05-06 17:41:22 +04:00
2007-06-20 03:33:04 +04:00
/**
* prepare
*
* @ param string $statement
*/
public function prepare ( $statement )
{
$this -> connect ();
2007-07-12 02:03:47 +04:00
try {
2008-07-27 23:38:56 +04:00
//$event = new Doctrine_Event($this, Doctrine_Event::CONN_PREPARE, $statement);
//$this->getAttribute(Doctrine::ATTR_LISTENER)->prePrepare($event);
2007-06-20 03:33:04 +04:00
2007-07-12 02:03:47 +04:00
$stmt = false ;
2008-03-26 14:10:45 +03:00
2008-07-27 23:38:56 +04:00
//if ( ! $event->skipOperation) {
2008-07-04 20:32:19 +04:00
$stmt = $this -> _pdo -> prepare ( $statement );
2008-07-27 23:38:56 +04:00
//}
2008-03-26 14:10:45 +03:00
2008-07-27 23:38:56 +04:00
//$this->getAttribute(Doctrine::ATTR_LISTENER)->postPrepare($event);
2008-03-26 14:10:45 +03:00
2007-07-12 02:03:47 +04:00
return new Doctrine_Connection_Statement ( $this , $stmt );
2008-07-21 00:13:24 +04:00
} catch ( PDOException $e ) {
$this -> rethrowException ( $e , $this );
}
2007-06-20 03:33:04 +04:00
}
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-07-27 23:38:56 +04:00
* Queries the database with limit and offset
2007-10-13 21:06:26 +04:00
* added to the query and returns a Doctrine_Connection_Statement object
2007-01-07 17:52:16 +03:00
*
* @ param string $query
* @ param integer $limit
* @ param integer $offset
2007-10-13 21:06:26 +04:00
* @ return Doctrine_Connection_Statement
2007-01-07 17:52:16 +03:00
*/
2007-06-12 23:39:03 +04:00
public function select ( $query , $limit = 0 , $offset = 0 )
2007-01-07 17:52:16 +03:00
{
if ( $limit > 0 || $offset > 0 ) {
$query = $this -> modifyLimitQuery ( $query , $limit , $offset );
}
2007-10-13 21:06:26 +04:00
return $this -> execute ( $query );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-25 01:50:49 +03:00
/**
* standaloneQuery
*
* @ param string $query sql query
* @ param array $params query parameters
*
* @ return PDOStatement | Doctrine_Adapter_Statement
*/
public function standaloneQuery ( $query , $params = array ())
{
return $this -> execute ( $query , $params );
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-08-01 22:46:14 +04:00
* Executes an SQL SELECT query with the given parameters .
*
2007-01-07 17:52:16 +03:00
* @ param string $query sql query
* @ param array $params query parameters
*
* @ return PDOStatement | Doctrine_Adapter_Statement
*/
2007-06-25 14:08:03 +04:00
public function execute ( $query , array $params = array ())
2007-01-25 01:50:49 +03:00
{
2007-09-03 18:57:18 +04:00
$this -> connect ();
2007-06-12 23:39:03 +04:00
2007-01-07 17:52:16 +03:00
try {
if ( ! empty ( $params )) {
2007-06-27 02:29:31 +04:00
$stmt = $this -> prepare ( $query );
2007-01-07 17:52:16 +03:00
$stmt -> execute ( $params );
2007-05-27 22:56:04 +04:00
return $stmt ;
2007-01-07 17:52:16 +03:00
} else {
2008-07-27 23:38:56 +04:00
//$event = new Doctrine_Event($this, Doctrine_Event::CONN_QUERY, $query, $params);
//$this->getAttribute(Doctrine::ATTR_LISTENER)->preQuery($event);
2007-06-20 03:33:04 +04:00
2008-07-27 23:38:56 +04:00
//if ( ! $event->skipOperation) {
2008-07-04 20:32:19 +04:00
$stmt = $this -> _pdo -> query ( $query );
$this -> _queryCount ++ ;
2008-07-27 23:38:56 +04:00
//}
//$this->getAttribute(Doctrine::ATTR_LISTENER)->postQuery($event);
2007-06-20 03:33:04 +04:00
return $stmt ;
2007-01-07 17:52:16 +03:00
}
2008-07-21 00:13:24 +04:00
} catch ( PDOException $e ) {
$this -> rethrowException ( $e , $this );
}
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-08-01 22:46:14 +04:00
* Executes an SQL INSERT / UPDATE / DELETE query with the given parameters .
*
2007-01-07 17:52:16 +03:00
* @ param string $query sql query
* @ param array $params query parameters
*
* @ return PDOStatement | Doctrine_Adapter_Statement
2008-08-01 22:46:14 +04:00
* @ todo Rename to executeUpdate () .
2007-01-07 17:52:16 +03:00
*/
public function exec ( $query , array $params = array ()) {
2007-09-03 18:57:18 +04:00
$this -> connect ();
2007-09-01 21:35:44 +04:00
2007-01-07 17:52:16 +03:00
try {
if ( ! empty ( $params )) {
2007-06-20 03:33:04 +04:00
$stmt = $this -> prepare ( $query );
2007-01-07 17:52:16 +03:00
$stmt -> execute ( $params );
2008-05-06 17:41:22 +04:00
2007-01-15 21:48:50 +03:00
return $stmt -> rowCount ();
2007-01-07 17:52:16 +03:00
} else {
2008-07-27 23:38:56 +04:00
//$event = new Doctrine_Event($this, Doctrine_Event::CONN_EXEC, $query, $params);
//$this->getAttribute(Doctrine::ATTR_LISTENER)->preExec($event);
2007-06-20 03:33:04 +04:00
2008-07-27 23:38:56 +04:00
//if ( ! $event->skipOperation) {
2008-07-04 20:32:19 +04:00
$count = $this -> _pdo -> exec ( $query );
$this -> _queryCount ++ ;
2008-07-27 23:38:56 +04:00
//}
//$this->getAttribute(Doctrine::ATTR_LISTENER)->postExec($event);
2007-06-20 03:33:04 +04:00
return $count ;
2007-01-07 17:52:16 +03:00
}
2008-07-21 00:13:24 +04:00
} catch ( PDOException $e ) {
$this -> rethrowException ( $e , $this );
}
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-07-21 00:13:24 +04:00
* Wraps the given exception into a driver - specific exception and rethrows it .
2007-01-07 17:52:16 +03:00
*
* @ throws Doctrine_Connection_Exception
*/
2007-07-12 02:03:47 +04:00
public function rethrowException ( Exception $e , $invoker )
2008-08-02 21:41:37 +04:00
{
2008-07-04 20:32:19 +04:00
$name = 'Doctrine_Connection_' . $this -> _driverName . '_Exception' ;
2008-01-05 22:55:56 +03:00
$exc = new $name ( $e -> getMessage (), ( int ) $e -> getCode ());
2007-01-07 17:52:16 +03:00
if ( ! is_array ( $e -> errorInfo )) {
$e -> errorInfo = array ( null , null , null , null );
}
$exc -> processErrorInfo ( $e -> errorInfo );
2008-07-21 00:13:24 +04:00
throw $exc ;
2007-01-07 17:52:16 +03:00
}
2008-02-20 23:54:20 +03:00
2007-01-07 17:52:16 +03:00
/**
2008-02-04 00:29:57 +03:00
* Returns the number of queries executed by the connection .
2007-01-07 17:52:16 +03:00
*
* @ return integer
2008-02-04 00:29:57 +03:00
* @ todo Better name : getQueryCount ()
2007-01-07 17:52:16 +03:00
*/
2008-08-01 22:46:14 +04:00
public function getQueryCount ()
2007-01-07 17:52:16 +03:00
{
2008-07-04 20:32:19 +04:00
return $this -> _queryCount ;
2007-01-07 17:52:16 +03:00
}
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-02-04 00:29:57 +03:00
* Closes the connection .
2007-01-07 17:52:16 +03:00
*
* @ return void
*/
public function close ()
{
2008-06-15 19:56:28 +04:00
//$event = new Doctrine_Event($this, Doctrine_Event::CONN_CLOSE);
//this->getAttribute(Doctrine::ATTR_LISTENER)->preClose($event);
2007-01-07 17:52:16 +03:00
$this -> clear ();
2008-07-04 20:32:19 +04:00
unset ( $this -> _pdo );
$this -> _isConnected = false ;
2007-01-07 17:52:16 +03:00
2008-06-15 19:56:28 +04:00
//$this->getAttribute(Doctrine::ATTR_LISTENER)->postClose($event);
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-02-04 00:29:57 +03:00
* Returns the current total transaction nesting level .
2007-01-07 17:52:16 +03:00
*
2008-02-04 00:29:57 +03:00
* @ return integer The nesting level . A value of 0 means theres no active transaction .
2007-01-07 17:52:16 +03:00
*/
public function getTransactionLevel ()
{
return $this -> transaction -> getTransactionLevel ();
}
2008-05-06 17:41:22 +04:00
2007-06-12 23:39:03 +04:00
/**
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @ return integer
*/
public function errorCode ()
{
2007-09-03 18:57:18 +04:00
$this -> connect ();
2008-02-04 00:29:57 +03:00
2008-07-04 20:32:19 +04:00
return $this -> _pdo -> errorCode ();
2007-06-12 23:39:03 +04:00
}
2007-10-21 10:23:59 +04:00
2007-06-12 23:39:03 +04:00
/**
* Fetch extended error information associated with the last operation on the database handle
*
* @ return array
*/
public function errorInfo ()
{
2007-09-03 18:57:18 +04:00
$this -> connect ();
2008-02-04 00:29:57 +03:00
2008-07-04 20:32:19 +04:00
return $this -> _pdo -> errorInfo ();
2007-06-12 23:39:03 +04:00
}
2008-05-06 17:41:22 +04:00
2007-11-24 21:11:09 +03:00
/**
2007-06-12 03:37:24 +04:00
* Returns the ID of the last inserted row , or the last value from a sequence object ,
* depending on the underlying driver .
*
2008-01-29 12:59:37 +03:00
* Note : This method may not return a meaningful or consistent result across different drivers ,
2007-06-12 03:37:24 +04:00
* because the underlying database may not even support the notion of auto - increment fields or sequences .
*
2008-02-04 00:29:57 +03:00
* @ param string $table Name of the table into which a new row was inserted .
* @ param string $field Name of the field into which a new row was inserted .
2007-06-12 03:37:24 +04:00
*/
public function lastInsertId ( $table = null , $field = null )
{
2008-08-22 13:05:14 +04:00
return $this -> getSequenceManager () -> lastInsertId ( $table , $field );
2007-06-12 03:37:24 +04:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
2007-06-12 03:25:46 +04:00
* Start a transaction or set a savepoint .
2007-01-07 17:52:16 +03:00
*
2007-06-12 03:25:46 +04:00
* if trying to set a savepoint and there is no active transaction
* a new transaction is being started
2007-01-07 17:52:16 +03:00
*
2007-06-12 03:25:46 +04:00
* Listeners : onPreTransactionBegin , onTransactionBegin
*
* @ param string $savepoint name of a savepoint to set
* @ throws Doctrine_Transaction_Exception if the transaction fails at database level
* @ return integer current transaction nesting level
2007-01-07 17:52:16 +03:00
*/
2007-06-12 03:25:46 +04:00
public function beginTransaction ( $savepoint = null )
2007-01-07 17:52:16 +03:00
{
2008-08-01 22:46:14 +04:00
return $this -> _transaction -> beginTransaction ( $savepoint );
2007-01-07 17:52:16 +03:00
}
2008-05-06 17:41:22 +04:00
2007-01-07 17:52:16 +03:00
/**
2008-07-27 23:38:56 +04:00
* Commits the database changes done during a transaction that is in
2007-06-12 03:25:46 +04:00
* progress or release a savepoint . This function may only be called when
* auto - committing is disabled , otherwise it will fail .
2007-01-07 17:52:16 +03:00
*
2007-06-12 03:25:46 +04:00
* Listeners : onPreTransactionCommit , onTransactionCommit
*
* @ param string $savepoint name of a savepoint to release
* @ throws Doctrine_Transaction_Exception if the transaction fails at PDO level
* @ throws Doctrine_Validator_Exception if the transaction fails due to record validations
* @ return boolean false if commit couldn ' t be performed , true otherwise
2007-01-07 17:52:16 +03:00
*/
2007-06-12 03:25:46 +04:00
public function commit ( $savepoint = null )
2007-01-07 17:52:16 +03:00
{
2008-08-02 21:41:37 +04:00
return $this -> _transaction -> commit ( $savepoint );
2007-01-07 17:52:16 +03:00
}
2007-10-21 10:23:59 +04:00
2007-01-07 17:52:16 +03:00
/**
2007-06-12 03:25:46 +04:00
* Cancel any database changes done during a transaction or since a specific
* savepoint that is in progress . This function may only be called when
* auto - committing is disabled , otherwise it will fail . Therefore , a new
* transaction is implicitly started after canceling the pending changes .
2007-01-07 17:52:16 +03:00
*
2007-06-12 03:25:46 +04:00
* this method can be listened with onPreTransactionRollback and onTransactionRollback
* eventlistener methods
2007-01-07 17:52:16 +03:00
*
2008-06-15 19:56:28 +04:00
* @ param string $savepoint Name of a savepoint to rollback to .
* @ throws Doctrine_Transaction_Exception If the rollback operation fails at database level .
* @ return boolean FALSE if rollback couldn ' t be performed , TRUE otherwise .
2007-01-07 17:52:16 +03:00
*/
2007-06-12 03:25:46 +04:00
public function rollback ( $savepoint = null )
2007-01-07 17:52:16 +03:00
{
2008-08-02 21:41:37 +04:00
$this -> _transaction -> rollback ( $savepoint );
2007-01-07 17:52:16 +03:00
}
2007-08-30 01:57:46 +04:00
2008-01-23 00:42:17 +03:00
/**
2008-06-15 19:56:28 +04:00
* Creates the database for the connection instance .
2008-01-23 00:58:03 +03:00
*
2008-06-15 19:56:28 +04:00
* @ return mixed Will return an instance of the exception thrown if the
* create database fails , otherwise it returns a string
* detailing the success .
2008-01-23 00:42:17 +03:00
*/
public function createDatabase ()
{
2008-01-23 00:58:03 +03:00
try {
if ( ! $dsn = $this -> getOption ( 'dsn' )) {
throw new Doctrine_Connection_Exception ( 'You must create your Doctrine_Connection by using a valid Doctrine style dsn in order to use the create/drop database functionality' );
}
2008-01-23 00:42:17 +03:00
2008-01-23 00:58:03 +03:00
$manager = $this -> getManager ();
2008-01-23 00:42:17 +03:00
2008-01-23 00:58:03 +03:00
$info = $manager -> parsePdoDsn ( $dsn );
$username = $this -> getOption ( 'username' );
$password = $this -> getOption ( 'password' );
2008-01-23 00:42:17 +03:00
2008-01-23 00:58:03 +03:00
// Make connection without database specified so we can create it
$connect = $manager -> openConnection ( new PDO ( $info [ 'scheme' ] . ':host=' . $info [ 'host' ], $username , $password ), 'tmp_connection' , false );
2008-01-23 00:42:17 +03:00
2008-01-23 00:58:03 +03:00
// Create database
$connect -> export -> createDatabase ( $info [ 'dbname' ]);
2008-01-23 00:42:17 +03:00
2008-01-23 00:58:03 +03:00
// Close the tmp connection with no database
$manager -> closeConnection ( $connect );
2008-01-23 00:42:17 +03:00
2008-01-23 00:58:03 +03:00
// Close original connection
$manager -> closeConnection ( $this );
// Reopen original connection with newly created database
$manager -> openConnection ( new PDO ( $info [ 'dsn' ], $username , $password ), $this -> getName (), true );
return 'Successfully created database for connection "' . $this -> getName () . '" named "' . $info [ 'dbname' ] . '"' ;
} catch ( Exception $e ) {
return $e ;
}
2008-01-23 00:42:17 +03:00
}
/**
2008-01-23 00:58:03 +03:00
* Method for dropping the database for the connection instance
*
2008-06-15 19:56:28 +04:00
* @ return mixed Will return an instance of the exception thrown if the drop
* database fails , otherwise it returns a string detailing the success .
2008-01-23 00:42:17 +03:00
*/
public function dropDatabase ()
{
try {
2008-01-23 00:58:03 +03:00
if ( ! $dsn = $this -> getOption ( 'dsn' )) {
throw new Doctrine_Connection_Exception ( 'You must create your Doctrine_Connection by using a valid Doctrine style dsn in order to use the create/drop database functionality' );
}
$info = $this -> getManager () -> parsePdoDsn ( $dsn );
2008-01-29 12:59:37 +03:00
2008-01-23 00:42:17 +03:00
$this -> export -> dropDatabase ( $info [ 'dbname' ]);
return 'Successfully dropped database for connection "' . $this -> getName () . '" named "' . $info [ 'dbname' ] . '"' ;
} catch ( Exception $e ) {
return $e ;
}
}
2008-07-27 23:38:56 +04:00
/**
* Quotes pattern ( % and _ ) characters in a string )
*
* EXPERIMENTAL
*
* WARNING : this function is experimental and may change signature at
* any time until labelled as non - experimental
*
* @ param string the input string to quote
*
* @ return string quoted string
*/
protected function _escapePattern ( $text )
{
return $text ;
/* if ( ! $this -> string_quoting [ 'escape_pattern' ]) {
return $text ;
}
$tmp = $this -> conn -> string_quoting ;
$text = str_replace ( $tmp [ 'escape_pattern' ],
$tmp [ 'escape_pattern' ] .
$tmp [ 'escape_pattern' ], $text );
foreach ( $this -> wildcards as $wildcard ) {
$text = str_replace ( $wildcard , $tmp [ 'escape_pattern' ] . $wildcard , $text );
}
return $text ; */
}
/**
* Removes any formatting in an sequence name using the 'seqname_format' option
*
* @ param string $sqn string that containts name of a potential sequence
* @ return string name of the sequence with possible formatting removed
*/
protected function _fixSequenceName ( $sqn )
{
$seqPattern = '/^' . preg_replace ( '/%s/' , '([a-z0-9_]+)' , $this -> conn -> getAttribute ( Doctrine :: ATTR_SEQNAME_FORMAT )) . '$/i' ;
$seqName = preg_replace ( $seqPattern , '\\1' , $sqn );
if ( $seqName && ! strcasecmp ( $sqn , $this -> getSequenceName ( $seqName ))) {
return $seqName ;
}
return $sqn ;
}
/**
* Removes any formatting in an index name using the 'idxname_format' option
*
* @ param string $idx string that containts name of anl index
* @ return string name of the index with possible formatting removed
*/
protected function _fixIndexName ( $idx )
{
$indexPattern = '/^' . preg_replace ( '/%s/' , '([a-z0-9_]+)' , $this -> conn -> getAttribute ( Doctrine :: ATTR_IDXNAME_FORMAT )) . '$/i' ;
$indexName = preg_replace ( $indexPattern , '\\1' , $idx );
if ( $indexName && ! strcasecmp ( $idx , $this -> getIndexName ( $indexName ))) {
return $indexName ;
}
return $idx ;
}
/**
* adds sequence name formatting to a sequence name
*
* @ param string name of the sequence
* @ return string formatted sequence name
*/
protected function _getSequenceName ( $sqn )
{
return sprintf ( $this -> conn -> getAttribute ( Doctrine :: ATTR_SEQNAME_FORMAT ),
preg_replace ( '/[^a-z0-9_\$.]/i' , '_' , $sqn ));
}
/**
* adds index name formatting to a index name
*
* @ param string name of the index
* @ return string formatted index name
*/
protected function _getIndexName ( $idx )
{
return sprintf ( $this -> conn -> getAttribute ( Doctrine :: ATTR_IDXNAME_FORMAT ),
preg_replace ( '/[^a-z0-9_\$]/i' , '_' , $idx ));
}
/**
* adds table name formatting to a table name
*
* @ param string name of the table
* @ return string formatted table name
*/
protected function _getTableName ( $table )
{
return $table ;
/*
return sprintf ( $this -> conn -> getAttribute ( Doctrine :: ATTR_TBLNAME_FORMAT ),
$table ); */
}
2008-01-23 00:42:17 +03:00
2007-01-07 17:52:16 +03:00
/**
* returns a string representation of this object
* @ return string
*/
public function __toString ()
{
return Doctrine_Lib :: getConnectionAsString ( $this );
}
2008-03-17 16:26:34 +03:00
2008-05-06 17:41:22 +04:00
/**
2008-08-02 21:41:37 +04:00
* Gets the SequenceManager that can be used to retrieve sequence values
* through this connection .
2008-05-06 17:41:22 +04:00
*
2008-08-02 21:41:37 +04:00
* @ return Doctrine :: DBAL :: Sequencing :: SequenceManager
2008-05-06 17:41:22 +04:00
*/
2008-07-27 23:38:56 +04:00
public function getSequenceManager ()
2008-07-21 00:13:24 +04:00
{
2008-08-02 21:41:37 +04:00
if ( ! $this -> _sequenceManager ) {
2008-07-21 00:13:24 +04:00
$class = " Doctrine_Sequence_ " . $this -> _driverName ;
2008-08-02 21:41:37 +04:00
$this -> _sequenceManager = new $class ;
2008-07-21 00:13:24 +04:00
}
2008-08-02 21:41:37 +04:00
return $this -> _sequenceManager ;
2008-07-21 00:13:24 +04:00
}
2008-08-09 13:45:28 +04:00
/**
* Gets the SchemaManager that can be used to inspect or change the
* database schema through the connection .
*
* @ return Doctrine :: DBAL :: Schema :: SchemaManager
*/
public function getSchemaManager ()
{
if ( ! $this -> _schemaManager ) {
$class = " Doctrine_Schema_ " . $this -> _driverName . " SchemaManager " ;
$this -> _schemaManager = new $class ( $this );
}
return $this -> _schemaManager ;
}
2008-01-29 12:59:37 +03:00
}