Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a lightweight sql parser class written in PHP to do this? [closed]

Before jumping in and rolling my own, I thought I'd ask in here first.

I am looking to do some elementary parsing of random SQL commands in order to:

  1. inspect the field names that are being used in 'SELECT's (including any subqueries)
  2. inspect the field names that are being used in 'JOIN's (including any subqueries)
  3. inspect the names of tables being used in the query (including any subqueries)

I have seen some SQL parser classes out there, but they are far too 'heavyweight' for the use cases described above.

Is anyone aware of a lightweight class that has atleast some of the required functionality?

Worst case scenario, if I have to write a parser, what would be the best way of writing such a parser (normally, to write a parser, I would normally resort to tools that are not available in PHP), any tips on how to write a 'rough and ready' class to do this parsing?

//rough sketch
<?php
class SqlParser()
{
    protected $sqlstr;
    protected $m_tablenames = array();
    protected $m_fieldnames = array();

    public function __construct($sql){
       $this->sqlstr = $sqlcmd;
       $this->parseString($sqlstr);
    }

    public function __destroy(){}
    public function getTableNames(){ return m_tablenames; }
    public function getFieldNames(){ return m_fieldnames; }

    private function parseString($sql)
    {
        //TODO
    }
}

?>

I would prefer the parsing to be SQL dialect agnostic (i.e. not tied to any particular SQL dialect or db specific SQL) as much as possible.

If that is not possible, then the SQL dialect I will be using is PostgreSQL.

like image 362
skyeagle Avatar asked Dec 09 '10 14:12

skyeagle


2 Answers

PHP SQL Parser might be what you're looking for. It'll handle fairly complex queries, as you can see from the link. Download the code from the projects front page. The only drawback is it targets MySQL only. Adding support for PostgreSQL should be no big problem.

There's also a more basic solution for SQL parsing: PHP SQL Tokenizer, but it does not offer you anything but select/from/where/order separation: no field names, subquery extraction, or such.

like image 80
jmz Avatar answered Oct 01 '22 09:10

jmz


You might give cbMySQL a try, i do not know it very well, but it might be the thing you are looking for.

like image 32
Thariama Avatar answered Oct 01 '22 07:10

Thariama