home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / SQL / Compiler.php next >
Encoding:
PHP Script  |  2008-07-02  |  10.8 KB  |  314 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2003-2004 John Griffin                                 |
  5. // +----------------------------------------------------------------------+
  6. // | This library is free software; you can redistribute it and/or        |
  7. // | modify it under the terms of the GNU Lesser General Public           |
  8. // | License as published by the Free Software Foundation; either         |
  9. // | version 2.1 of the License, or (at your option) any later version.   |
  10. // |                                                                      |
  11. // | This library is distributed in the hope that it will be useful,      |
  12. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  14. // | Lesser General Public License for more details.                      |
  15. // |                                                                      |
  16. // | You should have received a copy of the GNU Lesser General Public     |
  17. // | License along with this library; if not, write to the Free Software  |
  18. // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
  19. // +----------------------------------------------------------------------+
  20. // | Authors: John Griffin <jgriffin316@netscape.net>                     |
  21. // +----------------------------------------------------------------------+
  22. //
  23. // $Id: Compiler.php,v 1.1 2004/05/07 12:33:35 busterb Exp $
  24. //
  25.  
  26. require_once 'PEAR.php';
  27.  
  28. /**
  29.  * A SQL parse tree compiler.
  30.  *
  31.  * @author  John Griffin <jgriffin316@netscape.net>
  32.  * @version 0.1
  33.  * @access  public
  34.  * @package SQL_Parser
  35.  */
  36. class SQL_Compiler {
  37.     var $tree;
  38.  
  39. // {{{ function SQL_Compiler($array = null)
  40.     function SQL_Compiler($array = null)
  41.     {
  42.         $this->tree = $array;
  43.     }
  44. // }}}
  45.  
  46. //    {{{ function getWhereValue ($arg)
  47.     function getWhereValue ($arg)
  48.     {
  49.         switch ($arg['type']) {
  50.             case 'ident':
  51.             case 'real_val':
  52.             case 'int_val':
  53.                 $value = $arg['value'];
  54.                 break;
  55.             case 'text_val':
  56.                 $value = '\''.$arg['value'].'\'';
  57.                 break;
  58.             case 'subclause':
  59.                 $value = '('.$this->compileSearchClause($arg['value']).')';
  60.                 break;
  61.             default:
  62.                 return PEAR::raiseError('Unknown type: '.$arg['type']);
  63.         }
  64.         return $value;
  65.     }
  66. //    }}}
  67.  
  68. //    {{{ function getParams($arg)
  69.     function getParams($arg)
  70.     {
  71.         for ($i = 0; $i < sizeof ($arg['type']); $i++) {
  72.             switch ($arg['type'][$i]) {
  73.                 case 'ident':
  74.                 case 'real_val':
  75.                 case 'int_val':
  76.                     $value[] = $arg['value'][$i];
  77.                     break;
  78.                 case 'text_val':
  79.                     $value[] = '\''.$arg['value'][$i].'\'';
  80.                     break;
  81.                 default:
  82.                     return PEAR::raiseError('Unknown type: '.$arg['type']);
  83.             }
  84.         }
  85.         $value ='('.implode(', ', $value).')';
  86.         return $value;
  87.     }
  88. //    }}}
  89.  
  90. //    {{{ function compileSearchClause
  91.     function compileSearchClause($where_clause)
  92.     {
  93.         $value = '';
  94.         if (isset ($where_clause['arg_1']['value'])) {
  95.             $value = $this->getWhereValue ($where_clause['arg_1']);
  96.             if (PEAR::isError($value)) {
  97.                 return $value;
  98.             }
  99.             $sql = $value;
  100.         } else {
  101.             $value = $this->compileSearchClause($where_clause['arg_1']);
  102.             if (PEAR::isError($value)) {
  103.                 return $value;
  104.             }
  105.             $sql = $value;
  106.         }
  107.         if (isset ($where_clause['op'])) {
  108.             if ($where_clause['op'] == 'in') {
  109.                 $value = $this->getParams($where_clause['arg_2']);
  110.                 if (PEAR::isError($value)) {
  111.                     return $value;
  112.                 }
  113.                 $sql .= ' '.$where_clause['op'].' '.$value;
  114.             } elseif ($where_clause['op'] == 'is') {
  115.                 if (isset ($where_clause['neg'])) {
  116.                     $value = 'not null';
  117.                 } else {
  118.                     $value = 'null';
  119.                 }
  120.                 $sql .= ' is '.$value;
  121.             } else {
  122.                 $sql .= ' '.$where_clause['op'].' ';
  123.                 if (isset ($where_clause['arg_2']['value'])) {
  124.                     $value = $this->getWhereValue ($where_clause['arg_2']);
  125.                     if (PEAR::isError($value)) {
  126.                         return $value;
  127.                     }
  128.                     $sql .= $value;
  129.                 } else {
  130.                     $value = $this->compileSearchClause($where_clause['arg_2']);
  131.                     if (PEAR::isError($value)) {
  132.                         return $value;
  133.                     }
  134.                     $sql .= $value;
  135.                 }
  136.             }
  137.         }
  138.         return $sql;
  139.     }
  140. //    }}}
  141.  
  142. //    {{{ function compileSelect()
  143.     function compileSelect()
  144.     {
  145.         // save the command and set quantifiers
  146.         $sql = 'select ';
  147.         if (isset($this->tree['set_quantifier'])) {
  148.             $sql .= $this->tree['set_quantifier'].' ';
  149.         }
  150.         
  151.         // save the column names and set functions
  152.         for ($i = 0; $i < sizeof ($this->tree['column_names']); $i++) {
  153.             $column = $this->tree['column_names'][$i];
  154.             if ($this->tree['column_aliases'][$i] != '') {
  155.                 $column .= ' as '.$this->tree['column_aliases'][$i];
  156.             }
  157.             $column_names[] = $column;
  158.         }
  159.         for ($i = 0; $i < sizeof ($this->tree['set_function']); $i++) {
  160.             $column = $this->tree['set_function'][$i]['name'].'(';
  161.             if (isset ($this->tree['set_function'][$i]['distinct'])) {
  162.                 $column .= 'distinct ';
  163.             }
  164.             if (isset ($this->tree['set_function'][$i]['arg'])) {
  165.                 $column .= implode (',', $this->tree['set_function'][$i]['arg']);
  166.             }
  167.             $column .= ')';
  168.             if ($this->tree['set_function'][$i]['alias'] != '') {
  169.                 $column .= ' as '.$this->tree['set_function'][$i]['alias'];
  170.             }
  171.             $column_names[] = $column;
  172.         }
  173.         if (isset($column_names)) {
  174.             $sql .= implode (", ", $column_names);
  175.         }
  176.         
  177.         // save the tables
  178.         $sql .= ' from ';
  179.         for ($i = 0; $i < sizeof ($this->tree['table_names']); $i++) {
  180.             $sql .= $this->tree['table_names'][$i];
  181.             if ($this->tree['table_aliases'][$i] != '') {
  182.                 $sql .= ' as '.$this->tree['table_aliases'][$i];
  183.             }
  184.             if ($this->tree['table_join_clause'][$i] != '') {
  185.                 $search_string = $this->compileSearchClause ($this->tree['table_join_clause'][$i]);
  186.                 if (PEAR::isError($search_string)) {
  187.                     return $search_string;
  188.                 }
  189.                 $sql .= ' on '.$search_string;
  190.             }
  191.             if (isset($this->tree['table_join'][$i])) {
  192.                 $sql .= ' '.$this->tree['table_join'][$i].' ';
  193.             }
  194.         }
  195.         
  196.         // save the where clause
  197.         if (isset($this->tree['where_clause'])) {
  198.             $search_string = $this->compileSearchClause ($this->tree['where_clause']);
  199.             if (PEAR::isError($search_string)) {
  200.                 return $search_string;
  201.             }
  202.             $sql .= ' where '.$search_string;
  203.         }
  204.         
  205.         // save the group by clause
  206.         if (isset ($this->tree['group_by'])) {
  207.             $sql .= ' group by '.implode(', ', $this->tree['group_by']);
  208.         }
  209.  
  210.         // save the order by clause
  211.         if (isset ($this->tree['sort_order'])) {
  212.             foreach ($this->tree['sort_order'] as $key => $value) {
  213.                 $sort_order[] = $key.' '.$value;
  214.             }
  215.             $sql .= ' order by '.implode(', ', $sort_order);
  216.         }
  217.         
  218.         // save the limit clause
  219.         if (isset ($this->tree['limit_clause'])) {
  220.             $sql .= ' limit '.$this->tree['limit_clause']['start'].','.$this->tree['limit_clause']['length'];
  221.         }
  222.         
  223.         return $sql;
  224.     }
  225. //    }}}
  226.  
  227. //    {{{ function compileUpdate()
  228.     function compileUpdate()
  229.     {
  230.         $sql = 'update '.implode(', ', $this->tree['table_names']);
  231.         
  232.         // save the set clause
  233.         for ($i = 0; $i < sizeof ($this->tree['column_names']); $i++) {
  234.             $set_columns[] = $this->tree['column_names'][$i].' = '.$this->getWhereValue($this->tree['values'][$i]);
  235.         }
  236.         $sql .= ' set '.implode (', ', $set_columns);
  237.         
  238.         // save the where clause
  239.         if (isset($this->tree['where_clause'])) {
  240.             $search_string = $this->compileSearchClause ($this->tree['where_clause']);
  241.             if (PEAR::isError($search_string)) {
  242.                 return $search_string;
  243.             }
  244.             $sql .= ' where '.$search_string;
  245.         }
  246.         return $sql;
  247.     }
  248. //    }}}
  249.  
  250. //    {{{ function compileDelete()
  251.     function compileDelete()
  252.     {
  253.         $sql = 'delete from '.implode(', ', $this->tree['table_names']);
  254.         
  255.         // save the where clause
  256.         if (isset($this->tree['where_clause'])) {
  257.             $search_string = $this->compileSearchClause ($this->tree['where_clause']);
  258.             if (PEAR::isError($search_string)) {
  259.                 return $search_string;
  260.             }
  261.             $sql .= ' where '.$search_string;
  262.         }
  263.         return $sql;
  264.     }
  265. //    }}}
  266.  
  267. //    {{{ function compileInsert()
  268.     function compileInsert()
  269.     {
  270.         $sql = 'insert into '.$this->tree['table_names'][0].' ('.
  271.                 implode(', ', $this->tree['column_names']).') values (';
  272.         for ($i = 0; $i < sizeof ($this->tree['values']); $i++) {
  273.             $value = $this->getWhereValue ($this->tree['values'][$i]);
  274.             if (PEAR::isError($value)) {
  275.                 return $value;
  276.             }
  277.             $value_array[] = $value;
  278.         }
  279.         $sql .= implode(', ', $value_array).')';
  280.         return $sql;
  281.     }
  282. //    }}}
  283.  
  284. //    {{{ function compile($array = null)
  285.     function compile($array = null)
  286.     {
  287.         $this->tree = $array;
  288.         
  289.         switch ($this->tree['command']) {
  290.             case 'select':
  291.                 return $this->compileSelect();
  292.                 break;
  293.             case 'update':
  294.                 return $this->compileUpdate();
  295.                 break;
  296.             case 'delete':
  297.                 return $this->compileDelete();
  298.                 break;
  299.             case 'insert':
  300.                 return $this->compileInsert();
  301.                 break;
  302.             case 'create':
  303.             case 'drop':
  304.             case 'modify':
  305.             default:
  306.                 return PEAR::raiseError('Unknown action: '.$this->tree['command']);
  307.         }    // switch ($this->_tree["command"])
  308.  
  309.     }
  310. //    }}}
  311. }
  312. ?>
  313.  
  314.