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.
 
 
 
 
 

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