有时候做开发用到SQL文的时候,由于sql文太长,很难真正看清楚这个sql文的结构.

所以需要一个工具能够自动对SQL文进行排版,在网上有幸找到这个用php写的类能处理这个任务.

原文地址是http://jdorn.github.io/sql-formatter/

SqlFormatter

A PHP class for formatting and highlighting SQL statements.

 View on Github


Download

If you're using Composer in your project, simply add SqlFormatter to the require section of composer.json

require: {
"jdorn/sql-formatter": "dev-master"
}

Otherwise, Download from Github and include lib/SqlFormatter.php in your project.


Example Usage

The following example formats and highlights a really complex SQL statement.

<?php
$sql = "SELECT DATE_FORMAT(b.t_create, '%Y-%c-%d') dateID, b.title memo
FROM (SELECT id FROM orc_scheme_detail d WHERE d.business=208
AND d.type IN (29,30,31,321,33,34,3542,361,327,38,39,40,41,42,431,4422,415,4546,47,48,'a',
29,30,31,321,33,34,3542,361,327,38,39,40,41,42,431,4422,415,4546,47,48,'a')
AND d.title IS NOT NULL AND t_create >=
DATE_FORMAT((DATE_SUB(NOW(),INTERVAL 1 DAY)),'%Y-%c-%d') AND t_create
< DATE_FORMAT(NOW(), '%Y-%c-%d') ORDER BY d.id LIMIT 2,10) a,
orc_scheme_detail b WHERE a.id = b.id"; echo SqlFormatter::format($sql);
SELECT
DATE_FORMAT(b.t_create, '%Y-%c-%d') dateID,
b.title memo
FROM
(
SELECT
id
FROM
orc_scheme_detail d
WHERE
d.business = 208
AND d.type IN (
29, 30, 31, 321, 33, 34, 3542, 361, 327,
38, 39, 40, 41, 42, 431, 4422, 415, 4546,
47, 48, 'a', 29, 30, 31, 321, 33, 34, 3542,
361, 327, 38, 39, 40, 41, 42, 431, 4422,
415, 4546, 47, 48, 'a'
)
AND d.title IS NOT NULL
AND t_create >= DATE_FORMAT(
(
DATE_SUB(NOW(), INTERVAL 1 DAY)
),
'%Y-%c-%d'
)
AND t_create < DATE_FORMAT(NOW(), '%Y-%c-%d')
ORDER BY
d.id
LIMIT
2,
10
) a,
orc_scheme_detail b
WHERE
a.id = b.id

Format Only (No Syntax Highlighting)

If you pass false as the 2nd parameter to the format method, it will output formatted plain text.

<?php
$sql = "SELECT * FROM MyTable WHERE id = 46"; echo SqlFormatter::format($sql, false);
SELECT
*
FROM
MyTable
WHERE
id = 46

Syntax Highlighting Only

The highlight method keeps all the original whitespace intact and just adds syntax highlighting.

<?php
$sql = "SELECT * FROM MyTable WHERE id = 46"; echo SqlFormatter::highlight($sql);
SELECT * FROM MyTable WHERE id = 46

Compress SQL Query

The compress method compresses whitespace and removes comments.

<?php
$sql = "-- This is a comment
SELECT
/* This is another comment
On more than one line */
Id #This is one final comment
as temp, DateCreated as Created FROM MyTable;"; echo SqlFormatter::compress($sql);
SELECT Id as temp, DateCreated as Created FROM MyTable;

Remove SQL Comments

The removeComments method removes all comments, but otherwise preserves the original formatting.

<?php
$sql = "-- This is a comment
SELECT
/* This is another comment
On more than one line */
Id #This is one final comment
as temp, DateCreated as Created FROM MyTable;"; echo SqlFormatter::removeComments($sql);
SELECT
Id
as temp, DateCreated as Created FROM MyTable;

Split SQL into Individual Queries

The splitQuery method takes a semicolon delimited SQL string and returns an array of the individual queries.

<?php
$sql = "DROP TABLE IF EXISTS MyTable;
CREATE TABLE MyTable ( id int );
INSERT INTO MyTable (id) VALUES (1),(2),(3),(4); SELECT * FROM MyTable;"; var_export(SqlFormatter::splitQuery($sql));
array (
0 => 'DROP TABLE IF EXISTS MyTable;',
1 => 'CREATE TABLE MyTable ( id int );',
2 => 'INSERT INTO MyTable (id) VALUES (1),(2),(3),(4);',
3 => 'SELECT * FROM MyTable;'
)

Using from the PHP CLI

SqlFormatter will automatically detect when running in CLI mode and will use ASCII shell highlighting instead of HTML.


Created by Jeremy Dorn

推荐用于格式化以及高亮显示SQL文的PHP类-SqlFormatter的更多相关文章

  1. Shell脚本调用SQL文格式

    Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...

  2. 【强烈强烈推荐】《ORACLE PL/SQL编程详解》全原创(共八篇)--系列文章导航

    原文:[强烈强烈推荐]<ORACLE PL/SQL编程详解>全原创(共八篇)--系列文章导航 <ORACLE PL/SQL编程详解> 系列文章目录导航 ——通过知识共享树立个人 ...

  3. 使用正則表達式的格式化与高亮显示json字符串

    使用正則表達式的格式化与高亮显示json字符串 json字符串非常实用,有时候一些后台接口返回的信息是字符串格式的,可读性非常差,这个时候要是有个能够格式化并高亮显示json串的方法那就好多了,以下看 ...

  4. Numeral.js 是一个用于格式化和数字四则运算的js 库

    1.Numeral.js 是一个用于格式化和数字四则运算的js 库. 2.支持多种语言,包含中文在内的17种语言. 在浏览器中引用js文件: <script src="numeral. ...

  5. mysql中SQL执行过程详解与用于预处理语句的SQL语法

    mysql中SQL执行过程详解 客户端发送一条查询给服务器: 服务器先检查查询缓存,如果命中了缓存,则立刻返回存储在缓存中的结果.否则进入下一阶段. 服务器段进行SQL解析.预处理,在优化器生成对应的 ...

  6. 用batch调用DB2 CLPPlus执行多个SQL文

    不啰嗦直接上技能. 大概三部分组成: 1.bat文件.(run.bat) 2.辅助SQL文.(AllRun.sql) 3.要执行的SQL文.(S1.sql,S2.sql,S3.sql) +++++++ ...

  7. DecimalFormat(用于格式化十进制数字)

    1概念: DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字.该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言.阿拉伯语和印度语数 ...

  8. 一个是EF内联多表查询,一个是EF中写SQL文。

    public IList<MenuModel> GetAllMenu() { using (IMMEntities context = new IMMEntities()) { var m ...

  9. Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql) (文档 ID 1064487.1)

    the article from :http://m.blog.itpub.net/31393455/viewspace-2130875/ Script to Collect Log File Syn ...

随机推荐

  1. 基于Lucene的文件检索Demo

    通过Lucene实现了简单的文件检索功能的Demo.这个Demo支持基于文件内容的检索,支持中文分词和高亮显示. 下面简单的介绍下核心的类 1)索引相关的类 1.FileIndexBuilder -- ...

  2. android开发之PreferenceScreen使用详解

    是在惭愧,学习android也有一段时间了,今天才是第一次接触PreferenceScreen.记录下来,与大家分享. 本文参考:http://lovezhou.iteye.com/blog/1020 ...

  3. Hadoop 2.6.3运行自带WordCount程序笔记

    运行平台:Hadoop 2.6.3 模式:完全分布模式 1.准备统计文本,以一段文字为例:eg.txt The Project Gutenberg EBook of War and Peace, by ...

  4. python 面向对象简单理解

    面向对象: 是一种程序设计范型 作用: 提高软件的重用性和灵活性,扩展性 世界万物一切皆为对象,对象即是指由特定状态,特征,行为的实体   知识点一: 代码的重用 举个栗子 比如小月月有了一个女朋友1 ...

  5. HDU-1009(简单贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

  6. 黑马程序员-集合(二)contains()方法的内部探索

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 我们知道集合是用来存储对象的.在他们实现了众多的接口我们以Arraylist为列子 所有已实现的 ...

  7. Sql解锁 数据库死锁检测

    USE [DataBaseName]GO/****** Object:  StoredProcedure [dbo].[sp_check_deadlock]    Script Date: 07/04 ...

  8. C# - linq查询现有的DataTable

    可以通过linq对现有的DataTable进行查询,并将结果拷贝至新的DataTable中例如: // Query the SalesOrderHeader table for orders plac ...

  9. 浅析MySQL中exists与in的使用 (写的非常好)

    转自http://sunxiaqw.blog.163.com/blog/static/990654382013430105130443/ exists对外表用loop逐条查询,每次查询都会查看exis ...

  10. (传智博客)tp开发第一天之tp执行流程分析笔记

    1.入口文件index.php 2.ThinkPHP/ThinkPHP.php require THINK_PATH.'Common/runtime.php'; 3.ThinkPHP/Common/r ...