All Projects → kanmu → Go Sqlfmt

kanmu / Go Sqlfmt

Licence: mit
A SQL formatter written in Go

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to Go Sqlfmt

Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+1206.19%)
Mutual labels:  sql
Jcabi Jdbc
Fluent Wrapper of JDBC
Stars: ✭ 90 (-7.22%)
Mutual labels:  sql
Qtl
A friendly and lightweight C++ database library for MySQL, PostgreSQL, SQLite and ODBC.
Stars: ✭ 92 (-5.15%)
Mutual labels:  sql
Sequelize
Sequelize module for Nest framework (node.js) 🍈
Stars: ✭ 88 (-9.28%)
Mutual labels:  sql
Cloudquery
cloudquery transforms your cloud infrastructure into SQL or Graph database for easy monitoring, governance and security.
Stars: ✭ 1,300 (+1240.21%)
Mutual labels:  sql
Selectstarsql
An interactive SQL book
Stars: ✭ 92 (-5.15%)
Mutual labels:  sql
Training Material
A collection of code examples as well as presentations for training purposes
Stars: ✭ 85 (-12.37%)
Mutual labels:  sql
Node Background Management System
eggjs实现一个较为完整的后台管理系统
Stars: ✭ 96 (-1.03%)
Mutual labels:  sql
Cutehmi
CuteHMI is an open-source HMI (Human Machine Interface) software written in C++ and QML, using Qt libraries as a framework. GitHub repository is a mirror!
Stars: ✭ 90 (-7.22%)
Mutual labels:  sql
Firebirdwebadmin
FirebirdWebAdmin is a web frontend for the Firebird SQL database server written in PHP.
Stars: ✭ 92 (-5.15%)
Mutual labels:  sql
Plsql And Sql Coding Guidelines
Trivadis PL/SQL & SQL Coding Guidelines
Stars: ✭ 89 (-8.25%)
Mutual labels:  sql
Us Cities Database
🇺🇸 SQL dump of U.S. cities data containing latitude and longitude
Stars: ✭ 89 (-8.25%)
Mutual labels:  sql
Gorm2sql
auto generate sql from gorm model struct
Stars: ✭ 92 (-5.15%)
Mutual labels:  sql
Iysql
IYSQL - Improve Your SQL
Stars: ✭ 87 (-10.31%)
Mutual labels:  sql
Taffy
Test Automation Framework Based On Nosetests. ✨🍰✨
Stars: ✭ 94 (-3.09%)
Mutual labels:  sql
Docker Superset
Repository for Docker Image of Apache-Superset. [Docker Image: https://hub.docker.com/r/abhioncbr/docker-superset]
Stars: ✭ 86 (-11.34%)
Mutual labels:  sql
Jplusone
Tool for automatic detection and asserting "N+1 SELECT problem" occurences in JPA based Spring Boot Java applications and finding origin of JPA issued SQL statements in general
Stars: ✭ 91 (-6.19%)
Mutual labels:  sql
R2d2 Diesel
Stars: ✭ 96 (-1.03%)
Mutual labels:  sql
Toydb
Distributed SQL database in Rust, written as a learning project
Stars: ✭ 1,329 (+1270.1%)
Mutual labels:  sql
Nutz Onekey
NUTZ一键脚手架
Stars: ✭ 92 (-5.15%)
Mutual labels:  sql

sqlfmt

Build Status Go Report Card

Description

The sqlfmt formats PostgreSQL statements in .go files into a consistent style.

Example

Unformatted SQL in a .go file

package main

import (
	"database/sql"
)


func sendSQL() int {
	var id int
	var db *sql.DB
	db.QueryRow(`
	select xxx ,xxx ,xxx
	, case
	when xxx is null then xxx
	else true
end as xxx
from xxx as xxx join xxx on xxx = xxx join xxx as xxx on xxx = xxx
left outer join xxx as xxx
on xxx = xxx
where xxx in ( select xxx from ( select xxx from xxx ) as xxx where xxx = xxx )
and xxx in ($2, $3) order by xxx`).Scan(&id)
	return id
}

The above will be formatted into the following:

package main

import (
	"database/sql"
)

func sendSQL() int {
	var id int
	var db *sql.DB
	db.QueryRow(`
SELECT
  xxx
  , xxx
  , xxx
  , CASE
       WHEN xxx IS NULL THEN xxx
       ELSE true
    END AS xxx
FROM xxx AS xxx
JOIN xxx
ON xxx = xxx
JOIN xxx AS xxx
ON xxx = xxx
LEFT OUTER JOIN xxx AS xxx
ON xxx = xxx
WHERE xxx IN (
  SELECT
    xxx
  FROM (
    SELECT
      xxx
    FROM xxx
  ) AS xxx
  WHERE xxx = xxx
)
AND xxx IN ($2, $3)
ORDER BY
  xxx`).Scan(&id)
	return id
}

Installation

run git clone and go build -o sqlfmt 

Usage

  • Provide flags and input files or directory
    $ sqlfmt -w input_file.go 
    

Flags

  -l
		Do not print reformatted sources to standard output.
		If a file's formatting is different from src, print its name
		to standard output.
  -d
		Do not print reformatted sources to standard output.
		If a file's formatting is different than src, print diffs
		to standard output.
  -w
                Do not print reformatted sources to standard output.
                If a file's formatting is different from src, overwrite it
                with gofmt style.
  -distance     
                Write the distance from the edge to the begin of SQL statements

Limitations

  • The sqlfmt is only able to format SQL statements that are surrounded with back quotes and values in QueryRow, Query, Exec functions from the "database/sql" package.

    The following SQL statements will be formatted:

    func sendSQL() int {
    	var id int
    	var db *sql.DB
    	db.QueryRow(`select xxx from xxx`).Scan(&id)
    	return id
    }
    

    The following SQL statements will NOT be formatted:

    // values in fmt.Println() are not formatting targets
    func sendSQL() int {
        fmt.Println(`select * from xxx`)
    }
    
    // nor are statements surrounded with double quotes
    func sendSQL() int {
        var id int
        var db *sql.DB
        db.QueryRow("select xxx from xxx").Scan(&id)
        return id
    }
    

Not Supported

  • IS DISTINCT FROM

  • WITHIN GROUP

  • DISTINCT ON(xxx)

  • select(array)

  • Comments after commna such as select xxxx, --comment xxxx

  • Nested square brackets or braces such as [[xx], xx]

    • Currently being formatted into this: [[ xx], xx]
    • Ideally, it should be formatted into this: [[xx], xx]
  • Nested functions such as sum(average(xxx))

    • Currently being formatted into this: SUM( AVERAGE(xxx))
    • Ideally, it should be formatted into this: SUM(AVERAGE(xxx))

Future Work

  • [ ] Refactor
  • [ ] Turn it into a plug-in or an extension for editors

Contribution

Thank you for thinking of contributing to the sqlfmt! Pull Requests are welcome!

  1. Fork ([https://github.com/kanmu/go-sqlfmt))
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Create new Pull Request

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].