GithubHelp home page GithubHelp logo

sqlfmt's Introduction

sqlfmt

Installation

$ go get github.com/jackc/sqlfmt/...
$ which sqlfmt
$GOPATH/bin/sqlfmt

Usage

  • You can either:

    • Provide the path to one or more SQL files as command line arguments:

      $ sqlfmt testdata/select_where.input.sql
      select
        foo,
        bar
      from
        baz
      where
        foo > 5
        and bar < 2
    • Or, directly provide the SQL string via stdin:

      $ echo "select * from users" | sqlfmt
      select
        *
      from
        users
      $ sqlfmt < testdata/like.input.sql
      select
        foo,
        bar
      from
        baz
      where
        foo like 'abd%'
        or foo like 'ada%' escape '!'
        or foo not like 'abd%'
        or foo not like 'ada%' escape '!'
        or foo ilike 'efg%'
        or foo ilike 'ada%' escape '!'
        or foo not ilike 'efg%'
        or foo not ilike 'ada%' escape '!'
  • View testdata for more examples.

sqlfmt's People

Contributors

jackc avatar kashav avatar mattn avatar rganardi avatar yyoshiki41 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqlfmt's Issues

syntax error with vitess bootstrap sql files

Hi. I was curious about sqlfmt and fed it the first *.sql I could find, and got mysterious syntax errors:

$ cat $GOPATH/src/github.com/youtube/vitess/data/bootstrap/_vt_schema.sql 
CREATE DATABASE _vt;

CREATE TABLE _vt.replication_log (
  time_created_ns bigint primary key,
  note varchar(255));

CREATE TABLE _vt.reparent_log (
  time_created_ns bigint primary key,
  last_position varchar(255),
  new_addr varchar(255),
  new_position varchar(255),
  wait_position varchar(255),
  index (last_position));
$ sqlfmt $GOPATH/src/github.com/youtube/vitess/data/bootstrap/_vt_schema.sql 
2015/11/04 15:03:30 parse error: syntax error at character 328
Parse failed
$ head -1 $GOPATH/src/github.com/youtube/vitess/data/bootstrap/_vt_schema.sql | hexdump -C
00000000  43 52 45 41 54 45 20 44  41 54 41 42 41 53 45 20  |CREATE DATABASE |
00000010  5f 76 74 3b 0a                                    |_vt;.|
00000015
$ head -1 $GOPATH/src/github.com/youtube/vitess/data/bootstrap/_vt_schema.sql | sqlfmt
2015/11/04 15:03:38 parse error: syntax error at character 21
Parse failed
$ 

Parsing fails for unquoted identifiers

Did go get -u github.com/jackc/sqlfmt/cmd/sqlfmt a few minutes ago and trying to run it:

$ echo 'SELECT * FROM table' | sqlfmt
2017/11/15 01:03:53 parse error: syntax error at character 20
Parse failed

That's, umm, a bit surprising, as I'm quite used to write PG queries without quoting tables.

When you quote the table name - it works fine:

$ echo 'select * from "table"' | sqlfmt 
select
  *
from
  "table"

Seen this comment: #7 (comment) so not actually expecting a fix (if it should be "fixed" at all?), just logging an issue.

Also, a bit of offtop question - it seems to support only SELECT statements, right? (as seen in sql.y file)

Pass indent to TextRenderer

Any chance of changing TextRenderer struct to have public properties as below?

  W		io.Writer
  Indent	string

That would enable passing in a custom value for indent like this...

var out bytes.Buffer
r := &sqlfmt.TextRenderer{W: &out, Indent: "  "}
stmt.RenderTo(r)

...when using the code as a lib

Parse error: syntax error on joins

Hey @jackc,
Found this project today, its pretty cool, was going to use it for pgweb sql formatting feature.
I tested it on a few queries, it worked fine most of the time until i hit an error with this query:

select * from bookings left join users on users.id = bookings.user_id

If i take out left from the sql it works. Otherwise im getting 2017/09/03 14:32:12 parse error: syntax error at character 70 error. The original query in my test was quite large, so it was not clear exactly where the sqlfmt fails.

Backticks support?

Thank you for this nice formatter! A pleasure to have it.

One minor suggestion: Currently this MySQL query fails to parse:

SELECT `tax_class`.* FROM `tax_class` WHERE (`tax_class`.`class_id`='3');
$ pbpaste | sqlfmt -u
2017/06/11 20:52:48 parse error: syntax error at character 73
Parse failed

Removing the backticks and it works.

Do you plan to support the backticks? 8-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.