parse

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

Sqlfmt

Format the SQL of the Go code.

In the Go language, tools that allow you to adjust the format and import of the code are provided with standard tools. sqlfmt rewrites and format the SQL in the code like those tools. supports only MySQL SQL statements.

Install

go get -u github.com/sgswtky/sqlfmt

Usage

Replace file

The SQL is formatted if both of the following conditions are satisfied.

  • Variable name 'sql'
  • If '// sqlfmt' is commented one line before that variable

Please be aware that all comments will be deleted after formatting.

For example, if the following file exists,

package parse

import "fmt"

func main() {
	// sqlfmt
	sql := `select * from example where example_id = ? and example_name like '%example%'`

	fmt.Println(sql)
}

sqlfmt specifies the example file with the -f option and adds the -w option.

./sqlfmt -f sample.go -w

The SQL statement in the sample.go file is changed as follows.

package parse

import "fmt"

func main() {
	// sqlfmt
	sql := `
SELECT
  *
FROM
  example
WHERE
  example_id = ?
  AND example_name like "%example%"`
	fmt.Println(sql)
}

Action mode
option detail
-f File mode. Please specify the file name
-w Rewrite the target file. It works only when combined with file mode
-i Interactive mode.
Pipe mode. See below.
Pipe mode
⇒  echo "select * from example where example_id = ? and example_name LIKE '%sgsw%'" | sqlfmt
SELECT
  *
FROM
  example
WHERE
  example_id = ?
  AND example_name like "%sgsw%"
Detailed Behavior
  • Comments are not supported and will be deleted
  • Since it only supports static SQL
  • do not combine strings and variables in the format target variable.

Thanks

  • github.com/vitessio/vitess
  • github.com/xwb1989/sqlparser

License

apache license 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder interface {
	Parse() (string, error)
	// contains filtered or unexported methods
}

func NewBuilder

func NewBuilder(initialSQL string) Builder

NewBuilder Get the parsed SQL builder.

type BuilderStruct

type BuilderStruct struct {
	// contains filtered or unexported fields
}

Builder Structure for SQL parsing.

func (*BuilderStruct) Parse

func (b *BuilderStruct) Parse() (string, error)

Parse Parses the SQL passed at builder creation to formatted SQL.

Jump to

Keyboard shortcuts

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