Skip to content

Commit

Permalink
Fixed to call TRDSQL from another package (WIP).
Browse files Browse the repository at this point in the history
Work of issue (#41).
Modify variable name.
  • Loading branch information
noborus committed May 30, 2019
1 parent 48f9c76 commit 1cdebda
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 211 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ $ trdsql [options] SQL
### Input format

* `-ig`
Guess format from extension.
Guess format from extension(default).
* `-icsv`
CSV format for input(default).
CSV format for input.
* `-ijson`
JSON format for input.
* `-iltsv`
Expand Down Expand Up @@ -88,7 +88,7 @@ $ trdsql [options] SQL
Raw format for output.
* `-ovf`
Vertical format for output.
* `-tbln`
* `-otbln`
TBLN format for output.

#### Output option
Expand Down
26 changes: 13 additions & 13 deletions csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCsvInputNew(t *testing.T) {
if err != nil {
t.Error(err)
}
trdsql.inDelimiter = ","
trdsql.InDelimiter = ","
_, err = trdsql.csvInputNew(file)
if err != nil {
t.Error(`csvInputNew error`)
Expand All @@ -40,7 +40,7 @@ func TestCsvInputNew(t *testing.T) {

func TestCsvEmptyNew(t *testing.T) {
trdsql := trdsqlNew()
trdsql.inDelimiter = ","
trdsql.InDelimiter = ","
const csvStream = ``
s := strings.NewReader(csvStream)
r, err := trdsql.csvInputNew(s)
Expand All @@ -55,8 +55,8 @@ func TestCsvEmptyNew(t *testing.T) {

func TestCsvHeaderNew(t *testing.T) {
trdsql := trdsqlNew()
trdsql.inHeader = true
trdsql.inDelimiter = ","
trdsql.InHeader = true
trdsql.InDelimiter = ","
csvStream := `h1,h2
v1,v2`
s := strings.NewReader(csvStream)
Expand All @@ -69,8 +69,8 @@ func TestCsvHeaderNew(t *testing.T) {

func TestCsvEmptyColumnHeaderNew(t *testing.T) {
trdsql := trdsqlNew()
trdsql.inHeader = true
trdsql.inDelimiter = ","
trdsql.InHeader = true
trdsql.InDelimiter = ","
csvStream := `h1,
v1,v2`
s := strings.NewReader(csvStream)
Expand All @@ -83,8 +83,8 @@ func TestCsvEmptyColumnHeaderNew(t *testing.T) {

func TestCsvEmptyColumnRowNew(t *testing.T) {
trdsql := trdsqlNew()
trdsql.inHeader = true
trdsql.inDelimiter = ","
trdsql.InHeader = true
trdsql.InDelimiter = ","
csvStream := `h1,h2
,v2`
s := strings.NewReader(csvStream)
Expand All @@ -102,8 +102,8 @@ func TestCsvEmptyColumnRowNew(t *testing.T) {

func TestCsvColumnDifferenceNew(t *testing.T) {
trdsql := trdsqlNew()
trdsql.inHeader = true
trdsql.inDelimiter = ","
trdsql.InHeader = true
trdsql.InDelimiter = ","
csvStream := `h1,h2,h3
v1,v2,v3
x1,x2
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestCsvIndefiniteInputFile(t *testing.T) {
if err != nil {
t.Error(err)
}
trdsql.inDelimiter = ","
trdsql.InDelimiter = ","
var cr Input
cr, err = trdsql.csvInputNew(file)
if err != nil {
Expand All @@ -168,7 +168,7 @@ func TestCsvIndefiniteInputFile2(t *testing.T) {
if err != nil {
t.Error(err)
}
trdsql.inDelimiter = ","
trdsql.InDelimiter = ","
var cr Input
cr, err = trdsql.csvInputNew(file)
if err != nil {
Expand All @@ -189,7 +189,7 @@ func TestCsvIndefiniteInputFile3(t *testing.T) {
if err != nil {
t.Error(err)
}
trdsql.inDelimiter = ","
trdsql.InDelimiter = ","
var cr Input
cr, err = trdsql.csvInputNew(file)
if err != nil {
Expand Down
48 changes: 29 additions & 19 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ func (trdsql *TRDSQL) importTable(db *DDB, tablename string, sqlstr string) (str
return sqlstr, err
}

if trdsql.inSkip > 0 {
if trdsql.InSkip > 0 {
skip := make([]interface{}, 1)
for i := 0; i < trdsql.inSkip; i++ {
for i := 0; i < trdsql.InSkip; i++ {
r, e := input.ReadRow(skip)
if e != nil {
log.Printf("ERROR: skip error %s", e)
Expand All @@ -154,7 +154,7 @@ func (trdsql *TRDSQL) importTable(db *DDB, tablename string, sqlstr string) (str
}
rtable := db.EscapeTable(tablename)
sqlstr = db.RewriteSQL(sqlstr, tablename, rtable)
columnNames, err := input.GetColumn(trdsql.inPreRead)
columnNames, err := input.GetColumn(trdsql.InPreRead)
if err != nil {
if err != io.EOF {
return sqlstr, err
Expand All @@ -175,18 +175,18 @@ func (trdsql *TRDSQL) importTable(db *DDB, tablename string, sqlstr string) (str
if err != nil {
return sqlstr, err
}
err = db.Import(rtable, columnNames, input, trdsql.inPreRead)
err = db.Import(rtable, columnNames, input, trdsql.InPreRead)
return sqlstr, err
}

// InputNew is create input reader.
func (trdsql *TRDSQL) InputNew(reader io.Reader, tablename string) (Input, error) {
var err error
var input Input
if trdsql.inType == GUESS {
trdsql.inType = guessExtension(tablename)
if trdsql.InFormat == GUESS {
trdsql.InFormat = guessExtension(tablename)
}
switch trdsql.inType {
switch trdsql.InFormat {
case CSV:
input, err = trdsql.csvInputNew(reader)
case LTSV:
Expand Down Expand Up @@ -279,21 +279,31 @@ func tableFileOpen(filename string) (io.ReadCloser, error) {
return extFileReader(filename, file), nil
}

func guessExtension(tablename string) int {
func guessExtension(tablename string) InputFormat {
if strings.HasSuffix(tablename, ".gz") {
tablename = tablename[0 : len(tablename)-3]
}
pos := strings.LastIndex(tablename, ".")
if pos > 0 {
ext := strings.ToLower(tablename[pos:])
if strings.Contains(ext, ".ltsv") {
debug.Printf("Guess file type as LTSV: [%s]", tablename)
return LTSV
} else if strings.Contains(ext, ".json") {
debug.Printf("Guess file type as JSON: [%s]", tablename)
return JSON
}
if pos == 0 {
debug.Printf("Set in CSV because the extension is unknown: [%s]", tablename)
return CSV
}
ext := strings.ToUpper(tablename[pos+1:])
switch ext {
case "CSV":
debug.Printf("Guess file type as CSV: [%s]", tablename)
return CSV
case "LTSV":
debug.Printf("Guess file type as LTSV: [%s]", tablename)
return LTSV
case "JSON":
debug.Printf("Guess file type as JSON: [%s]", tablename)
return JSON
case "TBLN":
debug.Printf("Guess file type as TBLN: [%s]", tablename)
return TBLN
default:
debug.Printf("Set in CSV because the extension is unknown: [%s]", tablename)
return CSV
}
debug.Printf("Guess file type as CSV: [%s]", tablename)
return CSV
}
8 changes: 4 additions & 4 deletions input_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ type CSVIn struct {

func (trdsql *TRDSQL) csvInputNew(r io.Reader) (Input, error) {
var err error
if trdsql.inHeader {
trdsql.inPreRead--
if trdsql.InHeader {
trdsql.InPreRead--
}
cr := &CSVIn{}
cr.reader = csv.NewReader(r)
cr.reader.LazyQuotes = true
cr.reader.FieldsPerRecord = -1 // no check count
cr.reader.TrimLeadingSpace = true
cr.inHeader = trdsql.inHeader
cr.reader.Comma, err = delimiter(trdsql.inDelimiter)
cr.inHeader = trdsql.InHeader
cr.reader.Comma, err = delimiter(trdsql.InDelimiter)
return cr, err
}

Expand Down
16 changes: 8 additions & 8 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"unicode/utf8"
)

// Output is database export
type Output interface {
// Exporter is database export
type Exporter interface {
First([]string, []string) error
RowWrite([]interface{}, []string) error
WriteRow([]interface{}, []string) error
Last() error
}

// Export is execute SQL and output the result.
func (trdsql *TRDSQL) Export(db *DDB, sqlstr string, output Output) error {
// Export is execute SQL and Exporter the result.
func (trdsql *TRDSQL) Export(db *DDB, sqlstr string, w Exporter) error {
rows, err := db.Select(sqlstr)
if err != nil {
return err
Expand Down Expand Up @@ -48,7 +48,7 @@ func (trdsql *TRDSQL) Export(db *DDB, sqlstr string, output Output) error {
types[i] = convertType(ct.DatabaseTypeName())
}

err = output.First(columns, types)
err = w.First(columns, types)
if err != nil {
return err
}
Expand All @@ -57,12 +57,12 @@ func (trdsql *TRDSQL) Export(db *DDB, sqlstr string, output Output) error {
if err != nil {
return err
}
err = output.RowWrite(values, columns)
err = w.WriteRow(values, columns)
if err != nil {
return err
}
}
return output.Last()
return w.Last()
}

func convertType(dbtype string) string {
Expand Down
10 changes: 5 additions & 5 deletions output_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ type CSVOut struct {
outHeader bool
}

func (trdsql *TRDSQL) csvOutNew() Output {
func (trdsql *TRDSQL) csvOutNew() *CSVOut {
var err error
c := &CSVOut{}
c.writer = csv.NewWriter(trdsql.OutStream)
c.writer.Comma, err = delimiter(trdsql.outDelimiter)
c.writer.Comma, err = delimiter(trdsql.OutDelimiter)
if err != nil {
debug.Printf("%s\n", err)
}
c.outHeader = trdsql.outHeader
c.outHeader = trdsql.OutHeader
return c
}

Expand All @@ -35,8 +35,8 @@ func (c *CSVOut) First(columns []string, types []string) error {
return nil
}

// RowWrite is row output
func (c *CSVOut) RowWrite(values []interface{}, columns []string) error {
// WriteRow is row output
func (c *CSVOut) WriteRow(values []interface{}, columns []string) error {
for i, col := range values {
c.results[i] = valString(col)
}
Expand Down
6 changes: 3 additions & 3 deletions output_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type JSONOut struct {
results []map[string]interface{}
}

func (trdsql *TRDSQL) jsonOutNew() Output {
func (trdsql *TRDSQL) jsonOutNew() *JSONOut {
js := &JSONOut{}
js.writer = json.NewEncoder(trdsql.OutStream)
js.writer.SetIndent("", " ")
Expand All @@ -25,8 +25,8 @@ func (js *JSONOut) First(columns []string, types []string) error {
return nil
}

// RowWrite is Addition to array
func (js *JSONOut) RowWrite(values []interface{}, columns []string) error {
// WriteRow is Addition to array
func (js *JSONOut) WriteRow(values []interface{}, columns []string) error {
m := make(map[string]interface{}, len(columns))
for i, col := range values {
m[columns[i]] = valInterface(col)
Expand Down
6 changes: 3 additions & 3 deletions output_ltsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type LTSVOut struct {
results map[string]string
}

func (trdsql *TRDSQL) ltsvOutNew() Output {
func (trdsql *TRDSQL) ltsvOutNew() *LTSVOut {
lw := &LTSVOut{}
lw.delimiter = "\t"
lw.writer = bufio.NewWriter(trdsql.OutStream)
Expand All @@ -25,8 +25,8 @@ func (lw *LTSVOut) First(columns []string, types []string) error {
return nil
}

// RowWrite is Actual output
func (lw *LTSVOut) RowWrite(values []interface{}, columns []string) error {
// WriteRow is Actual output
func (lw *LTSVOut) WriteRow(values []interface{}, columns []string) error {
results := make([]string, len(values))
for i, col := range values {
results[i] = columns[i] + ":" + valString(col)
Expand Down
10 changes: 5 additions & 5 deletions output_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ type RawOut struct {
outHeader bool
}

func (trdsql *TRDSQL) rawOutNew() Output {
func (trdsql *TRDSQL) rawOutNew() *RawOut {
var err error
raw := &RawOut{}
raw.writer = bufio.NewWriter(trdsql.OutStream)
raw.sep, err = strconv.Unquote(`"` + trdsql.outDelimiter + `"`)
raw.sep, err = strconv.Unquote(`"` + trdsql.OutDelimiter + `"`)
if err != nil {
debug.Printf("%s\n", err)
}
raw.outHeader = trdsql.outHeader
raw.outHeader = trdsql.OutHeader
return raw
}

Expand All @@ -39,8 +39,8 @@ func (raw *RawOut) First(columns []string, types []string) error {
return nil
}

// RowWrite is row output
func (raw *RawOut) RowWrite(values []interface{}, columns []string) error {
// WriteRow is row output
func (raw *RawOut) WriteRow(values []interface{}, columns []string) error {
for i, col := range values {
raw.results[i] = valString(col)
}
Expand Down
6 changes: 3 additions & 3 deletions output_tablewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type TwOut struct {
results []string
}

func (trdsql *TRDSQL) twOutNew(markdown bool) Output {
func (trdsql *TRDSQL) twOutNew(markdown bool) *TwOut {
tw := &TwOut{}
tw.writer = tablewriter.NewWriter(trdsql.OutStream)
tw.writer.SetAutoFormatHeaders(false)
Expand All @@ -29,8 +29,8 @@ func (tw *TwOut) First(columns []string, types []string) error {
return nil
}

// RowWrite is Addition to array
func (tw *TwOut) RowWrite(values []interface{}, columns []string) error {
// WriteRow is Addition to array
func (tw *TwOut) WriteRow(values []interface{}, columns []string) error {
for i, col := range values {
tw.results[i] = valString(col)
}
Expand Down
Loading

0 comments on commit 1cdebda

Please sign in to comment.