dbms_primeidc/extdbms/lib/Core/Model.php
2025-03-21 18:48:48 +09:00

116 lines
3.6 KiB
PHP

<?php
namespace lib\Core;
use lib\Core\Entity;
use \PDO;
use PDOStatement;
abstract class Model
{
private $_db = null;
private $_debug = false;
private $_column = "*";
private $_wheres = [];
protected function __construct() {} //
abstract public function getTable();
abstract public function getEntity(): Entity;
abstract public function getEntitys(): array;
final public function getConnect(): PDO
{
if ($this->_db === null) {
$envs = parse_ini_file("./env.ini", true);
if (!$envs) {
throw new \Exception(var_export($envs, true));
}
//echo var_dump($envs);exit;
$dsn = sprintf("%s:host=%s;dbname=%s;charset=%s", $envs['db']['driver'], $envs['db']['host'], $envs['db']['name'], $envs['db']['charset']);
$this->_db = new PDO($dsn, $envs['db']['id'], $envs['db']['passwd']);
$this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $this->_db;
}
final public function setDebug($debug)
{
$this->_debug = $debug;
}
final public function getDebug()
{
return $this->_debug;
}
final public function where(array $wheres, $condition = "AND")
{
foreach ($wheres as $key => $value) {
if ($value !== "") {
$value = "='{$value}'";
}
$this->_wheres[] = sprintf("%s%s%s", count($this->_wheres) ? " " . $condition . " " : "", $key, $value);
}
}
final public function orWhere(array $wheres)
{
$this->where($wheres, "OR");
}
final public function getWhere()
{
return count($this->_wheres) ? "WHERE " . implode(" ", $this->_wheres) : "";
}
final public function execute($sql): bool|PDOStatement
{
$stmt = $this->getConnect()->prepare($sql);
$stmt->execute();
$this->_wheres = [];
$this->_column = "*";
return $stmt;
}
//CUD문
final public function create($datas)
{
$sql = sprintf("INSERT INTO %s VALUES(%s) %s", $this->getTable(), implode(",", $datas), $this->getWhere());
return $this->execute($sql);
} //
final public function modify($datas)
{
$sql = sprintf("UPDATE %s SET %s %s", $this->getTable(), implode(",", $datas), $this->getWhere());
return $this->execute($sql);
} //
final public function delete()
{
$sql = sprintf("DELETE FROM %s %s", $this->getTable(), $this->getWhere());
return $this->execute($sql);
} //
//Select문
final public function setColumn($column)
{
$this->_column = $column;
}
final public function getColumn()
{
return $this->_column;
}
final protected function getRow($mode = PDO::FETCH_OBJ)
{
$sql = sprintf("SELECT %s FROM %s WHERE %s", $this->getColumn(), $this->getTable(), $this->getWhere());
$stmt = $this->execute($sql);
return $stmt->fetch($mode);
}
final protected function getRows($mode = PDO::FETCH_OBJ)
{
$sql = sprintf("SELECT %s FROM %s WHERE %s", $this->getColumn(), $this->getTable(), $this->getWhere());
$stmt = $this->execute($sql);
return $stmt->fetchAll($mode);
}
final protected function getSelectColumn($column, int $return_column = 0)
{
$sql = sprintf("SELECT %s FROM %s WHERE %s", $column, $this->getTable(), $this->getWhere());
$stmt = $this->execute($sql);
return $stmt->fetchColumn($return_column);
}
} //Class