Commit ea4484b5 authored by Pouya Aghazadeh's avatar Pouya Aghazadeh
Browse files

Multi Database supported.

parent 75761c3c
......@@ -4,12 +4,14 @@ namespace Joonika;
use Medoo\Medoo;
use PDO;
use Joonika\dev\db;
class Database
{
public $database;
protected $is_connected;
private static $instance = null;
protected static $currentConnection;
private function __construct($info)
{
......@@ -41,10 +43,19 @@ class Database
}
}
public static function connect($info = null)
public static function connect($db = null)
{
if (!self::$instance) {
new Database($info);
$dbInfo = db::getDbConfig();
if (gettype($db) == 'string') {
if (isset($dbInfo[$db]))
$db = $dbInfo[$db];
else
$db = $dbInfo['main'];
} else
$db = $dbInfo['main'];
if (!self::$instance || self::$currentConnection != $db['db']) {
new Database($db);
self::$currentConnection = $db['db'];
}
return self::$instance;
}
......@@ -139,9 +150,10 @@ class Database
return self::$instance->exec($query, $map);
}
public static function create($table,$columns,$options=null){
public static function create($table, $columns, $options = null)
{
self::connect();
return self::$instance->create($table,$columns,$options);
return self::$instance->create($table, $columns, $options);
}
public static function action($actions)
......
......@@ -8,34 +8,66 @@ use Joonika\Database;
class db extends baseCommand
{
public function getDbConfig(){
$mainConfig = array_keys(array_column(JK_WEBSITES(),'database'));
print_r($mainConfig);
public static function getDbConfig()
{
$mainConfig = array_column(JK_WEBSITES(), 'database');
$mainConfig = reset($mainConfig);
$mainDb = [
'db' => $mainConfig['db'],
'host' => $mainConfig['host'],
'user' => $mainConfig['user'],
'pass' => $mainConfig['pass'],
'port' => $mainConfig['port'],
'charset' => $mainConfig['charset'],
'driver' => $mainConfig['driver'],
];
$others = $mainConfig['other'];
$dbList = [];
$dbList['main'] = $mainDb;
$entire = [];
foreach ($others as $other) {
if (gettype($other) == 'array') {
$entire = array_merge($mainDb, $other);
$dbList[$other['db']] = $entire;
} else {
$entire['db'] = $other;
$entire = array_merge($mainDb, $entire);
$dbList[$other] = $entire;
}
}
return $dbList;
}
public function backup(){
public function backup()
{
// $tableName = strtolower($this->checkInputArguments('tableName'));
// $this->ask('Please enter table name ( you can exit with Ctrl + C )', $tableName, true);
$tables = Database::query('SHOW TABLES');
$db = array_column(JK_WEBSITES(),'database')[0]['db'];
$username = array_column(JK_WEBSITES(),'database')[0]['user'];
$password = array_column(JK_WEBSITES(),'database')[0]['pass'];
// $tables = Database::query('SHOW TABLES');
$db = array_column(JK_WEBSITES(), 'database')[0]['db'];
$username = array_column(JK_WEBSITES(), 'database')[0]['user'];
$password = array_column(JK_WEBSITES(), 'database')[0]['pass'];
$this->getDbConfig();
// print_r(self::getDbConfig());
$impouyai_gateway_connection = Database::connect('impouyai_gateway');
$main_connection = Database::connect();
print_r($impouyai_gateway_connection->info());
print_r($main_connection->info());
// Database::connect('logs');
die('');
// print_r(array_search(JK_WEBSITE_ID(),));
foreach ($tables as $table){
// $this->writeInfo($table[0]);
foreach ($tables as $table) {
$this->writeInfo($table[0]);
}
date_default_timezone_set('Iran');
$filename='dbBackup_'.date('[Y-m-d][H:i:s]').'.sql';
FS::mkDir(JK_SITE_PATH().'/backup');
$result=exec('mysqldump '.$db.' --password='.$password.' --user='.$username.' --single-transaction >'.JK_SITE_PATH().'/backup/'.$filename,$output);
if(empty($output))
$filename = 'dbBackup_' . date('[Y-m-d][H:i:s]') . '.sql';
FS::mkDir(JK_SITE_PATH() . '/backup');
$result = exec('mysqldump ' . $db . ' --password=' . $password . ' --user=' . $username . ' --single-transaction >' . JK_SITE_PATH() . '/backup/' . $filename, $output);
if (empty($output))
$this->writeInfo('backedup successfuly');
else
$this->writeError('backup process faild : '.$output);
$this->writeError('backup process faild : ' . $output);
}
public function restore()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment