From 1c026b2d50118c465797c51bc746b86656e1ab18 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 16 Aug 2014 16:06:51 +0300 Subject: [PATCH] invert connection owning --- ipf/orm.php | 2 +- ipf/orm/collection.php | 3 +- ipf/orm/connection.php | 215 +------------------- ipf/orm/connection/module.php | 4 +- ipf/orm/connection/mysql.php | 6 - ipf/orm/export.php | 24 +-- ipf/orm/export/mysql.php | 10 - ipf/orm/import/builder.php | 6 - ipf/orm/manager.php | 371 +--------------------------------- ipf/orm/query.php | 6 - ipf/orm/query/abstract.php | 2 +- ipf/orm/query/check.php | 6 +- ipf/orm/rawsql.php | 5 +- ipf/orm/record.php | 3 +- ipf/orm/relation.php | 4 +- ipf/orm/transaction.php | 6 - 16 files changed, 27 insertions(+), 646 deletions(-) diff --git a/ipf/orm.php b/ipf/orm.php index a0f573e..fb0396f 100644 --- a/ipf/orm.php +++ b/ipf/orm.php @@ -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()) diff --git a/ipf/orm/collection.php b/ipf/orm/collection.php index 4ac8e3e..be683a5 100644 --- a/ipf/orm/collection.php +++ b/ipf/orm/collection.php @@ -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); diff --git a/ipf/orm/connection.php b/ipf/orm/connection.php index 1931761..7fddf02 100644 --- a/ipf/orm/connection.php +++ b/ipf/orm/connection.php @@ -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; diff --git a/ipf/orm/connection/module.php b/ipf/orm/connection/module.php index 1a76572..b894e2d 100644 --- a/ipf/orm/connection/module.php +++ b/ipf/orm/connection/module.php @@ -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 +} diff --git a/ipf/orm/connection/mysql.php b/ipf/orm/connection/mysql.php index fa7f772..4195f87 100644 --- a/ipf/orm/connection/mysql.php +++ b/ipf/orm/connection/mysql.php @@ -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; diff --git a/ipf/orm/export.php b/ipf/orm/export.php index 425235d..4a5ad4f 100644 --- a/ipf/orm/export.php +++ b/ipf/orm/export.php @@ -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(); diff --git a/ipf/orm/export/mysql.php b/ipf/orm/export/mysql.php index 34af481..1dbe90a 100644 --- a/ipf/orm/export/mysql.php +++ b/ipf/orm/export/mysql.php @@ -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) diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index a97b476..7a398ad 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -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); diff --git a/ipf/orm/manager.php b/ipf/orm/manager.php index e209a00..570de33 100644 --- a/ipf/orm/manager.php +++ b/ipf/orm/manager.php @@ -1,11 +1,8 @@ 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[] = "
";
-        $r[] = "IPF_ORM_Manager";
-        $r[] = "Connections : ".count($this->_connections);
-        $r[] = "
"; - return implode("\n",$r); + return "
\nIPF_ORM_Manager\n
"; } } diff --git a/ipf/orm/query.php b/ipf/orm/query.php index 7a8a410..1830c33 100644 --- a/ipf/orm/query.php +++ b/ipf/orm/query.php @@ -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(); diff --git a/ipf/orm/query/abstract.php b/ipf/orm/query/abstract.php index f09d5dc..025ac44 100644 --- a/ipf/orm/query/abstract.php +++ b/ipf/orm/query/abstract.php @@ -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(); diff --git a/ipf/orm/query/check.php b/ipf/orm/query/check.php index d2d521b..79728c3 100644 --- a/ipf/orm/query/check.php +++ b/ipf/orm/query/check.php @@ -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 +} diff --git a/ipf/orm/rawsql.php b/ipf/orm/rawsql.php index 8d31422..fce5016 100644 --- a/ipf/orm/rawsql.php +++ b/ipf/orm/rawsql.php @@ -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); diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 1290ff6..2b5b908 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -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++; diff --git a/ipf/orm/relation.php b/ipf/orm/relation.php index 04284ec..3d0c550 100644 --- a/ipf/orm/relation.php +++ b/ipf/orm/relation.php @@ -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() diff --git a/ipf/orm/transaction.php b/ipf/orm/transaction.php index c61c8c4..cc60631 100644 --- a/ipf/orm/transaction.php +++ b/ipf/orm/transaction.php @@ -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--; -- 2.49.0