crodas / sql-parser
Easiest way to parse SQL
Installs: 2 474
Dependents: 3
Suggesters: 0
Security: 0
Stars: 13
Watchers: 8
Forks: 3
Open Issues: 2
Requires (Dev)
- crodas/autoloader: ^0.1.16
This package is auto-updated.
Last update: 2024-10-11 03:50:29 UTC
README
SQL-Parser
Why?
Sometimes we need to parse and validate SQL.
What does it do?
It parses SQL (mostly MySQL's SQL) and returns the SQL query as an object. This object can be modified programmatically to generate another SQL query.
How to install?
composer install crodas/sql-parser
How to use it?
require __DIR__ . "/vendor/autoload.php"; $parser = new SQLParser; $queries = $parser->parse("SELECT * FROM table1 WHERE id = :id"); var_dump(get_class($queries[0])); // string(16) "SQLParser\Select" var_dump($queries[0]->getTable()[0]->getValue()); // string(6) "table1" /* array(1) { [0] => string(2) "id" } */ var_dump($queries[0]->getVariables()); // SELECT * FROM 'table1' WHERE 'id' = :id echo $queries[0] . "\n"; SQLParser\Writer\SQL::setInstance(new SQLParser\Writer\MySQL); // SELECT * FROM `table1` WHERE `id` = :id echo $queries[0] . "\n";
TODO:
- Better documentation
- Fluent-Interface to generate SQL statements and alter the parsed content
- parse CREATE TABLE/ALTER TABLE (for SQLite, MySQL and PostgreSQL flavors)