182 lines
7.3 KiB
PHP
182 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace lib\Core;
|
|
|
|
use \PDO;
|
|
use lib\Core\Entity;
|
|
use PDOException;
|
|
use PDOStatement;
|
|
|
|
abstract class Model
|
|
{
|
|
private $_db = null;
|
|
private $_debug = false;
|
|
private $_reset = true;
|
|
private $_querys = ["SELECT" => "SELECT *", "JOIN" => "", "ORDERBY" => "", "LIMIT" => ""];
|
|
private $_wheres = [];
|
|
private $_lastQuery = "";
|
|
protected function __construct() {} //
|
|
|
|
abstract public function getTable(): string;
|
|
abstract public function getPKField(): string;
|
|
abstract public function getTitleField(): string;
|
|
abstract public function getEntity(): Entity;
|
|
abstract public function getEntitys(): mixed;
|
|
final public function getConnect(): PDO
|
|
{
|
|
if ($this->_db === null) {
|
|
// $dsn = sprintf("%s:host=%s;dbname=%s;charset=%s", getenv('DATABASE_DRIVER'), getenv('DATABASE_HOST'), getenv('DATABASE_DB'), getenv('DATABASE_CHARSET'));
|
|
// $this->_db = new PDO($dsn, getenv('DATABASE_ID'), getenv('DATABASE_PASSWORD'));
|
|
$driver = $_ENV['DATABASE_DRIVER'] ?? $_SERVER['DATABASE_DRIVER'] ?? 'mysql';
|
|
$host = $_ENV['DATABASE_HOST'] ?? $_SERVER['DATABASE_HOST'] ?? 'localhost';
|
|
$dbname = $_ENV['DATABASE_DB'] ?? $_SERVER['DATABASE_DB'] ?? 'test';
|
|
$charset = $_ENV['DATABASE_CHARSET'] ?? $_SERVER['DATABASE_CHARSET'] ?? 'utf8';
|
|
$user = $_ENV['DATABASE_ID'] ?? $_SERVER['DATABASE_ID'] ?? 'root';
|
|
$pass = $_ENV['DATABASE_PASSWORD'] ?? $_SERVER['DATABASE_PASSWORD'] ?? '';
|
|
$dsn = sprintf("%s:host=%s;dbname=%s;charset=%s", $driver, $host, $dbname, $charset);
|
|
try {
|
|
$this->_db = new PDO($dsn, $user, $pass);
|
|
$this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
|
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
throw new \Exception("❌ DB 연결 실패: " . $e->getMessage());
|
|
}
|
|
}
|
|
return $this->_db;
|
|
}
|
|
final public function setDebug($debug)
|
|
{
|
|
$this->_debug = $debug;
|
|
}
|
|
final public function getDebug()
|
|
{
|
|
return $this->_debug;
|
|
}
|
|
final public function getLastQuery(): string
|
|
{
|
|
return $this->_lastQuery;
|
|
}
|
|
private function getValue(mixed $values): string|null
|
|
{
|
|
if ($values === null) {
|
|
return $values;
|
|
}
|
|
$temps = [];
|
|
if (is_array($values)) {
|
|
foreach ($values as $value) {
|
|
$temps[] = is_string($value) ? "'{$value}'" : $value;
|
|
}
|
|
} else {
|
|
$temps[] = is_string($values) ? "'{$values}'" : $values;
|
|
}
|
|
return implode(",", $temps);
|
|
}
|
|
final public function getData(mixed $columns, mixed $values = null, $delimeter = ","): string
|
|
{
|
|
$temps = [];
|
|
if (is_array($columns)) {
|
|
foreach ($columns as $column => $value) {
|
|
$value = $this->getValue($value);
|
|
$temps[] = $column . $value === null ? "" : $value;
|
|
}
|
|
} else {
|
|
$value = $this->getValue($values);
|
|
$temps[] = $columns . $value === null ? "" : $value;
|
|
}
|
|
// throw new \Exception("DATA:" . $columns . $value === null ? "NULL" : $value);
|
|
// var_export($temps);
|
|
return implode($delimeter, $temps);
|
|
}
|
|
final public function where(mixed $columns, mixed $values = null, string $delimeter = "AND"): void
|
|
{
|
|
$this->_wheres[] = $this->getData($columns, $values, $delimeter);
|
|
}
|
|
final public function orWhere(mixed $columns, mixed $values = null, string $delimeter = "OR"): void
|
|
{
|
|
$this->where($columns, $values, $delimeter);
|
|
}
|
|
final public function whereIn(string $column, array $values, string $delimeter = "AND", $range = "IN")
|
|
{
|
|
$this->_wheres[] = "{$delimeter} {$column} {$range} (" . $this->getValue($values) . ")";
|
|
}
|
|
final public function whereNotIn(string $column, array $values, string $delimeter = "AND", $range = "NOT IN")
|
|
{
|
|
$this->whereIn($column, $values, $delimeter, $range);
|
|
}
|
|
final public function getWhere(): string
|
|
{
|
|
return count($this->_wheres) ? "WHERE " . implode(" ", $this->_wheres) : "";
|
|
}
|
|
|
|
final public function execute(string $query): bool|PDOStatement
|
|
{
|
|
if ($this->_debug) {
|
|
echo "\nQuery:" . $query . "\n";
|
|
}
|
|
$this->_lastQuery = $query;
|
|
$stmt = $this->getConnect()->prepare($query);
|
|
$stmt->execute();
|
|
if ($this->_reset) {
|
|
$this->_wheres = [];
|
|
$this->_querys = ["SELECT" => "SELECT *", "JOIN" => "", "ORDERBY" => "", "LIMIT" => ""];
|
|
}
|
|
return $stmt;
|
|
}
|
|
|
|
//CURD문
|
|
final protected function create_process(mixed $columns, mixed $values = null): bool|PDOStatement
|
|
{
|
|
$query = sprintf("INSERT INTO %s VALUES(%s) %s", $this->getTable(), $this->getData($columns, $values), $this->getWhere());
|
|
return $this->execute($query);
|
|
} //
|
|
final protected function modify_process(mixed $columns, mixed $values = null): bool|PDOStatement
|
|
{
|
|
$query = sprintf("UPDATE %s SET %s %s", $this->getTable(), $this->getData($columns, $values), $this->getWhere());
|
|
return $this->execute($query);
|
|
} //
|
|
final protected function delete_process(): bool|PDOStatement
|
|
{
|
|
$query = sprintf("DELETE FROM %s %s", $this->getTable(), $this->getWhere());
|
|
return $this->execute($query);
|
|
} //
|
|
public function select(mixed $columns = "*"): void
|
|
{
|
|
$this->_querys["SELECT"] = "SELECT " . is_array($columns) ? implode(",", $columns) : $columns . " FROM {$this->getTable()} WHERE {$this->getWhere()}";
|
|
}
|
|
final public function join(string $table, mixed $match = null, $type = ""): void
|
|
{
|
|
$this->_querys["JOIN"] = " {$type} JOIN {$table} ON {$match}";
|
|
}
|
|
final public function orderBy(mixed $columns, mixed $direction = null): void
|
|
{
|
|
$this->_querys["ORDERBY"] = " ORDERBY " . $this->getData($columns, $direction, " ");
|
|
}
|
|
final public function limit(int $start, mixed $end = "", bool $offset = false): void
|
|
{
|
|
$this->_querys["LIMIT"] = " LIMIT {$start} " . $offset ? "OFFSET " : "," . $end;
|
|
}
|
|
|
|
//Result
|
|
protected function getResult($mode = PDO::FETCH_ASSOC): mixed
|
|
{
|
|
$query = "{$this->_querys['SELECT']} FROM {$this->getTable()} {$this->getWhere()} {$this->_querys['JOIN']} {$this->_querys['ORDERBY']}";
|
|
$stmt = $this->execute($query);
|
|
return $stmt->fetch($mode);
|
|
}
|
|
protected function getResults($mode = PDO::FETCH_ASSOC): mixed
|
|
{
|
|
$query = "{$this->_querys["SELECT"]} FROM {$this->getTable()} {$this->getWhere()} {$this->_querys['JOIN']} {$this->_querys['ORDERBY']}";
|
|
$query = "{$this->_querys['SELECT']} {$this->_querys['JOIN']} {$this->_querys['ORDERBY']}";
|
|
$stmt = $this->execute($query);
|
|
return $stmt->fetchAll($mode);
|
|
}
|
|
final public function countAllResults(string $column = "*", $reset = true): int
|
|
{
|
|
$this->_reset = $reset;
|
|
$query = "SELECT COUNT({$column}) FROM {$this->getTable()} {$this->getWhere()} {$this->_querys['JOIN']} {$this->_querys['ORDERBY']}";
|
|
$stmt = $this->execute($query);
|
|
$count = $stmt->fetchColumn(0);
|
|
$this->_reset = true;
|
|
return $count;
|
|
}
|
|
} //Class
|