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

Source for file PMO_MyParser.php

Documentation is available at PMO_MyParser.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_MyMapObject class which implements
  4.  * the generic PMO_Object that represents a tuple in 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. /** requires the interface */
  37. require_once(dirname(__FILE__).'/PMO_Parser.php');
  38.  
  39. /**
  40.  * This class    is used to parse SQL query and extract
  41.  * the tablename and the fields used
  42.  *
  43.  * @package        PhpMyObject
  44.  * @subpackage PMO_Core
  45.  */ 
  46. class PMO_MyParser implements PMO_Parser{
  47.     
  48.     protected $array_functions ;
  49.     protected $array_tables ;
  50.     protected $array_fields ;
  51.     
  52.     public function __construct(){
  53.         $this->array_fields = new PMO_MyArray();
  54.         $this->array_tables = new PMO_MyArray();
  55.         $this->array_functions = new PMO_MyArray();
  56.     }
  57.     
  58.     /**
  59.      * extract the name of the tables and fields
  60.      * from an SQL query and put them
  61.      * into fields & tables variables
  62.      * 
  63.      * @throws Exception
  64.      * @return TRUE 
  65.      */
  66.     public function parseRequest($string){
  67.         $string addslashes($string);
  68.         $str explode(' '$string);
  69.         $str str_replace(array(';''\r''\n')array(''' '' ')$str);
  70.  
  71.         $mode '';
  72.         foreach$str as $word){
  73.             switch($mode){
  74.                   case 'SELECT':
  75.                     $fields explode(','$word);
  76.                     foreach($fields as $field){
  77.                         if(preg_match('/[a-zA-Z_]+\([a-zA-Z_]+\)/'$field$alias)){
  78.                             $this->addFunction($alias[0]);
  79.                         }else{    
  80.                             if(preg_match('/\.[a-zA-Z_]+/'$field$alias)){
  81.                                 $this->addField(substr($alias[0]1strlen($alias[0])));
  82.                             }else{
  83.                                 $this->addField($field);
  84.                             }
  85.                         }
  86.                     }
  87.                     break;
  88.                   
  89.                 case 'FROM':
  90.                     $tables explode(','$word);
  91.                     foreach($tables as $table)
  92.                         $this->addTable($table);
  93.                     return TRUE;
  94.                     
  95.                 case 'WHERE':
  96.                     return TRUE;
  97.                 
  98.                 default:
  99.                     break;
  100.             }
  101.     
  102.             $mode '';
  103.             
  104.             $word strtoupper($word);
  105.             switch($word){
  106.                 case 'SELECT':
  107.                     $mode $word;
  108.                     break;
  109.                 case 'FROM':
  110.                     $mode $word;
  111.                     break;
  112.                 case 'WHERE':
  113.                     return TRUE;
  114.                 default:
  115.                     break;
  116.             }
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * add a tablename
  122.      * stocked in tables array
  123.      * 
  124.      * @return void 
  125.      * @throws Exception
  126.      */
  127.     public function addTable($tablename){
  128.         if(empty($tablename))
  129.             throw new Exception("Fatal Error: Your SQL QUERY must only contains ',' between tables name. ");
  130.         
  131.         $this->array_tables->append($tablename);
  132.     }
  133.  
  134.     /**
  135.      * add an sql function
  136.      * into function array
  137.      * 
  138.      * @return void 
  139.      */    
  140.     public function addFunction($function){
  141.         $this->array_functions->append($function);
  142.     }
  143.     
  144.     /**
  145.      * add a field name to
  146.      * field array
  147.      * 
  148.      * @return void 
  149.      */
  150.     public function addField($field){
  151.         if($field != '*')
  152.             $this->array_fields->append($field);
  153.     }
  154.     
  155.     /**
  156.      * count the number of fields
  157.      * 
  158.      * @return int 
  159.      */
  160.     public function countFields(){
  161.         return $this->array_fields->count();
  162.     }
  163.  
  164.     /**
  165.      * fetch fieldname stocked
  166.      * in the fields PMO_MyArray
  167.      * 
  168.      * @return string 
  169.      */
  170.     public function fetchField(){        
  171.         return $this->array_fields->fetch();    
  172.     }
  173.  
  174.     /**
  175.      * fetch sql function stocked
  176.      * in the functions PMO_MyArray
  177.      * 
  178.      * @return string 
  179.      */
  180.     public function fetchFunction(){
  181.         return $this->array_functions->fetch();
  182.     }
  183.     
  184.     /**
  185.      * fetch tablename stocked
  186.      * in the fields PMO_MyArray
  187.      * 
  188.      * @return string 
  189.      */    
  190.     public function fetchTable(){
  191.         return $this->array_tables->fetch();
  192.     }
  193.     
  194. }
  195. ?>

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