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:
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.
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.
You might give cbMySQL a try, i do not know it very well, but it might be the thing you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With