sql-formatter结合CodeMirror格式化SQL

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZYC88888/article/details/84069890

 

  • 源代码名称:sql-formatter
  • 源代码网址:http://www.github.com/zeroturnaround/sql-formatter
  • sql-formatter源代码文档
  • sql-formatter源代码下载
  • Git URL:

    复制代码

    git://www.github.com/zeroturnaround/sql-formatter.git
  • Git Clone代码到本地:

    复制代码

    git clone http://www.github.com/zeroturnaround/sql-formatter
  • Subversion代码到本地:

    复制代码

    $ svn co --depth empty http://www.github.com/zeroturnaround/sql-formatter
    Checked out revision 1.
    $ cd repo
    $ svn up trunk

SQL格式化程序NPM versionBuild StatusCoverage Status

SQL格式化程序是一个JavaScript库,用于打印。 它作为一个 PHP库插件的端口开始,但是从相当大的程度上看。 它支持标准 SQL 。Couchbase N1QL插件。IBM和Oracle pl/SQL方言。

→ 试用演示。

安装

从NPM获取最新版本:

复制代码

npm install --save sql-formatter

用法

复制代码

importsqlFormatterfrom"sql-formatter";console.log(sqlFormatter.format("SELECT * FROM table1"));

这将输出:

复制代码

SELECT
 *
FROM
 table1

你还可以传入配置选项:

复制代码

sqlFormatter.format("SELECT *", {
 language:"n1ql", // Defaults to"sql" indent:""// Defaults to two spaces});

目前只支持四种SQL方言:

占位符替换

复制代码

// Named placeholderssqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", {
 params: {foo:"'bar'"}
}));// Indexed placeholderssqlFormatter.format("SELECT * FROM tbl WHERE foo =?", {
 params: ["'bar'"]
}));

两个结果都包含:

复制代码

SELECT
 *
FROM
 tbl
WHERE
 foo = 'bar'

不使用NPM的用法

在 MODULE 目录中,从目录中选择一个文件,然后从目录中获取文件,使用 inside <script> 标记。 这使得SQL格式化程序成为全局变量 window.sqlFormatter 。

 

复制代码

# run linter and tests$ npm run check

猜你喜欢

转载自blog.csdn.net/ZYC88888/article/details/84069890