Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class queryBuilder
- {
- const MYSQL_ERR = -1;
- const SUCCESS = 0;
- const QUERY_ERR = 1;
- const UNKNOWN_TYPE = 11;
- const SIZE_NOT_MATCHED = 12;
- function __construct($auth_host,$auth_user,$auth_password,$auth_db)
- {
- $this->auth_host = $auth_host;
- $this->auth_user = $auth_user;
- $this->auth_password = $auth_password;
- $this->auth_db = $auth_db;
- $this->sql = "";
- $this->mysqli = new Mysqli($auth_host,$auth_user,$auth_password,$auth_db);
- }
- private function openDb(){
- if (!$this->mysqli->ping()) {
- $this->mysqli = new Mysqli($auth_host,$auth_user,$auth_password,$auth_db);
- }
- }
- private function closeDb(){
- if ($this->mysqli->ping()) {
- $this->mysqli->close();
- }
- }
- private function isTableExist(){
- if (!empty($this->table)) {
- throw new Exception("table is not set", $this::QUERY_ERR);
- }
- }
- protected function getSqlResult($sql){
- if ($this->mysqli->ping()) {
- $res = $this->mysqli->query($sql);
- if (!is_bool($res)) {
- $ret = array_merge($res->fetch_array(),array('status' => $this::SUCCESS));
- }
- else{
- $ret = array('status' => $this::QUERY_ERR,'info' => $this->mysqli->error);
- }
- return $ret;
- }
- else{
- throw new Exception("Error Connect Mysql [error : ".$this->mysqli->error."]", MYSQL_ERR);
- }
- }
- private function kvCommand($command,,$keys,$values){
- if (is_array($keys) && is_array($values) && !empty($command)){
- isTableExist();
- $keyCount = count($keys);
- $valueCount = count($values);
- if ($keyCount > 0 && $valueCount > 0 && $valueCount <= $keyCount) {
- $setValues = "";
- for ($index = 0; $index < $keyCount; $index++) {
- $values[$index] = is_string($values[$index]) ? "'".$values[$index]."'" : $values[$index];
- $setValues .= "`".$keys[$index]."` = ".$values[$index];
- if($index < $keyCount - 1){
- $setValues .= " AND ";
- }
- };
- $sql = $command." ".$setValues."";
- return $sql;
- }
- else{
- throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
- }
- }
- else{
- throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
- }
- }
- private function upadteCommand($command,$operator,$keys,$values){
- if (is_array($keys) && is_array($values) && !empty($command)){
- isTableExist();
- $keyCount = count($keys);
- $valueCount = count($values);
- if ($keyCount > 0 && $valueCount > 0 && $valueCount <= $keyCount) {
- $setValues = "";
- for ($index = 0; $index < $keyCount; $index++) {
- $values[$index] = is_string($values[$index]) ? "'".$values[$index]."'" : $values[$index];
- $setValues .= "`".$keys[$index]."` = ".$values[$index];
- if($index < $keyCount - 1){
- $setValues .= ", ";
- }
- };
- $sql = $command." `".$this->table."` ".$operator." ".$setValues."";
- return $sql;
- }
- else{
- throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
- }
- }
- else{
- throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
- }
- }
- private function insertCommand($command,$operator,$keys,$values){
- if (is_array($keys) && is_array($values) && !empty($command)){
- isTableExist();
- $keyCount = count($keys);
- $valueCount = count($values);
- if ($keyCount > 0 && $valueCount > 0 && $valueCount === $keyCount) {
- $column = "`".(is_array($keys) ? join($keys,"`,`") : $keys)."`";
- $values = (is_array($keys) ? join($keys,",") : $keys);
- $sql = $command." `".$this->table."` (".$column.") ".$operator." (".$values.")";
- return $sql;
- }
- else{
- throw new Exception("Key Value length is Not Matched", $this::SIZE_NOT_MATCHED);
- }
- }
- else{
- throw new Exception("Unknown Type (Only Array)", $this::UNKNOWN_TYPE);
- }
- }
- function query($sql){
- return $this->getSqlResult($sql);
- }
- function setTable($table){
- $this->table = $table;
- return $this;
- }
- function select(...$column){
- $this->isTableExist();
- $column = "`".(is_array($column) ? join($column,"`,`") : $column)."`";
- $this->sql += "SELECT `".$column." FROM `".$this->table."`";
- $this->mode = "select";
- return $this;
- }
- function insert($keys,$values){
- $this->sql += insertCommand("INSERT INTO","VALUES",$keys,$values);
- $this->mode = "insert";
- return $this;
- }
- function delete(){
- $this->isTableExist();
- $this->sql += "DELETE FROM `".$this->table."`";
- $this->mode = "delete";
- return $this;
- }
- function update($keys,$values){
- $this->sql += upadteCommand("UPDATE","SET",$keys,$values);
- $this->mode = "update";
- return $this;
- }
- function limit(){
- $arg = func_get_args();
- $countPreifx = (!empty($arg[0])) ? "LIMIT ".$arg[0] : "";
- $limitPreifx = (!empty($arg[1])) ? ",".$arg[1] : "";
- $this->sql += $countPreifx.$limitPreifx;
- }
- function where($keys,$values){
- $this->sql += kvCommand("WHERE",$keys,$values);
- return $this;
- }
- function exec(){
- return $this->getSqlResult($this->sql);
- $this->table = null;
- }
- function __destruct()
- {
- $this->closeDb();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement