PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/DAO/MySQL.class.php

https://github.com/blekkzor/pinetd2
PHP | 138 lines | 125 code | 13 blank | 0 comment | 29 complexity | accf7256b99543f7198e40a9c8dc152f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace DAO;
  3. class MySQL 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. $query = '';
  59. foreach($data as $var=>$val) {
  60. $query .= ($query == ''?'':', ').'`'.$var.'` = '.(is_null($val)?'NULL':'\''.$this->SQL->escape_string($val).'\'');
  61. }
  62. $query = 'INSERT INTO '.$this->formatTable($this->table).' SET '.$query;
  63. return $this->SQL->query($query);
  64. }
  65. protected function buildQuickWhere(array $qwhere) {
  66. $first = true;
  67. $query = '';
  68. foreach($qwhere as $var=>$val) {
  69. if ((is_int($var)) && (is_object($val))) {
  70. if (!($val instanceof \pinetd\SQL\Expr))
  71. throw new Exception('Expression of wrong type');
  72. $query.=($first?'':' AND ').$val;
  73. } else if (is_int($var)) {
  74. $query.=($first?'':' AND ').$this->buildWhere($val[0], $val[1]);
  75. } else {
  76. $query.=($first?'':' AND ').'`'.$var.'` '.(is_null($val)?'IS NULL':'= \''.$this->SQL->escape_string($val).'\'');
  77. }
  78. $first = false;
  79. }
  80. return $query;
  81. }
  82. public function createSelectQuery($qtype = 'SELECT', $qfields = '*', $qtable = null, $qwhere = null, $order_by = null, $limit = null) {
  83. $query = $qtype.' ';
  84. if (is_array($qfields)) {
  85. $first = true;
  86. foreach($qfields as $var) {
  87. $query.=($first?'':', ').'`'.$var.'`';
  88. $first = false;
  89. }
  90. $query.=' ';
  91. } else {
  92. $query.=$qfields.' ';
  93. }
  94. if (!is_null($qtable)) $query.='FROM '.$this->formatTable($qtable).' ';
  95. if (!is_null($qwhere)) {
  96. if (is_array($qwhere)) {
  97. $query.='WHERE ' . $this->buildQuickWhere($qwhere);
  98. } else {
  99. $query.='WHERE '.$qwhere;
  100. }
  101. }
  102. if (!is_null($order_by)) {
  103. if (is_array($order_by)) {
  104. $order = '';
  105. foreach($order_by as $field => $_order) {
  106. if (is_int($field)) {
  107. $field = $order;
  108. $_order = 'ASC';
  109. }
  110. $order .= ($order==''?'':', ').'`'.$field.'` '.$_order;
  111. }
  112. } else {
  113. $order = $order_by;
  114. }
  115. $query.=' ORDER BY '.$order;
  116. }
  117. if (!is_null($limit)) {
  118. $query.=' LIMIT '.$limit[0];
  119. if (isset($limit[1])) $query.=', '.$limit[1];
  120. }
  121. $res = $this->SQL->query($query);
  122. if (!$res) throw new \Exception($this->SQL->error);
  123. return $res;
  124. }
  125. }