Last active
December 2, 2021 12:23
-
-
Save rkdmf0000/182b7bafcdb14da75b2deb9a85b9e485 to your computer and use it in GitHub Desktop.
원본
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
개인 프로젝트인 CMS 에서 사용되던 QUERY OOP 입니다. | |
<?php | |
class db extends webMarkerDB\loader | |
{ | |
public $db; | |
public $stats; | |
public static $table; | |
public function __construct() | |
{ | |
// if (!empty(self::$table)) { | |
// | |
// } else { | |
// | |
// } | |
} | |
private function query_error_stop($e=false,$dump=false) | |
{ | |
global $system,$_SYSTEM; | |
$file_name = @array_pop(explode('/',$e->getFile())); | |
//wm_die($_SYSTEM['ERR_MSG'][31].$dump,'DB_QUERY','정의되지않은 예외가 발생하였습니다('.($file_name).'/'.$e->getLine().':'.$e->getCode().')','javascript:history.back()'); | |
//TODO : 관리자만 에러메세지 표시되도록. | |
//$e->getMessage() | |
exit; | |
} | |
public function db_connect($db_info=false){ | |
try{ | |
global $system; | |
if($db_info ===false){ | |
$system->__db_connect_switch(false); | |
return; | |
} | |
$host = !empty($db_info['HOST']) ? $db_info['HOST'] : ''; | |
$name = !empty($db_info['NAME']) ? $db_info['NAME'] : ''; | |
$id = !empty($db_info['ID']) ? $db_info['ID'] : ''; | |
$password = !empty($db_info['PASSWORD']) ? $db_info['PASSWORD'] : ''; | |
$charset = !empty($db_info['CHARSET']) ? $db_info['CHARSET'] : 'utf8'; | |
$this->db = new PDO('mysql:host='.$host.';dbname='.$name.';charset='.$charset.'',''.$id.'',''.$password.'',array(3=>1)); | |
if($this->db)$system->__db_connect_switch(true); | |
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
}catch(PDOException $e){ | |
$system->__db_connect_switch(false); | |
return false; | |
} | |
return true; | |
} | |
public function query($query,$bind = array()) { | |
try { | |
global $system,$_SYSTEM; | |
$conn=$this->db; | |
if(!$conn)$system->__error(true); | |
$stmt = $conn->prepare($query); | |
$stmt->execute($bind); | |
return $stmt; | |
} catch (PDOException $e) { | |
$system->__error(true); | |
if ($_SYSTEM['USER']['user_level'] == 1) { | |
$this->query_error_stop($e,'<hr />'.$query); | |
} else { | |
$this->query_error_stop($e); | |
} | |
$this->query_error_stop($e); | |
} catch (PDOStatement $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (Exception $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (db $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} | |
} | |
public function fetchall($query=false,$bind=array()){ | |
global $_SYSTEM,$system; | |
if($_SYSTEM['__']['DB']['IS_CONNECT']!=1){return array();} | |
try { | |
return $this->query($query, $bind)->fetchAll(PDO::FETCH_ASSOC); | |
} catch (PDOException $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (PDOStatement $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (Exception $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (db $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} | |
} | |
public function fetchone($query=false,$bind=array()){ | |
global $_SYSTEM,$system; | |
if($_SYSTEM['__']['DB']['IS_CONNECT']!=1){return array();} | |
try{ | |
$data = $this->fetchall($query, $bind); | |
return isset($data[0]) ? $data[0] : NULL; | |
} catch (PDOException $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (PDOStatement $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (Exception $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (db $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} | |
} | |
public function ufetchone($query=false,$bind=array()){ | |
global $_SYSTEM,$system; | |
if($_SYSTEM['__']['DB']['IS_CONNECT']!=1){return array();} | |
try{ | |
$data = $this->fetchall($query, $bind); | |
return isset($data[(count($data)-1)]) ? $data[(count($data)-1)] : NULL; | |
} catch (PDOException $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (PDOStatement $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (Exception $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (db $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} | |
} | |
public function fetchrow($query=false,$bind=array()){ | |
global $_SYSTEM,$system; | |
if($_SYSTEM['__']['DB']['IS_CONNECT']!=1){return array();} | |
try{ | |
return $this->query($query, $bind)->rowCount(); | |
} catch (PDOException $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (PDOStatement $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (Exception $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} catch (db $e) { | |
$system->__error(true); | |
$this->query_error_stop($e); | |
} | |
} | |
public function last_insert_id(){ | |
return $this->db->lastInsertId(); | |
} | |
public function get_connection() { | |
return $this->db; | |
} | |
public function close(){ | |
$this->db = NULL; | |
} | |
private $attributes = array( | |
"AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS", | |
"ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION", | |
"TIMEOUT" | |
); | |
public function get_all_stats() | |
{ | |
$errcnt = 0; | |
try { | |
foreach ($this->attributes as $val) { | |
echo "PDO::ATTR_$val: "; | |
echo $this->db->getAttribute(constant("PDO::ATTR_$val"))."\n"; | |
} | |
} catch (PDOException $e) { | |
$errcnt++; | |
} | |
echo "\nErrCnt : ".$errcnt; | |
} | |
public function get_stats($flush=false) | |
{ | |
if (!$this->stats || $flush === true) $this->stats = $this->db->getAttribute(PDO::ATTR_CONNECTION_STATUS); | |
return $this->stats; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment