Skip to content

Commit

Permalink
Refactor, and drop ext from table name but keep it based on file
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Bergeron committed May 20, 2015
1 parent d3968b7 commit a85ba30
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
9 changes: 3 additions & 6 deletions cmd/textql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"os"
"os/exec"
"path"
"strings"

"github.com/dinedal/textql/inputs"
Expand Down Expand Up @@ -53,11 +52,11 @@ func main() {
storage := storage.NewSQLite3Storage(storage_opts)

if (*tableName) != "" {
storage.LoadInput(input, *tableName)
} else {
storage.LoadInput(input, path.Base(input.Name()))
input.SetName(*tableName)
}

storage.LoadInput(input)

queryResults := storage.ExecuteSQLStrings(strings.Split(*commands, ";"))

if (*outputFile) != "" {
Expand Down Expand Up @@ -104,8 +103,6 @@ func main() {
cmd.Stderr = os.Stderr
cmd_err := cmd.Run()

log.Println("ok whut")

if cmd.Process != nil {
cmd.Process.Release()
}
Expand Down
20 changes: 13 additions & 7 deletions inputs/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"os"
"path"
"strconv"
)

Expand All @@ -14,6 +15,7 @@ type csvInput struct {
firstRow []string
header []string
minOutputLength int
name string
}

type CSVInputOptions struct {
Expand All @@ -35,17 +37,21 @@ func NewCSVInput(opts *CSVInputOptions) *csvInput {

this.readHeader()

if as_file, ok := this.options.ReadFrom.(*os.File); ok {
this.name = path.Base(as_file.Name())
} else {
this.name = "pipe"
}

return this
}

func (this *csvInput) Name() string {
if as_file, ok := this.options.ReadFrom.(*os.File); ok {
if as_file.Name() == "/dev/stdin" {
return "stdin"
}
return as_file.Name()
}
return "buffer"
return this.name
}

func (this *csvInput) SetName(name string) {
this.name = name
}

func (this *csvInput) ReadRecord() []string {
Expand Down
1 change: 1 addition & 0 deletions inputs/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Input interface {
ReadRecord() []string
Header() []string
Name() string
SetName(string)
}
10 changes: 6 additions & 4 deletions storage/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql"
"fmt"
"path"
"strings"

"log"
Expand Down Expand Up @@ -60,23 +61,24 @@ func (this *sqlite3Storage) open() {
this.db = db
}

func (this *sqlite3Storage) LoadInput(input inputs.Input, name string) {
this.createTable(name, input.Header(), false)
func (this *sqlite3Storage) LoadInput(input inputs.Input) {
tableName := strings.Replace(input.Name(), path.Ext(input.Name()), "", -1)
this.createTable(tableName, input.Header(), false)

tx, tx_err := this.db.Begin()

if tx_err != nil {
log.Fatalln(tx_err)
}

stmt := this.createLoadStmt(name, len(input.Header()), tx)
stmt := this.createLoadStmt(tableName, len(input.Header()), tx)

row := input.ReadRecord()
for {
if row == nil {
break
}
this.loadRow(name, len(input.Header()), row, tx, stmt, true)
this.loadRow(tableName, len(input.Header()), row, tx, stmt, true)
row = input.ReadRecord()
}
stmt.Close()
Expand Down
2 changes: 1 addition & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Storage interface {
LoadInput(*inputs.Input, string)
LoadInput(*inputs.Input)
SaveTo(string)
ExecuteSQLStrings([]string) []*sql.Rows
Close()
Expand Down

0 comments on commit a85ba30

Please sign in to comment.