PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/core/xpdo/om/mysql/xpdoquery.class.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 157 lines | 122 code | 4 blank | 31 comment | 44 complexity | 54281fe9887ec5bea5cea9f731670bde MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2010-2013 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 MySQL implementation of xPDOQuery.
  22. *
  23. * @package xpdo
  24. * @subpackage om.mysql
  25. */
  26. /** Include the base {@see xPDOQuery} class */
  27. include_once (dirname(dirname(__FILE__)) . '/xpdoquery.class.php');
  28. /**
  29. * An implementation of xPDOQuery for the MySQL database engine.
  30. *
  31. * @package xpdo
  32. * @subpackage om.mysql
  33. */
  34. class xPDOQuery_mysql 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[]= $table['table'];
  76. } else {
  77. $tables[]= $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'] . ' ' . $join['table'] . ' ' . $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. reset($this->query['set']);
  94. $clauses = array();
  95. while (list($setKey, $setVal) = each($this->query['set'])) {
  96. $value = $setVal['value'];
  97. $type = $setVal['type'];
  98. if ($value !== null && in_array($type, array(PDO::PARAM_INT, PDO::PARAM_STR))) {
  99. $value = $this->xpdo->quote($value, $type);
  100. } elseif ($value === null) {
  101. $value = 'NULL';
  102. }
  103. $clauses[] = $this->xpdo->escape($setKey) . ' = ' . $value;
  104. }
  105. if (!empty($clauses)) {
  106. $sql.= 'SET ' . implode(', ', $clauses) . ' ';
  107. }
  108. unset($clauses);
  109. }
  110. }
  111. if (!empty ($this->query['where'])) {
  112. if ($where= $this->buildConditionalClause($this->query['where'])) {
  113. $sql.= 'WHERE ' . $where . ' ';
  114. }
  115. }
  116. if ($command == 'SELECT' && !empty ($this->query['groupby'])) {
  117. $groupby= reset($this->query['groupby']);
  118. $sql.= 'GROUP BY ';
  119. $sql.= $groupby['column'];
  120. if ($groupby['direction']) $sql.= ' ' . $groupby['direction'];
  121. while ($groupby= next($this->query['groupby'])) {
  122. $sql.= ', ';
  123. $sql.= $groupby['column'];
  124. if ($groupby['direction']) $sql.= ' ' . $groupby['direction'];
  125. }
  126. $sql.= ' ';
  127. }
  128. if (!empty ($this->query['having'])) {
  129. $sql.= 'HAVING ';
  130. $sql.= $this->buildConditionalClause($this->query['having']);
  131. $sql.= ' ';
  132. }
  133. if ($command == 'SELECT' && !empty ($this->query['sortby'])) {
  134. $sortby= reset($this->query['sortby']);
  135. $sql.= 'ORDER BY ';
  136. $sql.= $sortby['column'];
  137. if ($sortby['direction']) $sql.= ' ' . $sortby['direction'];
  138. while ($sortby= next($this->query['sortby'])) {
  139. $sql.= ', ';
  140. $sql.= $sortby['column'];
  141. if ($sortby['direction']) $sql.= ' ' . $sortby['direction'];
  142. }
  143. $sql.= ' ';
  144. }
  145. if ($limit= intval($this->query['limit'])) {
  146. $sql.= 'LIMIT ';
  147. if ($offset= intval($this->query['offset'])) $sql.= $offset . ', ';
  148. $sql.= $limit . ' ';
  149. }
  150. $this->sql= $sql;
  151. return (!empty ($this->sql));
  152. }
  153. }