Advertisement
Guest User

phpis shit

a guest
Aug 10th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. <?php
  2. class queryBuilder
  3. {
  4. const MYSQL_ERR = -1;
  5. const SUCCESS = 0;
  6. const QUERY_ERR = 1;
  7.  
  8. const UNKNOWN_TYPE = 11;
  9. const SIZE_NOT_MATCHED = 12;
  10.  
  11.  
  12. function __construct($auth_host,$auth_user,$auth_password,$auth_db)
  13. {
  14. $this->auth_host = $auth_host;
  15. $this->auth_user = $auth_user;
  16. $this->auth_password = $auth_password;
  17. $this->auth_db = $auth_db;
  18. $this->sql = "";
  19.  
  20. $this->mysqli = new Mysqli($auth_host,$auth_user,$auth_password,$auth_db);
  21. }
  22. private function openDb(){
  23. if (!$this->mysqli->ping()) {
  24. $this->mysqli = new Mysqli($auth_host,$auth_user,$auth_password,$auth_db);
  25. }
  26. }
  27. private function closeDb(){
  28. if ($this->mysqli->ping()) {
  29. $this->mysqli->close();
  30. }
  31. }
  32. private function isTableExist(){
  33. if (!empty($this->table)) {
  34. throw new Exception("table is not set", $this::QUERY_ERR);
  35.  
  36. }
  37. }
  38. protected function getSqlResult($sql){
  39. if ($this->mysqli->ping()) {
  40. $res = $this->mysqli->query($sql);
  41. if (!is_bool($res)) {
  42. $ret = array_merge($res->fetch_array(),array('status' => $this::SUCCESS));
  43. }
  44. else{
  45. $ret = array('status' => $this::QUERY_ERR,'info' => $this->mysqli->error);
  46. }
  47. return $ret;
  48. }
  49. else{
  50. throw new Exception("Error Connect Mysql [error : ".$this->mysqli->error."]", MYSQL_ERR);
  51. }
  52. }
  53.  
  54. private function kvCommand($command,,$keys,$values){
  55. if (is_array($keys) && is_array($values) && !empty($command)){
  56. isTableExist();
  57. $keyCount = count($keys);
  58. $valueCount = count($values);
  59. if ($keyCount > 0 && $valueCount > 0 && $valueCount <= $keyCount) {
  60. $setValues = "";
  61. for ($index = 0; $index < $keyCount; $index++) {
  62. $values[$index] = is_string($values[$index]) ? "'".$values[$index]."'" : $values[$index];
  63. $setValues .= "`".$keys[$index]."` = ".$values[$index];
  64. if($index < $keyCount - 1){
  65. $setValues .= " AND ";
  66. }
  67. };
  68. $sql = $command." ".$setValues."";
  69. return $sql;
  70. }
  71. else{
  72. throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
  73. }
  74. }
  75. else{
  76. throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
  77. }
  78. }
  79. private function upadteCommand($command,$operator,$keys,$values){
  80. if (is_array($keys) && is_array($values) && !empty($command)){
  81. isTableExist();
  82. $keyCount = count($keys);
  83. $valueCount = count($values);
  84. if ($keyCount > 0 && $valueCount > 0 && $valueCount <= $keyCount) {
  85. $setValues = "";
  86. for ($index = 0; $index < $keyCount; $index++) {
  87. $values[$index] = is_string($values[$index]) ? "'".$values[$index]."'" : $values[$index];
  88. $setValues .= "`".$keys[$index]."` = ".$values[$index];
  89. if($index < $keyCount - 1){
  90. $setValues .= ", ";
  91. }
  92. };
  93. $sql = $command." `".$this->table."` ".$operator." ".$setValues."";
  94. return $sql;
  95. }
  96. else{
  97. throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
  98. }
  99. }
  100. else{
  101. throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
  102. }
  103. }
  104. private function insertCommand($command,$operator,$keys,$values){
  105. if (is_array($keys) && is_array($values) && !empty($command)){
  106. isTableExist();
  107. $keyCount = count($keys);
  108. $valueCount = count($values);
  109. if ($keyCount > 0 && $valueCount > 0 && $valueCount === $keyCount) {
  110. $column = "`".(is_array($keys) ? join($keys,"`,`") : $keys)."`";
  111. $values = (is_array($keys) ? join($keys,",") : $keys);
  112.  
  113. $sql = $command." `".$this->table."` (".$column.") ".$operator." (".$values.")";
  114. return $sql;
  115. }
  116. else{
  117. throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
  118. }
  119. }
  120. else{
  121. throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
  122. }
  123. }
  124. function query($sql){
  125. return $this->getSqlResult($sql);
  126. }
  127. function setTable($table){
  128. $this->table = $table;
  129. return $this;
  130. }
  131. function select(...$column){
  132. $this->isTableExist();
  133. $column = "`".(is_array($column) ? join($column,"`,`") : $column)."`";
  134. $this->sql += "SELECT `".$column." FROM `".$this->table."`";
  135. $this->mode = "select";
  136. return $this;
  137. }
  138. function insert($keys,$values){
  139. $this->sql += insertCommand("INSERT INTO","VALUES",$keys,$values);
  140. $this->mode = "insert";
  141. return $this;
  142. }
  143. function delete(){
  144.  
  145. $this->isTableExist();
  146. $this->sql += "DELETE FROM `".$this->table."`";
  147. $this->mode = "delete";
  148. return $this;
  149. }
  150. function update($keys,$values){
  151. $this->sql += upadteCommand("UPDATE","SET",$keys,$values);
  152. $this->mode = "update";
  153. return $this;
  154. }
  155. function limit(){
  156. $arg = func_get_args();
  157. $countPreifx = (!empty($arg[0])) ? "LIMIT ".$arg[0] : "";
  158. $limitPreifx = (!empty($arg[1])) ? ",".$arg[1] : "";
  159. $this->sql += $countPreifx.$limitPreifx;
  160. }
  161. function where($keys,$values){
  162. $this->sql += kvCommand("WHERE",$keys,$values);
  163. return $this;
  164. }
  165. function exec(){
  166. return $this->getSqlResult($this->sql);
  167. $this->table = null;
  168. }
  169. function __destruct()
  170. {
  171. $this->closeDb();
  172. }
  173. }
  174. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement