From 96ae3957f844ddbec2556bf8676b3162ae7e490d Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 14 Aug 2016 08:31:07 +0800 Subject: [PATCH] refactor Database class --- app/Services/Database.php | 59 +++++++++++++++---- .../setup/migrations/import-v2-textures.tpl | 5 +- setup/migrations/import_v2_textures.php | 4 +- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/Services/Database.php b/app/Services/Database.php index bc6688aa..766017f0 100644 --- a/app/Services/Database.php +++ b/app/Services/Database.php @@ -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 */ -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(); diff --git a/resources/views/setup/migrations/import-v2-textures.tpl b/resources/views/setup/migrations/import-v2-textures.tpl index 1ab5d6db..2afd988d 100644 --- a/resources/views/setup/migrations/import-v2-textures.tpl +++ b/resources/views/setup/migrations/import-v2-textures.tpl @@ -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']} 不存在"); } } diff --git a/setup/migrations/import_v2_textures.php b/setup/migrations/import_v2_textures.php index 9eff89c0..0682517b 100644 --- a/setup/migrations/import_v2_textures.php +++ b/setup/migrations/import_v2_textures.php @@ -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);