Skip to content

Commit

Permalink
Separate writeopts from the TRDSQL structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Jun 6, 2019
1 parent 175da0d commit 4151e7d
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 63 deletions.
8 changes: 5 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func Run(args []string) int {
flags := flag.NewFlagSet("trdsql", flag.ExitOnError)

tr := trdsql.NewTRDSQL()
wo := trdsql.DefaultWriteOpts

flags.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: %s [OPTIONS] [SQL(SELECT...)]
`, os.Args[0])
Expand Down Expand Up @@ -118,8 +120,8 @@ func Run(args []string) int {
flags.BoolVar(&inFlag.JSON, "ijson", false, "JSON format for input.")
flags.BoolVar(&inFlag.TBLN, "itbln", false, "TBLN format for input.")

flags.StringVar(&tr.OutDelimiter, "od", ",", "Field delimiter for output.")
flags.BoolVar(&tr.OutHeader, "oh", false, "Output column name as header.")
flags.StringVar(&wo.OutDelimiter, "od", ",", "Field delimiter for output.")
flags.BoolVar(&wo.OutHeader, "oh", false, "Output column name as header.")

flags.BoolVar(&outFlag.CSV, "ocsv", true, "CSV format for output.")
flags.BoolVar(&outFlag.LTSV, "oltsv", false, "LTSV format for output.")
Expand Down Expand Up @@ -185,7 +187,7 @@ Options:
}

tr.InFormat = inputFormat(inFlag)
tr.OutFormat = outputFormat(outFlag)
wo.OutFormat = outputFormat(outFlag)
err = tr.Exec()
if err != nil {
log.Fatal(err)
Expand Down
3 changes: 1 addition & 2 deletions csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ func TestCsvIndefiniteInputFile3(t *testing.T) {
}

func TestCsvOutNew(t *testing.T) {
trdsql := trdsqlNew()
out := trdsql.NewCSVWrite()
out := NewCSVWrite(",", false)
if out == nil {
t.Error(`csvOut error`)
}
Expand Down
49 changes: 31 additions & 18 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trdsql
import (
"encoding/hex"
"fmt"
"io"
"log"
"strings"
"time"
Expand All @@ -23,40 +24,44 @@ const (
OUT_TBLN
)

type WriteOpts struct {
OutFormat OutputFormat
OutDelimiter string
OutHeader bool
OutStream io.Writer
ErrStream io.Writer
}

// Writer is file format writer
type Writer interface {
First([]string, []string) error
WriteRow([]interface{}, []string) error
Last() error
}

func (trdsql *TRDSQL) NewWriter() Writer {
switch trdsql.OutFormat {
func NewWriter() Writer {
switch DefaultWriteOpts.OutFormat {
case OUT_LTSV:
return trdsql.NewLTSVWrite()
return NewLTSVWrite()
case OUT_JSON:
return trdsql.NewJSONWrite()
return NewJSONWrite()
case OUT_RAW:
return trdsql.NewRAWWrite()
return NewRAWWrite(DefaultWriteOpts.OutDelimiter, DefaultWriteOpts.OutHeader)
case OUT_MD:
return trdsql.NewTWWrite(true)
return NewTWWrite(true)
case OUT_AT:
return trdsql.NewTWWrite(false)
return NewTWWrite(false)
case OUT_VF:
return trdsql.NewVFWrite()
return NewVFWrite()
case OUT_TBLN:
return trdsql.NewTBLNWrite()
return NewTBLNWrite()
case OUT_CSV:
return trdsql.NewCSVWrite()
return NewCSVWrite(DefaultWriteOpts.OutDelimiter, DefaultWriteOpts.OutHeader)
default:
return trdsql.NewCSVWrite()
return NewCSVWrite(DefaultWriteOpts.OutDelimiter, DefaultWriteOpts.OutHeader)
}
}

type Exporter interface {
Export(db *DDB, sqlstr string) error
}

// Export is execute SQL and Exporter the result.
func (trdsql *TRDSQL) Export(db *DDB, sqlstr string) error {
w := trdsql.Writer
Expand Down Expand Up @@ -87,7 +92,7 @@ func (trdsql *TRDSQL) Export(db *DDB, sqlstr string) error {
}
types := make([]string, len(columns))
for i, ct := range columnTypes {
types[i] = convertType(ct.DatabaseTypeName())
types[i] = ct.DatabaseTypeName()
}

err = w.First(columns, types)
Expand All @@ -107,8 +112,16 @@ func (trdsql *TRDSQL) Export(db *DDB, sqlstr string) error {
return w.Last()
}

func convertType(dbtype string) string {
switch strings.ToLower(dbtype) {
func ConvertTypes(dbTypes []string) []string {
ret := make([]string, len(dbTypes))
for i, t := range dbTypes {
ret[i] = convertType(t)
}
return ret
}

func convertType(dbType string) string {
switch strings.ToLower(dbType) {
case "smallint", "integer", "int", "int2", "int4", "smallserial", "serial":
return "int"
case "bigint", "int8", "bigserial":
Expand Down
8 changes: 4 additions & 4 deletions output_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ type CSVWrite struct {
outHeader bool
}

func (trdsql *TRDSQL) NewCSVWrite() *CSVWrite {
func NewCSVWrite(delim string, header bool) *CSVWrite {
var err error
c := &CSVWrite{}
c.writer = csv.NewWriter(trdsql.OutStream)
c.writer.Comma, err = delimiter(trdsql.OutDelimiter)
c.writer = csv.NewWriter(DefaultWriteOpts.OutStream)
c.writer.Comma, err = delimiter(delim)
if err != nil {
debug.Printf("%s\n", err)
}
c.outHeader = trdsql.OutHeader
c.outHeader = header
return c
}

Expand Down
4 changes: 2 additions & 2 deletions output_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type JSONWrite struct {
results []map[string]interface{}
}

func (trdsql *TRDSQL) NewJSONWrite() *JSONWrite {
func NewJSONWrite() *JSONWrite {
js := &JSONWrite{}
js.writer = json.NewEncoder(trdsql.OutStream)
js.writer = json.NewEncoder(DefaultWriteOpts.OutStream)
js.writer.SetIndent("", " ")
return js
}
Expand Down
4 changes: 2 additions & 2 deletions output_ltsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type LTSVWrite struct {
results map[string]string
}

func (trdsql *TRDSQL) NewLTSVWrite() *LTSVWrite {
func NewLTSVWrite() *LTSVWrite {
lw := &LTSVWrite{}
lw.delimiter = "\t"
lw.writer = bufio.NewWriter(trdsql.OutStream)
lw.writer = bufio.NewWriter(DefaultWriteOpts.OutStream)
return lw
}

Expand Down
8 changes: 4 additions & 4 deletions output_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ type RawWrite struct {
outHeader bool
}

func (trdsql *TRDSQL) NewRAWWrite() *RawWrite {
func NewRAWWrite(delim string, header bool) *RawWrite {
var err error
raw := &RawWrite{}
raw.writer = bufio.NewWriter(trdsql.OutStream)
raw.sep, err = strconv.Unquote(`"` + trdsql.OutDelimiter + `"`)
raw.writer = bufio.NewWriter(DefaultWriteOpts.OutStream)
raw.sep, err = strconv.Unquote(`"` + delim + `"`)
if err != nil {
debug.Printf("%s\n", err)
}
raw.outHeader = trdsql.OutHeader
raw.outHeader = header
return raw
}

Expand Down
3 changes: 1 addition & 2 deletions output_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package trdsql
import "testing"

func TestRawOutNew(t *testing.T) {
trdsql := trdsqlNew()
out := trdsql.NewRAWWrite()
out := NewRAWWrite(",", false)
if out == nil {
t.Error(`rawOut error`)
}
Expand Down
4 changes: 2 additions & 2 deletions output_tablewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type TWWrite struct {
results []string
}

func (trdsql *TRDSQL) NewTWWrite(markdown bool) *TWWrite {
func NewTWWrite(markdown bool) *TWWrite {
tw := &TWWrite{}
tw.writer = tablewriter.NewWriter(trdsql.OutStream)
tw.writer = tablewriter.NewWriter(DefaultWriteOpts.OutStream)
tw.writer.SetAutoFormatHeaders(false)
if markdown {
tw.writer.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
Expand Down
6 changes: 3 additions & 3 deletions output_tbln.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type TBLNWrite struct {
results []string
}

func (trdsql *TRDSQL) NewTBLNWrite() *TBLNWrite {
func NewTBLNWrite() *TBLNWrite {
tw := &TBLNWrite{}
tw.writer = tbln.NewWriter(trdsql.OutStream)
tw.writer = tbln.NewWriter(DefaultWriteOpts.OutStream)
return tw
}

Expand All @@ -23,7 +23,7 @@ func (tw *TBLNWrite) First(columns []string, types []string) error {
if err != nil {
return err
}
err = d.SetTypes(types)
err = d.SetTypes(ConvertTypes(types))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions output_vertical.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type VFWrite struct {
count int
}

func (trdsql *TRDSQL) NewVFWrite() *VFWrite {
func NewVFWrite() *VFWrite {
var err error
vf := &VFWrite{}
vf.writer = bufio.NewWriter(trdsql.OutStream)
vf.writer = bufio.NewWriter(DefaultWriteOpts.OutStream)
vf.termWidth, _, err = terminal.GetSize(0)
if err != nil {
vf.termWidth = 40
Expand Down
3 changes: 1 addition & 2 deletions output_vertical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package trdsql
import "testing"

func TestVfOutNew(t *testing.T) {
trdsql := trdsqlNew()
out := trdsql.NewVFWrite()
out := NewVFWrite()
if out == nil {
t.Error(`vfOut error`)
}
Expand Down
29 changes: 13 additions & 16 deletions trdsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package trdsql

import (
"fmt"
"io"
"log"
"os"
)
Expand All @@ -21,27 +20,25 @@ type TRDSQL struct {
InHeader bool

Writer Writer

OutFormat OutputFormat
OutStream io.Writer
ErrStream io.Writer
OutDelimiter string
OutHeader bool
}

func NewTRDSQL() *TRDSQL {
return &TRDSQL{
Driver: "sqlite3",
Dsn: "",
SQL: "",
InDelimiter: ",",
InPreRead: 1,
OutDelimiter: ",",
OutStream: os.Stdout,
ErrStream: os.Stderr,
Driver: "sqlite3",
Dsn: "",
SQL: "",
InDelimiter: ",",
InPreRead: 1,
}
}

var DefaultWriteOpts = &WriteOpts{
OutDelimiter: ",",
OutHeader: false,
OutStream: os.Stdout,
ErrStream: os.Stderr,
}

func (trdsql *TRDSQL) Exec() error {
db, err := Connect(trdsql.Driver, trdsql.Dsn)
if err != nil {
Expand All @@ -55,7 +52,7 @@ func (trdsql *TRDSQL) Exec() error {
}()

if trdsql.Writer == nil {
trdsql.Writer = trdsql.NewWriter()
trdsql.Writer = NewWriter()
}

db.tx, err = db.Begin()
Expand Down
4 changes: 3 additions & 1 deletion trdsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ var outformat = []string{
}

func trdsqlNew() *TRDSQL {
trdsql := NewTRDSQL()
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
trdsql := &TRDSQL{OutStream: outStream, ErrStream: errStream}
DefaultWriteOpts.OutStream = outStream
DefaultWriteOpts.ErrStream = errStream
return trdsql
}

0 comments on commit 4151e7d

Please sign in to comment.