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

Source for file PMO_MyController.php

Documentation is available at PMO_MyController.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_MyController class which you can use
  4.  * to send queries to your database.
  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.1x
  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_Controller.php');
  39.  
  40. /** requires all the needed classes */
  41. require_once(dirname(__FILE__).'/PMO_MyObject.php');
  42. require_once(dirname(__FILE__).'/PMO_MyMap.php');
  43. require_once(dirname(__FILE__).'/PMO_MyParser.php');
  44. require_once(dirname(__FILE__).'/PMO_MyTable.php');
  45. require_once(dirname(__FILE__).'/PMO_MyDbms.php');
  46. require_once(dirname(__FILE__).'/PMO_MyConfig.php');
  47. require_once(dirname(__FILE__).'/PMO_MyRequest.php');
  48. require_once(dirname(__FILE__).'/PMO_MyArray.php');
  49. require_once(dirname(__FILE__).'/PMO_MyMemCache.php');
  50.  
  51.  
  52.  
  53. /**
  54.  * PMO_MyController enable you to send queries to you database
  55.  * and returns the data.
  56.  *
  57.  * This class also enables you to execute raw sql query and transform
  58.  * each tuples result into distinct {@link PMO_Object}.
  59.  * 
  60.  * Iterations schema is<br />
  61.  * For each tuple > For each Table > Build an object > For each column > Set the value of object
  62.  * 
  63.  * Before instanciation, a new object controller checks if it already exists
  64.  * in a hash map. If it does exist, the controller will retrieve its reference.
  65.  *
  66.  * Objects are always referenced in this hash map after their instanciation.
  67.  * 
  68.  * @package        PhpMyObject
  69.  * @subpackage PMO_Core
  70.  */ 
  71. class PMO_MyController implements PMO_Controller {
  72.  
  73.     /**
  74.      * the Dbms instance
  75.      * @var object 
  76.      * @access protected
  77.      */
  78.     protected $dbms_instance;
  79.  
  80.     /**
  81.      * the map of PMO_MyObject objects this controller holds
  82.      *
  83.      * @var object                {@link PMO_MyMap} object that holds the retrieved objects
  84.      * @access protected
  85.      */
  86.     protected $map_objects;
  87.  
  88.     /**
  89.      * a map of PMO_MyTable objects
  90.      *
  91.      * @var object            {@link PMO_MyMapTable} objects
  92.      * @access protected
  93.      */
  94.     protected $map_tables;
  95.  
  96.     /**
  97.      * ArrayIterator SPL Object.
  98.      *
  99.      * @var object            {@link ArrayIterator} object
  100.      * @access protected
  101.      */
  102.     protected $array_iterator;
  103.  
  104.     /**
  105.      * the SQL parser object
  106.      *
  107.      * @var object            {@link PMO_MyParser} object
  108.      * @access protected
  109.      */
  110.     protected $parsersql;
  111.  
  112.     /**
  113.      * the constructor
  114.      * the constrortor instanciates the Ddms engine and initialize itself
  115.      *
  116.      * @param object $object        a PDO object to use fr the database requests.
  117.      *                                         If NULL, the Ddms driver specified by the
  118.      *                                         configuration will be used
  119.      * @return object                    returns the PMO_MyController object
  120.      */
  121.     public function __construct(pdo $object NULL{
  122.         $this->dbms_instance = PMO_MyDbms::factory($object);
  123.         $this->init();        
  124.     }
  125.  
  126.     /**
  127.      * Short description
  128.      * @todo
  129.      */
  130.     private function populateCollector(){
  131.         while($sqlfunction $this->parsersql->fetchFunction()){
  132.             if(!isset($collector)){
  133.                 $collector new PMO_MyTable();
  134.                 $collector->setTableName(PMO_MyConfig::factory()->get('PMO_MyController.OBJECT_COLLECTOR_NAME'));
  135.                 $this->map_tables->append($collector);
  136.             }
  137.                         
  138.             $tmparray array("Field"=>$sqlfunction
  139.                 "Type" => ""
  140.                 "Null" => ""
  141.                 "Key"=> "PRI"
  142.                 "Default"=> ""
  143.                 "Extra"=> "",
  144.                 "Perm"=>"rw");
  145.  
  146.             $collector->set($tmparray);                
  147.         }            
  148.     }
  149.     
  150.     /**
  151.      * Retrieve the name of tables from an sql query,
  152.      * instanciate the tables objects PMO_Table corresponding and
  153.      * put them in a PMO_MapTable
  154.      * 
  155.      * @param string $query            the SQL query to get the table names from
  156.      * @return void 
  157.      */
  158.     private function populateMapTables($query){
  159.  
  160.         $this->parsersql->parseRequest($query);
  161.         $numfields $this->parsersql->countFields();
  162.  
  163.         while($table $this->parsersql->fetchTable()){
  164.             $objecttable PMO_MyTable::factory($table);
  165.             $this->map_tables->append($objecttable);
  166.             
  167.             if($numfields 0){
  168.                 $objecttable->setPermForAll("r");
  169.                 while($field $this->parsersql->fetchField()){
  170.                         if($objecttable->issetAttribute($field))
  171.                             $objecttable->setPerm($field"rw");
  172.                 }
  173.             }
  174.         }                        
  175.     }
  176.  
  177.     /**
  178.      * Transform results of an sql query to objects
  179.      * and put them in a map of objects
  180.      * 
  181.      * @param string $query        the SQL query
  182.      * @return PMO_Map 
  183.      * @throws Exception
  184.      */
  185.     private function populateMapObjects($query){    
  186.         $numtables $this->map_tables->count();
  187.  
  188.         /**
  189.         * SQL request with more than 1 table
  190.         */
  191.         if($numtables 1){
  192.             $this->array_iterator = new ArrayIterator();
  193.             $this->dbms_instance->query($query);
  194.             /**
  195.             * Foreach row of raw results, we build a row of objects 
  196.             */
  197.             while($db_result $this->dbms_instance->fetchArray()) {
  198.                 $row array();
  199.                 while($table $this->map_tables->fetch()){
  200.                     $tablename $table->getTableName();
  201.                     $fingerprint "";
  202.                     $arrayofpk $table->getPk();
  203.                     
  204.                     /**
  205.                      *  Build unique fingerprint for object
  206.                      *  from primary keys 
  207.                      */
  208.                     foreach($arrayofpk as $pk)
  209.                         $fingerprint $fingerprint.$pk.$db_result[$pk];
  210.  
  211.                     /**
  212.                      * We retrieve the object reference in arrayIterator
  213.                      * if object is not present, we create it
  214.                      * add it in the arrayIterator, and reference it
  215.                      * into a row and put the row into the PMO_Map
  216.                      */    
  217.                     if($this->array_iterator->offsetExists($tablename.$fingerprint)){
  218.                         $currentobject $this->array_iterator->offsetGet($tablename.$fingerprint);
  219.                     }else{
  220.                         $currentobject PMO_MyObject::internalfactory($table);
  221.                         $tablefields $table->getColumns()
  222.                         
  223.                         foreach$tablefields as $key=>$field){        
  224.                             if($table->getPerm($field== "rw")
  225.                                 $currentobject->set($field$db_result[$field]);
  226.                         }
  227.                         $this->array_iterator->offsetSet($tablename.$fingerprint$currentobject);
  228.                     }
  229.                     
  230.                     $row[$tablename=  $currentobject;
  231.                 }
  232.                 $this->map_objects->add($row);    
  233.             }
  234.         }else{
  235.             $table $this->map_tables->fetch();
  236.             $tablename $table->getTableName();
  237.             $tablefields $table->getColumns();
  238.             
  239.             $this->dbms_instance->query($query);
  240.             while($db_result $this->dbms_instance->fetchArray()) {
  241.                 $line array();
  242.                 $currentobject PMO_MyObject::internalfactory($table)
  243.                 foreach$tablefields as $key=>$field){
  244.                     if($table->getPerm($field== "rw")
  245.                         $currentobject->set($field$db_result[$field]);
  246.                 }
  247.                 $line[$tablename$currentobject;
  248.                 $this->map_objects->add($line);
  249.             }    
  250.         }
  251.         
  252.         if ($this->map_objects->count(0)
  253.             return $this->map_objects;
  254.         else
  255.             throw new Exception("Error: PMO_Map is empty");
  256.     }
  257.  
  258.     /**
  259.      * Return map of objects allready
  260.      * loaded by query()
  261.      * 
  262.      * @return PMO_Map 
  263.      * @throws Exception
  264.      */
  265.     public function getMapObjects(){
  266.         if(isset($this->map_objects))
  267.             return $this->map_objects;
  268.             
  269.         throw new Exception("Error: Map is empty");
  270.     }
  271.  
  272.     /**
  273.      * Execute a PMO_MyRequest query
  274.      *
  275.      * @param object request        a {@link PMO_Request} object
  276.      *                                         that has been used to build the query
  277.      * @return PMO_Map                a {@link PMO_Map} object
  278.      */
  279.     public function objectquery(PMO_Request $request){
  280.         return $this->query($request->toString());
  281.     }
  282.     
  283.     /**
  284.      * Execute an sql query and
  285.      * return the corresponding PMO_Map fill with PMO_Object
  286.      * 
  287.      * @param string $query            the SQL query to execute
  288.      * @return PMO_Map 
  289.      * @throws Exception
  290.      */
  291.     public function query($query){
  292.         $this->init();            
  293.         $this->populateMapTables($query);
  294.         $this->populateCollector();
  295.         $this->populateMapObjects($query);
  296.         return $this->getMapObjects();
  297.     }
  298.     
  299.     /**
  300.      * Execute an sql query without traitment
  301.      *
  302.      * @param string $query        the SQL query
  303.      * @return object|resource       the method will return either a PDO object
  304.      *                                         or a DBMS resource link, depending on the
  305.      *                                         selected driver
  306.      */
  307.     public function rawquery($query){
  308.         $this->dbms_instance->query($query);
  309.         return $this->dbms_instance;
  310.     }
  311.     
  312.     /**
  313.      * Should not be use
  314.      * return all tuples of one table
  315.      * equivalent : SELECT    * table;
  316.      * 
  317.      * @param object $table            a {@link PMO_Table} object
  318.      * @return PMO_Map 
  319.      * @throws Exception
  320.      */
  321.     public function queryAll(PMO_Table $table){
  322.         $this->init();
  323.         $tablename $table->getTableName();
  324.         
  325.         $this->dbms_instance->query("SELECT * FROM $tablename;");
  326.         while($db_result $this->dbms_instance->fetchArray()) {
  327.             $currentobject PMO_MyObject::internalfactory($table);
  328.             $currentobject->initObjectMap($this->map_objects);
  329.             $tablefields $table->getColumns()
  330.             foreach$tablefields as $key=>$field)
  331.                 $currentobject->set($field$db_result[$field]);
  332.             
  333.             $this->map_objects->addLine($currentobject);
  334.         }
  335.         return $this->map_objects;
  336.     }
  337.  
  338.     /**
  339.      * initializes the controller
  340.      */
  341.     public function init(){
  342.         $this->map_objects = new PMO_MyMap();
  343.         $this->map_tables = new PMO_MyArray();
  344.         $this->parsersql = new PMO_MyParser();        
  345.     }
  346.  
  347. }
  348. ?>

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