|
- <?php
- class Db
- {
- var $result;
- var $default_link;
- var $master_link;
- var $active_master;
-
- /*
- function __construct($host, $user, $pass, $dbase, $port=false) {
- $this->setAccount($host, $user, $pass, $dbase, $port);
- $this->active_master = false;
- }
- */
-
- //CONFIG------------------------------------------------------------------------------
- function setAccount($host, $user, $pass, $dbase, $port=false) {
- $this->host = $host;
- $this->user = $user;
- $this->pass = $pass;
- $this->dbase = $dbase;
- $this->port = ($port !== false)? $port : 3306;
- }
-
- function setMasterAccount($host, $user, $pass, $dbase, $port=false) {
- $this->m_host = $host;
- $this->m_user = $user;
- $this->m_pass = $pass;
- $this->m_dbase = $dbase;
- $this->m_port = ($port !== false)? $port : 3306;
- }
-
- //CONNECT-----------------------------------------------------------------------------
- function connect() {
- $host = ($this->active_master)? $this->m_host : $this->host;
- $user = ($this->active_master)? $this->m_user : $this->user;
- $pass = ($this->active_master)? $this->m_pass : $this->pass;
- $dbase = ($this->active_master)? $this->m_dbase : $this->dbase;
-
- $link = $this->getConnectResource($host, $user, $pass, $dbase);
- ($this->active_master)? $this->master_link = $link : $this->default_link = $link;
- }
-
- function getConnectResource($host, $user, $pass, $dbase) {
- $conn = mysqli_connect($host, $user, $pass, $dbase);
- if (!$conn) {
- //ERROR PROCESS
- $error_no = mysqli_connect_errno();
- $error_message = mysqli_connect_error();
- print "Error_Connect: {$error_no} : {$error_message}\n";
- exit();
- }
- return $conn;
- }
-
- function getConnect() {
- $this->checkConnect();
- return ($this->active_master)? $this->master_link : $this->default_link;
- }
-
- function changeMaster($flag) {
- $this->active_master = ($flag)? true : false;
- }
-
- function disconnect() {
- $conn = $this->getConnect();
- return mysqli_close($conn);
- }
-
- function checkConnect() {
- if (!$this->active_master && !$this->default_link) {
- $this->connect();
-
- } else if ($this->active_master && !$this->master_link) {
- $this->connect();
- }
- }
-
- //CONNECT-----------------------------------------------------------------------
- function executionSqlSelect($sql) {
- $conn = $this->getConnect();
- $this->result = mysqli_query($conn, $sql);
- }
-
- function executionSql($sql) {
- $conn = $this->getConnect();
- return mysqli_query($conn, $sql);
- }
-
- function getRow($sql) {
- $this->executionSqlSelect($sql);
- return ($this->result)? mysqli_fetch_assoc($this->result) : false;
- }
-
- function getMultiRow($sql, $setArrayPkey=false) {
- $this->executionSqlSelect($sql);
- if( $this->result == false ) {
- return false;
- }
- $array = array();
- while ($row = mysqli_fetch_assoc($this->result)) {
- if ($setArrayPkey) {
- $pkey = $row[$setArrayPkey];
- $array[$pkey] = $row;
- } else {
- $array[] = $row;
- }
- }
- return $array;
- }
-
- function escape($string) {
- $conn = $this->getConnect();
- if (is_array($string)) {
- foreach ($string as $key => $val) {
- $string[$key] = $this->escape($val);
- }
- } else {
- $string = mysqli_real_escape_string($conn, $string);
- }
-
- return $string;
- }
-
- function transactionExecution($sql) {
- $conn = $this->getConnect();
- mysqli_autocommit($conn, false);
- $query_success = true;
-
- if (is_array($sql)) {
- foreach ($sql as $query) {
- mysqli_query($conn, $query) ? null : $query_success = false;
- if (!$query_success) {
- break;
- }
- }
- } else {
- mysqli_query($conn, $sql) ? null : $query_success = false;
- }
-
- ($query_success) ? mysqli_commit($conn) : mysqli_rollback($conn);
- return $query_success;
- }
-
- function error() {
- $conn = $this->getConnect();
- $error_no = mysqli_errno($conn);
- $error_message = mysqli_error($conn);
- return "Error : {$error_no} : {$error_message}\n";
- }
-
- function selectDb($dbase) {
- $conn = $this->getConnect();
- return mysqli_select_db($conn, $dbase);
- }
-
- public function insertId() {
- $conn = $this->getConnect();
- return mysqli_insert_id($conn);
- }
-
- public function getAffectedCount() {
- $conn = $this->getConnect();
- return mysqli_affected_rows($conn);
- }
-
- public function getByKey($table, $key_name, $key_value) {
- $table = $this->escape($table);
- $key_name = $this->escape($key_name);
- $key_value = $this->escape($key_value);
- $sql = "SELECT * FROM {$table} WHERE {$key_name} = '{$key_value}'";
-
- return $this->getRow($sql);
- }
-
- public function isDuplicate($table, $checkArray, $exceptionArray) {
- $table = $this->escape($table);
- $checkArray = $this->escape($checkArray);
- $exceptionArray = $this->escape($exceptionArray);
-
- $tmp = array();
- foreach ($checkArray as $key => $val) {
- $tmp[] = "`{$key}` = '{$val}'";
- }
- $where = implode(' AND ', $tmp);
- $sql = "SELECT * FROM {$table} WHERE {$where}";
-
- if ($exceptionArray) {
- $exceptionArray = $this->escape($exceptionArray);
-
- $tmp = array();
- foreach ($exceptionArray as $key => $val) {
- $tmp[] = "`{$key}` = '{$val}'";
- }
- $not = implode(' OR ', $tmp);
- $sql .= " AND NOT($not)";
- }
- $res = $this->getRow($sql);
- return ($res)? true : false;
- }
-
- public function update($table, $array, $pkey_name, $pkey) {
-
- $array = $this->escape($array);
- $table = $this->escape($table);
- $pkey_name = $this->escape($pkey_name);
- $pkey = $this->escape($pkey);
-
- $tmp = array();
- foreach ($array as $col => $val) {
- //$tmp[] = (ctype_digit($val))? "`{$col}` = {$val}" : "`{$col}` = '{$val}'";
- $tmp[] = "`{$col}` = '{$val}'";
- }
- $setQuery = implode(',',$tmp);
-
- $pkey_name = "`{$pkey_name}`";
- if (!ctype_digit($pkey)) {
- $pkey = "'{$pkey}'";
- }
-
- $sql = '';
- $sql = "UPDATE {$table} "
- . " SET {$setQuery} "
- . " WHERE {$pkey_name} = {$pkey}";
-
- $res = $this->executionSql($sql);
- return ($res)? $this->getAffectedCount() : false;
- }
-
- public function update2($table, $array, $pkey_name, $pkey) {
-
- $array = $this->escape($array);
- $table = $this->escape($table);
- $pkey_name = $this->escape($pkey_name);
- $pkey = $this->escape($pkey);
-
- $tmp = array();
- foreach ($array as $col => $val) {
- //$tmp[] = (ctype_digit($val))? "`{$col}` = {$val}" : "`{$col}` = '{$val}'";
- $tmp[] = "`{$col}` = {$val}";
- }
- $setQuery = implode(',',$tmp);
-
- $pkey_name = "`{$pkey_name}`";
- if (!ctype_digit($pkey)) {
- $pkey = "'{$pkey}'";
- }
-
- $sql = '';
- $sql = "UPDATE {$table} "
- . " SET {$setQuery} "
- . " WHERE {$pkey_name} = {$pkey}";
-
- $res = $this->executionSql($sql);
- return ($res)? $this->getAffectedCount() : false;
- }
-
- public function insert($table, $array) {
- $array = $this->escape($array);
- $table = $this->escape($table);
-
- list($colQuery, $valQuery) = $this->generateInsertColVal($array);
-
- $sql = '';
- $sql = "INSERT INTO {$table} ({$colQuery}) "
- . " VALUES ($valQuery)";
-
- return $this->executionSql($sql);
- }
-
- public function delete($table, $pkey_name, $pkey) {
- $table = $this->escape($table);
- $pkey_name = $this->escape($pkey_name);
- $pkey = $this->escape($pkey);
-
- $pkey_name = "`{$pkey_name}`";
- if (!ctype_digit($pkey)) {
- $pkey = "'{$pkey}'";
- }
-
- $sql = " DELETE FROM {$table} "
- . " WHERE {$pkey_name} = {$pkey}";
-
- $res = $this->executionSql($sql);
- return ($res)? $this->getAffectedCount() : false;
- }
-
- public function delete2($table, $delete_where ) {
- $table = $this->escape($table);
-
- $sql = " DELETE FROM {$table} "
- . " WHERE {$delete_where}";
-
- $res = $this->executionSql($sql);
- return ($res)? $this->getAffectedCount() : false;
- }
-
- public function insertMulti($table, $multiArray, $return_sql=false) {
- $table = $this->escape($table);
- $multiArray = $this->escape($multiArray);
-
- $tmp = array();
- foreach ($multiArray as $array) {
- list($colQuery, $vals) = $this->generateInsertColVal($array);
- $tmp[] = "({$vals})";
- }
- $valueQuery = implode(",\n", $tmp);
-
- $sql = "INSERT INTO {$table} ({$colQuery}) \n"
- . " VALUES {$valueQuery}";
-
- return ($return_sql == false)? $this->executionSql($sql) : $sql;
- }
-
- /************************************************************************/
- /* GENERATE QUERY */
- /************************************************************************/
- private function generateInsertColVal($array) {
- $cols = array();
- $vals = array();
- foreach ($array as $col => $val) {
- $cols[] = "`{$col}`";
- //$vals[] = (ctype_digit($val))? "{$val}" : "'{$val}'";
- $vals[] = "'{$val}'";
- }
- return array(implode(',', $cols), implode(',', $vals));
- }
-
- }
|