]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
invert connection owning
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Aug 2014 13:06:51 +0000 (16:06 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Aug 2014 13:06:51 +0000 (16:06 +0300)
16 files changed:
ipf/orm.php
ipf/orm/collection.php
ipf/orm/connection.php
ipf/orm/connection/module.php
ipf/orm/connection/mysql.php
ipf/orm/export.php
ipf/orm/export/mysql.php
ipf/orm/import/builder.php
ipf/orm/manager.php
ipf/orm/query.php
ipf/orm/query/abstract.php
ipf/orm/query/check.php
ipf/orm/rawsql.php
ipf/orm/record.php
ipf/orm/relation.php
ipf/orm/transaction.php

index a0f573ec3810b6d9626c022145c9b3936456c82d..fb0396f4abb90c7945252c836c817cd0124f4610 100644 (file)
@@ -172,7 +172,7 @@ final class IPF_ORM
 
     public static function getTable($componentName)
     {
-        return IPF_ORM_Manager::getInstance()->getConnectionForComponent($componentName)->getTable($componentName);
+        return IPF_ORM_Manager::connection()->getTable($componentName);
     }
 
     public static function generateModelsFromYaml($directory, $extraAllwedReferences=array())
index 4ac8e3e969a6bc77fa6104b5c38b500e059112a6..be683a5089d9a83a3ac87808378d9bdac7779203 100644 (file)
@@ -59,8 +59,7 @@ class IPF_ORM_Collection extends IPF_ORM_Access implements Countable, IteratorAg
 
     public function unserialize($serialized)
     {
-        $manager    = IPF_ORM_Manager::getInstance();
-        $connection    = $manager->getCurrentConnection();
+        $connection = IPF_ORM_Manager::connection();
 
         $array = unserialize($serialized);
 
index 19317611c41c344448a793ba5ca13530c90307c0..7fddf02bd7a0d98c1baf9f389756c1ee70249532 100644 (file)
@@ -4,9 +4,8 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 {
     protected $dbh;
     protected $tables           = array();
-    protected $_name;
+    protected $_name = 0;
     protected $driverName;
-    protected $isConnected      = false;
     protected $pendingAttributes  = array();
 
     private $modules = array('transaction' => false,
@@ -40,25 +39,9 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 
     public $dbListeners = array();
 
-    public function __construct(IPF_ORM_Manager $manager, $adapter, $user = null, $pass = null)
+    public function __construct(IPF_ORM_Manager $manager, $pdo, $user = null, $pass = null)
     {
-        if (is_object($adapter)) {
-            if (!($adapter instanceof PDO)) {
-                throw new IPF_ORM_Exception('First argument should be an instance of PDO');
-            }
-            $this->dbh = $adapter;
-            $this->isConnected = true;
-        } else if (is_array($adapter)) {
-            $this->options['dsn']      = $adapter['dsn'];
-            $this->options['username'] = $adapter['username'];
-            $this->options['password'] = $adapter['password'];
-
-            $this->options['other']    = array();
-            if (isset($adapter['options'])) {
-                if (in_array('persistent', $adapter['options']))
-                    $this->options['other'][PDO::ATTR_PERSISTENT] = true;
-            }
-        }
+        $this->dbh = $pdo;
 
         $this->setParent($manager);
         $this->dbListeners = $manager->dbListeners;
@@ -100,19 +83,10 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
             return $this->attributes[$attribute];
         }
 
-        if ($this->isConnected) {
-            try {
-                return $this->dbh->getAttribute($attribute);
-            } catch (Exception $e) {
-                throw new IPF_ORM_Exception('Attribute ' . $attribute . ' not found.');
-            }
-        } else {
-            if ( ! isset($this->pendingAttributes[$attribute])) {
-                $this->connect();
-                $this->getAttribute($attribute);
-            }
-
-            return $this->pendingAttributes[$attribute];
+        try {
+            return $this->dbh->getAttribute($attribute);
+        } catch (Exception $e) {
+            throw new IPF_ORM_Exception('Attribute ' . $attribute . ' not found.');
         }
     }
 
@@ -135,11 +109,7 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
         if ($attribute >= 100) {
             parent::setAttribute($attribute, $value);
         } else {
-            if ($this->isConnected) {
-                $this->dbh->setAttribute($attribute, $value);
-            } else {
-                $this->pendingAttributes[$attribute] = $value;
-            }
+            $this->dbh->setAttribute($attribute, $value);
         }
 
         return $this;
@@ -150,11 +120,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
         return $this->_name;
     }
 
-    public function setName($name)
-    {
-        $this->_name = $name;
-    }
-
     public function getDriverName()
     {
         return $this->driverName;
@@ -190,57 +155,9 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 
     public function getDbh()
     {
-        $this->connect();
         return $this->dbh;
     }
 
-    public function connect()
-    {
-        if ($this->isConnected)
-            return false;
-
-        $event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_CONNECT);
-
-        $this->notifyDBListeners('preConnect', $event);
-
-        $e     = explode(':', $this->options['dsn']);
-        $found = false;
-
-        if (extension_loaded('pdo')) {
-            if (in_array($e[0], PDO::getAvailableDrivers())) {
-                try {
-                    $this->dbh = new PDO($this->options['dsn'], $this->options['username'], 
-                                     (!$this->options['password'] ? '':$this->options['password']), $this->options['other']);
-
-                    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-                } catch (PDOException $e) {
-                    throw new IPF_ORM_Exception('PDO Connection Error: ' . $e->getMessage());
-                }
-                $found = true;
-            }
-        }
-
-        if ( !$found) {
-            throw new IPF_Exception_Panic("Couldn't locate driver named " . $e[0]);          
-        }
-
-        // attach the pending attributes to adapter
-        foreach($this->pendingAttributes as $attr => $value) {
-            $this->dbh->setAttribute($attr, $value);
-        }
-
-        $this->isConnected = true;
-
-        $this->onConnect();
-
-        $this->notifyDBListeners('postConnect', $event);
-        return true;
-    }
-
-    protected function onConnect()
-    {
-    }
-
     public function incrementQueryCount() 
     {
         $this->_count++;
@@ -457,8 +374,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 
     public function prepare($statement)
     {
-        $this->connect();
-
         try {
             $event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_PREPARE, $statement);
 
@@ -506,8 +421,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 
     public function execute($query, array $params = array())
     {
-        $this->connect();
-
         try {
             if ( ! empty($params)) {
                 $stmt = $this->prepare($query);
@@ -536,8 +449,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 
     public function exec($query, array $params = array())
     {
-        $this->connect();
-
         try {
             if ( ! empty($params)) {
                 $stmt = $this->prepare($query);
@@ -646,20 +557,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
         $this->exported = array();
     }
 
-    public function close()
-    {
-        $event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_CLOSE);
-
-        $this->notifyDBListeners('preClose', $event);
-
-        $this->clear();
-        
-        unset($this->dbh);
-        $this->isConnected = false;
-
-        $this->notifyDBListeners('postClose', $event);
-    }
-
     public function getTransactionLevel()
     {
         return $this->transaction->getTransactionLevel();
@@ -667,15 +564,11 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
 
     public function errorCode()
     {
-        $this->connect();
-
         return $this->dbh->errorCode();
     }
 
     public function errorInfo()
     {
-        $this->connect();
-
         return $this->dbh->errorInfo();
     }
     
@@ -727,98 +620,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta
         $this->transaction->rollback($savepoint);
     }
 
-    public function createDatabase()
-    {
-        if ( ! $dsn = $this->getOption('dsn')) {
-            throw new IPF_ORM_Exception('You must create your IPF_ORM_Connection by using a valid IPF style dsn in order to use the create/drop database functionality');
-        }
-
-        // Parse pdo dsn so we are aware of the connection information parts
-        $info = $this->getManager()->parsePdoDsn($dsn);
-
-        // Get the temporary connection to issue the drop database command
-        $tmpConnection = $this->getTmpConnection($info);
-
-        try {
-            // Issue create database command
-            $tmpConnection->export->createDatabase($info['dbname']);
-        } catch (Exception $e) {}
-
-        // Close the temporary connection used to issue the drop database command
-        $this->getManager()->closeConnection($tmpConnection);
-
-        // Re-create IPF or PDO style dsn
-        if ($info['unix_socket']) {
-            $dsn = array($info['scheme'] . ':unix_socket=' . $info['unix_socket'] . ';dbname=' . $info['dbname'], $this->getOption('username'), $this->getOption('password'));
-        } else {
-            $dsn = $info['scheme'] . '://' . $this->getOption('username') . ':' . $this->getOption('password') . '@' . $info['host'] . '/' . $info['dbname'];
-        }
-
-        // Re-open connection with the newly created database
-        $this->getManager()->openConnection($dsn, $this->getName(), true);
-
-        if (isset($e)) {
-            return $e;
-        } else {
-            return 'Successfully created database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"';
-        }
-    }
-
-    public function dropDatabase()
-    {
-        if ( ! $dsn = $this->getOption('dsn')) {
-            throw new IPF_ORM_Exception('You must create your IPF_ORM_Connection by using a valid IPF style dsn in order to use the create/drop database functionality');
-        }
-
-        // Parse pdo dsn so we are aware of the connection information parts
-        $info = $this->getManager()->parsePdoDsn($dsn);
-
-        // Get the temporary connection to issue the drop database command
-        $tmpConnection = $this->getTmpConnection($info);
-
-        try {
-            // Issue drop database command
-            $tmpConnection->export->dropDatabase($info['dbname']);
-        } catch (Exception $e) {}
-
-        // Close the temporary connection used to issue the drop database command
-        $this->getManager()->closeConnection($tmpConnection);
-
-        // Re-create IPF or PDO style dsn
-        if ($info['unix_socket']) {
-            $dsn = array($info['scheme'] . ':unix_socket=' . $info['unix_socket'] . ';dbname=' . $info['dbname'], $this->getOption('username'), $this->getOption('password'));
-        } else {
-            $dsn = $info['scheme'] . '://' . $this->getOption('username') . ':' . $this->getOption('password') . '@' . $info['host'] . '/' . $info['dbname'];
-        }
-
-        // Re-open connection with the newly created database
-        $this->getManager()->openConnection($dsn, $this->getName(), true);
-
-        if (isset($e)) {
-            return $e;
-        } else {
-            return 'Successfully dropped database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"';
-        }
-    }
-
-    public function getTmpConnection($info)
-    {
-        if ($info['unix_socket']) {
-            $pdoDsn = $info['scheme'] . ':unix_socket=' . $info['unix_socket'];
-        } else {
-            $pdoDsn = $info['scheme'] . ':host=' . $info['host'];
-        }
-
-        if (isset($this->export->tmpConnectionDatabase) && $this->export->tmpConnectionDatabase) {
-            $pdoDsn .= ';dbname=' . $this->export->tmpConnectionDatabase;
-        }
-
-        $username = $this->getOption('username');
-        $password = $this->getOption('password');
-
-        return $this->getManager()->openConnection(new PDO($pdoDsn, $username, $password), 'ipf_tmp_connection', false);
-    }
-
     public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false)
     {
         return $query;
index 1a765721ebefda24693ee3ca43c6325996928598..b894e2de9e159786f03e86fe22571b425aacf5df 100644 (file)
@@ -8,7 +8,7 @@ class IPF_ORM_Connection_Module
     public function __construct($conn = null)
     {
         if ( ! ($conn instanceof IPF_ORM_Connection)) {
-            $conn = IPF_ORM_Manager::getInstance()->getCurrentConnection();
+            $conn = IPF_ORM_Manager::connection();
         }
         $this->conn = $conn;
 
@@ -26,4 +26,4 @@ class IPF_ORM_Connection_Module
     {
         return $this->moduleName;
     }
-}
\ No newline at end of file
+}
index fa7f77226264b9600dde9b5f6ee98768a0be2609..4195f871e612699b47a4fca2117024d36ad17894 100644 (file)
@@ -71,12 +71,6 @@ class IPF_ORM_Connection_Mysql extends IPF_ORM_Connection
         parent::__construct($manager, $adapter);
     }
 
-    protected function onConnect()
-    {
-        $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
-        $this->exec('SET NAMES \'utf8\'');
-    }
-
     public function quoteIdentifier($str)
     {
         $quote = $this->identifier_quoting;
index 425235dca576584525e27331c7fcd552541f8662..4a5ad4fc81475f070490cd7037a1e90df789dab7 100644 (file)
@@ -22,16 +22,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
         return $name . '_idx';
     }
 
-    public function dropDatabase($database)
-    {
-        $this->conn->execute($this->dropDatabaseSql($database));
-    }
-
-    public function dropDatabaseSql($database)
-    {
-        throw new IPF_ORM_Exception('Drop database not supported by this driver.');
-    }
-
     public function dropTableSql($table)
     {
         return 'DROP TABLE ' . $this->conn->quoteIdentifier($table);
@@ -67,16 +57,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
         return $this->dropConstraint($table, $name);
     }
 
-    public function createDatabase($database)
-    {
-        $this->conn->execute($this->createDatabaseSql($database));
-    }
-
-    public function createDatabaseSql($database)
-    {
-        throw new IPF_ORM_Exception('Create database not supported by this driver.');
-    }
-
     public function createConstraint($table, $name, $definition)
     {
         $sql = $this->createConstraintSql($table, $name, $definition);
@@ -311,7 +291,7 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
     {
          $connections = array();
          foreach ($classes as $class) {
-             $connection = IPF_ORM_Manager::getInstance()->getConnectionForComponent($class);
+             $connection = IPF_ORM_Manager::connection();
              $connectionName = $connection->getName();
 
              if ( ! isset($connections[$connectionName])) {
@@ -374,7 +354,7 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
          $queries = $this->exportSortedClassesSql($classes);
          
          foreach ($queries as $connectionName => $sql) {
-             $connection = IPF_ORM_Manager::getInstance()->getConnection($connectionName);
+             $connection = IPF_ORM_Manager::connection();
 
              $connection->beginTransaction();
 
index 34af481d0193d61602d1e45f203a100f3d2b9e37..1dbe90a992cf638e6c00aee67152918e8d45fab5 100644 (file)
@@ -2,16 +2,6 @@
 
 class IPF_ORM_Export_Mysql extends IPF_ORM_Export
 {
-    public function createDatabaseSql($name)
-    {
-        return 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name);
-    }
-
-    public function dropDatabaseSql($name)
-    {
-        return 'DROP DATABASE ' . $this->conn->quoteIdentifier($name);
-    }
-
     public function createTableSql($name, array $fields, array $options = array()) 
     {
         if ( ! $name)
index a97b47659e2ad6a9e54051f556b5124a34f427a7..7a398adbbcf41fa06b3f57c2c41129addd4d7379 100644 (file)
@@ -320,12 +320,6 @@ class IPF_ORM_Import_Builder
             '',
         );
 
-        if (isset($definition['connection']) && $definition['connection']) {
-            $code[] = '// Connection Component Binding';
-            $code[] = "IPF_ORM_Manager::getInstance()->bindComponent('" . $definition['connectionClassName'] . "', '" . $definition['connection'] . "');";
-            $code[] = '';
-        }
-
         $code[] = 'abstract class '.$this->_baseClassPrefix.$definition['className'].' extends IPF_ORM_Record';
         $code[] = '{';
         $code[] = $this->buildTableDefinition($definition);
index e209a00e82bb78801ca05b8940066031919d2982..570de3379e61ea6606ca7ce6dead21c84a0d5fdb 100644 (file)
@@ -1,11 +1,8 @@
 <?php
 
-class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, IteratorAggregate
+class IPF_ORM_Manager extends IPF_ORM_Configurable
 {
-    protected $_connections   = array();
-    protected $_bound         = array();
-    protected $_index         = 0;
-    protected $_currIndex     = 0;
+    protected $_connection = null;
     protected $_queryRegistry;
     public $dbListeners = array();
 
@@ -49,372 +46,18 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato
         return IPF_ORM_Manager::getInstance()->getCurrentConnection();
     }
 
-    public function openConnection($adapter, $name = null, $setCurrent = true)
-    {
-        if (is_object($adapter)) {
-            if (!($adapter instanceof PDO))
-                throw new IPF_ORM_Exception("First argument should be an instance of PDO");
-            $driverName = $adapter->getAttribute(PDO::ATTR_DRIVER_NAME);
-        } else {
-            $adapter = $this->connectionParameters($adapter);
-            $driverName = $adapter['scheme'];
-        }
-
-        if ($name !== null) {
-            $name = (string) $name;
-            if (isset($this->_connections[$name])) {
-                if ($setCurrent) {
-                    $this->_currIndex = $name;
-                }
-                return $this->_connections[$name];
-            }
-        } else {
-            $name = $this->_index;
-            $this->_index++;
-        }
-
-        $drivers = array('mysql'    => 'IPF_ORM_Connection_Mysql',
-                         //'sqlite'   => 'IPF_ORM_Connection_Sqlite',
-                         //'pgsql'    => 'IPF_ORM_Connection_Pgsql',
-                         //'oci'      => 'IPF_ORM_Connection_Oracle',
-                         //'oci8'     => 'IPF_ORM_Connection_Oracle',
-                         //'oracle'   => 'IPF_ORM_Connection_Oracle',
-                         //'mssql'    => 'IPF_ORM_Connection_Mssql',
-                         //'dblib'    => 'IPF_ORM_Connection_Mssql',
-                         //'firebird' => 'IPF_ORM_Connection_Firebird',
-                         //'informix' => 'IPF_ORM_Connection_Informix',
-                         //'mock'     => 'IPF_ORM_Connection_Mock'
-                         );
-
-        if ( ! isset($drivers[$driverName])) {
-            throw new IPF_ORM_Exception('Unknown driver ' . $driverName);
-        }
-
-        $className = $drivers[$driverName];
-        $conn = new $className($this, $adapter);
-        $conn->setName($name);
-
-        $this->_connections[$name] = $conn;
-
-        if ($setCurrent) {
-            $this->_currIndex = $name;
-        }
-        return $this->_connections[$name];
-    }
-    
-    public function connectionParameters($adapter)
-    {
-        if (is_array($adapter)) {
-            if (!count($adapter))
-                throw new IPF_ORM_Exception('Empty data source name given.');
-
-            if (array_key_exists('database', $adapter)) {
-                $adapter['dsn'] = $this->makeDsnForPDO($adapter['driver'], $adapter['host'], @$adapter['port'], $adapter['database']);
-                $adapter['scheme'] = $adapter['driver'];
-                return $adapter;
-            } else {
-                $dsn = urldecode($adapter[0]);
-                $result = $this->parseDsn($dsn);
-                $result['username'] = (isset($adapter[1])) ? urldecode($adapter[1]) : null;
-                $result['password'] = (isset($adapter[2])) ? urldecode($adapter[2]) : null;
-                return $result;
-            }
-        } else {
-            $result = $this->parseDsn($adapter);
-            $result['username'] = $result['user'];
-            $result['password'] = $result['pass'];
-            return $result;
-        }
-    }
-
-    public function parsePdoDsn($dsn)
-    {
-        $parts = array();
-
-        $names = array('dsn', 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment', 'unix_socket');
-
-        foreach ($names as $name) {
-            if ( ! isset($parts[$name])) {
-                $parts[$name] = null;
-            }
-        }
-
-        $e = explode(':', $dsn);
-        $parts['scheme'] = $e[0];
-        $parts['dsn'] = $dsn;
-
-        $e = explode(';', $e[1]);
-        foreach ($e as $string) {
-            if ($string) {
-                $e2 = explode('=', $string);
-
-                if (isset($e2[0]) && isset($e2[1])) {
-                    list($key, $value) = $e2;
-                    $parts[$key] = $value;
-                }
-            }
-        }
-
-        return $parts;
-    }
-
-    protected function _buildDsnPartsArray($dsn)
-    {
-        // fix sqlite dsn so that it will parse correctly
-        $dsn = str_replace("////", "/", $dsn);
-        $dsn = str_replace("\\", "/", $dsn);
-        $dsn = preg_replace("/\/\/\/(.*):\//", "//$1:/", $dsn);
-
-        // silence any warnings
-        $parts = @parse_url($dsn);
-
-        $names = array('dsn', 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment', 'unix_socket');
-
-        foreach ($names as $name) {
-            if ( ! isset($parts[$name])) {
-                $parts[$name] = null;
-            }
-        }
-
-        if (count($parts) == 0 || ! isset($parts['scheme'])) {
-            throw new IPF_ORM_Exception('Could not parse dsn');
-        }
-
-        return $parts;
-    }
-
-    public function parseDsn($dsn)
-    {
-        $parts = $this->_buildDsnPartsArray($dsn);
-
-        switch ($parts['scheme']) {
-            case 'sqlite':
-            case 'sqlite2':
-            case 'sqlite3':
-                if (isset($parts['host']) && $parts['host'] == ':memory') {
-                    $parts['database'] = ':memory:';
-                } else {
-                    //fix windows dsn we have to add host: to path and set host to null
-                    if (isset($parts['host'])) {
-                        $parts['path'] = $parts['host'] . ":" . $parts["path"];
-                        $parts['host'] = null;
-                    }
-                    $parts['database'] = $parts['path'];
-                }
-
-                $parts['dsn'] = $this->makeDsnForPDO($parts['scheme'], $parts['host'], @$parts['port'], $parts['database']);
-
-                break;
-
-            case 'mssql':
-            case 'dblib':
-                if ( ! isset($parts['path']) || $parts['path'] == '/') {
-                    throw new IPF_ORM_Exception('No database available in data source name');
-                }
-                if (isset($parts['path'])) {
-                    $parts['database'] = substr($parts['path'], 1);
-                }
-                if ( ! isset($parts['host'])) {
-                    throw new IPF_ORM_Exception('No hostname set in data source name');
-                }
-
-                $parts['dsn'] = $this->makeDsnForPDO($parts['scheme'], $parts['host'], @$parts['port'], $parts['database']);
-
-                break;
-
-            case 'mysql':
-            case 'informix':
-            case 'oci8':
-            case 'oci':
-            case 'firebird':
-            case 'pgsql':
-            case 'odbc':
-            case 'mock':
-            case 'oracle':
-                if ( ! isset($parts['path']) || $parts['path'] == '/') {
-                    throw new IPF_ORM_Exception('No database available in data source name');
-                }
-                if (isset($parts['path'])) {
-                    $parts['database'] = substr($parts['path'], 1);
-                }
-                if ( ! isset($parts['host'])) {
-                    throw new IPF_ORM_Exception('No hostname set in data source name');
-                }
-
-                $parts['dsn'] = $this->makeDsnForPDO($parts['scheme'], $parts['host'], @$parts['port'], $parts['database']);
-
-                break;
-            default:
-                throw new IPF_ORM_Exception('Unknown driver '.$parts['scheme']);
-        }
-
-        return $parts;
-    }
-
-    public function makeDsnForPDO($driver, $host, $port, $database)
-    {
-        switch ($driver) {
-            case 'sqlite':
-            case 'sqlite2':
-            case 'sqlite3':
-                if ($host == ':memory') {
-                    return 'sqlite::memory:';
-                } else {
-                    return $driver . ':' . $database;
-                }
-
-            case 'mssql':
-            case 'dblib':
-                return $driver . ':host=' . $host . ($port ? ':' . $port : '') . ';dbname=' . $database;
-
-            case 'mysql':
-            case 'informix':
-            case 'oci8':
-            case 'oci':
-            case 'firebird':
-            case 'pgsql':
-            case 'odbc':
-            case 'mock':
-            case 'oracle':
-                return $driver . ':host=' . $host . ($port ? ';port=' . $port : '') . ';dbname=' . $database;
-
-            default:
-                throw new IPF_ORM_Exception('Unknown driver '.$driver);
-        }
-    }
-
-    public function getConnection($name)
-    {
-        if ( ! isset($this->_connections[$name])) {
-            throw new IPF_ORM_Exception('Unknown connection: ' . $name);
-        }
-
-        return $this->_connections[$name];
-    }
-
-    public function getConnectionName(IPF_ORM_Connection $conn)
-    {
-        return array_search($conn, $this->_connections, true);
-    }
-
-    public function bindComponent($componentName, $connectionName)
-    {
-        $this->_bound[$componentName] = $connectionName;
-    }
-
-    public function getConnectionForComponent($componentName)
-    {
-        //IPF_ORM::autoload($componentName);
-
-        if (isset($this->_bound[$componentName])) {
-            return $this->getConnection($this->_bound[$componentName]);
-        }
-
-        return $this->getCurrentConnection();
-    }
-    
-    public function hasConnectionForComponent($componentName = null)
-    {
-        return isset($this->_bound[$componentName]);
-    }
-
-    public function closeConnection(IPF_ORM_Connection $connection)
-    {
-        $connection->close();
-
-        $key = array_search($connection, $this->_connections, true);
-
-        if ($key !== false) {
-            unset($this->_connections[$key]);
-        }
-        $this->_currIndex = key($this->_connections);
-
-        unset($connection);
-    }
-
-    public function getConnections()
-    {
-        return $this->_connections;
-    }
-
-    public function setCurrentConnection($key)
-    {
-        $key = (string) $key;
-        if ( ! isset($this->_connections[$key])) {
-            throw new InvalidKeyException();
-        }
-        $this->_currIndex = $key;
-    }
-
-    public function contains($key)
-    {
-        return isset($this->_connections[$key]);
-    }
-
-    public function count()
-    {
-        return count($this->_connections);
-    }
-
-    public function getIterator()
-    {
-        return new ArrayIterator($this->_connections);
-    }
-
     public function getCurrentConnection()
     {
-        $i = $this->_currIndex;
-        if ( ! isset($this->_connections[$i])) {
-            throw new IPF_ORM_Exception('There is no open connection');
-        }
-        return $this->_connections[$i];
-    }
-
-    public function createDatabases($specifiedConnections = array())
-    {
-        if ( ! is_array($specifiedConnections)) {
-            $specifiedConnections = (array) $specifiedConnections;
+        if (!$this->_connection) {
+            $pdo = \PFF\Container::databaseConnection();
+            $this->_connection = new IPF_ORM_Connection_Mysql($this, $pdo);
         }
-
-        $results = array();
-
-        foreach ($this as $name => $connection) {
-            if ( ! empty($specifiedConnections) && ! in_array($name, $specifiedConnections)) {
-                continue;
-            }
-
-            $results[$name] = $connection->createDatabase();
-        }
-
-        return $results;
-    }
-
-    public function dropDatabases($specifiedConnections = array())
-    {
-        if ( ! is_array($specifiedConnections)) {
-            $specifiedConnections = (array) $specifiedConnections;
-        }
-
-        $results = array();
-
-        foreach ($this as $name => $connection) {
-            if ( ! empty($specifiedConnections) && ! in_array($name, $specifiedConnections)) {
-                continue;
-            }
-
-            $results[$name] = $connection->dropDatabase();
-        }
-
-        return $results;
+        return $this->_connection;
     }
 
     public function __toString()
     {
-        $r[] = "<pre>";
-        $r[] = "IPF_ORM_Manager";
-        $r[] = "Connections : ".count($this->_connections);
-        $r[] = "</pre>";
-        return implode("\n",$r);
+        return "<pre>\nIPF_ORM_Manager\n</pre>";
     }
 }
 
index 7a8a410a1e74a387fb8b6592f5ca51e98d2fd6ad..1830c334ef53a39cb232fa8866c6021aab9c560e 100644 (file)
@@ -1097,9 +1097,6 @@ class IPF_ORM_Query extends IPF_ORM_Query_Abstract implements Countable, Seriali
     {
         // get the connection for the component
         $manager = IPF_ORM_Manager::getInstance();
-        if ($manager->hasConnectionForComponent($name)) {
-            $this->_conn = $manager->getConnectionForComponent($name);
-        }
 
         $table = $this->_conn->getTable($name);
         $tableName = $table->getTableName();
@@ -1128,9 +1125,6 @@ class IPF_ORM_Query extends IPF_ORM_Query_Abstract implements Countable, Seriali
     {
         // get the connection for the component
         $manager = IPF_ORM_Manager::getInstance();
-        if ($manager->hasConnectionForComponent($name)) {
-            $this->_conn = $manager->getConnectionForComponent($name);
-        }
 
         $table = $this->_conn->getTable($name);
         $tableName = $table->getTableName();
index f09d5dcfb3047e6dd5f39592f827303aff44adf3..025ac448d2bb20518d1daf77d38070abf81a4e1d 100644 (file)
@@ -79,7 +79,7 @@ abstract class IPF_ORM_Query_Abstract
             IPF_ORM_Hydrator_Abstract $hydrator = null)
     {
         if ($connection === null) {
-            $connection = IPF_ORM_Manager::getInstance()->getCurrentConnection();
+            $connection = IPF_ORM_Manager::connection();
         }
         if ($hydrator === null) {
             $hydrator = new IPF_ORM_Hydrator();
index d2d521bbcbb1025da692d6477f1bf7891f60550f..79728c34f44bcce0ac0032644ebb912948f19b56 100644 (file)
@@ -11,9 +11,7 @@ class IPF_ORM_Query_Check
     public function __construct($table)
     {
         if ( ! ($table instanceof IPF_ORM_Table)) {
-            $table = IPF_ORM_Manager::getInstance()
-                        ->getCurrentConnection()
-                        ->getTable($table);
+            $table = IPF_ORM_Manager::connection()->getTable($table);
         }
         $this->table = $table;
         $this->_tokenizer = new IPF_ORM_Query_Tokenizer();
@@ -98,4 +96,4 @@ class IPF_ORM_Query_Check
     {
         return $this->sql;
     }
-}
\ No newline at end of file
+}
index 8d314225b78e6987fa0a877c9e3bd9ae8829fd70..fce501650d6664555151d3243122e506b75b5b16 100644 (file)
@@ -213,10 +213,7 @@ class IPF_ORM_RawSql extends IPF_ORM_Query_Abstract
             }
 
             if (!isset($table)) {
-                $conn = IPF_ORM_Manager::getInstance()
-                        ->getConnectionForComponent($component);
-
-                $table = $conn->getTable($component);
+                $table = IPF_ORM_Manager::connection()->getTable($component);
                 $this->_queryComponents[$componentAlias] = array('table' => $table);
             } else {
                 $relation = $table->getRelation($component);
index 1290ff6341b02d5da22decf047271bcc3656cd9d..2b5b908bdae4cc70979f232d369bb42558f286db 100644 (file)
@@ -277,8 +277,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
 
         $this->preUnserialize($event);
 
-        $manager    = IPF_ORM_Manager::getInstance();
-        $connection = $manager->getConnectionForComponent(get_class($this));
+        $connection = IPF_ORM_Manager::connection();
 
         $this->_oid = self::$_index;
         self::$_index++;
index 04284ec4b24128a923eae9a4e001476d6d054511..3d0c550e96e6b58c83ef3ff8197bfce492c0d20b 100644 (file)
@@ -115,9 +115,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess
 
     final public function getTable()
     {
-        return IPF_ORM_Manager::getInstance()
-               ->getConnectionForComponent($this->definition['class'])
-               ->getTable($this->definition['class']);
+        return IPF_ORM_Manager::connection()->getTable($this->definition['class']);
     }
 
     final public function getClass()
index c61c8c4e6442b2cea9d2618816543294c2706d97..cc606316672b8d06fbe2e4340ffeb8bef445e92b 100644 (file)
@@ -59,8 +59,6 @@ class IPF_ORM_Transaction extends IPF_ORM_Connection_Module
 
     public function beginTransaction($savepoint = null)
     {
-        $this->conn->connect();
-
         if ( ! is_null($savepoint)) {
             $this->savePoints[] = $savepoint;
 
@@ -100,8 +98,6 @@ class IPF_ORM_Transaction extends IPF_ORM_Connection_Module
         if ($this->_nestingLevel == 0) {
             throw new IPF_ORM_Exception("Commit failed. There is no active transaction.");
         }
-        
-        $this->conn->connect();
 
         if ( ! is_null($savepoint)) {
             $this->_nestingLevel -= $this->removeSavePoints($savepoint);
@@ -161,8 +157,6 @@ class IPF_ORM_Transaction extends IPF_ORM_Connection_Module
         if ($this->_nestingLevel == 0) {
             throw new IPF_ORM_Exception("Rollback failed. There is no active transaction.");
         }
-        
-        $this->conn->connect();
 
         if ($this->_internalNestingLevel > 1 || $this->_nestingLevel > 1) {
             $this->_internalNestingLevel--;