PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/db.php

https://github.com/MilkZoft/zan
PHP | 1118 lines | 1118 code | 0 blank | 0 comment | 1 complexity | 3d1d35fd5c142847d4cd4d24c85972db MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_Db extends ZP_Load
  6. {
  7. private $caching = false;
  8. private $encode = true;
  9. private $fetchMode = "assoc";
  10. private $fields = "*";
  11. private $inserts = 0;
  12. private $join = null;
  13. private $primaryKey = false;
  14. private $query;
  15. private $row;
  16. private $select = "SELECT *";
  17. private $SQL = null;
  18. private $table;
  19. private $values;
  20. private $where = null;
  21. private $PDO = false;
  22. private static $connection = false;
  23. public $Rs = null;
  24. public function __construct()
  25. {
  26. $this->Cache = $this->core("Cache");
  27. $this->config("database");
  28. $this->exception("database");
  29. $this->helper(array("exceptions", "string"));
  30. $this->connect();
  31. }
  32. public function begin()
  33. {
  34. return ($this->PDO) ? $this->Database->beginTransaction() : $this->Database->begin();
  35. }
  36. public function call($procedure)
  37. {
  38. if ($this->PDO) {
  39. $this->Rs = $this->Database->prepare("CALL $procedure");
  40. $this->Rs->bindParam(1, $data, PDO::PARAM_STR, 4000);
  41. $data = $this->Rs->execute();
  42. } else {
  43. $data = $this->Database->call($procedure);
  44. }
  45. if ($this->encode) {
  46. $data = isset($data) ? $this->encoding($data) : false;
  47. } else {
  48. $data = isset($data) ? $data : false;
  49. }
  50. return $data;
  51. }
  52. public function close()
  53. {
  54. if ($this->PDO) {
  55. return empty($this->Database);
  56. }
  57. return (!self::$connection) ? false : $this->Database->close(self::$connection);
  58. }
  59. public function columns($table)
  60. {
  61. return $this->data("SHOW COLUMNS FROM ". $this->getTable($table) ."");
  62. }
  63. public function commit()
  64. {
  65. return $this->Database->commit();
  66. }
  67. public function connect()
  68. {
  69. if (!file_exists("www/config/database.php")) {
  70. getException("You must rename and configure your 'config/database.php'");
  71. }
  72. if (!self::$connection) {
  73. $port = (DB_PORT === 3306) ? "" : ":". DB_PORT;
  74. if (DB_PDO) {
  75. self::$connection = true;
  76. $this->PDO = DB_PDO;
  77. if (DB_DRIVER === "mysqli" or DB_DRIVER === "mysql") {
  78. try {
  79. $this->Database = new PDO("mysql:host=". DB_HOST . $port .";dbname=". DB_DATABASE, DB_USER, DB_PWD);
  80. } catch (PDOException $e) {
  81. getException("Database Error: ". $e->getMessage());
  82. }
  83. } elseif (DB_DRIVER === "pgsql") {
  84. try {
  85. $this->Database = new PDO("pgsql:host=". DB_HOST . $port .";dbname=". DB_DATABASE, DB_USER, DB_PWD);
  86. } catch (PDOException $e) {
  87. getException("Database Error: ". $e->getMessage());
  88. }
  89. } elseif (DB_DRIVER === "sqlite") {
  90. try {
  91. $this->Database = new PDO("sqlite:". DB_SQLITE_FILENAME);
  92. } catch (PDOException $e) {
  93. getException("Database Error: ". $e->getMessage());
  94. }
  95. } elseif (DB_DRIVER === "oracle") {
  96. try {
  97. $this->Database = new PDO("OCI:dbname=". DB_DATABASE .";charset=UTF-8", DB_USER, DB_PWD);
  98. } catch (PDOException $e) {
  99. getException("Database Error: ". $e->getMessage());
  100. }
  101. }
  102. } else {
  103. if (DB_DRIVER === "mysqli") {
  104. $this->Database = $this->driver("MySQLi_Db");
  105. self::$connection = $this->Database->connect();
  106. } elseif (DB_DRIVER === "mysql") {
  107. $this->Database = $this->driver("MySQL_Db");
  108. self::$connection = $this->Database->connect();
  109. } elseif (DB_DRIVER === "pgsql") {
  110. $this->Database = $this->driver("PgSQL_Db");
  111. self::$connection = $this->Database->connect();
  112. } elseif (DB_DRIVER === "sqlite") {
  113. $this->Database = $this->driver("SQLite_Db");
  114. self::$connection = $this->Database->connect();
  115. } elseif (DB_DRIVER === "oracle") {
  116. $this->Database = $this->driver("Oracle_Db");
  117. self::$connection = $this->Database->connect();
  118. } elseif (DB_DRIVER === "mssql") {
  119. $this->Database = $this->driver("MsSQL_Db");
  120. self::$connection = $this->Database->connect();
  121. }
  122. }
  123. }
  124. }
  125. public function countAll($table = null)
  126. {
  127. $this->table($table);
  128. $query = "SELECT COUNT(1) AS Total FROM $this->table";
  129. $data = $this->data($query);
  130. return isset($data[0]["Total"]) ? (int) $data[0]["Total"] : 0;
  131. }
  132. public function countBySQL($SQL, $table = null)
  133. {
  134. if ($SQL === "") {
  135. return false;
  136. }
  137. $this->table($table);
  138. $query = "SELECT COUNT(1) AS Total FROM $this->table WHERE $SQL";
  139. $data = $this->data($query);
  140. return (isset($data[0]["Total"]) and $data[0]["Total"]) ? (int) $data[0]["Total"] : 0;
  141. }
  142. private function data($query)
  143. {
  144. if ($query === "") {
  145. return false;
  146. }
  147. $this->Rs = $this->Database->query($query);
  148. if ($this->rows() === 0) {
  149. return false;
  150. } else {
  151. while ($row = $this->fetch($this->rows())) {
  152. $rows[] = $row;
  153. }
  154. }
  155. $this->free();
  156. if ($this->encode) {
  157. return isset($rows) ? $this->encoding($rows) : false;
  158. } else {
  159. return isset($rows) ? $rows : false;
  160. }
  161. }
  162. public function delete($ID = 0, $table = null)
  163. {
  164. if ($ID === 0) {
  165. return false;
  166. }
  167. if ($table) {
  168. $this->table($table);
  169. }
  170. return $this->Database->delete($this->table, $ID, $this->primaryKey);
  171. }
  172. public function deleteBy($field = null, $value = null, $table = null, $limit = 1)
  173. {
  174. if (!$field or !$value) {
  175. return false;
  176. }
  177. if ($table) {
  178. $this->table($table);
  179. }
  180. return $this->Database->deleteBy($this->table, $field, $value, $limit);
  181. }
  182. public function deleteBySQL($SQL = null, $table = null)
  183. {
  184. if (!$SQL) {
  185. return false;
  186. }
  187. if ($table) {
  188. $this->table($table);
  189. }
  190. if (DB_PDO) {
  191. return $this->Database->query("DELETE FROM $this->table WHERE $SQL");
  192. }
  193. return $this->Database->deleteBySQL($this->table, $SQL);
  194. }
  195. public function encode($encode = true)
  196. {
  197. $this->encode = $encode;
  198. }
  199. private function encoding($rows)
  200. {
  201. $this->encode = true;
  202. if (is_object($rows)) {
  203. $array[] = get_object_vars($rows);
  204. $key1 = array_keys($array);
  205. $size1 = sizeof($key1);
  206. for ($i = 0; $i < $size1; $i++) {
  207. $key2 = array_keys($array[$i]);
  208. $size2 = sizeof($key2);
  209. for ($j = 0; $j < $size2; $j++) {
  210. if ($array[$i][$key2[$j]] === "1") {
  211. $data[$i][$key2[$j]] = 1;
  212. } elseif ($array[$i][$key2[$j]] === "0") {
  213. $data[$i][$key2[$j]] = 0;
  214. } else {
  215. $data[$i][$key2[$j]] = encode($array[$i][$key2[$j]]);
  216. }
  217. }
  218. }
  219. return $data;
  220. } elseif (is_array($rows)) {
  221. $key1 = array_keys($rows);
  222. $size1 = sizeof($key1);
  223. for ($i = 0; $i < $size1; $i++) {
  224. $key2 = array_keys($rows[$i]);
  225. $size2 = sizeof($key2);
  226. for ($j = 0; $j < $size2; $j++) {
  227. if ($rows[$i][$key2[$j]] === "1") {
  228. $data[$i][$key2[$j]] = 1;
  229. } elseif ($rows[$i][$key2[$j]] === "0") {
  230. $data[$i][$key2[$j]] = 0;
  231. } else {
  232. $data[$i][$key2[$j]] = encode($rows[$i][$key2[$j]]);
  233. }
  234. }
  235. }
  236. return $data;
  237. } else {
  238. return false;
  239. }
  240. }
  241. public function fetch($count = 0)
  242. {
  243. if ($this->PDO) {
  244. return (!$this->Rs) ? false : $this->Rs->fetch(PDO::FETCH_ASSOC);
  245. } else {
  246. return (!$this->Rs) ? false : $this->Database->fetch($count);
  247. }
  248. }
  249. public function fetchMode($fetch = "assoc")
  250. {
  251. $this->fetchMode = $fetch;
  252. }
  253. public function find($ID, $table = null, $fields = "*")
  254. {
  255. if ($table) {
  256. $this->table($table, $fields);
  257. }
  258. $query = "SELECT $this->fields FROM $this->table WHERE $this->primaryKey = '$ID'";
  259. return $this->data($query);
  260. }
  261. public function findAll($table = null, $fields = "*", $group = null, $order = null, $limit = null)
  262. {
  263. $SQL = null;
  264. if ($table) {
  265. $this->table($table, $fields);
  266. }
  267. if (!is_null($group)) {
  268. $SQL .= " GROUP BY ". $group;
  269. }
  270. if (!$order) {
  271. $SQL .= "";
  272. } elseif ($order === "DESC") {
  273. $SQL .= " ORDER BY $this->primaryKey DESC";
  274. } elseif (!is_null($order)) {
  275. $SQL .= " ORDER BY ". $order;
  276. } elseif (is_null($order)) {
  277. $SQL .= " ORDER BY $this->primaryKey";
  278. }
  279. if (!is_null($limit)) {
  280. $SQL .= " LIMIT ". $limit;
  281. }
  282. $query = "SELECT $this->fields FROM $this->table$SQL";
  283. return $this->data($query);
  284. }
  285. public function findBy($field = null, $value = null, $table = null, $fields = "*", $group = null, $order = null, $limit = null)
  286. {
  287. $SQL = null;
  288. if ($table) {
  289. $this->table($table, $fields);
  290. }
  291. if (!is_null($group)) {
  292. $SQL .= " GROUP BY ". $group;
  293. }
  294. if (!$order) {
  295. $SQL .= "";
  296. } elseif ($order === "DESC") {
  297. $SQL .= " ORDER BY $this->primaryKey";
  298. } elseif (!is_null($order)) {
  299. $SQL .= " ORDER BY ". $order;
  300. } elseif ($order === "") {
  301. $SQL .= " ORDER BY $this->primaryKey";
  302. }
  303. if (!is_null($limit)) {
  304. $SQL .= " LIMIT ". $limit;
  305. }
  306. if (is_array($field)) {
  307. $i = 0;
  308. $_SQL = null;
  309. foreach ($field as $_field => $_value) {
  310. $_SQL .= "$_field = '$_value' AND ";
  311. }
  312. $_SQL = rtrim($_SQL, "AND ");
  313. $query = "SELECT $this->fields FROM $this->table WHERE $_SQL";
  314. } else {
  315. $query = "SELECT $this->fields FROM $this->table WHERE $field = '$value'$SQL";
  316. }
  317. return $this->data($query);
  318. }
  319. public function findBySQL($SQL, $table = null, $fields = "*", $group = null, $order = null, $limit = null)
  320. {
  321. if (!is_null($group)) {
  322. $SQL .= " GROUP BY ". $group;
  323. }
  324. if ($table) {
  325. $this->table($table, $fields);
  326. }
  327. if (is_null($order)) {
  328. $SQL .= "";
  329. } elseif ($order === "DESC") {
  330. $SQL .= " ORDER BY $this->primaryKey DESC";
  331. } elseif (!is_null($order)) {
  332. $SQL .= " ORDER BY ". $order;
  333. } elseif ($order === "") {
  334. $SQL .= " ORDER BY $this->primaryKey";
  335. }
  336. if ($limit) {
  337. $SQL .= " LIMIT ". $limit;
  338. }
  339. $query = "SELECT $this->fields FROM $this->table WHERE $SQL";
  340. return $this->data($query);
  341. }
  342. public function findFirst($table = null, $fields = "*")
  343. {
  344. if ($table) {
  345. $this->table($table, $fields);
  346. }
  347. $query = "SELECT $this->fields FROM $this->table ORDER BY $this->primaryKey ASC LIMIT 1";
  348. return $this->data($query);
  349. }
  350. public function findLast($table = null, $fields = "*")
  351. {
  352. if ($table) {
  353. $this->table($table, $fields);
  354. }
  355. $query = "SELECT $this->fields FROM $this->table ORDER BY $this->primaryKey DESC LIMIT 1";
  356. return $this->data($query);
  357. }
  358. public function free()
  359. {
  360. if ($this->PDO) {
  361. return ($this->Rs) ? $this->Rs->closeCursor() : false;
  362. }
  363. return ($this->Rs) ? $this->Rs->free() : false;
  364. }
  365. public function from($table)
  366. {
  367. $table = str_replace(DB_PREFIX, "", $table);
  368. $this->from = DB_PREFIX . $table;
  369. return $this;
  370. }
  371. public function get($table = null, $limit = 0, $offset = 0)
  372. {
  373. $table = str_replace(DB_PREFIX, "", $table);
  374. $table = ($table !== "") ? DB_PREFIX . $table : ($table !== "");
  375. if ($limit === 0 and $offset === 0) {
  376. $query = ($table) ? "$this->select FROM $table $this->join $this->where" : "$this->select FROM $this->from $this->join $this->where";
  377. } else {
  378. if ($table) {
  379. $query = "$this->select FROM $table $this->join $this->where LIMIT $limit, $offset";
  380. } else {
  381. $query = "$this->select FROM $this->from $this->join $this->where LIMIT $limit, $offset";
  382. }
  383. }
  384. $this->cleanUp();
  385. return $this->data($query);
  386. }
  387. public function cleanUp()
  388. {
  389. $this->select = null;
  390. $this->from = null;
  391. $this->join = null;
  392. $this->where = null;
  393. return true;
  394. }
  395. public function getTable($table)
  396. {
  397. $table = str_replace(DB_PREFIX, "", $table);
  398. $this->table($table);
  399. return DB_PREFIX . $table;
  400. }
  401. public function getWhere($table, $where, $limit = 0, $offset = 0)
  402. {
  403. foreach ($where as $field => $value) {
  404. $_where = "$field = '$value' AND ";
  405. }
  406. $_where = rtrim($_where, "AND ");
  407. $table = $this->getTable($table);
  408. if ($limit === 0 and $offset === 0) {
  409. $query = "$this->select FROM $table WHERE $_where";
  410. } else {
  411. $query = "SELECT $this->fields FROM $table WHERE $_where LIMIT $limit, $offset";
  412. }
  413. return $this->data($query);
  414. }
  415. public function insert($table = null, $data = null)
  416. {
  417. if (!$table) {
  418. if (!$this->table or !$this->fields or !$this->values) {
  419. return false;
  420. } else {
  421. $table = $this->table;
  422. $fields = $this->fields;
  423. $values = $this->values;
  424. }
  425. }
  426. $table = $this->getTable($table);
  427. if (is_array($data)) {
  428. $count = count($data) - 1;
  429. $_fields = null;
  430. $_values = null;
  431. $i = 0;
  432. foreach ($data as $field => $value) {
  433. if ($i === $count) {
  434. $_fields .= "$field";
  435. $_values .= "'$value'";
  436. } else {
  437. $_fields .= "$field, ";
  438. $_values .= "'$value', ";
  439. }
  440. $i++;
  441. }
  442. $query = "INSERT INTO $table ($_fields) VALUES ($_values)";
  443. } else {
  444. return false;
  445. }
  446. if (DB_PDO) {
  447. $this->Rs = $this->Database->exec($query);
  448. return $this->Database->lastInsertId();
  449. }
  450. $this->Rs = $this->Database->insert($table, $_fields, $_values);
  451. if ($this->Rs) {
  452. return ($this->PDO) ? $this->Database->lastInsertId() : $this->Database->insertID();
  453. }
  454. return false;
  455. }
  456. public function insertBatch($table, $data)
  457. {
  458. if (!$table or !$data) {
  459. return false;
  460. }
  461. $table = $this->getTable($table);
  462. if (isset($data[0])) {
  463. $count = count($data) - 1;
  464. $values = null;
  465. $_fields = null;
  466. $_values = null;
  467. $query = null;
  468. $i = 0;
  469. $j = 0;
  470. foreach ($data as $insert) {
  471. $total = count($data[$i]) - 1;
  472. foreach ($insert as $field => $value) {
  473. if ($j === $total) {
  474. $_fields .= "$field";
  475. $_values .= "'$value'";
  476. } else {
  477. $_fields .= "$field, ";
  478. $_values .= "'$value', ";
  479. }
  480. $j++;
  481. }
  482. $values .= ($i === $count) ? "($_values)" : "($_values), ";
  483. $fields = $_fields;
  484. $_fields = null;
  485. $_values = null;
  486. $i++;
  487. $j = 0;
  488. }
  489. $query .= "INSERT INTO $table ($fields) VALUES $values;";
  490. } else {
  491. return false;
  492. }
  493. return ($this->Database->query($query)) ? true : false;
  494. }
  495. public function insertID($table = null)
  496. {
  497. if ($table) {
  498. $query = "SELECT TOP 1 $this->primaryKey FROM $this->table ORDER BY $this->primaryKey DESC";
  499. $data = $this->data($query);
  500. return $data[0]["$primaryKey"];
  501. } else {
  502. return (self::$connection) ? $this->Database->insert_ID() : false;
  503. }
  504. }
  505. public function join($table, $condition, $position = false)
  506. {
  507. if (!$table or !$condition) {
  508. return false;
  509. }
  510. $this->join .= (!$position) ? "JOIN $table ON $condition " : "$position JOIN $table ON $condition ";
  511. return $this;
  512. }
  513. public function like($data, $match = null, $position = "both")
  514. {
  515. if (is_array($data)) {
  516. $count = count($data) - 1;
  517. $_where = null;
  518. $i = 0;
  519. foreach ($data as $field => $value) {
  520. if ($i === $count) {
  521. if ($position === "both") {
  522. $_where .= "$field LIKE '%$match%'";
  523. } elseif ($position === "before") {
  524. $_where .= "$field LIKE '%$match'";
  525. } elseif ($postion === "after") {
  526. $_where .= "$field LIKE '$match%'";
  527. }
  528. } else {
  529. if ($position === "both") {
  530. $_where .= " AND $field LIKE '%$match%'";
  531. } elseif ($position === "before") {
  532. $_where .= " AND $field LIKE '%$match'";
  533. } elseif ($postion === "after") {
  534. $_where .= " AND $field LIKE '$match%'";
  535. }
  536. }
  537. }
  538. } else {
  539. if (is_null($this->where)) {
  540. if ($position === "both") {
  541. $this->where = "WHERE $data LIKE '%$match%'";
  542. } elseif ($position === "before") {
  543. $this->where = "WHERE $data LIKE '%$match'";
  544. } elseif ($position === "after") {
  545. $this->where = "WHERE $data LIKE '$match%'";
  546. }
  547. } else {
  548. if ($position === "both") {
  549. $this->where .= " AND $data LIKE '%$match%'";
  550. } elseif ($position === "before") {
  551. $this->where .= " AND $data LIKE '%$match'";
  552. } elseif ($position === "after") {
  553. $this->where .= " AND $data LIKE '$match%'";
  554. }
  555. }
  556. }
  557. return $this;
  558. }
  559. public function notLike($data, $match = false, $position = "both")
  560. {
  561. if (is_array($data)) {
  562. $count = count($data) - 1;
  563. $_where = null;
  564. $i = 0;
  565. foreach ($data as $field => $value) {
  566. if ($i === $count) {
  567. if ($position === "both") {
  568. $_where .= "$field NOT LIKE '%$match%'";
  569. } elseif ($position === "before") {
  570. $_where .= "$field NOT LIKE '%$match'";
  571. } elseif ($postion === "after") {
  572. $_where .= "$field NOT LIKE '$match%'";
  573. }
  574. } else {
  575. if ($position === "both") {
  576. $_where .= " AND $field NOT LIKE '%$match%'";
  577. } elseif ($position === "before") {
  578. $_where .= " AND $field NOT LIKE '%$match'";
  579. } elseif ($postion === "after") {
  580. $_where .= " AND $field NOT LIKE '$match%'";
  581. }
  582. }
  583. }
  584. if (!is_null($this->where)) {
  585. $this->where .= " OR $field NOT IN ($values)";
  586. }
  587. } else {
  588. if (!is_null($this->where)) {
  589. $this->where .= " OR $field NOT IN ('$data')";
  590. }
  591. }
  592. return $this;
  593. }
  594. public function orLike($data, $match = false, $position = "both")
  595. {
  596. if (is_array($data)) {
  597. $count = count($data) - 1;
  598. $_where = null;
  599. $i = 0;
  600. foreach ($data as $field => $value) {
  601. if ($i === $count) {
  602. if ($position === "both") {
  603. $_where .= "$field LIKE '%$match%'";
  604. } elseif ($position === "before") {
  605. $_where .= "$field LIKE '%$match'";
  606. } elseif ($postion === "after") {
  607. $_where .= "$field LIKE '$match%'";
  608. }
  609. } else {
  610. if ($position === "both") {
  611. $_where .= " $field LIKE '%$match%' OR";
  612. } elseif ($position === "before") {
  613. $_where .= " $field LIKE '%$match' OR";
  614. } elseif ($postion === "after") {
  615. $_where .= " $field LIKE '$match%' OR";
  616. }
  617. }
  618. }
  619. if (!is_null($this->where)) {
  620. $this->where .= " OR $field NOT IN ($values)";
  621. }
  622. } else {
  623. if (!is_null($this->where)) {
  624. $this->where .= " OR $field NOT IN ('$data')";
  625. }
  626. }
  627. return $this;
  628. }
  629. public function orWhereIn($field, $data)
  630. {
  631. if (is_array($data)) {
  632. for ($i = 0; $i <= count($data) - 1; $i++) {
  633. $values .= ($i === count($data) - 1) ? "'$data[$i]'" : "'$data[$i]', ";
  634. }
  635. if (!is_null($this->where)) {
  636. $this->where .= " OR $field IN ($values)";
  637. }
  638. } else {
  639. if (!is_null($this->where)) {
  640. $this->where .= " OR $field IN ('$data')";
  641. }
  642. }
  643. return $this;
  644. }
  645. public function orWhereNotIn($field, $data)
  646. {
  647. if (is_array($data)) {
  648. for ($i = 0; $i <= count($data) - 1; $i++) {
  649. $values .= ($i === count($data) - 1) ? "'$data[$i]'" : "'$data[$i]', ";
  650. }
  651. if (!is_null($this->where)) {
  652. $this->where .= " OR $field NOT IN ($values)";
  653. }
  654. } else {
  655. if (!is_null($this->where)) {
  656. $this->where .= " OR $field NOT IN ('$data')";
  657. }
  658. }
  659. return $this;
  660. }
  661. public function query($query)
  662. {
  663. return $this->data($query);
  664. }
  665. public function rollBack()
  666. {
  667. return $this->Database->rollBack();
  668. }
  669. public function rows()
  670. {
  671. if ($this->PDO) {
  672. return (!$this->Rs) ? false : $this->Rs->rowCount();
  673. }
  674. return (!$this->Rs) ? false : $this->Database->rows();
  675. }
  676. public function save($option = null)
  677. {
  678. if (is_null($option)) {
  679. return $this->insert();
  680. } elseif ($option > 0) {
  681. return $this->update(false, false, $option);
  682. } elseif ($option === "begin") {
  683. return $this->insert(true);
  684. } elseif ($option) {
  685. return $this->updateBySQL();
  686. }
  687. }
  688. public function select($fields = "*", $normal = true)
  689. {
  690. $this->select = (!$normal) ? $fields : "SELECT $fields";
  691. $this->_fields = $fields;
  692. return $this;
  693. }
  694. public function selectAvg($field, $as = null)
  695. {
  696. $this->select = (isset($field) and $as) ? "SELECT AVG($field) as $as" : "SELECT AVG($field) as $field";
  697. return $this;
  698. }
  699. public function selectMax($field, $as = null)
  700. {
  701. $this->select = (isset($field) and $as) ? "SELECT MAX($field) as $as" : "SELECT MAX($field) as $field";
  702. return $this;
  703. }
  704. public function selectMin($field, $as = null)
  705. {
  706. $this->select = (isset($min) and $as) ? "SELECT MIN($field) as $as" : "SELECT MIN($field) as $field";
  707. return $this;
  708. }
  709. public function selectSum($field, $as = null)
  710. {
  711. $this->select = (isset($field) and $as) ? "SELECT SUM($field) as $as" : "SELECT SUM($field) as $field";
  712. return $this;
  713. }
  714. public function table($table, $fields = "*")
  715. {
  716. $fields = is_null($fields) ? "*" : $fields;
  717. $table = str_replace(DB_PREFIX, "", $table);
  718. $this->table = DB_PREFIX . $table;
  719. $this->fields = $fields;
  720. $data = $this->data("SHOW COLUMNS FROM $this->table");
  721. if (is_array($data)) {
  722. foreach ($data as $column) {
  723. if ($column["Key"] === "PRI") {
  724. $this->primaryKey = $column["Field"];
  725. return $this->primaryKey;
  726. }
  727. }
  728. }
  729. return false;
  730. }
  731. public function update($table = null, $fields = null, $ID = 0, $primaryKey = null)
  732. {
  733. if (!$table or !$fields) {
  734. if (!$this->table or !$this->fields) {
  735. return false;
  736. } else {
  737. $table = $this->table;
  738. $fields = $this->values;
  739. }
  740. }
  741. $table = $this->getTable($table);
  742. $primaryKey = is_null($primaryKey) ? $this->primaryKey : $primaryKey;
  743. if (is_array($fields)) {
  744. $count = count($fields) - 1;
  745. $_fields = null;
  746. $_values = null;
  747. $i = 0;
  748. foreach ($fields as $field => $value) {
  749. if (is_null($value)) {
  750. $_values .= "$field = DEFAULT, ";
  751. } else {
  752. $_values .= "$field = '$value', ";
  753. }
  754. }
  755. $_values = rtrim($_values, ", ");
  756. if ($ID > 0) {
  757. $query = "UPDATE $table SET $_values WHERE $primaryKey = '$ID'";
  758. } elseif (is_string($ID)) {
  759. $query = "UPDATE $table SET $_values WHERE $ID";
  760. } else {
  761. $query = "UPDATE $table SET $_values";
  762. }
  763. } else {
  764. if ($ID > 0) {
  765. $query = "UPDATE $table SET $fields WHERE $primaryKey = '$ID'";
  766. } elseif (is_string($ID)) {
  767. $query = "UPDATE $table SET $fields WHERE $ID";
  768. } else {
  769. $query = "UPDATE $table SET $fields";
  770. }
  771. }
  772. $this->Rs = $this->Database->query($query);
  773. if ($this->Rs) {
  774. return true;
  775. } else {
  776. return array("error" => $query);
  777. }
  778. }
  779. public function updateBySQL($table = null, $SQL = null)
  780. {
  781. if (!$table or !$SQL) {
  782. if (!$this->table or !$this->SQL) {
  783. return false;
  784. } else {
  785. $table = $this->table;
  786. $SQL = $this->SQL;
  787. }
  788. }
  789. $table = $this->getTable($table);
  790. if (DB_PDO) {
  791. $query = "UPDATE $table SET $SQL";
  792. return ($this->Database->exec($query)) ? true : false;;
  793. }
  794. return $this->Database->updateBySQL($table, $SQL);
  795. }
  796. public function values($values)
  797. {
  798. $this->values = $values;
  799. return $this;
  800. }
  801. public function where($data, $value = null)
  802. {
  803. if (is_array($data)) {
  804. $count = count($data) - 1;
  805. $i = 0;
  806. $_where = null;
  807. $this->where = null;
  808. foreach ($data as $field => $value) {
  809. $parts = explode(" ", $field);
  810. if ($i === $count) {
  811. $_where .= (count($parts) === 2) ? "$parts[0] $parts[1] '$value'" : "$field = '$value'";
  812. } else {
  813. $_where .= (count($parts) === 2) ? "$parts[0] $parts[1] '$value' AND " : "$field = '$value' AND ";
  814. }
  815. unset($parts);
  816. $i++;
  817. }
  818. $this->where = (is_null($this->where)) ? "WHERE $_where" : " AND $_where";
  819. } else {
  820. if (isset($data) and !$value) {
  821. $this->where = (is_null($this->where)) ? "WHERE $data" : " $data";
  822. } else {
  823. if (is_null($this->where)) {
  824. $parts = explode(" ", $data);
  825. $this->where = (count($parts) === 2) ? "WHERE parts[0] $parts[1] '$value'" : "WHERE $data = '$value'";
  826. } else {
  827. $parts = explode(" ", $data);
  828. $this->where .= (count($parts) === 2) ? " AND $parts[0] $parts[1] '$value'" : " AND $data = '$value'";
  829. }
  830. }
  831. }
  832. return $this;
  833. }
  834. public function whereIn($field, $data)
  835. {
  836. if (is_array($data)) {
  837. $values = null;
  838. for ($i = 0; $i <= count($data) - 1; $i++) {
  839. $values .= ($i === count($data) - 1) ? "'$data[$i]'" : "'$data[$i]', ";
  840. }
  841. if (is_null($this->where)) {
  842. $this->where = "WHERE $field IN ($values)";
  843. } else {
  844. $this->where .= " AND $field IN ($values)";
  845. }
  846. } else {
  847. if (is_null($this->where)) {
  848. $this->where = "WHERE $field IN ('$data')";
  849. } else {
  850. $this->where .= " AND $field IN ('$data')";
  851. }
  852. }
  853. return $this;
  854. }
  855. public function whereNotIn($field, $data)
  856. {
  857. if (is_array($data)) {
  858. for ($i = 0; $i <= count($data) - 1; $i++) {
  859. $values .= ($i === count($data) - 1) ? "'$data[$i]'" : "'$data[$i]', ";
  860. }
  861. if (is_null($this->where)) {
  862. $this->where = "WHERE $field NOT IN ($values)";
  863. } else {
  864. $this->where .= " AND $field NOT IN ($values)";
  865. }
  866. } else {
  867. if (is_null($this->where)) {
  868. $this->where = "WHERE $field NOT IN ('$data')";
  869. } else {
  870. $this->where .= " AND $field NOT IN ('$data')";
  871. }
  872. }
  873. return $this;
  874. }
  875. public function whereOr($data, $value = null)
  876. {
  877. if (is_array($data)) {
  878. $count = count($data) - 1;
  879. $i = 0;
  880. $_where = null;
  881. $this->where = null;
  882. foreach ($data as $field => $value) {
  883. $parts = explode(" ", $field);
  884. if ($i === $count) {
  885. $_where .= (count($parts) === 2) ? "$parts[0] $parts[1] '$value'" : "$field = '$value'";
  886. } else {
  887. $_where .= (count($parts) === 2) ? "$parts[0] $parts[1] '$value' OR " : "$field = '$value' OR ";
  888. }
  889. unset($parts);
  890. $i++;
  891. }
  892. if (is_null($this->where)) {
  893. $this->where = "WHERE $_where";
  894. } else {
  895. $this->where .= " OR $_where";
  896. }
  897. } else {
  898. if (isset($data) and !$value) {
  899. if (is_null($this->where)) {
  900. $this->where = "WHERE $data";
  901. } else {
  902. $this->where .= " $data";
  903. }
  904. } else {
  905. if (is_null($this->where)) {
  906. $parts = explode(" ", $data);
  907. $this->where = (count($parts) === 2) ? "WHERE parts[0] $parts[1] '$value'" : "WHERE $data = '$value'";
  908. } else {
  909. $parts = explode(" ", $data);
  910. $this->where .= (count($parts) === 2) ? " OR $parts[0] $parts[1] '$value'" : " OR $data = '$value'";
  911. }
  912. }
  913. }
  914. return $this;
  915. }
  916. }