refactor Database class
This commit is contained in:
parent
76c305d8c3
commit
96ae3957f8
@ -4,21 +4,50 @@ namespace App\Services;
|
||||
|
||||
use App\Exceptions\E;
|
||||
|
||||
/**
|
||||
* Facade for DatabaseHelper
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
public function __call($method, $args)
|
||||
{
|
||||
// Instantiate Helper
|
||||
$instance = new DatabaseHelper;
|
||||
// Call methods
|
||||
return call_user_func_array([$instance, $method], $args);
|
||||
}
|
||||
|
||||
public static function __callStatic($method, $args)
|
||||
{
|
||||
$instance = new DatabaseHelper;
|
||||
return call_user_func_array([$instance, $method], $args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Light-weight database helper
|
||||
*
|
||||
* @author <h@prinzeugen.net>
|
||||
*/
|
||||
class Database
|
||||
class DatabaseHelper
|
||||
{
|
||||
/**
|
||||
* Instance of MySQLi
|
||||
*
|
||||
* @var null
|
||||
*/
|
||||
private $connection = null;
|
||||
|
||||
/**
|
||||
* Connection config
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $config = null;
|
||||
|
||||
/**
|
||||
* Table name to do operations in
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $table_name = "";
|
||||
@ -29,15 +58,16 @@ class Database
|
||||
* @param string $table_name
|
||||
* @param array $config
|
||||
*/
|
||||
function __construct($table_name = '', $config = null, $no_prefix = false)
|
||||
public function __construct($config = null)
|
||||
{
|
||||
$config = is_null($config) ? Config::getDbConfig() : $config;
|
||||
$this->config = is_null($config) ? Config::getDbConfig() : $config;
|
||||
|
||||
@$this->connection = new \mysqli(
|
||||
$config['host'],
|
||||
$config['username'],
|
||||
$config['password'],
|
||||
$config['database'],
|
||||
$config['port']
|
||||
$this->config['host'],
|
||||
$this->config['username'],
|
||||
$this->config['password'],
|
||||
$this->config['database'],
|
||||
$this->config['port']
|
||||
);
|
||||
|
||||
if ($this->connection->connect_error)
|
||||
@ -45,7 +75,16 @@ class Database
|
||||
$this->connection->connect_error, $this->connection->connect_errno, true);
|
||||
|
||||
$this->connection->query("SET names 'utf8'");
|
||||
$this->table_name = $no_prefix ? $table_name : $config['prefix'].$table_name;
|
||||
}
|
||||
|
||||
public function table($table_name, $no_prefix = false)
|
||||
{
|
||||
if (Utils::convertString($table_name) == $table_name) {
|
||||
$this->table_name = $no_prefix ? $table_name : $this->config['prefix'].$table_name;
|
||||
return $this;
|
||||
} else {
|
||||
throw new E('Table name contains invalid characters', 1);
|
||||
}
|
||||
}
|
||||
|
||||
public function query($sql)
|
||||
@ -167,7 +206,7 @@ class Database
|
||||
return $statement;
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
public function __destruct()
|
||||
{
|
||||
if (!is_null($this->connection))
|
||||
$this->connection->close();
|
||||
|
@ -69,7 +69,10 @@
|
||||
} else {
|
||||
$_POST['uploader_uid'] = ($_POST['uploader_uid'] == "") ? 0 : (int)$_POST['uploader_uid'];
|
||||
|
||||
if (!(new Database)->hasTable($_POST['v2_table_name'])) {
|
||||
if (Utils::convertString($_POST['v2_table_name']) != $_POST['v2_table_name'])
|
||||
Http::redirect('index.php?action=import-v2-textures&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
|
||||
|
||||
if (!Database::hasTable($_POST['v2_table_name'])) {
|
||||
Http::redirect('index.php?action=import-v2-textures&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
* @Author: printempw
|
||||
* @Date: 2016-08-09 21:44:13
|
||||
* @Last Modified by: printempw
|
||||
* @Last Modified time: 2016-08-10 22:41:24
|
||||
* @Last Modified time: 2016-08-14 08:00:49
|
||||
*
|
||||
* There are still some coupling relationships here but,
|
||||
* Just let it go :)
|
||||
@ -16,7 +16,7 @@ $imported = 0;
|
||||
$duplicated = 0;
|
||||
|
||||
// use db helper instead of fat ORM
|
||||
$db = new Database($v2_table_name, null, true);
|
||||
$db = Database::table($v2_table_name, true);
|
||||
|
||||
$steps = ceil($db->getRecordNum() / 250);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user