/
jagepard
/
Rudra-Model
Обзор
Документация
Войти
/
jagepard
/
Rudra-Model
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/QB.php
197 строк
5 KB
Jagepard
add update
03 авг 2026, 15:17
03 авг 2026, 15:17
47d3d56
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. * * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> * @license https://mozilla.org/MPL/2.0/ MPL-2.0 */ namespace Rudra\Model; use Rudra\Model\Drivers\MySQL; use Rudra\Model\Drivers\PgSQL; use Rudra\Model\Drivers\SQLite; use Rudra\Container\Facades\Rudra; use Rudra\Exceptions\LogicException; class QB { private MySQL|PgSQL|SQLite $driver; private string $query = ''; /** * Initializes the database driver based on the provided connection or a default connection from the container. * If no connection is provided and none is available in the container, a LogicException is thrown. * The driver is selected based on the database type specified in the connection's driver attribute. * * @param mixed $connection DO NOT TYPE HINT - causes premature PDO resolution by IoC container * @throws LogicException */ public function __construct($connection = null) { $connection = $connection ?? Rudra::get('connection') ?? throw new LogicException("connection is not installed"); if ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME) === "mysql") { $this->driver = new MySQL; } elseif ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME) === "pgsql") { $this->driver = new PgSQL; } elseif ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME) === "sqlite") { $this->driver = new SQLite; } } public function select(string $fields = '*'): self { $this->query .= "SELECT {$fields} "; return $this; } public function delete(string $table): self { $this->query .= "DELETE FROM {$table} "; return $this; } public function insert(string $table, string $columns): self { $this->query .= "INSERT INTO {$table} ({$columns}) "; return $this; } public function values(string $placeholders): self { $this->query .= "VALUES ({$placeholders})"; return $this; } public function update(string $table, string $set): self { $this->query .= "UPDATE {$table} SET {$set} "; return $this; } public function groupConcat(string $fieldName, string $alias, ?string $orderBy = null): self { $this->query .= $this->driver->groupConcat($fieldName, $alias, $orderBy); return $this; } public function from(string $table): self { $this->query .= "FROM {$table} "; return $this; } public function where(string $param): self { $this->query .= "WHERE $param "; return $this; } public function and(string $param): self { $this->query .= "AND $param "; return $this; } public function or(string $param): self { $this->query .= "OR $param "; return $this; } public function limit(int|string $param): self { $this->query .= "LIMIT $param "; return $this; } public function offset(int|string $param): self { $this->query .= "OFFSET $param "; return $this; } public function orderBy(string $param): self { $this->query .= "ORDER BY $param "; return $this; } public function groupBy(string $param): self { $this->query .= "GROUP BY $param "; return $this; } public function join(string $param, string $type = "LEFT"): self { $this->query .= "$type JOIN $param "; return $this; } public function on(string $param): self { $this->query .= "ON $param "; return $this; } public function get(): string { $result = $this->query . ';'; $this->query = ''; return $result; } public function create(string $table): self { $this->query .= "CREATE TABLE {$table} ("; return $this; } public function close(): self { $this->query .= $this->driver->close(); return $this; } public function integer(string $field, string $default = "", bool $autoincrement = false, string $null = "NOT NULL"): self { $this->query .= $this->driver->integer($field, $default, $autoincrement, $null); return $this; } public function string(string $field, string $default = "", string $null = "NOT NULL"): self { $this->query .= $this->driver->string($field, $default, $null); return $this; } public function text(string $field, string $null = "NOT NULL"): self { $this->query .= $this->driver->text($field, $null); return $this; } public function createdAt(): self { $this->query .= $this->driver->createdAt(); return $this; } public function updatedAt(): self { $this->query .= $this->driver->updatedAt(); return $this; } public function primaryKey(?string $field = null): self { $this->query .= $this->driver->primaryKey($field); return $this; } }