{"id":13571637,"url":"https://github.com/jdorn/sql-formatter","last_synced_at":"2025-04-23T20:47:08.742Z","repository":{"id":3150614,"uuid":"4180420","full_name":"jdorn/sql-formatter","owner":"jdorn","description":"A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.","archived":false,"fork":false,"pushed_at":"2023-05-26T00:23:48.000Z","size":932,"stargazers_count":3879,"open_issues_count":37,"forks_count":185,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-04-22T06:19:40.922Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://jdorn.github.com/sql-formatter/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdorn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2012-04-30T06:33:10.000Z","updated_at":"2025-04-16T02:37:08.000Z","dependencies_parsed_at":"2023-07-05T20:32:25.387Z","dependency_job_id":null,"html_url":"https://github.com/jdorn/sql-formatter","commit_stats":{"total_commits":101,"total_committers":20,"mean_commits":5.05,"dds":0.4554455445544554,"last_synced_commit":"7ef9b85961956aa572413693e1194b60f50ab9ab"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdorn%2Fsql-formatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdorn%2Fsql-formatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdorn%2Fsql-formatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdorn%2Fsql-formatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdorn","download_url":"https://codeload.github.com/jdorn/sql-formatter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250513576,"owners_count":21443201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T14:01:04.134Z","updated_at":"2025-04-23T20:47:08.726Z","avatar_url":"https://github.com/jdorn.png","language":"HTML","readme":"SqlFormatter\n=============\n\nA lightweight php class for formatting sql statements.\n\nIt can automatically indent and add line breaks in addition to syntax highlighting.\n\nHistory\n============\n\nI found myself having to debug auto-generated SQL statements all the time and\nwanted some way to easily output formatted HTML without having to include a \nhuge library or copy and paste into online formatters.\n\nI was originally planning to extract the formatting code from PhpMyAdmin,\nbut that was 10,000+ lines of code and used global variables.\n\nI saw that other people had the same problem and used Stack Overflow user \nlosif's answer as a starting point.  http://stackoverflow.com/a/3924147\n\nUsage\n============\n\nThe SqlFormatter class has a static method 'format' which takes a SQL string  \nas input and returns a formatted HTML block inside a pre tag. \n\nSample usage:\n\n```php\n\u003c?php\nrequire_once('SqlFormatter.php');\n\n$query = \"SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1`\n    WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 \u003e= NOW()) )\n    GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10\";\n\necho SqlFormatter::format($query);\n```\n\nOutput:\n\n![](http://jdorn.github.com/sql-formatter/format-highlight.png)\n\nFormatting Only\n-------------------------\nIf you don't want syntax highlighting and only want the indentations and \nline breaks, pass in false as the second parameter.\n\nThis is useful for outputting to error logs or other non-html formats.\n\n```php\n\u003c?php\necho SqlFormatter::format($query, false);\n```\n\nOutput:\n\n![](http://jdorn.github.com/sql-formatter/format.png)\n\nSyntax Highlighting Only\n-------------------------\n\nThere is a separate method 'highlight' that preserves all original whitespace\nand just adds syntax highlighting.\n\nThis is useful for sql that is already well formatted and just needs to be a little\neasier to read.\n\n```php\n\u003c?php\necho SqlFormatter::highlight($query);\n```\n\nOutput:\n\n![](http://jdorn.github.com/sql-formatter/highlight.png)\n\nCompress Query\n--------------------------\n\nThe compress method removes all comments and compresses whitespace.\n\nThis is useful for outputting queries that can be copy pasted to the command line easily.\n\n```\n-- This is a comment\n    SELECT\n    /* This is another comment\n    On more than one line */\n    Id #This is one final comment\n    as temp, DateCreated as Created FROM MyTable;\n```\n\n```php\necho SqlFormatter::compress($query)\n```\n\nOutput:\n\n```\nSELECT Id as temp, DateCreated as Created FROM MyTable;\n```\n\nRemove Comments\n------------------------\nIf you want to keep all original whitespace formatting and just remove comments, \nyou can use the removeComments method instead of compress.\n\n```\n-- This is a comment\n    SELECT\n    /* This is another comment\n    On more than one line */\n    Id #This is one final comment\n    as temp, DateCreated as Created FROM MyTable;\n```\n\n```php\n\u003c?php\necho SqlFormatter::removeComments($query);\n```\n\nOutput:\n```\n\n    SELECT\n    \n    Id \n    as temp, DateCreated as Created FROM MyTable;\n```\n\nSplit SQL String into Queries\n--------------------------\n\nAnother feature, which is unrelated to formatting, is the ability to break up a SQL string into multiple queries.  \n\nFor Example:\n\n```sql\nDROP TABLE IF EXISTS MyTable;\nCREATE TABLE MyTable ( id int );\nINSERT INTO MyTable\t(id)\n\tVALUES\n\t(1),(2),(3),(4);\nSELECT * FROM MyTable;\n```\n\n```php\n\u003c?php\n$queries = SqlFormatter::splitQuery($sql);\n```\n\nResult:\n\n1.    `DROP TABLE IF EXISTS MyTable`;\n2.    `CREATE TABLE MyTable ( id int )`;\n3.    `INSERT INTO MyTable (id) VALUES (1),(2),(3),(4)`;\n4.    `SELECT * FROM MyTable`;\n\n### Why Not Regular Expressions?\n\nWhy not just use `explode(';', $sql)` or a regular expression?\n\nThe following example sql and others like it are _impossible_ to split correctly using regular expressions, no matter how complex.  \n\n```\nSELECT \";\"; SELECT \";\\\"; a;\";\nSELECT \";\n    abc\";\nSELECT a,b #comment;\nFROM test;\n```\n\nSqlFormatter breaks the string into tokens instead of using regular expressions and will correctly produce:\n\n1.    `SELECT \";\"`;\n2.    `SELECT \";\\\"; a;\"`;\n3.    `SELECT \"; abc\"`;\n4.    `SELECT a,b #comment;\nFROM test`;\n\nPlease note, the splitQuery method will still fail in the following cases:\n*    The DELIMITER command can be used to change the delimiter from the default ';' to something else.  \n*    The CREATE PROCEDURE command has a ';' in the middle of it\n*    The USE command is not terminated with a ';'\n","funding_links":[],"categories":["HTML"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdorn%2Fsql-formatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdorn%2Fsql-formatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdorn%2Fsql-formatter/lists"}