Git Product home page Git Product logo

sql-parser's Introduction

SQL Parser

A validating SQL lexer and parser with a focus on MySQL dialect.

Code status

Build Status Code Coverage codecov.io Scrutinizer Code Quality Translation status Packagist Open Source Helpers

Installation

Please use Composer to install:

composer require phpmyadmin/sql-parser

Documentation

The API documentation is available at https://develdocs.phpmyadmin.net/sql-parser/.

Usage

Command line utilities

Command line utility to syntax highlight SQL query:

./vendor/bin/highlight-query --query "SELECT 1"

Command line utility to lint SQL query:

./vendor/bin/lint-query --query "SELECT 1"

Command line utility to tokenize SQL query:

./vendor/bin/tokenize-query --query "SELECT 1"

All commands are able to parse input from stdin (standard in), such as:

echo "SELECT 1" | ./vendor/bin/highlight-query
cat example.sql | ./vendor/bin/lint-query

Formatting SQL query

echo PhpMyAdmin\SqlParser\Utils\Formatter::format($query, ['type' => 'html']);

Discoverying query type

use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Utils\Query;

$query = 'OPTIMIZE TABLE tbl';
$parser = new Parser($query);
$flags = Query::getFlags($parser->statements[0]);

echo $flags['querytype'];

Parsing and building SQL query

require __DIR__ . '/vendor/autoload.php';

$query1 = "select * from a";
$parser = new PhpMyAdmin\SqlParser\Parser($query1);

// inspect query
var_dump($parser->statements[0]); // outputs object(PhpMyAdmin\SqlParser\Statements\SelectStatement)

// modify query by replacing table a with table b
$table2 = new \PhpMyAdmin\SqlParser\Components\Expression('', 'b', '', '');
$parser->statements[0]->from[0] = $table2;

// build query again from an array of object(PhpMyAdmin\SqlParser\Statements\SelectStatement) to a string
$statement = $parser->statements[0];
$query2 = $statement->build();
var_dump($query2); // outputs string(19) 'SELECT  * FROM `b` '

// Change SQL mode
PhpMyAdmin\SqlParser\Context::setMode('ANSI_QUOTES');

// build the query again using different quotes
$query2 = $statement->build();
var_dump($query2); // outputs string(19) 'SELECT  * FROM "b" '

Localization

You can localize error messages installing phpmyadmin/motranslator version 5.0 or newer:

composer require phpmyadmin/motranslator:^5.0

The locale is automatically detected from your environment, you can also set a different locale

From cli:

LC_ALL=pl ./vendor/bin/lint-query --query "SELECT 1"

From php:

require __DIR__ . '/vendor/autoload.php';

$GLOBALS['lang'] = 'pl';

$query1 = 'select * from a';
$parser = new PhpMyAdmin\SqlParser\Parser($query1);

More information

This library was originally created during the Google Summer of Code 2015 and has been used by phpMyAdmin since version 4.5.

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.