推荐用于格式化以及高亮显示SQL文的PHP类-SqlFormatter
有时候做开发用到SQL文的时候,由于sql文太长,很难真正看清楚这个sql文的结构.
所以需要一个工具能够自动对SQL文进行排版,在网上有幸找到这个用php写的类能处理这个任务.
原文地址是http://jdorn.github.io/sql-formatter/
SqlFormatter
A PHP class for formatting and highlighting SQL statements.
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的更多相关文章
- Shell脚本调用SQL文格式
Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...
- 【强烈强烈推荐】《ORACLE PL/SQL编程详解》全原创(共八篇)--系列文章导航
原文:[强烈强烈推荐]<ORACLE PL/SQL编程详解>全原创(共八篇)--系列文章导航 <ORACLE PL/SQL编程详解> 系列文章目录导航 ——通过知识共享树立个人 ...
- 使用正則表達式的格式化与高亮显示json字符串
使用正則表達式的格式化与高亮显示json字符串 json字符串非常实用,有时候一些后台接口返回的信息是字符串格式的,可读性非常差,这个时候要是有个能够格式化并高亮显示json串的方法那就好多了,以下看 ...
- Numeral.js 是一个用于格式化和数字四则运算的js 库
1.Numeral.js 是一个用于格式化和数字四则运算的js 库. 2.支持多种语言,包含中文在内的17种语言. 在浏览器中引用js文件: <script src="numeral. ...
- mysql中SQL执行过程详解与用于预处理语句的SQL语法
mysql中SQL执行过程详解 客户端发送一条查询给服务器: 服务器先检查查询缓存,如果命中了缓存,则立刻返回存储在缓存中的结果.否则进入下一阶段. 服务器段进行SQL解析.预处理,在优化器生成对应的 ...
- 用batch调用DB2 CLPPlus执行多个SQL文
不啰嗦直接上技能. 大概三部分组成: 1.bat文件.(run.bat) 2.辅助SQL文.(AllRun.sql) 3.要执行的SQL文.(S1.sql,S2.sql,S3.sql) +++++++ ...
- DecimalFormat(用于格式化十进制数字)
1概念: DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字.该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言.阿拉伯语和印度语数 ...
- 一个是EF内联多表查询,一个是EF中写SQL文。
public IList<MenuModel> GetAllMenu() { using (IMMEntities context = new IMMEntities()) { var m ...
- 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 ...
随机推荐
- C#通过FTP账号上传、修改、删除文件 FTPClient
下面类文件中,主要做的工作是:从ftp服务器上下载文件把本地文件替换.添加.或删除功能,在替换本地文件时会先备份一下本地的文件,若整个操作都完成了就会发出commit命令,表示全部替换成功.若中间操作 ...
- richTextBox插入表格 完整版
附件 http://files.cnblogs.com/xe2011/richTextBox_InsertTable_Full.rar 调用 richTextBoxTableDlg dlg = new ...
- Activiti5.13数据库表结构设计
1.结构设计 1.1. 逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...
- [Webpack 2] Tree shaking with Webpack 2
The less code you can send to the browser, the better. The concept of tree shaking basically says th ...
- (转载)MyEclipse github
最近Git火得如日中天,而且速度体验和团队模式都很不错.手头正好有个学生实训项目,时间紧任务重,而且学校内网管理太紧,所以就想借助于Internet的分布式开发,因此想到了Github. ...
- IOS Remote Notification
1. 本地证书合成 rm *.pem echo "export cert..." openssl pkcs12 -clcerts -nokeys -out push_cert.pe ...
- 学习完毕-css
最近零零散散学习了css 最后附带链接,里面有css的全部demo.有空的可以练习练习,下一步 --->js -----http://www.w3cschool.cc/css/css-examp ...
- NHibernate动态加载资源文件
最近做项目,又用到了以前做过的ORM框架--NHibernate. 此次想要实现的目标: 1.简单SQL用NHibernate的Session的CRUD方法实现 2.复杂SQL用Native SQL实 ...
- Java String.format 自动补全不够的位数
http://www.blogjava.net/java-blog/articles/189040.html
- 第八章 CTE 递归 及 分组汇总 高级部分(多维数据集)
UNION 等集合操作符: UNION 等以第一个 SELECT 的 列明 作为 整个结果集的列明,整个结果集 唯一认可的 唯一逻辑处理阶段 是 ORDER BY 这个意思是说 只有 ORDER ...