xpdoquery.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * Copyright 2010-2015 by MODX, LLC.
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * The SQLite implementation of xPDOQuery.
  22. *
  23. * @package xpdo
  24. * @subpackage om.sqlite
  25. */
  26. /** Include the base {@see xPDOQuery} class */
  27. include_once (dirname(__DIR__) . '/xpdoquery.class.php');
  28. /**
  29. * An implementation of xPDOQuery for the SQLite database engine.
  30. *
  31. * @package xpdo
  32. * @subpackage om.sqlite
  33. */
  34. class xPDOQuery_sqlite extends xPDOQuery {
  35. public function __construct(& $xpdo, $class, $criteria= null) {
  36. parent :: __construct($xpdo, $class, $criteria);
  37. $this->query['priority']= '';
  38. }
  39. public function construct() {
  40. $constructed= false;
  41. $this->bindings= array ();
  42. $command= strtoupper($this->query['command']);
  43. $sql= $this->query['command'] . ' ';
  44. if ($command == 'SELECT') $sql.= $this->query['distinct'] ? $this->query['distinct'] . ' ' : '';
  45. if ($command == 'SELECT') $sql.= $this->query['priority'] ? $this->query['priority'] . ' ' : '';
  46. if ($command == 'SELECT') {
  47. $columns= array ();
  48. if (empty ($this->query['columns'])) {
  49. $this->select('*');
  50. }
  51. foreach ($this->query['columns'] as $alias => $column) {
  52. $ignorealias = is_int($alias);
  53. $escape = !preg_match('/\bAS\b/i', $column) && !preg_match('/\./', $column) && !preg_match('/\(/', $column);
  54. if ($escape) {
  55. $column= $this->xpdo->escape(trim($column));
  56. } else {
  57. $column= trim($column);
  58. }
  59. if (!$ignorealias) {
  60. $alias = $escape ? $this->xpdo->escape($alias) : $alias;
  61. $columns[]= "{$column} AS {$alias}";
  62. } else {
  63. $columns[]= "{$column}";
  64. }
  65. }
  66. $sql.= implode(', ', $columns);
  67. $sql.= ' ';
  68. }
  69. if ($command != 'UPDATE') {
  70. $sql.= 'FROM ';
  71. }
  72. $tables= array ();
  73. foreach ($this->query['from']['tables'] as $table) {
  74. if ($command != 'SELECT') {
  75. $tables[]= $this->xpdo->escape($table['table']);
  76. } else {
  77. $tables[]= $this->xpdo->escape($table['table']) . ' AS ' . $this->xpdo->escape($table['alias']);
  78. }
  79. }
  80. $sql.= $this->query['from']['tables'] ? implode(', ', $tables) . ' ' : '';
  81. if (!empty ($this->query['from']['joins'])) {
  82. foreach ($this->query['from']['joins'] as $join) {
  83. $sql.= $join['type'] . ' ' . $this->xpdo->escape($join['table']) . ' AS ' . $this->xpdo->escape($join['alias']) . ' ';
  84. if (!empty ($join['conditions'])) {
  85. $sql.= 'ON ';
  86. $sql.= $this->buildConditionalClause($join['conditions']);
  87. $sql.= ' ';
  88. }
  89. }
  90. }
  91. if ($command == 'UPDATE') {
  92. if (!empty($this->query['set'])) {
  93. $clauses = array();
  94. foreach ($this->query['set'] as $setKey => $setVal) {
  95. $value = $setVal['value'];
  96. $type = $setVal['type'];
  97. if ($value !== null && in_array($type, array(PDO::PARAM_INT, PDO::PARAM_STR))) {
  98. $value = $this->xpdo->quote($value, $type);
  99. } elseif ($value === null) {
  100. $value = 'NULL';
  101. }
  102. $clauses[] = $this->xpdo->escape($setKey) . ' = ' . $value;
  103. }
  104. if (!empty($clauses)) {
  105. $sql.= 'SET ' . implode(', ', $clauses) . ' ';
  106. }
  107. unset($clauses);
  108. }
  109. }
  110. if (!empty ($this->query['where'])) {
  111. if ($where= $this->buildConditionalClause($this->query['where'])) {
  112. $sql.= 'WHERE ' . $where . ' ';
  113. }
  114. }
  115. if ($command == 'SELECT' && !empty ($this->query['groupby'])) {
  116. $groupby= reset($this->query['groupby']);
  117. $sql.= 'GROUP BY ';
  118. $sql.= $groupby['column'];
  119. if ($groupby['direction']) $sql.= ' ' . $groupby['direction'];
  120. while ($groupby= next($this->query['groupby'])) {
  121. $sql.= ', ';
  122. $sql.= $groupby['column'];
  123. if ($groupby['direction']) $sql.= ' ' . $groupby['direction'];
  124. }
  125. $sql.= ' ';
  126. }
  127. if (!empty ($this->query['having'])) {
  128. $sql.= 'HAVING ';
  129. $sql.= $this->buildConditionalClause($this->query['having']);
  130. $sql.= ' ';
  131. }
  132. if ($command == 'SELECT' && !empty ($this->query['sortby'])) {
  133. $sortby= reset($this->query['sortby']);
  134. $sql.= 'ORDER BY ';
  135. $sql.= $sortby['column'];
  136. if ($sortby['direction']) $sql.= ' ' . $sortby['direction'];
  137. while ($sortby= next($this->query['sortby'])) {
  138. $sql.= ', ';
  139. $sql.= $sortby['column'];
  140. if ($sortby['direction']) $sql.= ' ' . $sortby['direction'];
  141. }
  142. $sql.= ' ';
  143. }
  144. if ($limit= intval($this->query['limit'])) {
  145. $sql.= 'LIMIT ';
  146. if ($offset= intval($this->query['offset'])) $sql.= $offset . ', ';
  147. $sql.= $limit . ' ';
  148. }
  149. $this->sql= $sql;
  150. return (!empty ($this->sql));
  151. }
  152. }