Git Product home page Git Product logo

sql-parser's Introduction

sql-parser

This project contains a lightweight SQL parser written in PHP.

This parser enables you to analyze SQL requests and build your own syntax helpers - very usefull for an ORM project.

Why

  • Doctrine HQL engine : 3100 LOC (+lexer)
  • php-sql-parser (from google code) : 1600 LOC
  • and this project : 500 LOC

Sample

            SELECT 
                t.*, 
                o.id as o_id,
                max(o.order) as max_order,
                count(o.*) as nb_o
            FROM 
                my_table t
            INNER JOIN
                other_table o 
                ON o.fk_table = t.id
                AND o.criteria > 123
            WHERE
                t.other_criteria = \'some text\'
            GROUP BY t.id
            ORDER BY
                t.order ASC
                t.date DESC
            LIMIT 0, 50

And the result :

Array
(
    [-s] => Array
        (
            [0] => t.*
            [o_id] => o.id
            [max_order] => Array
                (
                    [?] => Array
                        (
                            [0] => o.order
                        )

                    [$] => max
                )

            [nb_o] => Array
                (
                    [?] => Array
                        (
                            [0] => o.*
                        )

                    [$] => count
                )

        )

    [f] => Array
        (
            [t] => my_table
        )

    [w] => Array
        (
            [0] => Array
                (
                    [f] => t.other_criteria
                    [c] => =
                    [v] => 'some text'
                )

        )

    [g] => Array
        (
            [0] => t.id
        )
    [o] => Array
        (
            [0] => Array
                (
                    [0] => t.order
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => t.date
                    [1] =>
                )

        )

    [l] => Array
        (
            [0] => 0
            [1] => 50
        )

    [j] => Array
        (
            [o] => Array
                (
                    [$] => inner
                    [t] => other_table
                    [a] => o
                    [c] => Array
                        (
                            [0] => Array
                                (
                                    [f] => o.fk_table
                                    [c] => =
                                    [v] => t.id
                                )

                            [1] => and
                            [2] => Array
                                (
                                    [f] => o.criteria
                                    [c] => >
                                    [v] => 123
                                )

                        )

                )

        )

)

Extending the lib

<?php
/**
 * Defines a parser that handles named parameters
 */
class ParamParser extends \beaba\storage\Parser {
    protected $params = array();
    
    // Injecting the request object
    public function read($statement)
    {
        $this->params = array();
        return new Request(
            parent::read($statement),
            $this->params
        );
    }
    
    // lazy load any parameter
    protected function getParam( $name ) {
        if ( !isset($this->params[$name])) {
            $this->params[$name] = new Param($name);
        }
        return $this->params[$name];
    }
    
    // intercept parameters from token parsing 
    protected function addToken($token)
    {
        if ( $token[0] === ':' ) {
            return $this->getParam( substr($token, 1) );
        } else {
            return parent::addToken($token);
        }
    }
}

// defines a simple parameter class
class Param {
    protected $name;
    protected $value;
    public function __construct($name) {
        $this->name = $name;
    }
    public function setValue( $value ) {
        $this->value = $value;
    }
}

// defines a simple request class
class Request {
    protected $request;
    protected $params;
    public function __construct( $request, $params ) {
        $this->request = $request;
        $this->params = $params;
    }
    public function setParam( $name, $value ) {
        if ( !isset($this->params[ $name ]) ) {
            throw new \OutOfBoundsException(
                'Undefined parameter : ' . $name
            );
        }
        $this->params[ $name ]->setValue( $value );
        return $this;
    }
}

MIT License

Copyright (C) <2012> <PHP Hacks Team : http://coderwall.com/team/php-hacks>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

sql-parser's People

Contributors

ichiriac avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.