All Projects → phpmyadmin → Sql Parser

phpmyadmin / Sql Parser

Licence: gpl-2.0
A validating SQL lexer and parser with a focus on MySQL dialect.

Projects that are alternatives of or similar to Sql Parser

Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+1553.17%)
Mutual labels:  sql, sql-query, mysql
Jsqlparser
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern
Stars: ✭ 3,405 (+1098.94%)
Mutual labels:  sql, parser, mysql
Sql Template Tag
ES2015 tagged template string for preparing SQL statements, works with `pg` and `mysql`
Stars: ✭ 132 (-53.52%)
Mutual labels:  sql, sql-query, mysql
Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (+31.34%)
Mutual labels:  sql, sql-query, mysql
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (+160.92%)
Mutual labels:  sql, sql-query, mysql
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+246.48%)
Mutual labels:  sql, sql-query, mysql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+643.31%)
Mutual labels:  sql, sql-query, mysql
Wilayah
Kode dan Data Wilayah Administarsi Indonesia sesuai Permendagri No 72 Tahun 2019 dengan PHP+MySQL+AJaX. Demo link : https://wilayah.cahyadsn.com/v2/
Stars: ✭ 231 (-18.66%)
Mutual labels:  sql, mysql
Il Ilce Mahalle Sokak Cadde Sql
Türkiye İl, İlçe, Mahalle, Sokak, Cadde Bilgisi SQL Şeklinde
Stars: ✭ 235 (-17.25%)
Mutual labels:  sql, mysql
Sqlfiddle3
New version based on vert.x and docker
Stars: ✭ 242 (-14.79%)
Mutual labels:  sql, mysql
Yearning Go
Mysql web端sql审核平台
Stars: ✭ 249 (-12.32%)
Mutual labels:  sql, mysql
Scany
Library for scanning data from a database into Go structs and more
Stars: ✭ 228 (-19.72%)
Mutual labels:  sql, mysql
Sql Lint
An SQL linter
Stars: ✭ 243 (-14.44%)
Mutual labels:  sql, mysql
Swifql
💎 A Swift DSL for type-safe, extensible, and transformable SQL queries.
Stars: ✭ 250 (-11.97%)
Mutual labels:  sql, mysql
Sequelize Auto Migrations
Migration generator && runner for sequelize
Stars: ✭ 233 (-17.96%)
Mutual labels:  sql, mysql
Granite
ORM Model with Adapters for mysql, pg, sqlite in the Crystal Language.
Stars: ✭ 238 (-16.2%)
Mutual labels:  sql, mysql
Sqliterally
Lightweight SQL query builder
Stars: ✭ 231 (-18.66%)
Mutual labels:  sql, mysql
E Commerce Db
Database schema for e-commerce (webstores) sites.
Stars: ✭ 245 (-13.73%)
Mutual labels:  sql, mysql
Huge Collection Of Cheatsheet
Share of my Huge Collection of Cheatsheet (Coding, Cheat, Pinouts, Command Lists, Etc.)
Stars: ✭ 250 (-11.97%)
Mutual labels:  sql, mysql
Bitnami Docker Mariadb
Bitnami MariaDB Docker Image
Stars: ✭ 251 (-11.62%)
Mutual labels:  sql, mysql

SQL Parser

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

Code status

Tests 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].