Merge pull request #47 from kamermans/mysql_enhanced

MySQL import file feature
This commit is contained in:
Dan Cryer 2013-05-19 12:24:54 -07:00
commit 78ff184d54

View file

@ -14,17 +14,28 @@ use PDO;
/** /**
* MySQL Plugin - Provides access to a MySQL database. * MySQL Plugin - Provides access to a MySQL database.
* @author Dan Cryer <dan@block8.co.uk> * @author Dan Cryer <dan@block8.co.uk>
* @author Steve Kamerman <stevekamerman@gmail.com>
* @package PHPCI * @package PHPCI
* @subpackage Plugins * @subpackage Plugins
*/ */
class Mysql implements \PHPCI\Plugin class Mysql implements \PHPCI\Plugin
{ {
/**
* @var \PHPCI\Builder
*/
protected $phpci; protected $phpci;
protected $queries = array(); protected $queries = array();
protected $host; protected $host;
protected $user; protected $user;
protected $pass; protected $pass;
/**
* Database Connection
* @var PDO
*/
protected $pdo;
public function __construct(\PHPCI\Builder $phpci, array $options = array()) public function __construct(\PHPCI\Builder $phpci, array $options = array())
{ {
@ -51,18 +62,75 @@ class Mysql implements \PHPCI\Plugin
*/ */
public function execute() public function execute()
{ {
$success = true;
try { try {
$opts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); $opts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts); $this->pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts);
foreach ($this->queries as $query) { foreach ($this->queries as $query) {
$pdo->query($query); if (!is_array($query)) {
// Simple query
$this->pdo->query($query);
} else if (isset($query['import'])) {
// SQL file execution
$this->executeFile($query['import']);
}
} }
} catch (\Exception $ex) { } catch (\Exception $ex) {
$this->phpci->logFailure($ex->getMessage()); $this->phpci->logFailure($ex->getMessage());
return false; return false;
} }
return $success;
}
protected function executeFile($query)
{
if (!isset($query['file'])) {
throw new \Exception("Import statement must contiain an 'file' key");
}
$import_file = $this->phpci->buildPath . $query['file'];
if (!is_readable($import_file)) {
throw new \Exception("Cannot open SQL import file: $import_file");
}
$database = isset($query['database'])? $query['database']: null;
$import_command = $this->getImportCommand($import_file, $database);
if (!$this->phpci->executeCommand($import_command)) {
throw new \Exception("Unable to execute SQL file");
}
return true; return true;
} }
}
/**
* Builds the MySQL import command required to import/execute the specified file
* @param string $import_file Path to file, relative to the build root
* @param string $database If specified, this database is selected before execution
* @return string
*/
protected function getImportCommand($import_file, $database=null) {
$decompression = array(
'bz2' => '| bzip2 --decompress',
'gz' => '| gzip --decompress',
);
$extension = strtolower(pathinfo($import_file, PATHINFO_EXTENSION));
$decomp_cmd = '';
if (array_key_exists($extension, $decompression)) {
$decomp_cmd = $decompression[$extension];
}
$args = array(
':import_file' => escapeshellarg($import_file),
':decomp_cmd' => $decomp_cmd,
':user' => escapeshellarg($this->user),
':pass' => escapeshellarg($this->pass),
':database' => ($database === null)? '': escapeshellarg($database),
);
return strtr('cat :import_file :decomp_cmd | mysql -u:user -p:pass :database', $args);
}
}