From a6d916c4ae66d061d042d216c96139499f1eebc4 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Fri, 8 Jul 2022 18:00:59 +0300 Subject: [PATCH] =?UTF-8?q?SQLite=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=80=D0=B5=D0=B6=D0=B8=D0=BC=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/register.php | 2 +- core/lib/Core/Models/DB.class.php | 64 +++++++++++++++++++---------- core/lib/Core/Models/User.class.php | 4 +- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/admin/register.php b/admin/register.php index 48352a2..1685dea 100644 --- a/admin/register.php +++ b/admin/register.php @@ -26,7 +26,7 @@ if (User::isAuthorized()) { } if(empty($arErrors)) { - $userId = User::create($_REQUEST['login'], $_REQUEST['pass'], $_REQUEST['email'], $_REQUEST['name'], '/uploads/users/system.png'); + $userId = User::create($_REQUEST['login'], $_REQUEST['pass'], $_REQUEST['email'], $_REQUEST['name']); User::authorize($userId); header('Location: index.php'); diff --git a/core/lib/Core/Models/DB.class.php b/core/lib/Core/Models/DB.class.php index 53b8eab..57a4beb 100644 --- a/core/lib/Core/Models/DB.class.php +++ b/core/lib/Core/Models/DB.class.php @@ -35,7 +35,7 @@ /** * ID последней добавленной записи * - * @var int|null + * @var string|null */ private ?string $lastInsertId = null; @@ -49,15 +49,29 @@ */ public function __construct(string $db_server = DB_HOST, string $db_user = DB_USER, ?string $db_pass = DB_PASSWORD, string $db_name = DB_NAME) { - $dsn = 'mysql:host=' . $db_server . ';dbname=' . $db_name . ';charset=utf8'; - $opt = [ - \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, - \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, - \PDO::ATTR_EMULATE_PREPARES => false, - ]; - $this->db = new \PDO($dsn, $db_user, $db_pass, $opt); + if (DB_TYPE === DB_TYPE_SQL) { + $dsn = 'mysql:host=' . $db_server . ';dbname=' . $db_name . ';charset=utf8'; + $opt = [ + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_EMULATE_PREPARES => false, + ]; + $this->db = new \PDO($dsn, $db_user, $db_pass, $opt); + } elseif (DB_TYPE === DB_TYPE_SQLITE) { + $this->db = new \SQLite3(SQLITE_DB_FILE); + } } + /** + * Деструктор + */ + public function __destruct() + { + unset($this->instance); + unset($this->db); + } + + /** * Получить объект класса * @@ -180,20 +194,28 @@ { $startTime = microtime(true); self::$quantity++; - try { - $stmt = $this->db->query($sql); - } catch (\Throwable $t) { - throw new CoreException('В SQL запросе произошла ошибка', CoreException::ERROR_SQL_QUERY); - } - $this->lastInsertId = $this->db->lastInsertId() ?? null; - $endTime = microtime(true); - self::$workingTime += ($endTime - $startTime); - if ($returnObject) { - return $stmt; - } else { - $result = $stmt->fetchAll(); - return $result ?: null; + if(DB_TYPE === DB_TYPE_SQL) { + try { + $stmt = $this->db->query($sql); + } catch (\Throwable $t) { + throw new CoreException('В SQL запросе произошла ошибка', CoreException::ERROR_SQL_QUERY); + } + $this->lastInsertId = $this->db->lastInsertId() ?? null; + + + $endTime = microtime(true); + self::$workingTime += ($endTime - $startTime); + + if ($returnObject) { + return $stmt; + } else { + $result = $stmt->fetchAll(); + return $result ?: null; + } + } elseif(DB_TYPE === DB_TYPE_SQLITE) { + $result = $this->db->query($sql); + return $result->fetchArray(SQLITE3_ASSOC); } } diff --git a/core/lib/Core/Models/User.class.php b/core/lib/Core/Models/User.class.php index 7af9e6c..00e4782 100644 --- a/core/lib/Core/Models/User.class.php +++ b/core/lib/Core/Models/User.class.php @@ -302,7 +302,7 @@ * * @throws CoreException */ - public static function create(string $login, string $password, string $email, string $name = '', string $image = ''): int + public static function create(string $login, string $password, string $email, string $name = ''): int { Log::logToFile(__METHOD__, 'User.log', func_get_args()); @@ -312,7 +312,7 @@ 'login' => $login, 'password' => self::passwordEncryption($password), 'name' => $name, - 'image' => $image, + 'image_id' => null, 'token' => '', 'email' => $email, 'last_active' => time(),