Advertisement
Guest User

phpis shit

a guest
Aug 10th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 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.  
  55. private function upadteCommand($command,$operator,$keys,$values){
  56. if (is_array($keys) && is_array($values) && !empty($command)){
  57. isTableExist();
  58. $keyCount = count($keys);
  59. $valueCount = count($values);
  60. if ($keyCount > 0 && $valueCount > 0 && $valueCount <= $keyCount) {
  61. $setValues = "";
  62. for ($index = 0; $index < $keyCount; $index++) {
  63. $values[$index] = is_string($values[$index]) ? "'".$values[$index]."'" : $values[$index];
  64. $setValues .= "`".$keys[$index]."` = ".$values[$index];
  65. if($index < $keyCount - 1){
  66. $setValues .= ", ";
  67. }
  68. };
  69. $sql = $command." `".$this->table."` ".$operator." ".$setValues."";
  70. return $sql;
  71. }
  72. else{
  73. throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
  74. }
  75. }
  76. else{
  77. throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
  78. }
  79. }
  80. private function insertCommand($command,$operator,$keys,$values){
  81. if (is_array($keys) && is_array($values) && !empty($command)){
  82. isTableExist();
  83. $keyCount = count($keys);
  84. $valueCount = count($values);
  85. if ($keyCount > 0 && $valueCount > 0 && $valueCount === $keyCount) {
  86. $column = "`".(is_array($keys) ? join($keys,"`,`") : $keys)."`";
  87. $values = (is_array($keys) ? join($keys,",") : $keys);
  88.  
  89. $sql = $command." `".$this->table."` (".$column.") ".$operator." (".$values.")";
  90. return $sql;
  91. }
  92. else{
  93. throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
  94. }
  95. }
  96. else{
  97. throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
  98. }
  99. }
  100. protected
  101. function query($sql){
  102. return $this->getSqlResult($sql);
  103. }
  104. function setTable($table){
  105. $this->table = $table;
  106. return $this;
  107. }
  108. function select(...$column){
  109. $this->isTableExist();
  110. $column = "`".(is_array($column) ? join($column,"`,`") : $column)."`";
  111. $this->sql += "SELECT `".$column." FROM `".$this->table."`";
  112. $this->mode = "select";
  113. return $this;
  114. }
  115. function insert($keys,$values){
  116. $this->sql += insertCommand("INSERT INTO","VALUES",$keys,$values);
  117. $this->mode = "insert";
  118. return $this;
  119. }
  120. function delete(){
  121.  
  122. $this->isTableExist();
  123. $this->sql += "DELETE FROM `".$this->table."`";
  124. $this->mode = "delete";
  125. return $this;
  126. }
  127. function update($keys,$values){
  128. $this->sql += upadteCommand("UPDATE","SET",$keys,$values);
  129. $this->mode = "update";
  130. return $this;
  131. }
  132. function limit(){
  133. $arg = func_get_args();
  134. $countPreifx = (!empty($arg[0])) ? "LIMIT ".$arg[0] : "";
  135. $limitPreifx = (!empty($arg[1])) ? ",".$arg[1] : "";
  136. $this->sql += $countPreifx.$limitPreifx;
  137. }
  138. function where(){
  139. $this->sql += commandBuild("WHERE","")
  140. }
  141. function exec(){
  142. return $this->getSqlResult($this->sql);
  143. $this->table = null;
  144. }
  145. function __destruct()
  146. {
  147. $this->closeDb();
  148. }
  149. }
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement