Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Класс для работы с MySQL базой данных, позволяет делать запросы к базе,
- обрабатывать данные запросов и т.д. */
- include 'mysql.preprocessor.php';
- class MySQL
- {
- public $mysqli;
- public $query_string;
- private $result;
- private $tables_real_id = array();
- private $func_args_parser = array();
- private $func_args_last = 1;
- private $temp_001 = "";
- private $temp_002 = array();
- public $assoc = array();
- /* Функцию подключения к MySQL базе данных */
- public function connect($mode = "")
- {
- @$this->mysqli = new mysqli("localhost", "root", "", "phpjs");
- if ($mode == "strict" && $this->mysqli->connect_errno) die($this->mysqli->connect_error);
- return mysqli_connect_errno($this->mysqli);
- }
- /* Функция сразу выполняет запрос и получает его данные
- которые передает в обработчик */
- public function query_each($query, $callback)
- {
- $this->result = $this->mysqli->query($query);
- $this->result->data_seek(0);
- while ($row = $this->result->fetch_assoc())
- {
- $callback($row);
- }
- }
- public function update($query)
- {
- $q_part = explode("->", $query);
- $this->func_args_parser = func_get_args();
- //Первая часть
- $tables = explode(",", $q_part[0]);
- $qret = "UPDATE ".implode(",", $tables)." SET ";
- //Что обновлять
- //Case
- $toupdate = $q_part[1];
- $toupdate = preg_replace_callback(
- '/(.*?)=\/case\/(.*?)\/@([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $rret = $matches[1]."=case\n";
- $var_data = $this->func_args_parser[($matches[3] * 1) + 1];
- for ($i = 0; $i < count($var_data); $i+= 2)
- {
- $casetype = $var_data[$i + 1];
- if (gettype($casetype) == "string") $casetype = "'".$this->mysqli->real_escape_string($casetype)."'";
- $rret .= "when ".$matches[2]." = ".$var_data[$i]." then ".$casetype."\n";
- }
- $rret .= "end\n";
- return $rret;
- },
- $toupdate
- );
- //Переменные
- $toupdate = preg_replace_callback(
- '/@([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$matches[1] + 1];
- if (gettype($var_data) == "string") return "'".$this->mysqli->real_escape_string($var_data)."'";
- return $var_data;
- },
- $toupdate
- );
- $qret .= $toupdate;
- //Условие
- if (isset($q_part[2]))
- {
- $condition = $q_part[2];
- //Массивы
- $condition = preg_replace_callback(
- '/(.*?)=@([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$matches[2] + 1];
- if (gettype($var_data) == "array")
- {
- return $matches[1]." IN (".implode(",", $var_data).")";
- }
- return $matches[1]."=@".$matches[2];
- },
- $condition
- );
- //Остальное
- $condition = preg_replace_callback(
- '/@([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$matches[1] + 1];
- if (gettype($var_data) == "string") return "'".$this->mysqli->real_escape_string($var_data)."'";
- return $var_data;
- },
- $condition
- );
- $qret .= " WHERE ".$condition;
- }
- echo $qret;
- }
- public function delete($query)
- {
- $q_part = explode("->", $query);
- //Первая часть
- $from_tables = explode(",", $q_part[0]);
- for ($i = 0; $i < count($from_tables); $i++)
- $from_tables[$i] = str_replace(" ", "", $from_tables[$i]);
- $sp_tables = "";
- for ($i = 0; $i < count($from_tables); $i++)
- $sp_tables .= $from_tables[$i].".*,";
- $sp_tables = substr($sp_tables, 0, -1);
- $rquery = "DELETE $sp_tables FROM ".implode(",", $from_tables)." ";
- $this->tables_real_id = $from_tables;
- $this->func_args_parser = func_get_args();
- //Условие удаления
- //Быстрые переменные
- $condition = preg_replace_callback(
- '/\$([0-9])+/',
- function ($matches)
- {
- return $this->tables_real_id[($matches[1] * 1) - 1];
- },
- $q_part[1]
- );
- //Группы переменных
- $condition = preg_replace_callback(
- '/\[(.*?)\]\.([a-zA-Z0-9]+)=(@?)([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $tbls = explode(",", $matches[1]);
- $qret = "";
- if ($matches[3] == "@")
- {
- for ($i = 0; $i < count($tbls); $i++)
- {
- $type_data = $this->func_args_parser[$matches[4] + 1];
- if (gettype($type_data) == "string") $type_data = '"' . $this->mysqli->real_escape_string($type_data) . '"';
- $qret .= $tbls[$i] . "." . $matches[2] . " = " . $type_data . " and ";
- }
- $qret = substr($qret, 0, -5);
- } else {
- for ($i = 0; $i < count($tbls); $i++)
- $qret .= $tbls[$i] . "." . $matches[2] . " = " . $matches[4] . " and ";
- $qret = substr($qret, 0, -5);
- }
- return $qret;
- },
- $condition
- );
- //Переменные
- $condition = preg_replace_callback(
- '/@([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$matches[1] + 1];
- if (gettype($var_data) == "string") return "'".$this->mysqli->real_escape_string($var_data)."'";
- return $var_data;
- },
- $condition
- );
- $rquery .= "WHERE ".$condition;
- $this->result = $this->mysqli->query($rquery);
- return $this->result;
- }
- /*
- * Синтаксис функции "вставить".
- * insert(table_name:@i,@s ...params) - обычный синтаксис
- * insert(table_name:@i,@s ...params -> table.field=@1 or table.field_2='test' - с условием
- * */
- public function insert($query)
- {
- $q_part = explode("->", $query);
- //Первая часть
- $q_data = explode(":", $q_part[0]);
- $table_name = $q_data[0];
- $qret = "INSERT INTO $table_name VALUES(";
- $this->func_args_parser = func_get_args();
- $datas = explode(",", $q_data[1]);
- for ($i = 0; $i < count($datas); $i++)
- {
- $datas[$i] = str_replace(" ", "", $datas[$i]);
- if ($datas[$i] == "@i") $datas[$i] = intval($this->func_args_parser[$i + 1]);
- if ($datas[$i] == "@s") $datas[$i] = "'".$this->mysqli->real_escape_string($this->func_args_parser[$i + 1])."'";
- $qret .= $datas[$i].",";
- }
- $qret = substr($qret, 0, -1);
- $qret .= ")";
- //Условие
- if (isset($q_part[1]))
- {
- $this->temp_001 = $this->temp_002 = null;
- $this->temp_001 = "SELECT ";
- $condition = preg_replace_callback(
- '/([a-zA-Z0-9_\.]+)(=|\!=)(@?)([a-zA-Z0-9]+)/',
- function ($matches)
- {
- if ($matches[3] == "@")
- {
- $this->temp_001 .= $matches[1] . ",";
- $this->temp_002[explode(".", $matches[1])[0]] = explode(".", $matches[1])[0];
- $type_data = $this->func_args_parser[$matches[4] + 1];
- if (gettype($type_data) == "string") $type_data = '"' . $this->mysqli->real_escape_string($type_data) . '"';
- return $matches[1] . $matches[2] . $type_data;
- } else {
- $this->temp_001 .= $matches[1] . ",";
- $this->temp_002[explode(".", $matches[1])[0]] = explode(".", $matches[1])[0];
- return $matches[1] . $matches[2] . $matches[4];
- }
- },
- $q_part[1]
- );
- $this->temp_001 = substr($this->temp_001, 0, -1)." FROM ".implode(",", $this->temp_002)." WHERE $condition";
- echo $this->temp_001;
- $this->result = $this->mysqli->query($this->temp_001);
- if ($this->result->num_rows > 0) return false;
- }
- $this->result = $this->mysqli->query($qret);
- return $this->result;
- }
- /*
- * Функция выборки из базы данных
- * синтаксис следующий
- * select(table:fields|table_2:field,field) - обычный
- * select(table:fields|table_2:field,field -> $1.id=@s) - с условием
- * где $1 это ссылка на первую таблицу в списке
- * [$1,$2].uid это группая полей с одинаковым полем
- * @s,@i,@a,@search - это фильтры, для фильтрации переменных по типу
- */
- public function select($query)
- {
- $q_data = explode("->", $query);
- $data = $q_data[0];
- $condition = $q_data[1];
- $this->func_args_parser = func_get_args();
- //Первая часть
- $real_tables = array();
- $tables = explode("|", $data);
- $query_str = "SELECT ";
- for ($i = 0; $i < count($tables); $i++)
- {
- $table_data = explode(":", $tables[$i]);
- $field_data = explode(",", $table_data[1]);
- $table_data[0] = str_replace(" ", "", $table_data[0]);
- $real_tables[$table_data[0]] = $table_data[0];
- $this->tables_real_id[$i] = $table_data[0];
- for ($j = 0; $j < count($field_data); $j++)
- {
- $query_str .= $table_data[0].".".$field_data[$j].",";
- }
- }
- $query_str = substr($query_str, 0, -1);
- $query_str .= " FROM ".implode(",", $real_tables);
- $this->query_string = "sas";
- //Вторая часть
- //Быстрые переменные
- $condition = preg_replace_callback(
- '/\$([0-9])+/',
- function ($matches)
- {
- return $this->tables_real_id[($matches[1] * 1) - 1];
- },
- $condition
- );
- //Группы переменных
- $condition = preg_replace_callback(
- '/(.*?)=\[(.*?)\]\.([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $tbls = explode(",", $matches[2]);
- $rstr = "";
- for ($i = 0; $i < count($tbls); $i++)
- {
- $rstr .= $matches[1]."=".$tbls[$i].".".$matches[3]." and ";
- }
- $rstr = substr($rstr, 0, -5);
- return $rstr;
- },
- $condition
- );
- //Фильтры поиска
- $condition = preg_replace_callback(
- '/(.*?)=@search/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$this->func_args_last++];
- $str = $matches[1]." LIKE '%".$this->mysqli->real_escape_string($var_data)."%'";
- return $str;
- },
- $condition
- );
- //Фильтры массивы
- $condition = preg_replace_callback(
- '/(.*?)=@a/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$this->func_args_last++];
- $str = $matches[1]." IN (";
- if (gettype($var_data) == "array") $str .= implode(",", $var_data);
- $str .= ")";
- return $str;
- },
- $condition
- );
- //Фильтры прочие
- $condition = preg_replace_callback(
- '/@([a-zA-Z0-9]+)/',
- function ($matches)
- {
- $var_data = $this->func_args_parser[$this->func_args_last++];
- if ($matches[1] == "i") return intval($var_data);
- if ($matches[1] == "f") return floatval($var_data);
- if ($matches[1] == "s") return "'".addslashes($var_data)."'";
- if ($matches[1] == "es") return "'".$this->mysqli->real_escape_string($var_data)."'";
- return $var_data;
- },
- $condition
- );
- $query_str .= " WHERE $condition";
- $this->result = $this->mysqli->query($query_str);
- return $this->result;
- }
- }
- $MYSQL = new MySQL;
- $MYSQL_PP->mysqli = &$MYSQL->mysqli;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement