推荐用于格式化以及高亮显示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 ...
随机推荐
- 使用OpenXML将Excel内容读取到DataTable中
前言:前面的几篇文章简单的介绍了如何使用OpenXML创建Excel文档.由于在平时的工作中需要经常使用到Excel的读写操作,简单的介绍下使用 OpenXML读取Excel中得数据.当然使用Open ...
- HDU 4389——X mod f(x)(数位DP)
X mod f(x) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- mybatis04 根据用户名称模糊查询用户信息
根据用户名称模糊查询用户信息可能返回多条记录. 1.1.1User.xml 编码 如果用%进行模糊查询,#{}表示一个占位符会被翻译为一个?号(SELECT * FROM USER WHERE id= ...
- locale------- linux字符集
查看当前系统字符集 [root@server1 ~]# locale LANG=zh_CN.UTF-8LC_CTYPE="zh_CN.UTF-8"LC_NUMERIC=" ...
- LCA问题
基本概念 LCA:树上的最近公共祖先,对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. RMQ:区间最小值查询问题.对于长度为n的 ...
- C# winform线程的使用 制作提醒休息小程序(长时间计算机工作者必备)
最近发现日常的工作中,经常因为敲代码而忘记了休息,晚上眼睛特别的累. 并且经常长时间看着显示器,对眼睛一定是不好的,所以今天开发了一个小程序,用于提醒休息. 下面先看看运行效果: 1.程序启动后,后台 ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- C# 跨线程调用问题
纠结了好久,终于知道了winform和WPF的UI的跨线程调用的解决方法: winform下如果为了省事,可以直接禁用跨线程检查: Control.CheckForIllegalCrossThread ...
- Stream To String , String To Stream
public static string StreamToString(Stream stream) { stream.Position = 0; using (StreamReader stremR ...
- Oralce9 的新方法: Merge into Using
一.语义 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无 ...