Advertisement
Guest User

medoo

a guest
Dec 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.71 KB | None | 0 0
  1. <?php
  2. /*!
  3. * Medoo database framework
  4. * http://medoo.in
  5. * Version 1.0.2
  6. *
  7. * Copyright 2016, Angel Lai
  8. * Released under the MIT license
  9. */
  10. class medoo
  11. {
  12. // General
  13. protected $database_type;
  14.  
  15. protected $charset;
  16.  
  17. protected $database_name;
  18.  
  19. // For MySQL, MariaDB, MSSQL, Sybase, PostgreSQL, Oracle
  20. protected $server;
  21.  
  22. protected $username;
  23.  
  24. protected $password;
  25.  
  26. // For SQLite
  27. protected $database_file;
  28.  
  29. // For MySQL or MariaDB with unix_socket
  30. protected $socket;
  31.  
  32. // Optional
  33. protected $port;
  34.  
  35. protected $prefix;
  36.  
  37. protected $option = array();
  38.  
  39. // Variable
  40. protected $logs = array();
  41.  
  42. protected $debug_mode = false;
  43.  
  44. public function __construct($options = null)
  45. {
  46. try {
  47. $commands = array();
  48. $dsn = '';
  49.  
  50. if (is_array($options))
  51. {
  52. foreach ($options as $option => $value)
  53. {
  54. $this->$option = $value;
  55. }
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61.  
  62. if (
  63. isset($this->port) &&
  64. is_int($this->port * 1)
  65. )
  66. {
  67. $port = $this->port;
  68. }
  69.  
  70. $type = strtolower($this->database_type);
  71. $is_port = isset($port);
  72.  
  73. if (isset($options[ 'prefix' ]))
  74. {
  75. $this->prefix = $options[ 'prefix' ];
  76. }
  77.  
  78. switch ($type)
  79. {
  80. case 'mariadb':
  81. $type = 'mysql';
  82.  
  83. case 'mysql':
  84. if ($this->socket)
  85. {
  86. $dsn = $type . ':unix_socket=' . $this->socket . ';dbname=' . $this->database_name;
  87. }
  88. else
  89. {
  90. $dsn = $type . ':host=' . $this->server . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->database_name;
  91. }
  92.  
  93. // Make MySQL using standard quoted identifier
  94. $commands[] = 'SET SQL_MODE=ANSI_QUOTES';
  95. break;
  96.  
  97. case 'pgsql':
  98. $dsn = $type . ':host=' . $this->server . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->database_name;
  99. break;
  100.  
  101. case 'sybase':
  102. $dsn = 'dblib:host=' . $this->server . ($is_port ? ':' . $port : '') . ';dbname=' . $this->database_name;
  103. break;
  104.  
  105. case 'oracle':
  106. $dbname = $this->server ?
  107. '//' . $this->server . ($is_port ? ':' . $port : ':1521') . '/' . $this->database_name :
  108. $this->database_name;
  109.  
  110. $dsn = 'oci:dbname=' . $dbname . ($this->charset ? ';charset=' . $this->charset : '');
  111. break;
  112.  
  113. case 'mssql':
  114. $dsn = strstr(PHP_OS, 'WIN') ?
  115. 'sqlsrv:server=' . $this->server . ($is_port ? ',' . $port : '') . ';database=' . $this->database_name :
  116. 'dblib:host=' . $this->server . ($is_port ? ':' . $port : '') . ';dbname=' . $this->database_name;
  117.  
  118. // Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting
  119. $commands[] = 'SET QUOTED_IDENTIFIER ON';
  120. break;
  121.  
  122. case 'sqlite':
  123. $dsn = $type . ':' . $this->database_file;
  124. $this->username = null;
  125. $this->password = null;
  126. break;
  127. }
  128.  
  129. if (
  130. in_array($type, explode(' ', 'mariadb mysql pgsql sybase mssql')) &&
  131. $this->charset
  132. )
  133. {
  134. $commands[] = "SET NAMES '" . $this->charset . "'";
  135. }
  136.  
  137. $this->pdo = new PDO(
  138. $dsn,
  139. $this->username,
  140. $this->password,
  141. $this->option
  142. );
  143.  
  144. foreach ($commands as $value)
  145. {
  146. $this->pdo->exec($value);
  147. }
  148. }
  149. catch (PDOException $e) {
  150. throw new Exception($e->getMessage());
  151. }
  152. }
  153.  
  154. public function query($query)
  155. {
  156. if ($this->debug_mode)
  157. {
  158. echo $query;
  159.  
  160. $this->debug_mode = false;
  161.  
  162. return false;
  163. }
  164.  
  165. array_push($this->logs, $query);
  166.  
  167. return $this->pdo->query($query);
  168. }
  169.  
  170. public function exec($query)
  171. {
  172. if ($this->debug_mode)
  173. {
  174. echo $query;
  175.  
  176. $this->debug_mode = false;
  177.  
  178. return false;
  179. }
  180.  
  181. array_push($this->logs, $query);
  182.  
  183. return $this->pdo->exec($query);
  184. }
  185.  
  186. public function quote($string)
  187. {
  188. return $this->pdo->quote($string);
  189. }
  190.  
  191. protected function column_quote($string)
  192. {
  193. return '"' . str_replace('.', '"."', preg_replace('/(^#|\(JSON\)\s*)/', '', $string)) . '"';
  194. }
  195.  
  196. protected function column_push($columns)
  197. {
  198. if ($columns == '*')
  199. {
  200. return $columns;
  201. }
  202.  
  203. if (is_string($columns))
  204. {
  205. $columns = array($columns);
  206. }
  207.  
  208. $stack = array();
  209.  
  210. foreach ($columns as $key => $value)
  211. {
  212. preg_match('/([a-zA-Z0-9_\-\.]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $value, $match);
  213.  
  214. if (isset($match[ 1 ], $match[ 2 ]))
  215. {
  216. array_push($stack, $this->column_quote( $match[ 1 ] ) . ' AS ' . $this->column_quote( $match[ 2 ] ));
  217. }
  218. else
  219. {
  220. array_push($stack, $this->column_quote( $value ));
  221. }
  222. }
  223.  
  224. return implode($stack, ',');
  225. }
  226.  
  227. protected function array_quote($array)
  228. {
  229. $temp = array();
  230.  
  231. foreach ($array as $value)
  232. {
  233. $temp[] = is_int($value) ? $value : $this->pdo->quote($value);
  234. }
  235.  
  236. return implode($temp, ',');
  237. }
  238.  
  239. protected function inner_conjunct($data, $conjunctor, $outer_conjunctor)
  240. {
  241. $haystack = array();
  242.  
  243. foreach ($data as $value)
  244. {
  245. $haystack[] = '(' . $this->data_implode($value, $conjunctor) . ')';
  246. }
  247.  
  248. return implode($outer_conjunctor . ' ', $haystack);
  249. }
  250.  
  251. protected function fn_quote($column, $string)
  252. {
  253. return (strpos($column, '#') === 0 && preg_match('/^[A-Z0-9\_]*\([^)]*\)$/', $string)) ?
  254.  
  255. $string :
  256.  
  257. $this->quote($string);
  258. }
  259.  
  260. protected function data_implode($data, $conjunctor, $outer_conjunctor = null)
  261. {
  262. $wheres = array();
  263.  
  264. foreach ($data as $key => $value)
  265. {
  266. $type = gettype($value);
  267.  
  268. if (
  269. preg_match("/^(AND|OR)(\s+#.*)?$/i", $key, $relation_match) &&
  270. $type == 'array'
  271. )
  272. {
  273. $wheres[] = 0 !== count(array_diff_key($value, array_keys(array_keys($value)))) ?
  274. '(' . $this->data_implode($value, ' ' . $relation_match[ 1 ]) . ')' :
  275. '(' . $this->inner_conjunct($value, ' ' . $relation_match[ 1 ], $conjunctor) . ')';
  276. }
  277. else
  278. {
  279. preg_match('/(#?)([\w\.\-]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i', $key, $match);
  280. $column = $this->column_quote($match[ 2 ]);
  281.  
  282. if (isset($match[ 4 ]))
  283. {
  284. $operator = $match[ 4 ];
  285.  
  286. if ($operator == '!')
  287. {
  288. switch ($type)
  289. {
  290. case 'NULL':
  291. $wheres[] = $column . ' IS NOT NULL';
  292. break;
  293.  
  294. case 'array':
  295. $wheres[] = $column . ' NOT IN (' . $this->array_quote($value) . ')';
  296. break;
  297.  
  298. case 'integer':
  299. case 'double':
  300. $wheres[] = $column . ' != ' . $value;
  301. break;
  302.  
  303. case 'boolean':
  304. $wheres[] = $column . ' != ' . ($value ? '1' : '0');
  305. break;
  306.  
  307. case 'string':
  308. $wheres[] = $column . ' != ' . $this->fn_quote($key, $value);
  309. break;
  310. }
  311. }
  312.  
  313. if ($operator == '<>' || $operator == '><')
  314. {
  315. if ($type == 'array')
  316. {
  317. if ($operator == '><')
  318. {
  319. $column .= ' NOT';
  320. }
  321.  
  322. if (is_numeric($value[ 0 ]) && is_numeric($value[ 1 ]))
  323. {
  324. $wheres[] = '(' . $column . ' BETWEEN ' . $value[ 0 ] . ' AND ' . $value[ 1 ] . ')';
  325. }
  326. else
  327. {
  328. $wheres[] = '(' . $column . ' BETWEEN ' . $this->quote($value[ 0 ]) . ' AND ' . $this->quote($value[ 1 ]) . ')';
  329. }
  330. }
  331. }
  332.  
  333. if ($operator == '~' || $operator == '!~')
  334. {
  335. if ($type != 'array')
  336. {
  337. $value = array($value);
  338. }
  339.  
  340. $like_clauses = array();
  341.  
  342. foreach ($value as $item)
  343. {
  344. $item = strval($item);
  345. $suffix = mb_substr($item, -1, 1);
  346.  
  347. if ($suffix === '_')
  348. {
  349. $item = substr_replace($item, '%', -1);
  350. }
  351. elseif ($suffix === '%')
  352. {
  353. $item = '%' . substr_replace($item, '', -1, 1);
  354. }
  355. elseif (preg_match('/^(?!%).+(?<!%)$/', $item))
  356. {
  357. $item = '%' . $item . '%';
  358. }
  359.  
  360. $like_clauses[] = $column . ($operator === '!~' ? ' NOT' : '') . ' LIKE ' . $this->fn_quote($key, $item);
  361. }
  362.  
  363. $wheres[] = implode(' OR ', $like_clauses);
  364. }
  365.  
  366. if (in_array($operator, array('>', '>=', '<', '<=')))
  367. {
  368. if (is_numeric($value))
  369. {
  370. $wheres[] = $column . ' ' . $operator . ' ' . $value;
  371. }
  372. elseif (strpos($key, '#') === 0)
  373. {
  374. $wheres[] = $column . ' ' . $operator . ' ' . $this->fn_quote($key, $value);
  375. }
  376. else
  377. {
  378. $wheres[] = $column . ' ' . $operator . ' ' . $this->quote($value);
  379. }
  380. }
  381. }
  382. else
  383. {
  384. switch ($type)
  385. {
  386. case 'NULL':
  387. $wheres[] = $column . ' IS NULL';
  388. break;
  389.  
  390. case 'array':
  391. $wheres[] = $column . ' IN (' . $this->array_quote($value) . ')';
  392. break;
  393.  
  394. case 'integer':
  395. case 'double':
  396. $wheres[] = $column . ' = ' . $value;
  397. break;
  398.  
  399. case 'boolean':
  400. $wheres[] = $column . ' = ' . ($value ? '1' : '0');
  401. break;
  402.  
  403. case 'string':
  404. $wheres[] = $column . ' = ' . $this->fn_quote($key, $value);
  405. break;
  406. }
  407. }
  408. }
  409. }
  410.  
  411. return implode($conjunctor . ' ', $wheres);
  412. }
  413.  
  414. protected function where_clause($where)
  415. {
  416. $where_clause = '';
  417.  
  418. if (is_array($where))
  419. {
  420. $where_keys = array_keys($where);
  421. $where_AND = preg_grep("/^AND\s*#?$/i", $where_keys);
  422. $where_OR = preg_grep("/^OR\s*#?$/i", $where_keys);
  423.  
  424. $single_condition = array_diff_key($where, array_flip(
  425. explode(' ', 'AND OR GROUP ORDER HAVING LIMIT LIKE MATCH')
  426. ));
  427.  
  428. if ($single_condition != array())
  429. {
  430. $condition = $this->data_implode($single_condition, '');
  431.  
  432. if ($condition != '')
  433. {
  434. $where_clause = ' WHERE ' . $condition;
  435. }
  436. }
  437.  
  438. if (!empty($where_AND))
  439. {
  440. $value = array_values($where_AND);
  441. $where_clause = ' WHERE ' . $this->data_implode($where[ $value[ 0 ] ], ' AND');
  442. }
  443.  
  444. if (!empty($where_OR))
  445. {
  446. $value = array_values($where_OR);
  447. $where_clause = ' WHERE ' . $this->data_implode($where[ $value[ 0 ] ], ' OR');
  448. }
  449.  
  450. if (isset($where[ 'MATCH' ]))
  451. {
  452. $MATCH = $where[ 'MATCH' ];
  453.  
  454. if (is_array($MATCH) && isset($MATCH[ 'columns' ], $MATCH[ 'keyword' ]))
  455. {
  456. $where_clause .= ($where_clause != '' ? ' AND ' : ' WHERE ') . ' MATCH ("' . str_replace('.', '"."', implode($MATCH[ 'columns' ], '", "')) . '") AGAINST (' . $this->quote($MATCH[ 'keyword' ]) . ')';
  457. }
  458. }
  459.  
  460. if (isset($where[ 'GROUP' ]))
  461. {
  462. $where_clause .= ' GROUP BY ' . $this->column_quote($where[ 'GROUP' ]);
  463.  
  464. if (isset($where[ 'HAVING' ]))
  465. {
  466. $where_clause .= ' HAVING ' . $this->data_implode($where[ 'HAVING' ], ' AND');
  467. }
  468. }
  469.  
  470. if (isset($where[ 'ORDER' ]))
  471. {
  472. $rsort = '/(^[a-zA-Z0-9_\-\.]*)(\s*(DESC|ASC))?/';
  473. $ORDER = $where[ 'ORDER' ];
  474.  
  475. if (is_array($ORDER))
  476. {
  477. if (
  478. isset($ORDER[ 1 ]) &&
  479. is_array($ORDER[ 1 ])
  480. )
  481. {
  482. $where_clause .= ' ORDER BY FIELD(' . $this->column_quote($ORDER[ 0 ]) . ', ' . $this->array_quote($ORDER[ 1 ]) . ')';
  483. }
  484. else
  485. {
  486. $stack = array();
  487.  
  488. foreach ($ORDER as $column)
  489. {
  490. preg_match($rsort, $column, $order_match);
  491.  
  492. array_push($stack, '"' . str_replace('.', '"."', $order_match[ 1 ]) . '"' . (isset($order_match[ 3 ]) ? ' ' . $order_match[ 3 ] : ''));
  493. }
  494.  
  495. $where_clause .= ' ORDER BY ' . implode($stack, ',');
  496. }
  497. }
  498. else
  499. {
  500. preg_match($rsort, $ORDER, $order_match);
  501.  
  502. $where_clause .= ' ORDER BY "' . str_replace('.', '"."', $order_match[ 1 ]) . '"' . (isset($order_match[ 3 ]) ? ' ' . $order_match[ 3 ] : '');
  503. }
  504. }
  505.  
  506. if (isset($where[ 'LIMIT' ]))
  507. {
  508. $LIMIT = $where[ 'LIMIT' ];
  509.  
  510. if (is_numeric($LIMIT))
  511. {
  512. $where_clause .= ' LIMIT ' . $LIMIT;
  513. }
  514.  
  515. if (
  516. is_array($LIMIT) &&
  517. is_numeric($LIMIT[ 0 ]) &&
  518. is_numeric($LIMIT[ 1 ])
  519. )
  520. {
  521. if ($this->database_type === 'pgsql')
  522. {
  523. $where_clause .= ' OFFSET ' . $LIMIT[ 0 ] . ' LIMIT ' . $LIMIT[ 1 ];
  524. }
  525. else
  526. {
  527. $where_clause .= ' LIMIT ' . $LIMIT[ 0 ] . ',' . $LIMIT[ 1 ];
  528. }
  529. }
  530. }
  531. }
  532. else
  533. {
  534. if ($where != null)
  535. {
  536. $where_clause .= ' ' . $where;
  537. }
  538. }
  539.  
  540. return $where_clause;
  541. }
  542.  
  543. protected function select_context($table, $join, &$columns = null, $where = null, $column_fn = null)
  544. {
  545. $table = '"' . $this->prefix . $table . '"';
  546. $join_key = is_array($join) ? array_keys($join) : null;
  547.  
  548. if (
  549. isset($join_key[ 0 ]) &&
  550. strpos($join_key[ 0 ], '[') === 0
  551. )
  552. {
  553. $table_join = array();
  554.  
  555. $join_array = array(
  556. '>' => 'LEFT',
  557. '<' => 'RIGHT',
  558. '<>' => 'FULL',
  559. '><' => 'INNER'
  560. );
  561.  
  562. foreach($join as $sub_table => $relation)
  563. {
  564. preg_match('/(\[(\<|\>|\>\<|\<\>)\])?([a-zA-Z0-9_\-]*)\s?(\(([a-zA-Z0-9_\-]*)\))?/', $sub_table, $match);
  565.  
  566. if ($match[ 2 ] != '' && $match[ 3 ] != '')
  567. {
  568. if (is_string($relation))
  569. {
  570. $relation = 'USING ("' . $relation . '")';
  571. }
  572.  
  573. if (is_array($relation))
  574. {
  575. // For ['column1', 'column2']
  576. if (isset($relation[ 0 ]))
  577. {
  578. $relation = 'USING ("' . implode($relation, '", "') . '")';
  579. }
  580. else
  581. {
  582. $joins = array();
  583.  
  584. foreach ($relation as $key => $value)
  585. {
  586. $joins[] = $this->prefix . (
  587. strpos($key, '.') > 0 ?
  588. // For ['tableB.column' => 'column']
  589. '"' . str_replace('.', '"."', $key) . '"' :
  590.  
  591. // For ['column1' => 'column2']
  592. $table . '."' . $key . '"'
  593. ) .
  594. ' = ' .
  595. '"' . (isset($match[ 5 ]) ? $match[ 5 ] : $match[ 3 ]) . '"."' . $value . '"';
  596. }
  597.  
  598. $relation = 'ON ' . implode($joins, ' AND ');
  599. }
  600. }
  601.  
  602. $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN "' . $this->prefix . $match[ 3 ] . '" ' . (isset($match[ 5 ]) ? 'AS "' . $match[ 5 ] . '" ' : '') . $relation;
  603. }
  604. }
  605.  
  606. $table .= ' ' . implode($table_join, ' ');
  607. }
  608. else
  609. {
  610. if (is_null($columns))
  611. {
  612. if (is_null($where))
  613. {
  614. if (
  615. is_array($join) &&
  616. isset($column_fn)
  617. )
  618. {
  619. $where = $join;
  620. $columns = null;
  621. }
  622. else
  623. {
  624. $where = null;
  625. $columns = $join;
  626. }
  627. }
  628. else
  629. {
  630. $where = $join;
  631. $columns = null;
  632. }
  633. }
  634. else
  635. {
  636. $where = $columns;
  637. $columns = $join;
  638. }
  639. }
  640.  
  641. if (isset($column_fn))
  642. {
  643. if ($column_fn == 1)
  644. {
  645. $column = '1';
  646.  
  647. if (is_null($where))
  648. {
  649. $where = $columns;
  650. }
  651. }
  652. else
  653. {
  654. if (empty($columns))
  655. {
  656. $columns = '*';
  657. $where = $join;
  658. }
  659.  
  660. $column = $column_fn . '(' . $this->column_push($columns) . ')';
  661. }
  662. }
  663. else
  664. {
  665. $column = $this->column_push($columns);
  666. }
  667.  
  668. return 'SELECT ' . $column . ' FROM ' . $table . $this->where_clause($where);
  669. }
  670.  
  671. public function select($table, $join, $columns = null, $where = null)
  672. {
  673. $query = $this->query($this->select_context($table, $join, $columns, $where));
  674.  
  675. return $query ? $query->fetchAll(
  676. (is_string($columns) && $columns != '*') ? PDO::FETCH_COLUMN : PDO::FETCH_ASSOC
  677. ) : false;
  678. }
  679.  
  680. public function insert($table, $datas)
  681. {
  682. $lastId = array();
  683.  
  684. // Check indexed or associative array
  685. if (!isset($datas[ 0 ]))
  686. {
  687. $datas = array($datas);
  688. }
  689.  
  690. foreach ($datas as $data)
  691. {
  692. $values = array();
  693. $columns = array();
  694.  
  695. foreach ($data as $key => $value)
  696. {
  697. array_push($columns, $this->column_quote($key));
  698.  
  699. switch (gettype($value))
  700. {
  701. case 'NULL':
  702. $values[] = 'NULL';
  703. break;
  704.  
  705. case 'array':
  706. preg_match("/\(JSON\)\s*([\w]+)/i", $key, $column_match);
  707.  
  708. $values[] = isset($column_match[ 0 ]) ?
  709. $this->quote(json_encode($value)) :
  710. $this->quote(serialize($value));
  711. break;
  712.  
  713. case 'boolean':
  714. $values[] = ($value ? '1' : '0');
  715. break;
  716.  
  717. case 'integer':
  718. case 'double':
  719. case 'string':
  720. $values[] = $this->fn_quote($key, $value);
  721. break;
  722. }
  723. }
  724.  
  725. $this->exec('INSERT INTO "' . $this->prefix . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')');
  726.  
  727. $lastId[] = $this->pdo->lastInsertId();
  728. }
  729.  
  730. return count($lastId) > 1 ? $lastId : $lastId[ 0 ];
  731. }
  732.  
  733. public function update($table, $data, $where = null)
  734. {
  735. $fields = array();
  736.  
  737. foreach ($data as $key => $value)
  738. {
  739. preg_match('/([\w]+)(\[(\+|\-|\*|\/)\])?/i', $key, $match);
  740.  
  741. if (isset($match[ 3 ]))
  742. {
  743. if (is_numeric($value))
  744. {
  745. $fields[] = $this->column_quote($match[ 1 ]) . ' = ' . $this->column_quote($match[ 1 ]) . ' ' . $match[ 3 ] . ' ' . $value;
  746. }
  747. }
  748. else
  749. {
  750. $column = $this->column_quote($key);
  751.  
  752. switch (gettype($value))
  753. {
  754. case 'NULL':
  755. $fields[] = $column . ' = NULL';
  756. break;
  757.  
  758. case 'array':
  759. preg_match("/\(JSON\)\s*([\w]+)/i", $key, $column_match);
  760.  
  761. $fields[] = $column . ' = ' . $this->quote(
  762. isset($column_match[ 0 ]) ? json_encode($value) : serialize($value)
  763. );
  764. break;
  765.  
  766. case 'boolean':
  767. $fields[] = $column . ' = ' . ($value ? '1' : '0');
  768. break;
  769.  
  770. case 'integer':
  771. case 'double':
  772. case 'string':
  773. $fields[] = $column . ' = ' . $this->fn_quote($key, $value);
  774. break;
  775. }
  776. }
  777. }
  778.  
  779. return $this->exec('UPDATE "' . $this->prefix . $table . '" SET ' . implode(', ', $fields) . $this->where_clause($where));
  780. }
  781.  
  782. public function delete($table, $where)
  783. {
  784. return $this->exec('DELETE FROM "' . $this->prefix . $table . '"' . $this->where_clause($where));
  785. }
  786.  
  787. public function replace($table, $columns, $search = null, $replace = null, $where = null)
  788. {
  789. if (is_array($columns))
  790. {
  791. $replace_query = array();
  792.  
  793. foreach ($columns as $column => $replacements)
  794. {
  795. foreach ($replacements as $replace_search => $replace_replacement)
  796. {
  797. $replace_query[] = $column . ' = REPLACE(' . $this->column_quote($column) . ', ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')';
  798. }
  799. }
  800.  
  801. $replace_query = implode(', ', $replace_query);
  802. $where = $search;
  803. }
  804. else
  805. {
  806. if (is_array($search))
  807. {
  808. $replace_query = array();
  809.  
  810. foreach ($search as $replace_search => $replace_replacement)
  811. {
  812. $replace_query[] = $columns . ' = REPLACE(' . $this->column_quote($columns) . ', ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')';
  813. }
  814.  
  815. $replace_query = implode(', ', $replace_query);
  816. $where = $replace;
  817. }
  818. else
  819. {
  820. $replace_query = $columns . ' = REPLACE(' . $this->column_quote($columns) . ', ' . $this->quote($search) . ', ' . $this->quote($replace) . ')';
  821. }
  822. }
  823.  
  824. return $this->exec('UPDATE "' . $this->prefix . $table . '" SET ' . $replace_query . $this->where_clause($where));
  825. }
  826.  
  827. public function get($table, $join = null, $column = null, $where = null)
  828. {
  829. $query = $this->query($this->select_context($table, $join, $column, $where) . ' LIMIT 1');
  830.  
  831. if ($query)
  832. {
  833. $data = $query->fetchAll(PDO::FETCH_ASSOC);
  834.  
  835. if (isset($data[ 0 ]))
  836. {
  837. $column = $where == null ? $join : $column;
  838.  
  839. if (is_string($column) && $column != '*')
  840. {
  841. return $data[ 0 ][ $column ];
  842. }
  843.  
  844. return $data[ 0 ];
  845. }
  846. else
  847. {
  848. return false;
  849. }
  850. }
  851. else
  852. {
  853. return false;
  854. }
  855. }
  856.  
  857. public function has($table, $join, $where = null)
  858. {
  859. $column = null;
  860.  
  861. $query = $this->query('SELECT EXISTS(' . $this->select_context($table, $join, $column, $where, 1) . ')');
  862.  
  863. if ($query)
  864. {
  865. return $query->fetchColumn() === '1';
  866. }
  867. else
  868. {
  869. return false;
  870. }
  871. }
  872.  
  873. public function count($table, $join = null, $column = null, $where = null)
  874. {
  875. $query = $this->query($this->select_context($table, $join, $column, $where, 'COUNT'));
  876.  
  877. return $query ? 0 + $query->fetchColumn() : false;
  878. }
  879.  
  880. public function max($table, $join, $column = null, $where = null)
  881. {
  882. $query = $this->query($this->select_context($table, $join, $column, $where, 'MAX'));
  883.  
  884. if ($query)
  885. {
  886. $max = $query->fetchColumn();
  887.  
  888. return is_numeric($max) ? $max + 0 : $max;
  889. }
  890. else
  891. {
  892. return false;
  893. }
  894. }
  895.  
  896. public function min($table, $join, $column = null, $where = null)
  897. {
  898. $query = $this->query($this->select_context($table, $join, $column, $where, 'MIN'));
  899.  
  900. if ($query)
  901. {
  902. $min = $query->fetchColumn();
  903.  
  904. return is_numeric($min) ? $min + 0 : $min;
  905. }
  906. else
  907. {
  908. return false;
  909. }
  910. }
  911.  
  912. public function avg($table, $join, $column = null, $where = null)
  913. {
  914. $query = $this->query($this->select_context($table, $join, $column, $where, 'AVG'));
  915.  
  916. return $query ? 0 + $query->fetchColumn() : false;
  917. }
  918.  
  919. public function sum($table, $join, $column = null, $where = null)
  920. {
  921. $query = $this->query($this->select_context($table, $join, $column, $where, 'SUM'));
  922.  
  923. return $query ? 0 + $query->fetchColumn() : false;
  924. }
  925.  
  926. public function action($actions)
  927. {
  928. if (is_callable($actions))
  929. {
  930. $this->pdo->beginTransaction();
  931.  
  932. $result = $actions($this);
  933.  
  934. if ($result === false)
  935. {
  936. $this->pdo->rollBack();
  937. }
  938. else
  939. {
  940. $this->pdo->commit();
  941. }
  942. }
  943. else
  944. {
  945. return false;
  946. }
  947. }
  948.  
  949. public function debug()
  950. {
  951. $this->debug_mode = true;
  952.  
  953. return $this;
  954. }
  955.  
  956. public function error()
  957. {
  958. return $this->pdo->errorInfo();
  959. }
  960.  
  961. public function last_query()
  962. {
  963. return end($this->logs);
  964. }
  965.  
  966. public function log()
  967. {
  968. return $this->logs;
  969. }
  970.  
  971. public function info()
  972. {
  973. $output = array(
  974. 'server' => 'SERVER_INFO',
  975. 'driver' => 'DRIVER_NAME',
  976. 'client' => 'CLIENT_VERSION',
  977. 'version' => 'SERVER_VERSION',
  978. 'connection' => 'CONNECTION_STATUS'
  979. );
  980.  
  981. foreach ($output as $key => $value)
  982. {
  983. $output[ $key ] = $this->pdo->getAttribute(constant('PDO::ATTR_' . $value));
  984. }
  985.  
  986. return $output;
  987. }
  988. }
  989. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement