Skip to content

Commit

Permalink
#165 - added initial implementations of checkDatabaseExists()/createD…
Browse files Browse the repository at this point in the history
…atabase()/dropDatabase() methods of the MySQL active record implementation
  • Loading branch information
alphadevx committed Aug 17, 2015
1 parent d869b99 commit 0fd30da
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions Alpha/Model/ActiveRecordProviderMySQL.php
Expand Up @@ -115,7 +115,16 @@ public static function getConnection()
$config = ConfigProvider::getInstance();

if (!isset(self::$connection)) {
self::$connection = new Mysqli($config->get('db.hostname'), $config->get('db.username'), $config->get('db.password'), $config->get('db.name'));
try {
self::$connection = new Mysqli($config->get('db.hostname'), $config->get('db.username'), $config->get('db.password'), $config->get('db.name'));
} catch (\Exception $e) {
// if we failed to connect because the database does not exist, create it and try again
if (strpos($e->getMessage(),'HY000/1049') !== false) {
self::createDatabase();
self::$connection = new Mysqli($config->get('db.hostname'), $config->get('db.username'), $config->get('db.password'), $config->get('db.name'));
}
}

self::$connection->set_charset('utf8');

if (mysqli_connect_error())
Expand Down Expand Up @@ -1629,7 +1638,8 @@ public static function checkBOTableExists($BOClassName, $checkHistoryTable = fal
* (non-PHPdoc)
* @see Alpha\Model\ActiveRecordProviderInterface::checkTableNeedsUpdate()
*/
public function checkTableNeedsUpdate() {
public function checkTableNeedsUpdate()
{
self::$logger->debug('>>checkTableNeedsUpdate()');

$updateRequired = false;
Expand Down Expand Up @@ -2321,7 +2331,17 @@ private function findOffendingValue($error)
*/
public static function checkDatabaseExists()
{
// TODO: implement
$config = ConfigProvider::getInstance();

$connection = new Mysqli($config->get('db.hostname'), $config->get('db.username'), $config->get('db.password'));

$result = $connection->query('SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = \''.$config->get('db.name').'\'');

if (count($result) > 0) {
return true;
} else {
return false;
}
}

/**
Expand All @@ -2330,7 +2350,11 @@ public static function checkDatabaseExists()
*/
public static function createDatabase()
{
// TODO: implement
$config = ConfigProvider::getInstance();

$connection = new Mysqli($config->get('db.hostname'), $config->get('db.username'), $config->get('db.password'));

$result = $connection->query('CREATE DATABASE '.$config->get('db.name'));
}

/**
Expand All @@ -2339,7 +2363,11 @@ public static function createDatabase()
*/
public static function dropDatabase()
{
// TODO: implement
$config = ConfigProvider::getInstance();

$connection = new Mysqli($config->get('db.hostname'), $config->get('db.username'), $config->get('db.password'));

$result = $connection->query('DROP DATABASE '.$config->get('db.name'));
}
}

Expand Down

0 comments on commit 0fd30da

Please sign in to comment.