sqlfmt

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2019 License: MIT Imports: 12 Imported by: 1

README

sqlfmt

Build Status Go Report Card

Demo

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

$ go get github.com/kanmu/go-sqlfmt/cmd/sqlfmt

Usage

  • Provide an input file then state the location and name of the output file:
    $ sqlfmt -s input_file.go -o output_file.go
    

OR

  • Omit the output file location & name and the result will be displayed on your terminal:
    $ sqlfmt -s input_file.go
    

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)
  • 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]

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

Documentation

Index

Constants

View Source
const (
	QUERY    = "Query"
	QUERYROW = "QueryRow"
	EXEC     = "Exec"
)

sqlfmt retrieves all strings from "Query" and "QueryRow" and "Exec" functions in .go file

Variables

This section is empty.

Functions

func NewWriter

func NewWriter(rs []group.Reindenter) *writer

NewWriter returns a pointer of writer

Types

type FormatError

type FormatError struct {
	Msg string
}

FormatError is an error that occurs during Format

func (*FormatError) Error

func (f *FormatError) Error() string

Error ...

type Formatter

type Formatter struct{}

Formatter formats SQL statements

func (*Formatter) Format

func (f *Formatter) Format(src string) (string, error)

Format formats src in 3 steps 1: tokenize src 2: parse tokens by SQL clause group 3: for each clause group (Reindenter), add indentation or new line in the correct position

type SQLFormatter

type SQLFormatter struct {
	AstNode   *ast.File
	Formatter *Formatter
	Fset      *token.FileSet
}

SQLFormatter represents SQLformatter

func NewSQLFormatter

func NewSQLFormatter(src io.Reader) (*SQLFormatter, error)

NewSQLFormatter creates SQLFormatter

func (*SQLFormatter) Format

func (s *SQLFormatter) Format() error

Format formats SQL statements after retrieving from AstNode

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL