博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
推荐用于格式化以及高亮显示SQL文的PHP类-SqlFormatter
阅读量:4709 次
发布时间:2019-06-10

本文共 2672 字,大约阅读时间需要 8 分钟。

有时候做开发用到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,  from Github and include lib/SqlFormatter.php in your project.


Example Usage

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

= 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.

SELECT   * FROM   MyTable WHERE   id = 46

Syntax Highlighting Only

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

SELECT * FROM MyTable WHERE id = 46

Compress SQL Query

The compress method compresses whitespace and removes comments.

SELECT Id as temp, DateCreated as Created FROM MyTable;

Remove SQL Comments

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

SELECTId 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.

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.

CLI syntax highlighting screenshot


转载于:https://www.cnblogs.com/guoyongrong/p/4435044.html

你可能感兴趣的文章
JSP Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
查看>>
Angular Tour of Heroes getHeroNo404()
查看>>
编译安装LAMP并实现wordpress
查看>>
字符串、集合、文件操作
查看>>
lvs原理及各种调度算法详解
查看>>
String.valueOf()方法的使用
查看>>
leetcode 15 三数之和
查看>>
OpenFastPath(1):快平面接口是否支持多ip
查看>>
eclipse最有用快捷键整理
查看>>
【File类:获取指定目录下的所有文件或者文件夹的名称】
查看>>
Xcode开发技巧之Code Snippets Library
查看>>
Linux平台上DPDK入门指南
查看>>
Java中的别名现象
查看>>
Nginx
查看>>
Interactive Extensions简介
查看>>
Integer’s Power HDU - 3208(容斥原理)
查看>>
牛客OI周赛7-提高组
查看>>
verilog 仿真时读取txt文件
查看>>
django 之admin后台管理
查看>>
android开发笔记
查看>>