Coder Social home page Coder Social logo

nilportugues / php-sql-query-formatter Goto Github PK

View Code? Open in Web Editor NEW
36.0 4.0 9.0 101 KB

A very lightweight PHP class that re-formats unreadable or computer-generated SQL query statements to human-friendly readable text.

Home Page: http://nilportugues.com

License: MIT License

PHP 100.00%
sql-format sql-query-formatter php formatter sql sqlite mysql mssql php7

php-sql-query-formatter's Introduction

SQL Query Formatter

Build Status Coverage Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

A very lightweight PHP class that re-formats unreadable or computer-generated SQL query statements to human-friendly readable text.

1.Installation

The recommended way to install the SQL Query Formatter is through Composer. Run the following command to install it:

php composer.phar require nilportugues/sql-query-formatter

2. Features

Human readable SQL formatting

  • Human readable plain text. No colours, no highlighting. Plain text is good enough in most cases.

Data Binding Awareness

  • SQL Query Formatter takes data binding seriously.
  • Placeholder syntax such as :variable or ? is taken into account and is preserved when formatting.

3. Usage

Sample code:

<?php
use NilPortugues\Sql\QueryFormatter\Formatter;

$query = <<<SQL
SELECT user.user_id, user.username, (SELECT 
role.role_name FROM role WHERE (role.role_id = :v1) 
LIMIT :v2, :v3 ) AS user_role, (SELECT 
role.role_name FROM role WHERE (role.role_id = :v4)
LIMIT :v5, :v6 ) AS role FROM user WHERE (user.user_id = :v7)
SQL;

$formatter = new Formatter();
echo $formatter->format($query);

Real output:

SELECT
    user.user_id,
    user.username,
    (
        SELECT
            role.role_name
        FROM
            role
        WHERE
            (role.role_id = :v1)
        LIMIT
            :v2,
            :v3
    ) AS user_role,
    (
        SELECT
            role.role_name
        FROM
            role
        WHERE
            (role.role_id = :v4)
        LIMIT
            :v5,
            :v6
    ) AS role
FROM
    user
WHERE
    (user.user_id = :v7)

4. Fully tested

Testing has been done using PHPUnit and Travis-CI. All code has been tested to be compatible from PHP 5.4 up to PHP 5.6 and HHVM (nightly release).

To run the test suite, you need Composer:

    php composer.phar install --dev
    bin/phpunit

5. Author

Nil Portugués Calderó

6. Special Thanks

I would like to thank the following people:

7. License

SQL Query Formatter is licensed under the MIT license.

Copyright (c) 2015 Nil Portugués Calderó

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

php-sql-query-formatter's People

Contributors

nilportugues avatar scrutinizer-auto-fixer avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

php-sql-query-formatter's Issues

Formatter.php could be split

New classes can be created for the following concepts:

  • Indent
  • Parentheses
  • NewLine

This will reduce complexity.

OFFSET is not parsed

Test case:

SELECT * FROM users LIMIT 50 OFFSET 100

Expected:

SELECT
    *
FROM
    users
LIMIT
    50
OFFSET
    100

Actual:

SELECT
    *
FROM
    users
LIMIT
    50 OFFSET 100

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.