PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_MyRequest.php

Documentation is available at PMO_MyRequest.php

  1. <?php
  2. /**
  3.  * This file contains PMO_MyRequest class which is used to build a SQL query
  4.  * using provided fields, tables names, where clause, etc.
  5.  *
  6.  * This file is part of the PhpMyObject project.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * @package        PhpMyObject
  26.  * @subpackage PMO_Core
  27.  * @author        Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @link            http://pmo.developpez.com/
  29.  * @since        PhpMyObject v0.14
  30.  * @version        $Revision: $
  31.  * @copyright    Copyright (C) 2007-2008 Nicolas Boiteux
  32.  * @license        GPLv3 {@link http://www.gnu.org/licenses/gpl}
  33.  * @filesource
  34.  *
  35.  */ 
  36.  
  37. /** requires the interface */
  38. require_once(dirname(__FILE__).'/PMO_Request.php');
  39.  
  40. /**
  41.  * This class is used to provide a SQL query to PMO_MyController
  42.  * 
  43.  * You provide the different parts of the query by using the
  44.  * corresponding methods. Each method returns the PMO_MyRequest object
  45.  * which allows you to link the methods together, e.g.
  46.  * 
  47.  * <code>
  48.  * $request = new PMO_MyRequest;
  49.  * $request->field('film_id')->from('film_actor')->limit('10')
  50.  * $controller = new PMO_MyController;
  51.  * $map = $cntroller->objectquery($request)
  52.  * </code>
  53.  *
  54.  * This could also be written like this:
  55.  *
  56.  * <code>
  57.  * $controller = new PMO_MyController;
  58.  * $request = new PMO_MyRequest;
  59.  * $controller->objectquery($request->field('film_id')->from('film_actor')->limit('10'));
  60.  * </code>
  61.  *
  62.  * Most methods accept a variable number of arguments. So you can do something like this:
  63.  *
  64.  * <code>
  65.  * $request = new PMO_MyRequest;
  66.  * $request->field('film_id','actor_id')->from('film_actor')->limit('10')
  67.  * </code>
  68.  *
  69.  * @package     PhpMyObject
  70.  * @subpackage PMO_Core
  71.  */
  72. class PMO_MyRequest implements PMO_Request{
  73.     
  74.     /**
  75.      * the SQL query fields
  76.      *
  77.      * if none is provided, a SELECT * will be performed.
  78.      *
  79.      * @var array 
  80.      * @access protected
  81.      */
  82.     protected $field;
  83.  
  84.     /**
  85.      * the table names of the query
  86.      * 
  87.      * @var array 
  88.      * @access protected
  89.      */
  90.     protected $from;
  91.  
  92.     /**
  93.      * the where clause of the query
  94.      * 
  95.      * @var array 
  96.      * @access protected
  97.      */
  98.     protected $where;
  99.     
  100.     /**
  101.      * the order clause
  102.      * 
  103.      * @var array 
  104.      * @access protected
  105.      */
  106.     protected $order;
  107.  
  108.     /**
  109.      * the having clause
  110.      * 
  111.      * @var array 
  112.      * @access protected
  113.      */
  114.     protected $having;
  115.  
  116.     /**
  117.      * the limit clause
  118.      *
  119.      * @var array 
  120.      * @access protected
  121.      */
  122.     protected $limit;
  123.  
  124.     /**
  125.      * the groupby clause
  126.      * 
  127.      * @var array 
  128.      * @access protected
  129.      */
  130.     protected $groupby;
  131.     
  132.     public function __construct(){
  133.         $this->field = new PMO_MyArray();
  134.         $this->from = new PMO_MyArray();
  135.         $this->where = new PMO_MyArray();
  136.         $this->order = new PMO_MyArray();
  137.         $this->having = new PMO_MyArray();
  138.     }
  139.     
  140.     
  141.     /**
  142.      * stores the provided column names from which data will be read.
  143.      *
  144.      * This method accepts a variable number of arguments.
  145.      *
  146.      * <code>
  147.      * $request = new PMO_MyRequest;
  148.      * $request->field('column1', 'column2')->from('mytable');
  149.      * </code>
  150.      *
  151.      * If you do not provide column names, a "SELECT *" will be performed.
  152.      *
  153.      * @param string $field,...    any number of column names you want to read
  154.      */
  155.     public function field($field){
  156.         $arg func_get_args();
  157.         $this->field->merge($arg);
  158.         return $this;
  159.     }
  160.  
  161.     /**
  162.      * stores the table names on which the request is going to be run
  163.      *
  164.      * This method accepts a variable number of arguments.
  165.      *
  166.      * <code>
  167.      * $request = new PMO_MyRequest;
  168.      * $request->field('column1', 'column2')->from('mytable1', 'mytable2');
  169.      * </code>
  170.      *
  171.      * @param string $from,...        any number of table names you want to read from
  172.      */
  173.     public function from($from){
  174.         $arg func_get_args();    
  175.         $this->from->merge($arg);
  176.         return $this;
  177.     }
  178.     
  179.     /**
  180.      * stores the where clause
  181.      *
  182.      * This method accepts a variable number of arguments.
  183.      * You create the where clause by providing conditions.
  184.      *
  185.      * <code>
  186.      * $request = new PMO_MyRequest;
  187.      * $request->from('MyTable')->where('name = "value"', 'active = 1');
  188.      * </code>
  189.      *
  190.      * {@link toString()} will create your where clause by linking them with
  191.      * the 'AND' keyword.
  192.      *
  193.      * @param string $where,...    any number of conditions to build your
  194.      *                                         where clause with
  195.      */
  196.     public function where($where){
  197.         $arg func_get_args();
  198.         $this->where->merge($arg);
  199.         return $this;
  200.     }
  201.     
  202.     /**
  203.      * store the order clause using the provided $order parameter
  204.      *
  205.      * This method accepts a variable number of arguments.
  206.      * You create the order clause by providing the field names.
  207.      *
  208.      * <code>
  209.      * $request = new PMO_MyRequest;
  210.      * $request->from('MyTable')->where('active = 1')->order('lastname','firstname');
  211.      * </code>
  212.      *
  213.      * @param string $order,...  the order field name
  214.      */
  215.     public function order($order){
  216.         $arg func_get_args();
  217.         $this->order->merge($arg);
  218.         return $this;
  219.     }
  220.     
  221.     /**
  222.      * stores the having clause
  223.      *
  224.      * @param string $having,...     the having clause
  225.      */
  226.     public function having($having){
  227.         $arg func_get_args();
  228.         $this->having->merge($arg);
  229.         return $this;
  230.     }
  231.  
  232.     /**
  233.      * stores the groupby clause
  234.      *
  235.      * @param string $groupby,...     the groupby clause
  236.      */    
  237.     public function groupby($groupby){
  238.         $this->groupby = $groupby;
  239.         return $this;        
  240.     }
  241.     
  242.     /**
  243.      * stores the limit clause
  244.      *
  245.      * @param string|integer$limit      the limit you want to impose to your query
  246.      */
  247.     public function limit($limit){
  248.         $this->limit = $limit;
  249.         return $this;
  250.     }    
  251.  
  252.     /**
  253.      * Build the actual SQL query using all the previoulsy
  254.      * provided parts
  255.      *
  256.      * You should not need to use this directly. Since the
  257.      * {@link PMO_MyController::objectquery()} takes a PMO_MyRequest object
  258.      * and use this method to call {@see PMO_MyController::query()}
  259.      *
  260.      * @todo                    implement having
  261.      * @return string        the SQL query
  262.      */
  263.     public function toString(){
  264.                             
  265.         if($this->field->count(0){
  266.             $query "SELECT " $this->field->implode(',');
  267.         }else{
  268.             $query "SELECT *";
  269.         }
  270.                             
  271.         if($this->from->count(0){
  272.             $query .= " FROM " $this->from->implode(',');
  273.         }else{
  274.             throw new Exception("Error: FROM clause is empty");
  275.         }
  276.                             
  277.         if($this->where->count(0){
  278.             $query .= " WHERE " $this->where->implode(' AND ');
  279.         }
  280.         
  281.         if(isset($this->groupby))
  282.             $query .= " GROUP BY " $this->groupby;
  283.         
  284.         if($this->having->count(0){
  285.             $query .= " HAVING " $this->having->implode(' AND ');
  286.         }        
  287.  
  288.         if(count($this->limit0){
  289.             $query .= " LIMIT " $this->limit;
  290.         }
  291.         
  292.         return $query;
  293.     }
  294.     
  295.     /**
  296.      * resets the PMO_MyRequest object so that it can reused
  297.      */
  298.     public function reset(){
  299.         $this->field = new PMO_MyArray();
  300.         $this->from = new PMO_MyArray();
  301.         $this->where = new PMO_MyArray();
  302.         $this->order = new PMO_MyArray();
  303.         $this->having = new PMO_MyArray();
  304.         $this->groupby = new PMO_MyArray();    
  305.     }
  306.         
  307.     public function getLinked(){
  308.         foreach($this->from as $tablename){
  309.             $table PMO_MyTable::factory($tablename);
  310.             $fk $table->getFk();
  311.  
  312.             if($fk->count(0)
  313.                 foreach($fk as $column=>$value){
  314.                     foreach($value['Fk'as $table2=>$column2){
  315.                         $this->from($table2);
  316.                         $this->where($table->getTableName().".".$column." = ".$table2.".".$column2);
  317.                     }
  318.                 }
  319.         }
  320.         return $this;
  321.     }
  322.     
  323. }
  324.  
  325. ?>

Documentation generated on Wed, 15 Oct 2008 09:17:43 -0400 by phpDocumentor 1.4.1