From a5065fc31566f5909d47d28615c21da0a51eed84 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Fri, 21 Jun 2013 01:10:53 +0300 Subject: [PATCH] db console --- ipf.php | 2 +- ipf/cli.php | 1 + ipf/command/db.php | 23 +++++++++++++++++++++++ ipf/orm/manager.php | 45 ++++++++++++++++++++++++++------------------- ipf/shell.php | 25 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 ipf/command/db.php create mode 100644 ipf/shell.php diff --git a/ipf.php b/ipf.php index 8287709..3281b9f 100644 --- a/ipf.php +++ b/ipf.php @@ -11,7 +11,7 @@ final class IPF { private static $settings = array(); - private static function applySettings(&$settings) + private static function applySettings($settings) { foreach($settings as $key=>$val) IPF::$settings[strtolower($key)] = $val; diff --git a/ipf/cli.php b/ipf/cli.php index 4fa2325..37c5f1a 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -10,6 +10,7 @@ class IPF_Cli new IPF_Command_BuildModels, new IPF_Command_BuildContribModels, new IPF_Command_Sql, + new IPF_Command_DB, new IPF_Command_SyncDB, new IPF_Command_Fixtures, new IPF_Command_CreateSuperUser, diff --git a/ipf/command/db.php b/ipf/command/db.php new file mode 100644 index 0000000..15d9aa2 --- /dev/null +++ b/ipf/command/db.php @@ -0,0 +1,23 @@ +connectionParameters(IPF::get('database', IPF::get('dsn'))); + + if ($db['scheme'] === 'mysql') { + IPF_Shell::call('mysql', + '-h'.$db['host'], + '-u'.$db['username'], + '-p'.$db['password'], + $db['database']); + } else { + print 'Do not know how to connect to "'.$db['scheme'].'" database.'; + } + } +} + diff --git a/ipf/orm/manager.php b/ipf/orm/manager.php index f852ac5..f28ad39 100644 --- a/ipf/orm/manager.php +++ b/ipf/orm/manager.php @@ -64,26 +64,8 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato 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 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']); - } else { - $adapter = array( - 'dsn' => urldecode($adapter[0]), - 'username' => (isset($adapter[1])) ? urldecode($adapter[1]) : null, - 'password' => (isset($adapter[2])) ? urldecode($adapter[2]) : null, - ); - } - - $e = explode(':', $adapter['dsn']); - $driverName = $e[0] !== 'uri' ? $e[0] : 'odbc'; } else { - $adapter = $this->parseDsn($adapter); - $adapter['username'] = $adapter['user']; - $adapter['password'] = $adapter['pass']; + $adapter = $this->connectionParameters($adapter); $driverName = $adapter['scheme']; } @@ -129,6 +111,31 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato 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(); diff --git a/ipf/shell.php b/ipf/shell.php new file mode 100644 index 0000000..f29db27 --- /dev/null +++ b/ipf/shell.php @@ -0,0 +1,25 @@ + STDIN, + 1 => STDOUT, + 2 => STDERR + ); + $process = proc_open($str, $descriptorspec, $pipes); + proc_close($process); + } +} + -- 2.49.0