Login   Register  
PHP Classes
elePHPant
Icontem

File: Sql_Parser/Sql_ParserInsert.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tom Schaefer  >  SQL Parse and Compile  >  Sql_Parser/Sql_ParserInsert.class.php  >  Download  
File: Sql_Parser/Sql_ParserInsert.class.php
Role: Class source
Content type: text/plain
Description: parses insert statement into object
Class: SQL Parse and Compile
Parse and compose SQL queries programatically
Author: By
Last change: enable multiple (bulk) inserts:

#sample sql

$SQL = "INSERT INTO
northwind.employees
(LastName, FirstName, BirthDate, HireDate, Address, City, Region, PostalCode, Country, HomePhone)
VALUES
('Davolio', 'Nancy', '1992-05-01', 'Seattle', 'WA', 98122, 'United States', '(206) 555-9857', 5467, ''),
('Smith', 'Tom', '1991-02-01', 'New York', 'NY', 12345, 'United States', '(555) 555-9857', 1234, 'some text here'),
('Doe', 'John', '1993-04-01', 'Los Angeles', 'CA', 12345, 'United States', '(555) 555-9857', 4321, 'Some text here')";

$sqlObject = new Sql_Parser($SQL);
$parsedSQL = $sqlObject->parse();


Date: 2009-02-07 04:03
Size: 2,740 bytes
 

Contents

Class file image Download
<?php

/**
 *
 * Sql_ParserInsert
 * @package Sql
 * @subpackage Sql_Parser
 * @author Thomas Sch&#65533;fer
 * @since 30.11.2008 07:49:30
 * @desc parses a sql insert statement into object
 */
/**
 *
 * Sql_ParserInsert
 * @package Sql
 * @subpackage Sql_Parser
 * @author Thomas Sch&#65533;fer
 * @since 30.11.2008 07:49:30
 * @desc parses a sql insert statement into object
 */
class Sql_ParserInsert implements Sql_InterfaceParser {

    private function 
doParse($type="insert") {

        
$tree = array('Command' => $type);

        
Sql_Parser::getTok();

        if(
Sql_Object::token() == "ident" and Sql_Object::lexer()->tokText=="DELAYED") {
                
$tree["Statement"][] = Sql_Object::lexer()->tokText;
                
Sql_Parser::getTok();
        }

        if(
Sql_Object::token() == "ident" and Sql_Object::lexer()->tokText=="IGNORE") {
                
$tree["Statement"][] = Sql_Object::lexer()->tokText;
                
Sql_Parser::getTok();
        }

        if (
Sql_Object::token() == 'into')
        {
            
Sql_Parser::getTok();
            if (
Sql_Object::token() == 'ident')
            {
                
$tree['TableNames'][] = Sql_Object::lexer()->tokText;
                
Sql_Parser::getTok();
                if(
Sql_Object::token()=="select") {
                    
$tree['SubClause'] = Sql_ParserSelect::doParse();
                    return 
$tree;
                }
            }
            else
            {
                return 
Sql_Parser::raiseError('Expected table name');
            }

            if (
Sql_Object::token() == '(')
            {
                
$results Sql_Parser::getParams();

                if (
Sql_Parser::isError($results))
                {
                    return 
$results;
                }
                else
                {
                    if (
sizeof($results))
                    {
                        
$tree['ColumnNames'] = $results["values"];
                    }
                }
                
Sql_Parser::getTok();
            }

            if (
strtolower(Sql_Object::token()) == 'values')
            {
                do {
                    
Sql_Parser::getTok();
                    
$results Sql_Parser::getParams();
                    if (
Sql_Parser::isError($results))
                    {
                        return 
$results;
                    }
                    else
                    {
                        if (isset(
$tree['ColumnDefs']) && (count($tree['ColumnDefs']) != count($results)))
                        {
                            return 
Sql_Parser::raiseError('field/value mismatch');
                        }
                        if (
count($results)) {
                            
$values = array();
                            foreach (
$results["values"] as $key=>$value) {
                                
$values[$key] = array('Value'=>$value'Type'=>$results["types"][$key]);
                            }
                            
$tree['Values'][] = $values;
                        }
                        else
                        {
                            return 
Sql_Parser::raiseError('No fields to insert');
                        }
                    }
                    
Sql_Parser::getTok();
                } while (
Sql_Object::token() == ',');
            }
            else
            {
                return 
Sql_Parser::raiseError('Expected "values"');
            }
        }
        else
        {
            return 
Sql_Parser::raiseError('Expected "into"');
        }
        return 
$tree;
    }

    public static function 
parse(){
        return 
self::doParse();
    }

}