From 3ac5df9eb3c2e4bd3c2c221f81f6322eff382289 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 16 May 2013 00:51:48 +0100 Subject: [PATCH] Updating MySQL plugin to use PDO, and accept connection parameters from build_settings.mysql --- PHPCI/Plugin/Mysql.php | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/PHPCI/Plugin/Mysql.php b/PHPCI/Plugin/Mysql.php index 12786e44..b3308d07 100644 --- a/PHPCI/Plugin/Mysql.php +++ b/PHPCI/Plugin/Mysql.php @@ -1,29 +1,51 @@ phpci = $phpci; $this->queries = $options; + + $db = \b8\Database::getConnection('write')->getDetails(); + $this->host = PHPCI_DB_HOST; + $this->user = $db['user']; + $this->pass = $db['pass']; + + $buildSettings = $phpci->getConfig('build_settings'); + if(isset($buildSettings['mysql'])) { + $sql = $buildSettings['mysql']; + + $this->host = !empty($sql['host']) ? $sql['host'] : $this->host; + $this->user = !empty($sql['user']) ? $sql['user'] : $this->user; + $this->pass = array_key_exists('pass', $sql) ? $sql['pass'] : $this->pass; + } } public function execute() { - $rtn = true; + try { + $pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); - $db = \b8\Database::getConnection('write')->getDetails(); - - foreach($this->queries as $query) - { - $rtn = !$this->phpci->executeCommand('mysql -h'.PHPCI_DB_HOST.' -u'.$db['user'].(!empty($db['pass']) ? ' -p' . $db['pass'] : '').' -e "'.$query.'"') ? false : $rtn; + foreach($this->queries as $query) { + $pdo->query($query); + } + } + catch(\Exception $ex) { + $this->phpci->logFailure($ex->getMessage()); + return false; } - return $rtn; + return true; } } \ No newline at end of file