Source for file Compiler.php
Documentation is available at Compiler.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 John Griffin |
// +----------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU Lesser General Public |
// | License as published by the Free Software Foundation; either |
// | version 2.1 of the License, or (at your option) any later version. |
// | This library is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | Lesser General Public License for more details. |
// | You should have received a copy of the GNU Lesser General Public |
// | License along with this library; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
// +----------------------------------------------------------------------+
// | Authors: John Griffin <jgriffin316@netscape.net> |
// +----------------------------------------------------------------------+
// $Id: Compiler.php 239225 2007-07-06 13:44:33Z cybot $
* A SQL parse tree compiler.
* @author John Griffin <jgriffin316@netscape.net>
// {{{ function SQL_Parser_Compiler($array = null)
// {{{ function getWhereValue ($arg)
$value = '\''. $arg['value']. '\'';
return PEAR ::raiseError ('Unknown type: '. $arg['type']);
// {{{ function getParams($arg)
$types = count($arg['type']);
for ($i = 0; $i < $types; $i++ ) {
switch ($arg['type'][$i]) {
$value[] = $arg['value'][$i];
$value[] = '\''. $arg['value'][$i]. '\'';
return PEAR ::raiseError ('Unknown type: '. $arg['type'][$i]);
$value = '('. implode(', ', $value). ')';
// {{{ function compileFunctionOpts($arg)
$types = count($arg['type']);
for ($i = 0; $i < $types; $i++ ) {
switch ($arg['type'][$i]) {
$value[] = $arg['arg'][$i];
$value[] = '\''. $arg['arg'][$i]. '\'';
return PEAR ::raiseError ('Unknown type: '. $arg['type'][$i]);
// {{{ function compileSearchClause
if (isset ($where_clause['arg_1']['value'])) {
if (PEAR ::isError ($value)) {
if (PEAR ::isError ($value)) {
if (isset ($where_clause['op'])) {
if ($where_clause['op'] == 'in') {
$value = $this->getParams($where_clause['arg_2']);
if (PEAR ::isError ($value)) {
if (isset ($where_clause['neg'])) {
$sql .= ' '. $where_clause['op']. ' '. $value;
} elseif ($where_clause['op'] == 'is') {
$value = isset ($where_clause['neg']) ? 'not null' : 'null';
$sql .= ' '. $where_clause['op']. ' ';
if (isset ($where_clause['arg_2']['value'])) {
if (PEAR ::isError ($value)) {
if (PEAR ::isError ($value)) {
// {{{ function compileSelect()
// save the command and set quantifiers
if (isset ($this->tree['set_quantifier'])) {
$sql .= $this->tree['set_quantifier']. ' ';
// save the column names and set functions
for ($i = 0; $i < $cols; $i++ ) {
$column = $this->tree['column_names'][$i];
if ($this->tree['column_aliases'][$i] != '') {
$column .= ' as '. $this->tree['column_aliases'][$i];
$column_names[] = $column;
for ($i = 0; $i < $funcs; $i++ ) {
$column = $this->tree['set_function'][$i]['name']. '(';
if (isset ($this->tree['set_function'][$i]['distinct'])) {
if (isset ($this->tree['set_function'][$i]['arg'])) {
if ($this->tree['set_function'][$i]['alias'] != '') {
$column .= ' as '. $this->tree['set_function'][$i]['alias'];
$column_names[] = $column;
if (isset ($column_names)) {
$sql .= implode (", ", $column_names);
$c_tables = count($this->tree['table_names']);
for ($i = 0; $i < $c_tables; $i++ ) {
$sql .= $this->tree['table_names'][$i];
if ($this->tree['table_aliases'][$i] != '') {
$sql .= ' as '. $this->tree['table_aliases'][$i];
if ($this->tree['table_join_clause'][$i] != '') {
if (PEAR ::isError ($search_string)) {
$sql .= ' on '. $search_string;
if (isset ($this->tree['table_join'][$i])) {
$sql .= ' '. $this->tree['table_join'][$i]. ' ';
if (isset ($this->tree['where_clause'])) {
if (PEAR ::isError ($search_string)) {
$sql .= ' where '. $search_string;
// save the group by clause
if (isset ($this->tree['group_by'])) {
$sql .= ' group by '. implode(', ', $this->tree['group_by']);
// save the order by clause
if (isset ($this->tree['sort_order'])) {
foreach ($this->tree['sort_order'] as $key => $value) {
$sort_order[] = $key. ' '. $value;
$sql .= ' order by '. implode(', ', $sort_order);
if (isset ($this->tree['limit_clause'])) {
$sql .= ' limit '. $this->tree['limit_clause']['start']. ','. $this->tree['limit_clause']['length'];
// {{{ function compileUpdate()
$sql = 'update '. implode(', ', $this->tree['table_names']);
for ($i = 0; $i < $cols; $i++ ) {
$sql .= ' set '. implode (', ', $set_columns);
if (isset ($this->tree['where_clause'])) {
if (PEAR ::isError ($search_string)) {
$sql .= ' where '. $search_string;
// {{{ function compileDelete()
$sql = 'delete from '. implode(', ', $this->tree['table_names']);
if (isset ($this->tree['where_clause'])) {
if (PEAR ::isError ($search_string)) {
$sql .= ' where '. $search_string;
// {{{ function compileInsert()
$sql = 'insert into '. $this->tree['table_names'][0 ]. ' ('.
implode(', ', $this->tree['column_names']). ') values (';
for ($i = 0; $i < $c_vals; $i++ ) {
if (PEAR ::isError ($value)) {
$sql .= implode(', ', $value_array). ')';
// {{{ function compile($array = null)
switch ($this->tree['command']) {
return PEAR ::raiseError ('Unknown action: '. $this->tree['command']);
} // switch ($this->_tree["command"])
Documentation generated on Mon, 11 Mar 2019 15:39:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|