You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

330 lines
9.3 KiB

  1. <?php
  2. class Db
  3. {
  4. var $result;
  5. var $default_link;
  6. var $master_link;
  7. var $active_master;
  8. /*
  9. function __construct($host, $user, $pass, $dbase, $port=false) {
  10. $this->setAccount($host, $user, $pass, $dbase, $port);
  11. $this->active_master = false;
  12. }
  13. */
  14. //CONFIG------------------------------------------------------------------------------
  15. function setAccount($host, $user, $pass, $dbase, $port=false) {
  16. $this->host = $host;
  17. $this->user = $user;
  18. $this->pass = $pass;
  19. $this->dbase = $dbase;
  20. $this->port = ($port !== false)? $port : 3306;
  21. }
  22. function setMasterAccount($host, $user, $pass, $dbase, $port=false) {
  23. $this->m_host = $host;
  24. $this->m_user = $user;
  25. $this->m_pass = $pass;
  26. $this->m_dbase = $dbase;
  27. $this->m_port = ($port !== false)? $port : 3306;
  28. }
  29. //CONNECT-----------------------------------------------------------------------------
  30. function connect() {
  31. $host = ($this->active_master)? $this->m_host : $this->host;
  32. $user = ($this->active_master)? $this->m_user : $this->user;
  33. $pass = ($this->active_master)? $this->m_pass : $this->pass;
  34. $dbase = ($this->active_master)? $this->m_dbase : $this->dbase;
  35. $port = ($this->active_master)? $this->m_port : $this->port;
  36. $link = $this->getConnectResource($host, $user, $pass, $dbase,$port);
  37. ($this->active_master)? $this->master_link = $link : $this->default_link = $link;
  38. }
  39. function getConnectResource($host, $user, $pass, $dbase,$port) {
  40. $conn = mysqli_connect($host, $user, $pass, $dbase,$port);
  41. if (!$conn) {
  42. //ERROR PROCESS
  43. $error_no = mysqli_connect_errno();
  44. $error_message = mysqli_connect_error();
  45. print "Error_Connect: {$error_no} : {$error_message}\n";
  46. exit();
  47. }
  48. return $conn;
  49. }
  50. function getConnect() {
  51. $this->checkConnect();
  52. return ($this->active_master)? $this->master_link : $this->default_link;
  53. }
  54. function changeMaster($flag) {
  55. $this->active_master = ($flag)? true : false;
  56. }
  57. function disconnect() {
  58. $conn = $this->getConnect();
  59. return mysqli_close($conn);
  60. }
  61. function checkConnect() {
  62. if (!$this->active_master && !$this->default_link) {
  63. $this->connect();
  64. } else if ($this->active_master && !$this->master_link) {
  65. $this->connect();
  66. }
  67. }
  68. //CONNECT-----------------------------------------------------------------------
  69. function executionSqlSelect($sql) {
  70. $conn = $this->getConnect();
  71. $this->result = mysqli_query($conn, $sql);
  72. }
  73. function executionSql($sql) {
  74. $conn = $this->getConnect();
  75. return mysqli_query($conn, $sql);
  76. }
  77. function getRow($sql) {
  78. $this->executionSqlSelect($sql);
  79. return ($this->result)? mysqli_fetch_assoc($this->result) : false;
  80. }
  81. function getMultiRow($sql, $setArrayPkey=false) {
  82. $this->executionSqlSelect($sql);
  83. if( $this->result == false ) {
  84. return false;
  85. }
  86. $array = array();
  87. while ($row = mysqli_fetch_assoc($this->result)) {
  88. if ($setArrayPkey) {
  89. $pkey = $row[$setArrayPkey];
  90. $array[$pkey] = $row;
  91. } else {
  92. $array[] = $row;
  93. }
  94. }
  95. return $array;
  96. }
  97. function escape($string) {
  98. $conn = $this->getConnect();
  99. if (is_array($string)) {
  100. foreach ($string as $key => $val) {
  101. $string[$key] = $this->escape($val);
  102. }
  103. } else {
  104. $string = mysqli_real_escape_string($conn, $string);
  105. }
  106. return $string;
  107. }
  108. function transactionExecution($sql) {
  109. $conn = $this->getConnect();
  110. mysqli_autocommit($conn, false);
  111. $query_success = true;
  112. if (is_array($sql)) {
  113. foreach ($sql as $query) {
  114. mysqli_query($conn, $query) ? null : $query_success = false;
  115. if (!$query_success) {
  116. break;
  117. }
  118. }
  119. } else {
  120. mysqli_query($conn, $sql) ? null : $query_success = false;
  121. }
  122. ($query_success) ? mysqli_commit($conn) : mysqli_rollback($conn);
  123. return $query_success;
  124. }
  125. function error() {
  126. $conn = $this->getConnect();
  127. $error_no = mysqli_errno($conn);
  128. $error_message = mysqli_error($conn);
  129. return "Error : {$error_no} : {$error_message}\n";
  130. }
  131. function selectDb($dbase) {
  132. $conn = $this->getConnect();
  133. return mysqli_select_db($conn, $dbase);
  134. }
  135. public function insertId() {
  136. $conn = $this->getConnect();
  137. return mysqli_insert_id($conn);
  138. }
  139. public function getAffectedCount() {
  140. $conn = $this->getConnect();
  141. return mysqli_affected_rows($conn);
  142. }
  143. public function getByKey($table, $key_name, $key_value) {
  144. $table = $this->escape($table);
  145. $key_name = $this->escape($key_name);
  146. $key_value = $this->escape($key_value);
  147. $sql = "SELECT * FROM {$table} WHERE {$key_name} = '{$key_value}'";
  148. return $this->getRow($sql);
  149. }
  150. public function isDuplicate($table, $checkArray, $exceptionArray) {
  151. $table = $this->escape($table);
  152. $checkArray = $this->escape($checkArray);
  153. $exceptionArray = $this->escape($exceptionArray);
  154. $tmp = array();
  155. foreach ($checkArray as $key => $val) {
  156. $tmp[] = "`{$key}` = '{$val}'";
  157. }
  158. $where = implode(' AND ', $tmp);
  159. $sql = "SELECT * FROM {$table} WHERE {$where}";
  160. if ($exceptionArray) {
  161. $exceptionArray = $this->escape($exceptionArray);
  162. $tmp = array();
  163. foreach ($exceptionArray as $key => $val) {
  164. $tmp[] = "`{$key}` = '{$val}'";
  165. }
  166. $not = implode(' OR ', $tmp);
  167. $sql .= " AND NOT($not)";
  168. }
  169. $res = $this->getRow($sql);
  170. return ($res)? true : false;
  171. }
  172. public function update($table, $array, $pkey_name, $pkey) {
  173. $array = $this->escape($array);
  174. $table = $this->escape($table);
  175. $pkey_name = $this->escape($pkey_name);
  176. $pkey = $this->escape($pkey);
  177. $tmp = array();
  178. foreach ($array as $col => $val) {
  179. //$tmp[] = (ctype_digit($val))? "`{$col}` = {$val}" : "`{$col}` = '{$val}'";
  180. $tmp[] = "`{$col}` = '{$val}'";
  181. }
  182. $setQuery = implode(',',$tmp);
  183. $pkey_name = "`{$pkey_name}`";
  184. if (!ctype_digit($pkey)) {
  185. $pkey = "'{$pkey}'";
  186. }
  187. $sql = '';
  188. $sql = "UPDATE {$table} "
  189. . " SET {$setQuery} "
  190. . " WHERE {$pkey_name} = {$pkey}";
  191. $res = $this->executionSql($sql);
  192. return ($res)? $this->getAffectedCount() : false;
  193. }
  194. public function update2($table, $array, $pkey_name, $pkey) {
  195. $array = $this->escape($array);
  196. $table = $this->escape($table);
  197. $pkey_name = $this->escape($pkey_name);
  198. $pkey = $this->escape($pkey);
  199. $tmp = array();
  200. foreach ($array as $col => $val) {
  201. //$tmp[] = (ctype_digit($val))? "`{$col}` = {$val}" : "`{$col}` = '{$val}'";
  202. $tmp[] = "`{$col}` = {$val}";
  203. }
  204. $setQuery = implode(',',$tmp);
  205. $pkey_name = "`{$pkey_name}`";
  206. if (!ctype_digit($pkey)) {
  207. $pkey = "'{$pkey}'";
  208. }
  209. $sql = '';
  210. $sql = "UPDATE {$table} "
  211. . " SET {$setQuery} "
  212. . " WHERE {$pkey_name} = {$pkey}";
  213. $res = $this->executionSql($sql);
  214. return ($res)? $this->getAffectedCount() : false;
  215. }
  216. public function insert($table, $array) {
  217. $array = $this->escape($array);
  218. $table = $this->escape($table);
  219. list($colQuery, $valQuery) = $this->generateInsertColVal($array);
  220. $sql = '';
  221. $sql = "INSERT INTO {$table} ({$colQuery}) "
  222. . " VALUES ($valQuery)";
  223. return $this->executionSql($sql);
  224. }
  225. public function delete($table, $pkey_name, $pkey) {
  226. $table = $this->escape($table);
  227. $pkey_name = $this->escape($pkey_name);
  228. $pkey = $this->escape($pkey);
  229. $pkey_name = "`{$pkey_name}`";
  230. if (!ctype_digit($pkey)) {
  231. $pkey = "'{$pkey}'";
  232. }
  233. $sql = " DELETE FROM {$table} "
  234. . " WHERE {$pkey_name} = {$pkey}";
  235. $res = $this->executionSql($sql);
  236. return ($res)? $this->getAffectedCount() : false;
  237. }
  238. public function delete2($table, $delete_where ) {
  239. $table = $this->escape($table);
  240. $sql = " DELETE FROM {$table} "
  241. . " WHERE {$delete_where}";
  242. $res = $this->executionSql($sql);
  243. return ($res)? $this->getAffectedCount() : false;
  244. }
  245. public function insertMulti($table, $multiArray, $return_sql=false) {
  246. $table = $this->escape($table);
  247. $multiArray = $this->escape($multiArray);
  248. $tmp = array();
  249. foreach ($multiArray as $array) {
  250. list($colQuery, $vals) = $this->generateInsertColVal($array);
  251. $tmp[] = "({$vals})";
  252. }
  253. $valueQuery = implode(",\n", $tmp);
  254. $sql = "INSERT INTO {$table} ({$colQuery}) \n"
  255. . " VALUES {$valueQuery}";
  256. return ($return_sql == false)? $this->executionSql($sql) : $sql;
  257. }
  258. /************************************************************************/
  259. /* GENERATE QUERY */
  260. /************************************************************************/
  261. private function generateInsertColVal($array) {
  262. $cols = array();
  263. $vals = array();
  264. foreach ($array as $col => $val) {
  265. $cols[] = "`{$col}`";
  266. //$vals[] = (ctype_digit($val))? "{$val}" : "'{$val}'";
  267. $vals[] = "'{$val}'";
  268. }
  269. return array(implode(',', $cols), implode(',', $vals));
  270. }
  271. }