推荐用于格式化以及高亮显示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# 创建新RTF文件
这个和WINDOWS创建RTF文件一样 public void CreateRtfFile(string RtfFileName) { RichTextBox richTextBox1 = new R ...
- Chrome/Chromium HTML5 video 视频播放硬件加速
Chromium站点上有个大致的框图.描写叙述了Chromium的video在各个平台 - 包含Android - 上是怎样使用硬件资源来做视频编解码加速的: 而依据Android Kitkat上的C ...
- [Javascript] MetaProgramming: function name
Each function should have a 'name' property. It can be anonymous, empty, the same as function name, ...
- 最简单的基于FFMPEG的视频编码器(YUV编码为H.264)
本文介绍一个最简单的基于FFMPEG的视频编码器.该编码器实现了YUV420P的像素数据编码为H.264的压缩编码数据.编码器代码十分简单,可是每一行代码都非常重要,适合好好研究一下.弄清楚了本代码也 ...
- careercup-数组和字符串1.3
1.3 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排序后,能否变成另一个字符串. C++实现代码: #include<iostream> #include<map> ...
- hadoop错误Cannot load libcrypto.so (libcrypto.so cannot open shared object file No such file or directory)
报如下错误 解决方法: 1.使用hadoop checknative –a命令检查,报如下错误 2.使用如下命令建立libcrypto.so文件的符号链接 如果,您认为阅读这篇博客让您有些收获,不妨点 ...
- 为centos6.5系统添加epel源
1.进去http://fedoraproject.org/wiki/EPEL 2.因为系统是centos 6.5,所以获取epel-release-latest-6.noarch.rpm的地址(htt ...
- LPC2378-Jlink 能下载程序,但是调试出现各种奇怪问题
LPC2378-Jlink调试经验 1.Jlink(d版的没有关系)不能下载程序? 把下载速率设置到500Khz及以下的速率. 2.Jlink能在500Khz的速率下载程序,但是不能调试?添加 ...
- javascript split() 正则表达式
路由匹配 http.createServer(function(req, res) { var items = req.url.split('/'); if (items.length < 3 ...
- [转]toString()方法
文章转自:http://blog.sina.com.cn/s/blog_85c1dc100101bxgg.html 今天看JS学习资料,看到一个toString()方法,在JS中,定义的所有对象都具有 ...