Pelzini

This is the code documentation for the Pelzini project

source of /viewer/select_query.php

A system for the automatic creation of select queries
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. /**
  22.  * A system for the automatic creation of select queries
  23.  *
  24.  * @package Viewer
  25.  * @author Josh Heidenreich
  26.  * @since 0.2
  27.  **/
  28.  
  29.  
  30. /**
  31.  * This class simplifies the creation of select queries
  32.  **/
  33. class SelectQuery
  34. {
  35. private $fields;
  36. private $from;
  37. private $joins;
  38. private $where;
  39. private $group;
  40. private $order;
  41.  
  42.  
  43. public function __construct()
  44. {
  45. $this->joins = array();
  46. $this->where = array();
  47. }
  48.  
  49.  
  50. /**
  51.   * Generic method for adding fields to the query
  52.   *
  53.   * @param string The fields to add to the query
  54.   **/
  55. public function addFields($fields)
  56. {
  57. if ($this->fields) $this->fields .= ', ';
  58. $this->fields .= $fields;
  59. }
  60.  
  61.  
  62. /**
  63.   * Sets the FROM clause for the query
  64.   **/
  65. public function setFrom($from)
  66. {
  67. $this->from = $from;
  68. }
  69.  
  70.  
  71. /**
  72.   * Adds an INNER JOIN
  73.   **/
  74. public function addInnerJoin($join)
  75. {
  76. $this->joins[] = 'INNER JOIN ' . $join;
  77. }
  78.  
  79.  
  80. /**
  81.   * Adds a LEFT JOIN
  82.   **/
  83. public function addLeftJoin($join)
  84. {
  85. $this->joins[] = 'LEFT JOIN ' . $join;
  86. }
  87.  
  88.  
  89. /**
  90.   * Adds a WHERE clause. Where clauses are ANDed together
  91.   **/
  92. public function addWhere($where)
  93. {
  94. $this->where[] = $where;
  95. }
  96.  
  97.  
  98. /**
  99.   * Sets a GROUP BY clause.
  100.   **/
  101. public function setGroupBy($group_by)
  102. {
  103. $this->group = $group_by;
  104. }
  105.  
  106.  
  107. /**
  108.   * Sets an ORDER BY clause.
  109.   **/
  110. public function setOrderBy($order_by)
  111. {
  112. $this->order = $order_by;
  113. }
  114.  
  115.  
  116. /**
  117.   * This creates the SQL query
  118.   **/
  119. public function buildQuery()
  120. {
  121. $q = "SELECT {$this->fields}\n";
  122. $q .= " FROM {$this->from}\n";
  123.  
  124. foreach ($this->joins as $join) {
  125. $q .= " {$join}\n";
  126. }
  127.  
  128. if (count($this->where)) {
  129. $q .= " WHERE " . implode(' AND ', $this->where) . "\n";
  130. }
  131.  
  132. if ($this->group) {
  133. $q .= " GROUP BY {$this->group}\n";
  134. }
  135.  
  136. if ($this->order) {
  137. $q .= " ORDER BY {$this->order}\n";
  138. }
  139.  
  140. return $q;
  141. }
  142.  
  143.  
  144. /**
  145.   * Adds the required WHERE clauses for multiple-project support
  146.   **/
  147. public function addProjectWhere()
  148. {
  149. global $project;
  150. $this->addWhere("{$this->from}.projectid = {$project['id']}");
  151. }
  152.  
  153. }
  154.  
  155.