PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/code/classes/DAO/SQLite3.class.php

https://github.com/blekkzor/pinetd2
PHP | 133 lines | 120 code | 13 blank | 0 comment | 28 complexity | 4be4ae177c233b945f23c59da11ad5a5 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace DAO;
  3. class SQLite3 extends \DAO\Base {
  4. public function __construct($SQL, $table, $key) {
  5. $this->SQL = $SQL;
  6. parent::__construct($table, $key);
  7. }
  8. public function buildWhere($key, $key_val) {
  9. if (!is_array($key)) {
  10. return $this->buildWhere(array($key), array($key_val));
  11. }
  12. $res = '';
  13. foreach($key as $idx => $k) {
  14. $res.=($res == ''?'':' AND ').'`'.$k.'` = \''.$this->SQL->escape_string($key_val[$idx]).'\'';
  15. }
  16. return $res;
  17. }
  18. public function deleteBean($bean) {
  19. $key = $this->key;
  20. $query = 'DELETE FROM '.$this->formatTable($this->table).' WHERE '.$this->buildWhere($key, $bean->__get($key));
  21. return $this->SQL->query($query);
  22. }
  23. public function delete(array $where) {
  24. $query = 'DELETE FROM '.$this->formatTable($this->table).' WHERE '.$this->buildQuickWhere($where);
  25. return $this->SQL->query($query);
  26. }
  27. public function formatTable($table) {
  28. if (is_array($table)) {
  29. return '`'.implode('`.`',$table).'`';
  30. }
  31. return '`'.$table.'`';
  32. }
  33. public function createUpdateQuery($data, $table, $qwhere) {
  34. $query = '';
  35. foreach($data as $var=>$val) {
  36. $query .= ($query == ''?'':', ').'`'.$var.'` = '.(is_null($val)?'NULL':'\''.$this->SQL->escape_string($val).'\'');
  37. }
  38. $query = 'UPDATE '.$this->formatTable($table).' SET '.$query.' ';
  39. if (!is_null($qwhere)) {
  40. if (is_array($qwhere)) {
  41. $query.='WHERE ';
  42. $first = true;
  43. foreach($qwhere as $var=>$val) {
  44. if (is_int($var)) {
  45. $query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
  46. } else {
  47. $query.=($first?'':' AND ').'`'.$var.'` = \''.$this->SQL->escape_string($val).'\'';
  48. }
  49. $first = false;
  50. }
  51. } else {
  52. $query.='WHERE '.$qwhere;
  53. }
  54. }
  55. return $this->SQL->query($query);
  56. }
  57. public function insertValues($data) {
  58. return $this->SQL->insert($this->table, $data);
  59. }
  60. protected function buildQuickWhere(array $qwhere) {
  61. $first = true;
  62. $query = '';
  63. foreach($qwhere as $var=>$val) {
  64. if ((is_int($var)) && (is_object($val))) {
  65. if (!($val instanceof \pinetd\SQL\Expr))
  66. throw new Exception('Expression of wrong type');
  67. $query.=($first?'':' AND ').$val;
  68. } else if (is_int($var)) {
  69. $query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
  70. } else {
  71. $query.=($first?'':' AND ').'`'.$var.'` '.(is_null($val)?'IS NULL':'= \''.$this->SQL->escape_string($val).'\'');
  72. }
  73. $first = false;
  74. }
  75. return $query;
  76. }
  77. public function createSelectQuery($qtype = 'SELECT', $qfields = '*', $qtable = null, $qwhere = null, $order_by = null, $limit = null) {
  78. $query = $qtype.' ';
  79. if (is_array($qfields)) {
  80. $first = true;
  81. foreach($qfields as $var) {
  82. $query.=($first?'':', ').'`'.$var.'`';
  83. $first = false;
  84. }
  85. $query.=' ';
  86. } else {
  87. $query.=$qfields.' ';
  88. }
  89. if (!is_null($qtable)) $query.='FROM '.$this->formatTable($qtable).' ';
  90. if (!is_null($qwhere)) {
  91. if (is_array($qwhere)) {
  92. $query.='WHERE ' . $this->buildQuickWhere($qwhere);
  93. } else {
  94. $query.='WHERE '.$qwhere;
  95. }
  96. }
  97. if (!is_null($order_by)) {
  98. if (is_array($order_by)) {
  99. $order = '';
  100. foreach($order_by as $field => $_order) {
  101. if (is_int($field)) {
  102. $field = $order;
  103. $_order = 'ASC';
  104. }
  105. $order .= ($order==''?'':', ').'`'.$field.'` '.$_order;
  106. }
  107. } else {
  108. $order = $order_by;
  109. }
  110. $query.=' ORDER BY '.$order;
  111. }
  112. if (!is_null($limit)) {
  113. $query.=' LIMIT '.$limit[0];
  114. if (isset($limit[1])) $query.=', '.$limit[1];
  115. }
  116. $res = $this->SQL->query($query);
  117. if (!$res) throw new \Exception($this->SQL->error);
  118. return $res;
  119. }
  120. }