Skip to content

Instantly share code, notes, and snippets.

@rkdmf0000
Last active December 2, 2021 12:20
Show Gist options
  • Save rkdmf0000/48e423911e6bac66eb377be13f9e22f0 to your computer and use it in GitHub Desktop.
Save rkdmf0000/48e423911e6bac66eb377be13f9e22f0 to your computer and use it in GitHub Desktop.
db_pdo.php 에서 Extend 대상
<?php
/*
* TITLE : webmarker
*/
namespace webMarkerDB;
/*
* ###########################################################################################
* ###########################################################################################
*/
/*
* ###########################################################################################
* ###########################################################################################
*/
/*
* TITLE : 요청 전 데이터를 가공하거나 환경데이터를 조작 할 수 있는 클레스
*/
abstract class loader
{
public static $requestCollectionList = array();
public function prepare($tableName)
{
try {
$tableName = (!empty($tableName) ? $tableName : '');
$cnt = $this->getCollectionCnt();
$ins = new request(new defaultStructure($tableName,$cnt));
$this->insertCollection($ins);
return $ins;
} catch (Exception $e) {
return false;
}
}
public function insertCollection(request $data)
{
try {
if (!empty($data)) {
$cnt = $this->getCollectionCnt();
self::$requestCollectionList[$cnt] = $data;
return (int)$cnt;
} else {
return -1;
}
} catch (Exception $e) {
return -1;
}
}
public function getCollectionCnt()
{
return (int)count(self::$requestCollectionList);
}
}
/*
* ###########################################################################################
* ###########################################################################################
*/
/*
* TITLE : 요청 데이터를 처리 가능한 메서드를 포함하는 메서드 클레스
* DESC : loader\prepare 메서드를 통하여 반환된다.
*/
class request
{
private $globals = NULL;
public $status = NULL;
public static $queryHeader = array(
'undefined', 'insert', 'delete', 'update', 'select'
);
public function __construct(defaultStructure $std)
{
$this->globals = $std;
return $this;
}
//rid가 없는경우 입력가능
public function setRid($id)
{
try {
$id = intval($id);
if ($id != -1) {
$rid = $this->globals->rid;
if ($rid == -1) {
$this->globals->rid = $id;
} else {
return false;
}
} else {
return false;
}
} catch (Exception $e) {
return false;
}
}
public function action($action)
{
$querys = self::$queryHeader;
try {
if (array_search($action,$querys)) {
$this->globals->queryHeader = $action;
return $this;
} else {
throw new WMException('Please check again action in "queryHeader" variable type');
}
} catch (WMException $e) {
echo $e;
}
}
public function selector($string='*')
{
$this->globals->querySelector = $string;
}
public function where($array=array(),$type='AND',$chain='AND')
{
try {
if (is_array($array)) {
$this->globals->where[] = array('type'=>$type,'value'=>$array,'chain'=>$chain,'operator'=>'=');
return $this;
} else {
throw new WMException('Please check again where in "where" variable type');
}
} catch (WMException $e) {
echo $e;
} catch (Exception $e) {
echo $e;
}
}
public function whereLike($array=array(),$type='AND',$chain='AND')
{
try {
if (is_array($array)) {
$this->globals->where[] = array('type'=>$type,'value'=>$array,'chain'=>$chain,'operator'=>'LIKE');
return $this;
} else {
throw new WMException('Please check again whereLike in "where" variable type');
}
} catch (WMException $e) {
echo $e;
} catch (Exception $e) {
echo $e;
}
}
public function whereBetween($array=array(),$type='AND',$chain='AND')
{
try {
if (is_array($array)) {
$this->globals->where[] = array('type'=>$type,'value'=>$array,'chain'=>$chain,'operator'=>'BETWEEN');
return $this;
} else {
throw new WMException('Please check again whereLike in "where" variable type');
}
} catch (WMException $e) {
echo $e;
} catch (Exception $e) {
echo $e;
}
}
public function whereUni($array=array(),$type='AND',$chain='AND')
{
try {
if (is_array($array)) {
$this->globals->where[] = array('type'=>$type,'value'=>$array,'chain'=>$chain,'operator'=>'*');
return $this;
} else {
throw new WMException('Please check again whereLike in "where" variable type');
}
} catch (WMException $e) {
echo $e;
} catch (Exception $e) {
echo $e;
}
}
public function limit($min=0,$length=0)
{
$this->globals->selectLimitMin = (!empty($min) ? intval($min) : 0);
$this->globals->selectLimitMax = (!empty($length) ? intval($length) : NULL);
return $this;
}
public function order($orderKey=array(),$direction=null)
{
try {
if (is_array($orderKey)) {
$this->globals->orderKey = implode(',',$orderKey);
} else {
$this->globals->orderKey = $orderKey;
}
if (!empty($this->globals->orderKey)) {
switch (strtolower(trim($direction))) {
case 'asc':
$this->globals->orderBy = 'ASC';
break;
case 'desc':
$this->globals->orderBy = 'DESC';
break;
default:
$this->globals->orderKey = null;
$this->globals->orderBy = null;
throw new WMException('Please check again order in "direction" parameter type');
break;
}
} else {
throw new WMException('Please check again order in "orderKey" parameter type');
}
return $this;
} catch (WMException $e) {
echo $e;
} catch (Exception $e) {
echo $e;
}
}
public function defaultSetValueIsNULL($null = false)
{
$this->globals->defaultSetValueIsNULL = ($null == true ? true : false);
}
public function set($array = array())
{
try {
if (is_array($array)) {
$this->globals->set = $array;
return $this;
} else {
throw new WMException('Please check again set in "array" parameter type');
}
} catch (WMException $e) {
echo $e;
} catch (Exception $e) {
echo $e;
}
}
private function whereSetter()
{
$query_array = '';
if (is_array($this->globals->where) && !empty($this->globals->where)) {
$query_array.= 'WHERE';
$group_idx = 0;
$group_cnt = count($this->globals->where);
foreach ($this->globals->where as $c=>$fields) {
if (empty($fields)) continue;
$query_array.= '(';
$where_stack = 0;
foreach ($fields['value'] as $k=>$d) {
//Custom Operator
if ($fields['operator'] == '*') {
$query_array.= ($where_stack>0 ? $fields['type'].' ' : '').sprintf('`%s` %s "%s"',$k,(!empty($d[0]) ? $d[0] : '='),$d[1]);
} else if($fields['operator'] == 'BETWEEN') {
$query_array.= ($where_stack>0 ? $fields['type'].' ' : '').sprintf('`%s` %s "%s" AND "%s"',$k,'BETWEEN',$d[0],$d[1]);
} else {
$query_array.= ($where_stack>0 ? $fields['type'].' ' : '').sprintf('`%s` %s "%s"',$k,(!empty($fields['operator']) ? $fields['operator'] : '='),$d);
}
$where_stack++;
}
$query_array.= ')';
if (!empty($this->globals->where[($group_idx+1)])) $query_array.= ' '.$fields['chain'].' ';
$group_idx++;
}
}
return $query_array;
}
private function setSetter()
{
$query_array = '';
if (is_array($this->globals->set) && !empty($this->globals->set)) {
$query_array.= 'SET';
$set_stack = 0;
foreach ($this->globals->set as $key=>$value) {
if ($this->globals->defaultSetValueIsNULL === false) {
$query_array.= ($set_stack>0 ? ' , ' : '').sprintf('`%s` = "%s"',$key,$value);
} else {
if (!empty($value)) {
$query_array.= ($set_stack>0 ? ' , ' : '').sprintf('`%s` = "%s"',$key,$value);
} else {
$query_array.= ($set_stack>0 ? ' , ' : '').sprintf('`%s` = NULL',$key);
}
}
$set_stack++;
}
}
return $query_array;
}
public function go($opt=array())
{
$query_array = array();
$query_array[] = '/*WMB - QUERY OOP REQUEST [START]*/';
$query_array[] = strtoupper(strval($this->globals->queryHeader));
$partition = '';
switch ($this->globals->queryHeader) {
case 'select':
$query_array[] = $this->globals->querySelector;
$query_array[] = 'FROM';
$query_array[] = $this->globals->tableName;
$query_array[] = $this->whereSetter();
if (isset($this->globals->orderKey) && isset($this->globals->orderBy)) {
$query_array[] = 'ORDER BY';
$query_array[] = $this->globals->orderKey;
$query_array[] = $this->globals->orderBy;
}
if (isset($this->globals->selectLimitMin)) {
$query_array[] = 'LIMIT';
if (isset($this->globals->selectLimitMin) && isset($this->globals->selectLimitMax)) {
$query_array[] = $this->globals->selectLimitMin.','.$this->globals->selectLimitMax;
} else if(isset($this->globals->selectLimitMin) && !isset($this->globals->selectLimitMax)) {
$query_array[] = $this->globals->selectLimitMin;
} else if(!isset($this->globals->selectLimitMin) && isset($this->globals->selectLimitMax)) {
$query_array[] = '0,'.$this->globals->selectLimitMax;
}
}
break;
case 'update':
$query_array[] = $this->globals->tableName;
$query_array[] = $this->setSetter();
$query_array[] = $this->whereSetter();
break;
case 'insert':
$query_array[] = 'INTO';
$query_array[] = $this->globals->tableName;
$query_array[] = $this->setSetter();
break;
case 'delete':
$query_array[] = 'FROM';
$query_array[] = $this->globals->tableName;
$query_array[] = $this->whereSetter();
break;
}
$query_array[] = '/*WMB - QUERY OOP REQUEST [END]*/';
if ($opt['query'] == true) {
return implode(' ',$query_array);
}
$query_ready = implode(' ',$query_array);
if ($opt['maintain'] == true) {
return new result($query_ready,$this->globals->queryHeader);
} else {
return new result($query_ready,$this->globals->queryHeader,function() {
$this->garbageCollect();
});
}
}
public function garbageCollect()
{
$this->globals->tableName = NULL;
$this->globals->queryHeader = NULL;
$this->globals->querySelector = NULL;
$this->globals->where = NULL;
$this->globals->orderKey = NULL;
$this->globals->orderBy = NULL;
$this->globals->set = NULL;
$this->globals->selectJoint = NULL;
$this->globals->selectLimitMin = NULL;
$this->globals->selectLimitMax = NULL;
$this->globals->insertInto = NULL;
$this->globals = NULL;
unset($this->globals->tableName,$this->globals->queryHeader,$this->globals->querySelector);
unset($this->globals->orderKey,$this->globals->orderBy,$this->globals->where,$this->globals->set,$this->globals->selectJoint);
unset($this->globals->selectLimitMin,$this->globals->selectLimitMax,$this->globals->insertInto);
unset($this->globals);
$this->status = 'deleted';
}
}
/*
* ###########################################################################################
* ###########################################################################################
*/
/*
* TITLE : 요청 데이터를 보관하는 구조체 클레스
*/
class defaultStructure
{
const VERSION = '0.0.0.1';
/*default*/
private $rid = -1;
public $tableName = '';
public $queryHeader = '';
public $querySelector = '*';
/*select*/
public $where = array();
public $set = array();
public $orderBy = array();
public $selectJoint = array(); //not using
public $selectLimitMin = NULL;
public $selectLimitMax = NULL;
public $selectGroup = array(); //not using
public $defaultSetValueIsNULL = false;
/*insert*/
public $insertInto = array(); //not using
public function __construct($tableName,$rid)
{
$this->rid = $rid;
$this->tableName = $tableName;
}
}
/*
* ###########################################################################################
* ###########################################################################################
*/
/*
* TITLE : 결과 데이터를 처리하는 메서드를 포함하는 클레스
*/
class result
{
private $query = '';
private $mysqlc = '';
private $query_header = '';
public function __construct($query,$query_header,$pack_garbage = null)
{
$this->query = $query;
$this->query_header = $query_header;
global $mysql;
$this->mysqlc = $mysql;
if (!empty($pack_garbage)) $pack_garbage();
}
public function __invoke($head = null)
{
try {
if ($head === null) {
throw new WMException('Invalid parameter');
}
if (!method_exists($this,$head)) {
throw new WMException('Method not exists');
}
return $this->$head();
} catch (WMException $e) {
return false;
}
}
public function query()
{
if ($this->query_header != 'select') {
if ($this->mysqlc->query($this->query)) {
return $this->mysqlc->last_insert_id();
}
return false;
} else {
return false;
}
}
public function fetchall()
{
if ($this->query_header == 'select') {
return $this->mysqlc->fetchall($this->query);
} else {
return false;
}
}
public function fetchone()
{
if ($this->query_header == 'select') {
return $this->mysqlc->fetchone($this->query);
} else {
return false;
}
}
public function fetchrow()
{
if ($this->query_header == 'select') {
return $this->mysqlc->fetchrow($this->query);
} else {
return false;
}
}
}
/*
* ###########################################################################################
* ###########################################################################################
*/
/*
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct($message = null, $code = 0, Exception $previous = null);
final private function __clone(); // Inhibits cloning of exceptions.
final public function getMessage(); // message of exception
final public function getCode(); // code of exception
final public function getFile(); // source filename
final public function getLine(); // source line
final public function getTrace(); // an array of the backtrace()
final public function getPrevious(); // previous exception
final public function getTraceAsString(); // formatted string of trace
// Overrideable
public function __toString(); // formatted string for display
*/
use Exception;
class WMException extends Exception
{
public static $errCnt = 0;
public static $errorType = array(
//negative
'-1' => 'webMarkerDB Undefined Error',
'0' => 'webMarkerDB Notice',
'1' => 'webMarkerDB Alert',
'2' => 'webMarkerDB Danger',
'3' => 'webMarkerDB Warning',
'4' => 'webMarkerDB Fetal Error',
'6' => 'webMarkerDB Died',
//positive
'7' => 'webMarkerDB Message',
'8' => 'webMarkerDB Success'
);
//super override
public function __construct($message = null, $code = 0, Exception $previous = null)
{
self::$errCnt++;
parent::__construct($message, $code, $previous);
}
public function WM_getMessage()
{
}
}
/*
* ###########################################################################################
* ###########################################################################################
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment