Changeset 4723 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
08/01/08 19:46:14 (5 months ago)
Author:
romanb
Message:

Continued refactorings. Started to refactor the DBAL layer.

Location:
trunk/lib/Doctrine
Files:
9 added
49 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine/Adapter.php

    r3882 r4723  
    3131 * @version     $Revision$ 
    3232 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
     33 * @todo Remove. 
    3334 */ 
    3435class Doctrine_Adapter 
  • trunk/lib/Doctrine/Cache/Apc.php

    r3931 r4723  
    2020 */ 
    2121 
     22#namespace Doctrine::Common::Cache; 
     23 
    2224/** 
    2325 * Doctrine_Cache_Apc 
     
    3032 * @version     $Revision$ 
    3133 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
     34 * @todo Rename to ApcCache 
    3235 */ 
    3336class Doctrine_Cache_Apc extends Doctrine_Cache_Driver 
  • trunk/lib/Doctrine/ClassMetadata.php

    r4718 r4723  
    18381838        $this->_generatorType = $type; 
    18391839    } 
     1840     
     1841    public function completeIdentifierMapping() 
     1842    { 
     1843        if ($this->getIdGeneratorType() == self::GENERATOR_TYPE_AUTO) { 
     1844            $platform = $this->_em->getConnection()->getDatabasePlatform(); 
     1845            if ($platform->prefersSequences()) { 
     1846                $this->_generatorType = self::GENERATOR_TYPE_SEQUENCE; 
     1847            } else if ($platform->prefersIdentityColumns()) { 
     1848                $this->_generatorType = self::GENERATOR_TYPE_IDENTITY; 
     1849            } else { 
     1850                $this->_generatorType = self::GENERATOR_TYPE_TABLE; 
     1851            } 
     1852        } 
     1853    } 
    18401854 
    18411855    /** 
  • trunk/lib/Doctrine/ClassMetadata/Factory.php

    r4699 r4723  
    296296        // the connection (or another) object. We just need to decide where to put 
    297297        // the id generation types. 
    298         if ($class->getIdGeneratorType() == Doctrine_ClassMetadata::GENERATOR_TYPE_AUTO) { 
    299             switch (strtolower($this->_em->getConnection()->getDriverName())) { 
    300                 case 'mysql': 
    301                     // pick IDENTITY 
    302                     $class->setIdGeneratorType(Doctrine_ClassMetadata::GENERATOR_TYPE_IDENTITY); 
    303                     break; 
    304                 case 'oracle': 
    305                     //pick SEQUENCE 
    306                     break; 
    307                 case 'postgres': 
    308                     //pick SEQUENCE 
    309                     break; 
    310                 case 'firebird': 
    311                     //pick what? 
    312                     break; 
    313                 case 'mssql': 
    314                     //pick what? 
    315                 default: 
    316                     throw new Doctrine_Exception("Encountered unknown database driver: " 
    317                             . $this->_em->getConnection()->getDriverName()); 
    318             } 
    319         } 
     298        $class->completeIdentifierMapping(); 
    320299    } 
    321300     
  • trunk/lib/Doctrine/Connection.php

    r4718 r4723  
    2323 
    2424#use Doctrine::Common::Configuration; 
     25#use Doctrine::Common::EventManager; 
     26#use Doctrine::DBAL::Exceptions::ConnectionException; 
    2527 
    2628/** 
     
    4143 * 
    4244 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    43  * @link        www.phpdoctrine.org 
    4445 * @since       1.0 
    4546 * @version     $Revision$ 
     
    6364 *  
    6465 * Doctrine::DBAL could ship with a simple standard broker that uses a primitive 
    65  * round-robin approach to distribution. User can provide its own resolvers. 
     66 * round-robin approach to distribution. User can provide its own brokers. 
    6667 */ 
    67 abstract class Doctrine_Connection implements Countable 
     68abstract class Doctrine_Connection 
    6869{ 
    6970    /** 
     
    8990     
    9091    /** 
    91      * The attributes. 
    92      * 
    93      * @var array 
    94      */ 
    95     protected $_attributes = array(); 
    96      
    97     /** 
    9892     * Name of the connection 
    9993     * 
     
    112106     * Whether or not a connection has been established. 
    113107     * 
    114      * @var boolean $isConnected                 
     108     * @var boolean                
    115109     */ 
    116110    protected $_isConnected = false; 
     
    122116     */ 
    123117    protected $_quoteIdentifiers; 
    124      
    125     /** 
    126      * The connection properties. 
    127      *  
    128      * @var array $properties                
    129      */ 
    130     protected $properties = array( 
    131             'sql_comments' => array( 
    132                     array('start' => '--', 'end' => "\n", 'escape' => false), 
    133                     array('start' => '/*', 'end' => '*/', 'escape' => false)), 
    134             'identifier_quoting' => array('start' => '"', 'end' => '"','escape' => '"'), 
    135             'string_quoting' => array('start' => "'", 'end' => "'", 'escape' => false, 
    136                                       'escape_pattern' => false), 
    137             'wildcards' => array('%', '_'), 
    138             'varchar_max_length' => 255, 
    139             ); 
    140118     
    141119    /** 
     
    174152     * @var Doctrine::DBAL::Platforms::DatabasePlatform 
    175153     */ 
    176     protected $_databasePlatform;     
     154    protected $_platform; 
     155 
     156    /** 
     157     * Enter description here... 
     158     * 
     159     * @var Doctrine::DBAL::Transactions::Transaction 
     160     */ 
     161    protected $_transaction; 
    177162 
    178163    /** 
     
    238223     
    239224    /** 
    240      * Enter description here... 
    241      * 
    242      * @param unknown_type $name 
    243      * @return unknown 
    244      * @todo Remove. Move properties to DatabasePlatform. 
    245      */ 
    246     public function getProperty($name) 
    247     { 
    248         if ( ! isset($this->properties[$name])) { 
    249             throw Doctrine_Connection_Exception::unknownProperty($name); 
    250         } 
    251         return $this->properties[$name]; 
     225     * Gets the DatabasePlatform for the connection. 
     226     * 
     227     * @return Doctrine::DBAL::Platforms::DatabasePlatform 
     228     */ 
     229    public function getDatabasePlatform() 
     230    { 
     231        throw new Doctrine_Connection_Exception("No DatabasePlatform available " 
     232                . "for connection " . get_class($this)); 
    252233    } 
    253234     
     
    828809 
    829810    /** 
    830      * execute 
     811     * Executes an SQL SELECT query with the given parameters. 
     812     *  
    831813     * @param string $query     sql query 
    832814     * @param array $params     query parameters 
     
    861843 
    862844    /** 
    863      * exec 
     845     * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters. 
     846     *  
    864847     * @param string $query     sql query 
    865848     * @param array $params     query parameters 
    866849     * 
    867850     * @return PDOStatement|Doctrine_Adapter_Statement 
     851     * @todo Rename to executeUpdate(). 
    868852     */ 
    869853    public function exec($query, array $params = array()) { 
     
    892876        } 
    893877    } 
    894      
    895     /** 
    896      *  
    897      * 
    898      * @return string 
    899      * @todo Rather orm stuff 
    900      */ 
    901     public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false) 
    902     { 
    903         return $query; 
    904     } 
    905      
    906     /** 
    907      * Creates dbms specific LIMIT/OFFSET SQL for the subqueries that are used in the 
    908      * context of the limit-subquery algorithm. 
    909      * 
    910      * @return string 
    911      * @todo Rather ORM stuff 
    912      */ 
    913     public function modifyLimitSubquery(Doctrine_Table $rootTable, $query, $limit = false, 
    914             $offset = false, $isManip = false) 
    915     { 
    916         return $this->modifyLimitQuery($query, $limit, $offset, $isManip); 
    917     } 
    918878 
    919879    /** 
     
    946906     * @todo Better name: getQueryCount() 
    947907     */ 
    948     public function count() 
     908    public function getQueryCount() 
    949909    { 
    950910        return $this->_queryCount; 
     
    1031991    public function beginTransaction($savepoint = null) 
    1032992    { 
    1033         return $this->transaction->beginTransaction($savepoint); 
     993        return $this->_transaction->beginTransaction($savepoint); 
    1034994    } 
    1035995     
     
    12611221        } 
    12621222    } 
    1263  
    1264     public function getFormatter() 
    1265     { 
    1266         if ( ! $this->modules['formatter']) { 
    1267             $this->modules['formatter'] = new Doctrine_Formatter($this); 
    1268         } 
    1269         return $this->modules['formatter']; 
    1270     } 
    12711223     
    12721224    public function getSequenceManager() 
     
    12781230        return $this->modules['sequence']; 
    12791231    } 
    1280      
    1281     /** 
    1282      * Gets the default (preferred) Id generation strategy of the database platform. 
    1283      *  
    1284      * @todo Sure, the id generator types are more ORM functionality but they're 
    1285      * still kind of dbal related. Maybe we need another set of classes (DatabasePlatform?) 
    1286      * but im not so sure... 
    1287      */ 
    1288     /*abstract*/ public function getDefaultIdGeneratorType() 
    1289     { 
    1290          
    1291     } 
    12921232} 
  • trunk/lib/Doctrine/Connection/Db2.php

    r4374 r4723  
    1919 * <http://www.phpdoctrine.org>. 
    2020 */ 
    21 Doctrine::autoload('Doctrine_Connection'); 
     21 
    2222/** 
    2323 * Doctrine_Connection_Db2 
  • trunk/lib/Doctrine/Connection/Firebird.php

    r4523 r4723  
    4646     * @param PDO $pdo                          database handle 
    4747     */ 
    48     public function __construct(Doctrine_Manager $manager, $adapter) 
     48    public function __construct(array $params) 
    4949    { 
    50  
    51         $this->supported = array( 
    52                           'sequences'             => true, 
    53                           'indexes'               => true, 
    54                           'affected_rows'         => true, 
    55                           'summary_functions'     => true, 
    56                           'order_by_text'         => true, 
    57                           'transactions'          => true, 
    58                           'savepoints'            => true, 
    59                           'current_id'            => true, 
    60                           'limit_queries'         => 'emulated', 
    61                           'LOBs'                  => true, 
    62                           'replace'               => 'emulated', 
    63                           'sub_selects'           => true, 
    64                           'auto_increment'        => true, 
    65                           'primary_key'           => true, 
    66                           'result_introspection'  => true, 
    67                           'prepared_statements'   => true, 
    68                           'identifier_quoting'    => false, 
    69                           'pattern_escaping'      => true 
    70                           ); 
    71         // initialize all driver options 
    72         /** 
    73         $this->options['DBA_username'] = false; 
    74         $this->options['DBA_password'] = false; 
    75         $this->options['database_path'] = ''; 
    76         $this->options['database_extension'] = '.gdb'; 
    77         $this->options['server_version'] = ''; 
    78         */ 
    79         parent::__construct($manager, $adapter); 
     50        parent::__construct($params); 
    8051    } 
    8152 
     
    9364    } 
    9465 
    95     /** 
    96      * Adds an driver-specific LIMIT clause to the query 
    97      * 
    98      * @param string $query     query to modify 
    99      * @param integer $limit    limit the number of rows 
    100      * @param integer $offset   start reading from given offset 
    101      * @return string modified  query 
    102      */ 
    103     public function modifyLimitQuery($query, $limit, $offset) 
    104     { 
    105         if ( ! $offset) { 
    106             $offset = 0; 
    107         } 
    108         if ($limit > 0) { 
    109             $query = preg_replace('/^([\s(])*SELECT(?!\s*FIRST\s*\d+)/i', 
    110                 "SELECT FIRST $limit SKIP $offset", $query); 
    111         } 
    112         return $query; 
    113     } 
    11466} 
  • trunk/lib/Doctrine/Connection/Informix.php

    r3882 r4723  
    1919 * <http://www.phpdoctrine.org>. 
    2020 */ 
    21 Doctrine::autoload('Doctrine_Connection'); 
     21 
    2222/** 
    2323 * Doctrine_Connection_Mysql 
  • trunk/lib/Doctrine/Connection/Mock.php

    r4628 r4723  
    3333 * @link        www.phpdoctrine.org 
    3434 * @since       1.0 
     35 * @todo Remove. 
    3536 */ 
    3637class Doctrine_Connection_Mock extends Doctrine_Connection_Common 
     
    4950    public function __construct() 
    5051    { 
    51  
     52         
     53    } 
     54     
     55    public function getDatabasePlatform() 
     56    { 
     57        return new Doctrine_DatabasePlatform_MySqlPlatform(); 
    5258    } 
    5359     
  • trunk/lib/Doctrine/Connection/Mssql.php

    r4718 r4723  
    4545     * @param PDO $pdo                          database handle 
    4646     */ 
    47     public function __construct(Doctrine_Manager $manager, $adapter) 
     47    public function __construct(array $params) 
    4848    { 
    49         // initialize all driver options 
    50         $this->supported = array( 
    51                           'sequences'             => 'emulated', 
    52                           'indexes'               => true, 
    53                           'affected_rows'         => true, 
    54                           'transactions'          => true, 
    55                           'summary_functions'     => true, 
    56                           'order_by_text'         => true, 
    57                           'current_id'            => 'emulated', 
    58                           'limit_queries'         => 'emulated', 
    59                           'LOBs'                  => true, 
    60                           'replace'               => 'emulated', 
    61                           'sub_selects'           => true, 
    62                           'auto_increment'        => true, 
    63                           'primary_key'           => true, 
    64                           'result_introspection'  => true, 
    65                           'prepared_statements'   => 'emulated', 
    66                           ); 
    67  
    68         parent::__construct($manager, $adapter); 
     49        parent::__construct($params); 
    6950    } 
    7051 
     
    9677         
    9778        return '[' . str_replace(']', ']]', $identifier) . ']'; 
    98     } 
    99  
    100     /** 
    101      * Adds an adapter-specific LIMIT clause to the SELECT statement. 
    102      * [ borrowed from Zend Framework ] 
    103      * 
    104      * @param string $query 
    105      * @param mixed $limit 
    106      * @param mixed $offset 
    107      * @link http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html 
    108      * @return string 
    109      */ 
    110     public function modifyLimitQuery($query, $limit, $offset, $isManip = false) 
    111     { 
    112         if ($limit > 0) { 
    113             $count = intval($limit); 
    114  
    115             $offset = intval($offset); 
    116             if ($offset < 0) { 
    117                 throw new Doctrine_Connection_Exception("LIMIT argument offset=$offset is not valid"); 
    118             } 
    119      
    120             $orderby = stristr($query, 'ORDER BY'); 
    121             if ($orderby !== false) { 
    122                 $sort = (stripos($orderby, 'desc') !== false) ? 'desc' : 'asc'; 
    123                 $order = str_ireplace('ORDER BY', '', $orderby); 
    124                 $order = trim(preg_replace('/ASC|DESC/i', '', $order)); 
    125             } 
    126      
    127             $query = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($count+$offset) . ' ', $query); 
    128      
    129             $query = 'SELECT * FROM (SELECT TOP ' . $count . ' * FROM (' . $query . ') AS inner_tbl'; 
    130             if ($orderby !== false) { 
    131                 $query .= ' ORDER BY ' . $order . ' '; 
    132                 $query .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC'; 
    133             } 
    134             $query .= ') AS outer_tbl'; 
    135             if ($orderby !== false) { 
    136                 $query .= ' ORDER BY ' . $order . ' ' . $sort; 
    137             } 
    138      
    139             return $query; 
    140  
    141         } 
    142  
    143         return $query; 
    14479    } 
    14580 
  • trunk/lib/Doctrine/Connection/Mysql.php

    r4628 r4723  
    2525 * Doctrine_Connection_Mysql 
    2626 * 
    27  * @package     Doctrine 
    28  * @subpackage  Connection 
    2927 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3028 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
     
    5149    public function __construct(array $params) 
    5250    { 
    53         $this->supported = array( 
    54                 'sequences'            => 'emulated', 
    55                 'indexes'              => true, 
    56                 'affected_rows'        => true, 
    57                 'transactions'         => true, 
    58                 'savepoints'           => false, 
    59                 'summary_functions'    => true, 
    60                 'order_by_text'        => true, 
    61                 'current_id'           => 'emulated', 
    62                 'limit_queries'        => true, 
    63                 'LOBs'                 => true, 
    64                 'replace'              => true, 
    65                 'sub_selects'          => true, 
    66                 'auto_increment'       => true, 
    67                 'primary_key'          => true, 
    68                 'result_introspection' => true, 
    69                 'prepared_statements'  => 'emulated', 
    70                 'identifier_quoting'   => true, 
    71                 'pattern_escaping'     => true 
    72                 ); 
    73  
    74         $this->properties['string_quoting'] = array( 
    75                 'start' => "'", 
    76                 'end' => "'", 
    77                 'escape' => '\\', 
    78                 'escape_pattern' => '\\'); 
    79  
    80         $this->properties['identifier_quoting'] = array( 
    81                 'start' => '`', 
    82                 'end' => '`', 
    83                 'escape' => '`'); 
    84  
    85         $this->properties['sql_comments'] = array( 
    86                 array('start' => '-- ', 'end' => "\n", 'escape' => false), 
    87                 array('start' => '#', 'end' => "\n", 'escape' => false), 
    88                 array('start' => '/*', 'end' => '*/', 'escape' => false), 
    89                 ); 
    90  
    91         $this->properties['varchar_max_length'] = 255; 
    92  
    9351        parent::__construct($params); 
    9452    } 
     
    234192        return $dsn; 
    235193    } 
     194     
     195    /** 
     196     * Gets the DatabasePlatform for the connection. 
     197  &