Skip to content

Commit

Permalink
Add -split flag to reform-db query and exec.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Jul 22, 2018
1 parent 30d7c2e commit 4e5c24c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
36 changes: 30 additions & 6 deletions reform-db/cmd_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import (
)

var (
execFlags = flag.NewFlagSet("exec", flag.ExitOnError)
execFlags = flag.NewFlagSet("exec", flag.ExitOnError)
execSplitF = execFlags.Bool("split", false, "Split statements by semicolon; does not handles them in string literals")
)

func init() {
execFlags.Usage = func() {
fmt.Fprintf(os.Stderr, "`exec` command executes SQL queries from given files or stdin.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " %s [global flags] exec [file names]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [global flags] exec [exec flags] [file names]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Global flags:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nExec flags:\n")
execFlags.PrintDefaults()

// TODO mention -split flag
fmt.Fprintf(os.Stderr, `
Each file's content is executed as a single query. If it contains multiple
statements, make sure SQL driver supports them. If file names are not given,
Expand All @@ -31,7 +35,7 @@ a query is read from stdin until EOF, then executed.
}

// readFiles reads queries from given files, or from stdin, if files are not given
func readFiles(files []string) (queries []string) {
func readFiles(files []string, split bool) (queries []string) {
// read stdin
if len(files) == 0 {
logger.Debugf("no files are given, reading stdin")
Expand All @@ -41,7 +45,17 @@ func readFiles(files []string) (queries []string) {
}
b = bytes.TrimSpace(b)
if len(b) > 0 {
queries = append(queries, string(b))
if split {
s := bytes.Split(b, []byte(";"))
for _, ss := range s {
ss = bytes.TrimSpace(ss)
if len(ss) > 0 {
queries = append(queries, string(ss))
}
}
} else {
queries = append(queries, string(b))
}
}

return
Expand All @@ -56,7 +70,17 @@ func readFiles(files []string) (queries []string) {
}
b = bytes.TrimSpace(b)
if len(b) > 0 {
queries = append(queries, string(b))
if split {
s := bytes.Split(b, []byte(";"))
for _, ss := range s {
ss = bytes.TrimSpace(ss)
if len(ss) > 0 {
queries = append(queries, string(ss))
}
}
} else {
queries = append(queries, string(b))
}
}
}

Expand All @@ -65,7 +89,7 @@ func readFiles(files []string) (queries []string) {

// cmdExec implements exec command.
func cmdExec(db *reform.DB, files []string) {
queries := readFiles(files)
queries := readFiles(files, *execSplitF)
for _, q := range queries {
if _, err := db.Exec(q); err != nil {
logger.Fatalf("failed to execute %s: %s", q, err)
Expand Down
10 changes: 7 additions & 3 deletions reform-db/cmd_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import (
)

var (
queryFlags = flag.NewFlagSet("query", flag.ExitOnError)
queryFlags = flag.NewFlagSet("query", flag.ExitOnError)
querySplitF = queryFlags.Bool("split", false, "Split statements by semicolon; does not handles them in string literals")
)

func init() {
queryFlags.Usage = func() {
fmt.Fprintf(os.Stderr, "`query` command executes SQL queries from given files or stdin, and returns results.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " %s [global flags] query [file names]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [global flags] query [query flags] [file names]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Global flags:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nQuery flags:\n")
queryFlags.PrintDefaults()

// TODO mention -split flag
fmt.Fprintf(os.Stderr, `
Each file's content is executed as a single query. If it contains multiple
statements, make sure SQL driver supports them. If file names are not given,
Expand All @@ -33,7 +37,7 @@ a query is read from stdin until EOF, then executed.

// cmdQuery implements query command.
func cmdQuery(db *reform.DB, files []string) {
queries := readFiles(files)
queries := readFiles(files, *querySplitF)
for _, q := range queries {
rows, err := db.Query(q)
if err != nil {
Expand Down

0 comments on commit 4e5c24c

Please sign in to comment.