Advertisement
Guest User

Untitled

a guest
May 27th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.25 KB | None | 0 0
  1. <?php
  2. /* Класс для работы с MySQL базой данных, позволяет делать запросы к базе,
  3. обрабатывать данные запросов и т.д. */
  4.  
  5. include 'mysql.preprocessor.php';
  6.  
  7. class MySQL
  8. {
  9. public $mysqli;
  10. public $query_string;
  11. private $result;
  12. private $tables_real_id = array();
  13. private $func_args_parser = array();
  14. private $func_args_last = 1;
  15. private $temp_001 = "";
  16. private $temp_002 = array();
  17. public $assoc = array();
  18.  
  19. /* Функцию подключения к MySQL базе данных */
  20.  
  21. public function connect($mode = "")
  22. {
  23. @$this->mysqli = new mysqli("localhost", "root", "", "phpjs");
  24. if ($mode == "strict" && $this->mysqli->connect_errno) die($this->mysqli->connect_error);
  25.  
  26. return mysqli_connect_errno($this->mysqli);
  27. }
  28.  
  29. /* Функция сразу выполняет запрос и получает его данные
  30. которые передает в обработчик */
  31.  
  32. public function query_each($query, $callback)
  33. {
  34. $this->result = $this->mysqli->query($query);
  35. $this->result->data_seek(0);
  36. while ($row = $this->result->fetch_assoc())
  37. {
  38. $callback($row);
  39. }
  40. }
  41.  
  42. public function update($query)
  43. {
  44. $q_part = explode("->", $query);
  45. $this->func_args_parser = func_get_args();
  46.  
  47. //Первая часть
  48. $tables = explode(",", $q_part[0]);
  49. $qret = "UPDATE ".implode(",", $tables)." SET ";
  50.  
  51. //Что обновлять
  52.  
  53. //Case
  54. $toupdate = $q_part[1];
  55. $toupdate = preg_replace_callback(
  56. '/(.*?)=\/case\/(.*?)\/@([a-zA-Z0-9]+)/',
  57. function ($matches)
  58. {
  59. $rret = $matches[1]."=case\n";
  60. $var_data = $this->func_args_parser[($matches[3] * 1) + 1];
  61.  
  62. for ($i = 0; $i < count($var_data); $i+= 2)
  63. {
  64. $casetype = $var_data[$i + 1];
  65.  
  66. if (gettype($casetype) == "string") $casetype = "'".$this->mysqli->real_escape_string($casetype)."'";
  67.  
  68. $rret .= "when ".$matches[2]." = ".$var_data[$i]." then ".$casetype."\n";
  69. }
  70.  
  71. $rret .= "end\n";
  72. return $rret;
  73. },
  74. $toupdate
  75. );
  76.  
  77. //Переменные
  78. $toupdate = preg_replace_callback(
  79. '/@([a-zA-Z0-9]+)/',
  80. function ($matches)
  81. {
  82. $var_data = $this->func_args_parser[$matches[1] + 1];
  83.  
  84. if (gettype($var_data) == "string") return "'".$this->mysqli->real_escape_string($var_data)."'";
  85.  
  86. return $var_data;
  87. },
  88. $toupdate
  89. );
  90.  
  91. $qret .= $toupdate;
  92.  
  93. //Условие
  94. if (isset($q_part[2]))
  95. {
  96. $condition = $q_part[2];
  97.  
  98. //Массивы
  99. $condition = preg_replace_callback(
  100. '/(.*?)=@([a-zA-Z0-9]+)/',
  101. function ($matches)
  102. {
  103. $var_data = $this->func_args_parser[$matches[2] + 1];
  104.  
  105. if (gettype($var_data) == "array")
  106. {
  107. return $matches[1]." IN (".implode(",", $var_data).")";
  108. }
  109.  
  110. return $matches[1]."=@".$matches[2];
  111. },
  112. $condition
  113. );
  114.  
  115. //Остальное
  116. $condition = preg_replace_callback(
  117. '/@([a-zA-Z0-9]+)/',
  118. function ($matches)
  119. {
  120. $var_data = $this->func_args_parser[$matches[1] + 1];
  121.  
  122. if (gettype($var_data) == "string") return "'".$this->mysqli->real_escape_string($var_data)."'";
  123.  
  124. return $var_data;
  125. },
  126. $condition
  127. );
  128.  
  129. $qret .= " WHERE ".$condition;
  130. }
  131.  
  132. echo $qret;
  133. }
  134.  
  135. public function delete($query)
  136. {
  137. $q_part = explode("->", $query);
  138.  
  139. //Первая часть
  140. $from_tables = explode(",", $q_part[0]);
  141. for ($i = 0; $i < count($from_tables); $i++)
  142. $from_tables[$i] = str_replace(" ", "", $from_tables[$i]);
  143.  
  144. $sp_tables = "";
  145. for ($i = 0; $i < count($from_tables); $i++)
  146. $sp_tables .= $from_tables[$i].".*,";
  147. $sp_tables = substr($sp_tables, 0, -1);
  148.  
  149. $rquery = "DELETE $sp_tables FROM ".implode(",", $from_tables)." ";
  150. $this->tables_real_id = $from_tables;
  151. $this->func_args_parser = func_get_args();
  152.  
  153. //Условие удаления
  154. //Быстрые переменные
  155. $condition = preg_replace_callback(
  156. '/\$([0-9])+/',
  157. function ($matches)
  158. {
  159. return $this->tables_real_id[($matches[1] * 1) - 1];
  160. },
  161. $q_part[1]
  162. );
  163.  
  164. //Группы переменных
  165. $condition = preg_replace_callback(
  166. '/\[(.*?)\]\.([a-zA-Z0-9]+)=(@?)([a-zA-Z0-9]+)/',
  167. function ($matches)
  168. {
  169. $tbls = explode(",", $matches[1]);
  170. $qret = "";
  171.  
  172. if ($matches[3] == "@")
  173. {
  174. for ($i = 0; $i < count($tbls); $i++)
  175. {
  176. $type_data = $this->func_args_parser[$matches[4] + 1];
  177. if (gettype($type_data) == "string") $type_data = '"' . $this->mysqli->real_escape_string($type_data) . '"';
  178.  
  179. $qret .= $tbls[$i] . "." . $matches[2] . " = " . $type_data . " and ";
  180. }
  181. $qret = substr($qret, 0, -5);
  182. } else {
  183. for ($i = 0; $i < count($tbls); $i++)
  184. $qret .= $tbls[$i] . "." . $matches[2] . " = " . $matches[4] . " and ";
  185. $qret = substr($qret, 0, -5);
  186. }
  187.  
  188. return $qret;
  189. },
  190. $condition
  191. );
  192.  
  193. //Переменные
  194. $condition = preg_replace_callback(
  195. '/@([a-zA-Z0-9]+)/',
  196. function ($matches)
  197. {
  198. $var_data = $this->func_args_parser[$matches[1] + 1];
  199.  
  200. if (gettype($var_data) == "string") return "'".$this->mysqli->real_escape_string($var_data)."'";
  201. return $var_data;
  202. },
  203. $condition
  204. );
  205.  
  206. $rquery .= "WHERE ".$condition;
  207.  
  208. $this->result = $this->mysqli->query($rquery);
  209. return $this->result;
  210. }
  211.  
  212. /*
  213. * Синтаксис функции "вставить".
  214. * insert(table_name:@i,@s ...params) - обычный синтаксис
  215. * insert(table_name:@i,@s ...params -> table.field=@1 or table.field_2='test' - с условием
  216. * */
  217.  
  218. public function insert($query)
  219. {
  220. $q_part = explode("->", $query);
  221.  
  222. //Первая часть
  223. $q_data = explode(":", $q_part[0]);
  224. $table_name = $q_data[0];
  225. $qret = "INSERT INTO $table_name VALUES(";
  226. $this->func_args_parser = func_get_args();
  227.  
  228. $datas = explode(",", $q_data[1]);
  229. for ($i = 0; $i < count($datas); $i++)
  230. {
  231. $datas[$i] = str_replace(" ", "", $datas[$i]);
  232. if ($datas[$i] == "@i") $datas[$i] = intval($this->func_args_parser[$i + 1]);
  233. if ($datas[$i] == "@s") $datas[$i] = "'".$this->mysqli->real_escape_string($this->func_args_parser[$i + 1])."'";
  234. $qret .= $datas[$i].",";
  235. }
  236.  
  237. $qret = substr($qret, 0, -1);
  238.  
  239. $qret .= ")";
  240.  
  241. //Условие
  242. if (isset($q_part[1]))
  243. {
  244. $this->temp_001 = $this->temp_002 = null;
  245. $this->temp_001 = "SELECT ";
  246. $condition = preg_replace_callback(
  247. '/([a-zA-Z0-9_\.]+)(=|\!=)(@?)([a-zA-Z0-9]+)/',
  248. function ($matches)
  249. {
  250. if ($matches[3] == "@")
  251. {
  252. $this->temp_001 .= $matches[1] . ",";
  253. $this->temp_002[explode(".", $matches[1])[0]] = explode(".", $matches[1])[0];
  254. $type_data = $this->func_args_parser[$matches[4] + 1];
  255. if (gettype($type_data) == "string") $type_data = '"' . $this->mysqli->real_escape_string($type_data) . '"';
  256. return $matches[1] . $matches[2] . $type_data;
  257. } else {
  258. $this->temp_001 .= $matches[1] . ",";
  259. $this->temp_002[explode(".", $matches[1])[0]] = explode(".", $matches[1])[0];
  260. return $matches[1] . $matches[2] . $matches[4];
  261. }
  262. },
  263. $q_part[1]
  264. );
  265.  
  266. $this->temp_001 = substr($this->temp_001, 0, -1)." FROM ".implode(",", $this->temp_002)." WHERE $condition";
  267. echo $this->temp_001;
  268. $this->result = $this->mysqli->query($this->temp_001);
  269. if ($this->result->num_rows > 0) return false;
  270. }
  271.  
  272.  
  273.  
  274. $this->result = $this->mysqli->query($qret);
  275. return $this->result;
  276. }
  277.  
  278. /*
  279. * Функция выборки из базы данных
  280. * синтаксис следующий
  281. * select(table:fields|table_2:field,field) - обычный
  282. * select(table:fields|table_2:field,field -> $1.id=@s) - с условием
  283. * где $1 это ссылка на первую таблицу в списке
  284. * [$1,$2].uid это группая полей с одинаковым полем
  285. * @s,@i,@a,@search - это фильтры, для фильтрации переменных по типу
  286. */
  287. public function select($query)
  288. {
  289. $q_data = explode("->", $query);
  290. $data = $q_data[0];
  291. $condition = $q_data[1];
  292. $this->func_args_parser = func_get_args();
  293.  
  294. //Первая часть
  295. $real_tables = array();
  296. $tables = explode("|", $data);
  297. $query_str = "SELECT ";
  298.  
  299. for ($i = 0; $i < count($tables); $i++)
  300. {
  301. $table_data = explode(":", $tables[$i]);
  302. $field_data = explode(",", $table_data[1]);
  303. $table_data[0] = str_replace(" ", "", $table_data[0]);
  304. $real_tables[$table_data[0]] = $table_data[0];
  305. $this->tables_real_id[$i] = $table_data[0];
  306.  
  307. for ($j = 0; $j < count($field_data); $j++)
  308. {
  309. $query_str .= $table_data[0].".".$field_data[$j].",";
  310. }
  311. }
  312.  
  313. $query_str = substr($query_str, 0, -1);
  314. $query_str .= " FROM ".implode(",", $real_tables);
  315. $this->query_string = "sas";
  316.  
  317. //Вторая часть
  318. //Быстрые переменные
  319. $condition = preg_replace_callback(
  320. '/\$([0-9])+/',
  321. function ($matches)
  322. {
  323. return $this->tables_real_id[($matches[1] * 1) - 1];
  324. },
  325. $condition
  326. );
  327.  
  328. //Группы переменных
  329. $condition = preg_replace_callback(
  330. '/(.*?)=\[(.*?)\]\.([a-zA-Z0-9]+)/',
  331. function ($matches)
  332. {
  333. $tbls = explode(",", $matches[2]);
  334. $rstr = "";
  335.  
  336. for ($i = 0; $i < count($tbls); $i++)
  337. {
  338. $rstr .= $matches[1]."=".$tbls[$i].".".$matches[3]." and ";
  339. }
  340.  
  341. $rstr = substr($rstr, 0, -5);
  342. return $rstr;
  343. },
  344. $condition
  345. );
  346.  
  347. //Фильтры поиска
  348. $condition = preg_replace_callback(
  349. '/(.*?)=@search/',
  350. function ($matches)
  351. {
  352. $var_data = $this->func_args_parser[$this->func_args_last++];
  353. $str = $matches[1]." LIKE '%".$this->mysqli->real_escape_string($var_data)."%'";
  354. return $str;
  355. },
  356. $condition
  357. );
  358.  
  359. //Фильтры массивы
  360. $condition = preg_replace_callback(
  361. '/(.*?)=@a/',
  362. function ($matches)
  363. {
  364. $var_data = $this->func_args_parser[$this->func_args_last++];
  365. $str = $matches[1]." IN (";
  366. if (gettype($var_data) == "array") $str .= implode(",", $var_data);
  367. $str .= ")";
  368. return $str;
  369. },
  370. $condition
  371. );
  372.  
  373. //Фильтры прочие
  374. $condition = preg_replace_callback(
  375. '/@([a-zA-Z0-9]+)/',
  376. function ($matches)
  377. {
  378. $var_data = $this->func_args_parser[$this->func_args_last++];
  379. if ($matches[1] == "i") return intval($var_data);
  380. if ($matches[1] == "f") return floatval($var_data);
  381. if ($matches[1] == "s") return "'".addslashes($var_data)."'";
  382. if ($matches[1] == "es") return "'".$this->mysqli->real_escape_string($var_data)."'";
  383. return $var_data;
  384. },
  385. $condition
  386. );
  387.  
  388. $query_str .= " WHERE $condition";
  389.  
  390. $this->result = $this->mysqli->query($query_str);
  391.  
  392. return $this->result;
  393. }
  394. }
  395.  
  396. $MYSQL = new MySQL;
  397. $MYSQL_PP->mysqli = &$MYSQL->mysqli;
  398. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement