123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?php
-
- /**
- * A class for creating SQL SELECT statements
- *
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- */
- class Staple_Query_Select extends Staple_Query
- {
- const ALL = 'ALL';
- const DISTINCT = 'DISTINCT';
- const DISTINCTROW = 'DISTINCTROW';
- const HIGH_PRIORITY = 'HIGH_PRIORITY';
- const STRAIGHT_JOIN = 'STRAIGHT_JOIN';
- const SQL_SMALL_RESULT = 'SQL_SMALL_RESULT';
- const SQL_BIG_RESULT = 'SQL_BIG_RESULT';
- const SQL_BUFFER_RESULT = 'SQL_BUFFER_RESULT';
- const SQL_CACHE = 'SQL_CACHE';
- const SQL_NO_CACHE = 'SQL_NO_CACHE';
- const SQL_CALC_FOUND_ROWS = 'SQL_CALC_FOUND_ROWS';
-
- /**
- * Additional Query Flags
- * @var array[string]
- */
- protected $flags = array();
- /**
- * The columns with which to act upon.
- * @var array[string]
- */
- public $columns = array();
- /**
- * Holds the order of the SQL query. It can be either a string or an array of the columns to order by.
- * @var string | array
- */
- protected $order;
- /**
- * Limit number of rows to return.
- * @var int
- */
- protected $limit;
- /**
- * The Limit Offset. Used to skip a number of rows before selecting.
- * @var int
- */
- protected $limitOffset;
- /**
- * Stores the GROUP BY columns;
- * @var unknown_type
- */
- protected $groupBy;
- /**
- * An array that holds the HAVING clauses
- * @var array[Staple_Query_Condition]
- */
- protected $having = array();
- /**
- * Array of Staple_Query_Join objects that represent table joins on the query
- * @var array[Staple_Query_Join]
- */
- protected $joins = array();
-
- public function __construct($table = NULL, array $columns = NULL, $db = NULL, $order = NULL, $limit = NULL)
- {
- parent::__construct(NULL, $db);
-
- //Set Table
- if(isset($table))
- {
- $this->setTable($table);
- }
- //Set Columns
- if(isset($columns))
- {
- $this->setColumns($columns);
- }
- //Set Order
- if(isset($order))
- {
- $this->orderBy($order);
- }
- //Set Limit
- if(isset($limit))
- {
- $this->limit($limit);
- }
- }
-
- public function addFlag($flag)
- {
- switch($flag)
- {
- case self::ALL:
- case self::DISTINCT:
- case self::DISTINCTROW:
- case self::HIGH_PRIORITY:
- case self::STRAIGHT_JOIN:
- case self::SQL_SMALL_RESULT:
- case self::SQL_BIG_RESULT:
- case self::SQL_BUFFER_RESULT:
- case self::SQL_CACHE:
- case self::SQL_NO_CACHE:
- case self::SQL_CALC_FOUND_ROWS:
- $this->flags[] = $flag;
- break;
- }
- return $this;
- }
-
- public function clearFlags()
- {
- $this->flags = array();
- return $this;
- }
- /**
- * @return the $columns
- */
- public function getColumns()
- {
- return $this->columns;
- }
- /**
- * Returns the order.
- * @return string | array
- */
- public function getOrder()
- {
- return $this->order;
- }
-
- /**
- * @return the $groupBy
- */
- public function getGroupBy()
- {
- return $this->groupBy;
- }
- /**
- * @return the $limit
- */
- public function getLimit()
- {
- return $this->limit;
- }
- /**
- * @return the $limitOffset
- */
- public function getLimitOffset()
- {
- return $this->limitOffset;
- }
- /**
- * @param mixed $table
- * @param string $alias
- */
- public function setTable($table,$alias = NULL)
- {
- if(is_array($table))
- {
- $this->table = $table;
- }
- elseif($table instanceof Staple_Query || $table instanceof Staple_Query_Union)
- {
- if(!isset($alias))
- {
- throw new Exception('Every derived table must have its own alias', Staple_Error::DB_ERROR);
- }
- $this->table = array($alias=>$table);
- }
- elseif(isset($alias) && is_string($table))
- {
- $this->table = array($alias=>$table);
- }
- else
- {
- $this->table = $table;
- }
- return $this;
- }
-
- /**
- * Different from addColumnsArray(), this function replaces all existing columns in the query.
- */
- public function setColumns(array $columns)
- {
- $this->columns = array();
- foreach($columns as $name=>$col)
- {
- if(is_string($name))
- {
- $this->columns[$name] = $col;
- }
- else
- {
- $this->columns[] = (string)$col;
- }
- }
- return $this;
- }
-
- /**
- * Set the order.
- * @param string | array $order
- */
- public function setOrder($order)
- {
- $this->order = $order;
- return $this;
- }
-
- /**
- * @param string | array $groupBy
- */
- public function setGroupBy($groupBy)
- {
- $this->groupBy = $groupBy;
- return $this;
- }
- /**
- * @param int $limit
- * @return Staple_Query_Select
- */
- public function setLimit($limit)
- {
- $this->limit = (int)$limit;
- return $this;
- }
-
- /**
- * @param int $limitOffset
- * @return Staple_Query_Select
- */
- public function setLimitOffset($limitOffset)
- {
- $this->limitOffset = (int)$limitOffset;
- return $this;
- }
- /**
- * Add to the list of columns. Optional parameter to name a column.
- * @param string | Staple_Query_Select $col
- * @param string $name
- */
- public function addColumn($col,$name = NULL)
- {
- if($col instanceof Staple_Query)
- $col = '('.(string)$col.')';
-
- if(isset($name))
- {
- $this->columns[(string)$name] = $col;
- }
- else
- {
- $this->columns[] = (string)$col;
- }
-
- return $this;
- }
-
- /**
- * Add an array of columns to the list of selected columns
- * @param array $columns
- */
- public function addColumnsArray(array $columns)
- {
- foreach($columns as $name=>$col)
- {
- if(is_string($name))
- {
- $this->columns[$name] = $col;
- }
- else
- {
- $this->columns[] = (string)$col;
- }
- }
- return $this;
- }
- /**
- * An alias of setColumns()
- * @param array $cols
- * @return Staple_Query_Select
- */
- public function columns(array $cols)
- {
- return $this->setColumns($cols);
- }
-
- /**
- * Remove a column from the $columns array.
- * @param string $col
- */
- public function removeColumn($col)
- {
- if(($key = array_search($col, $this->columns)) !== false)
- {
- unset($this->columns[$key]);
- return true;
- }
- return false;
- }
-
- /**
- * Remove a column from the $columns property by it's alias.
- * @param string $name
- */
- public function removeColumnByName($name)
- {
- if(array_key_exists($name, $this->columns))
- {
- unset($this->columns[$name]);
- return true;
- }
- return false;
- }
-
- /**
- * Alias of setOrder()
- * @see self::setOrder()
- */
- public function orderBy($order)
- {
- return $this->setOrder($order);
- }
-
- /**
- * Alias of setGroupBy()
- * @param string | array $group
- * @see self::setGroupBy()
- */
- public function groupBy($group)
- {
- return $this->setGroupBy($group);
- }
-
- /**
- * Sets the limit and the offset in one function.
- * @param int | Staple_Pager $limit
- * @param int $offset
- */
- public function limit($limit,$offset = NULL)
- {
- if($limit instanceof Staple_Pager)
- {
- $this->setLimit($limit->getItemsPerPage());
- $this->setLimitOffset($limit->getStartingItem());
- }
- else
- {
- $this->setLimit($limit);
- if(isset($offset))
- $this->setLimitOffset($offset);
- }
- return $this;
- }
- /*-----------------------------------------------HAVING CLAUSES-----------------------------------------------*/
-
- public function addHaving(Staple_Query_Condition $having)
- {
- $this->having[] = $having;
- return $this;
- }
-
- public function clearHaving()
- {
- $this->having = array();
- return $this;
- }
-
- public function havingCondition($column, $operator, $value, $columnJoin = NULL)
- {
- $this->addHaving(Staple_Query_Condition::Get($column, $operator, $value, $columnJoin));
- return $this;
- }
-
- public function havingStatement($statement)
- {
- $this->addHaving(Staple_Query_Condition::Statement($statement));
- return $this;
- }
-
- public function havingEqual($column, $value, $columnJoin = NULL)
- {
- $this->addHaving(Staple_Query_Condition::Equal($column, $value, $columnJoin));
- return $this;
- }
-
- public function havingLike($column, $value)
- {
- $this->addHaving(Staple_Query_Condition::Like($column, $value));
- return $this;
- }
-
- public function havingNull($column)
- {
- $this->addHaving(Staple_Query_Condition::Null($column));
- return $this;
- }
-
- public function havingIn($column, array $values)
- {
- $this->addHaving(Staple_Query_Condition::In($column, $values));
- return $this;
- }
-
- public function havingBetween($column, $start, $end)
- {
- $this->addHaving(Staple_Query_Condition::Between($column, $start, $end));
- return $this;
- }
-
- /*-----------------------------------------------JOIN FUNCTIONS-----------------------------------------------*/
- /**
- * Add a join to the query.
- * @param Staple_Query_Join $join
- */
- public function addJoin(Staple_Query_Join $join)
- {
- $this->joins[] = $join;
- return $this;
- }
-
- /**
- * Remove a join from the query by table name.
- * @param string $table
- * @return boolean
- */
- public function removeJoin($table)
- {
- foreach($this->joins as $key=>$join)
- {
- if($join->getTable() == $table)
- {
- unset($this->joins[$key]);
- return true;
- }
- }
- return false;
- }
-
- /**
- * Returns true is specified table is already joined to the query, false otherwise.
- * @param string $table
- * @return boolean
- */
- public function isJoined($table)
- {
- foreach($this->joins as $key=>$join)
- {
- if($join->getTable() == $table)
- {
- return true;
- }
- }
- return false;
- }
-
- public function leftJoin($table, $condition)
- {
- $this->addJoin(Staple_Query_Join::left($table, $condition));
- return $this;
- }
-
- public function innerJoin($table, $condition)
- {
- $this->addJoin(Staple_Query_Join::inner($table, $condition));
- return $this;
- }
-
- /**
- * Returns the joins array
- * @return array[Staple_Query_Join]
- */
- public function getJoins()
- {
- return $this->joins;
- }
-
- /*-----------------------------------------------BUILD FUNCTION-----------------------------------------------*/
-
- /**
- *
- * @see Staple_Query::build()
- */
- function build()
- {
- $stmt = 'SELECT ';
-
- //Flags
- if(count($this->flags) > 0)
- {
- $stmt .= ' '.implode(' ', $this->flags);
- }
-
- //Collect Select Columns
- if(count($this->columns) == 0)
- {
- $columns = '*';
- }
- else
- {
- $columns = '';
- foreach($this->columns as $name=>$col)
- {
- if(strlen($columns) >= 1)
- {
- $columns .= ',';
- }
- if($col instanceof Staple_Query_Select)
- {
- $columns .= '('.$col.')';
- }
- else
- {
- $columns .= $col;
- }
- if(is_string($name))
- {
- $columns .= " AS `$name`";
- }
- }
- }
-
- //Columns and FROM CLAUSE
- $stmt .= "\n$columns \nFROM ";
- if(is_array($this->table))
- {
- $tables = '';
- foreach($this->table as $name=>$tbl)
- {
- if(strlen($tables) > 1)
- {
- $tables .= ',';
- }
- if($tbl instanceof Staple_Query || $tbl instanceof Staple_Query_Union)
- {
- $tables = '('.$tbl.')';
- }
- else
- {
- $tables .= $tbl;
- }
- if(is_string($name))
- {
- $tables .= " AS `$name`";
- }
- }
- $stmt .= $tables;
- }
- else
- {
- $stmt .= $this->table;
- }
-
- //JOINS
- if(count($this->joins) > 0)
- {
- $stmt .= "\n".implode("\n", $this->joins);
- }
-
- //WHERE CLAUSE
- if(count($this->where) > 0)
- {
- $stmt .= "\nWHERE ".implode(' AND ', $this->where);
- }
-
- //GROUP BY
- if(isset($this->groupBy))
- {
- $stmt .= "\nGROUP BY ";
- if(is_array($this->groupBy))
- {
- $stmt .= implode(',', $this->groupBy);
- }
- else
- {
- $stmt .= $this->groupBy;
- }
- }
-
- //HAVING
- if(count($this->having) > 0)
- {
- $stmt .= "\nHAVING ".implode(' AND ', $this->having);
- }
-
- //ORDER CLAUSE
- if(isset($this->order))
- {
- $stmt .= "\nORDER BY ";
- if(is_array($this->order))
- {
- $stmt .= implode(', ', $this->order);
- }
- else
- {
- $stmt .= $this->order;
- }
- }
-
- //LIMIT CLAUSE
- if(isset($this->limit))
- {
- $stmt .= "\nLIMIT ".$this->getLimit();
- if(isset($this->limitOffset))
- {
- $stmt .= ' OFFSET '.$this->limitOffset;
- }
- }
- return $stmt;
- }
- }
- ?>
|