Code Monkey home page Code Monkey logo

sqlparser's Introduction

SQLParser

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:

  1. Better documentation
  2. Fluent-Interface to generate SQL statements and alter the parsed content
  3. parse CREATE TABLE/ALTER TABLE (for SQLite, MySQL and PostgreSQL flavors)

sqlparser's People

Contributors

crodas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqlparser's Issues

Issue parsing SELECT statement with database

Hi

Thanks for your work. I ran across an error when i tried to parse a SELECT statement which contains the database name.

SELECT myAttribute FROM myDatabase.myTable;
Illegal offset type vendor\crodas\sql-parser\src\SQL\Select.php on line 50

Would be nice if you could take a look at it.

Some important bugs

  • IS and IS NOT are treated and translated as = and !=, but x IS NULL is not the same as x = NULL
  • OR and AND statements are parsed with equal precedence, though AND must have more precedence (maybe it is the problem with many more operators
  • Parenthesis dissapear when writting the query

SQL\Select->getAllColumnsAsArray - new method to retrieve all columns!

Please add similar function as below.

public function getAllColumnsAsArray()
{
$result = [];
foreach ($this->columns as $k => $v) {
	if (!empty($v[1])) {
	    $result[] = $v[1];
	} else {
		$result[] = $v[0]->getMembers()[0];
	}
}
return $result;
}

This should return:

Array
(
[0] => ID
[1] => Code
[2] => Name
[3] => Parent
)

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.