. * * @package PhpMyObject * @subpackage PMO_Core * @author Nicolas Boiteux * @author Louis Lapointe * @link http://pmo.developpez.com/ * @since PhpMyObject v0.14 * @version $Revision: $ * @copyright Copyright (C) 2007-2008 Nicolas Boiteux * @copyright Copyright (C) 2008 Louis Lapointe * @license GPLv3 {@link http://www.gnu.org/licenses/gpl} * @filesource * */ /** requires the interface */ require_once(dirname(__FILE__).'/PMO_Request.php'); /** * This class is used to provide a SQL query to PMO_MyController * * You provide the different parts of the query by using the * corresponding methods. Each method returns the PMO_MyRequest object * which allows you to link the methods together, e.g. * * * $request = new PMO_MyRequest; * $request->field('film_id')->from('film_actor')->limit('10') * $controller = new PMO_MyController; * $map = $cntroller->objectquery($request) * * * This could also be written like this: * * * $controller = new PMO_MyController; * $request = new PMO_MyRequest; * $controller->objectquery($request->field('film_id')->from('film_actor')->limit('10')); * * * Most methods accept a variable number of arguments. So you can do something like this: * * * $request = new PMO_MyRequest; * $request->field('film_id','actor_id')->from('film_actor')->limit('10') * * * @todo implement sql commands insert, update, delete * * @package PhpMyObject * @subpackage PMO_Core */ class PMO_MyRequest implements PMO_Request{ /** * the SQL query fields * * if none is provided, a SELECT * will be performed. * * @var array * @access protected */ protected $field; /** * the table names of the query * * @var array * @access protected */ protected $from; /** * the where clause of the query * * @var array * @access protected */ protected $where; /** * the order clause * * @var array * @access protected */ protected $order; /** * the having clause * * @var array * @access protected */ protected $having; /** * the limit clause * * @var array * @access protected */ protected $limit; /** * the offset part of the limit clause * * @var array * @access protected */ protected $offset; /** * the groupby clause * * @var array * @access protected */ protected $groupby; public function __construct(){ $this->field = new PMO_MyArray(); $this->from = new PMO_MyArray(); $this->where = new PMO_MyArray(); $this->order = new PMO_MyArray(); $this->having = new PMO_MyArray(); $this->groupby = new PMO_MyArray(); } /** * stores the provided column names from which data will be read. * * This method accepts a variable number of arguments. * * * $request = new PMO_MyRequest; * $request->field('column1', 'column2')->from('mytable'); * * * If you do not provide column names, a "SELECT *" will be performed. * * @param string $field,... any number of column names you want to read * @return object return $this object */ public function field($field){ $args = func_get_args(); if (!empty($args)) { foreach($args as $arg) { if (!$this->field->in_array($arg,array())) { $this->field->append($arg); } } } return $this; } /** * stores the table names on which the request is going to be run * * This method accepts a variable number of arguments. * * * $request = new PMO_MyRequest; * $request->field('column1', 'column2')->from('mytable1', 'mytable2'); * * * @param string $from,... any number of table names you want to read from * @return object return $this object */ public function from($from){ $args = func_get_args(); if (!empty($args)) { foreach($args as $arg) { if (!$this->from->in_array($arg,array())) { $this->from->append($arg); } } } return $this; } /** * stores the where clause * * This method accepts a variable number of arguments. * You create the where clause by providing conditions. * * * $request = new PMO_MyRequest; * $request->from('MyTable')->where('name = "value"', 'active = 1'); * * * {@link toString()} will create your where clause by linking them with * the 'AND' keyword. * * @param string $where,... any number of conditions to build your * where clause with * @return object return $this object */ public function where($where){ $arg = func_get_args(); $this->where->merge($arg); return $this; } /** * store the order clause using the provided $order parameter * * This method accepts a variable number of arguments. * You create the order clause by providing the field names. * * * $request = new PMO_MyRequest; * $request->from('MyTable')->where('active = 1')->order('lastname','firstname'); * * * @param string $order,... the order field name * @return object return $this object */ public function order($order){ $args = func_get_args(); if (!empty($args)) { foreach($args as $arg) { if (!$this->order->in_array($arg,array())) { $this->order->append($arg); } } } return $this; } /** * stores the having clause * * @param string $having,... the having clause * @return object return $this object */ public function having($having){ $arg = func_get_args(); $this->having->merge($arg); return $this; } /** * stores the groupby clause * * @param string $groupby,... the groupby clause * @return object return $this object */ public function groupby($groupby){ $arg = func_get_args(); $this->groupby->merge($arg); return $this; } /** * stores the limit value. * * @param integer $limit the limit you want to impose to your query * @return object return $this object */ public function limit($limit){ $this->limit = $limit; return $this; } /** * stores the offset value. * * @param integer $offset offset to start the query from * @return object return $this object */ public function offset($offset){ $this->offset = $offset; return $this; } /** * Build the actual SQL query using all the previoulsy * provided parts * * You should not need to use this directly. Since the * {@link PMO_MyController::objectquery()} takes a PMO_MyRequest object * and use this method to call {@see PMO_MyController::query()} * * @return string the SQL query */ public function toString(){ if($this->field->count() > 0){ $query = "SELECT " . $this->field->implode(','); }else{ $query = "SELECT *"; } if($this->from->count() > 0){ $query .= " FROM " . $this->from->implode(','); }else{ throw new Exception("Error: FROM clause is empty"); } if($this->where->count() > 0){ $query .= " WHERE " . $this->where->implode(' AND '); } if($this->groupby->count() > 0) $query .= " GROUP BY " . $this->groupby->implode(','); if($this->having->count() > 0){ $query .= " HAVING " . $this->having->implode(' AND '); } if($this->order->count() > 0){ $query .= " ORDER BY " . $this->order->implode(','); } if(count($this->limit) > 0){ $query .= " LIMIT " . $this->limit; if (count($this->offset) > 0) { $query .= ' OFFSET ' . $this->offset; } } return $query; } /** * resets the PMO_MyRequest object so that it can reused */ public function reset(){ $this->field = new PMO_MyArray(); $this->from = new PMO_MyArray(); $this->where = new PMO_MyArray(); $this->order = new PMO_MyArray(); $this->having = new PMO_MyArray(); $this->groupby = new PMO_MyArray(); } public function getLinked(){ foreach($this->from as $tablename){ $table = PMO_MyTable::factory($tablename); $fk = $table->getFk(); if($fk->count() > 0) foreach($fk as $column=>$value){ foreach($value['Fk'] as $table2=>$column2){ $this->from($table2); $this->where($table->getTableName().".".$column." = ".$table2.".".$column2); } } } return $this; } } ?>