Skip to content

Commit

Permalink
Fix a bug on multiple statements, added a proper close to the result …
Browse files Browse the repository at this point in the history
…set when we are done between statements
  • Loading branch information
Paul Bergeron committed Jun 19, 2015
1 parent 32d1d60 commit 4470a0f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 51 deletions.
14 changes: 11 additions & 3 deletions cmd/textql.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (this *CommandLineOptions) GetConsole() bool {

func main() {
cmdLineOpts := NewCommandLineOptions()
var outputer outputs.Output

if cmdLineOpts.GetConsole() {
if cmdLineOpts.GetSourceFile() == "stdin" {
Expand Down Expand Up @@ -117,7 +118,7 @@ func main() {

storage.LoadInput(input)

queryResults := storage.ExecuteSQLStrings(strings.Split(cmdLineOpts.GetCommands(), ";"))
sqlStrings := strings.Split(cmdLineOpts.GetCommands(), ";")

if cmdLineOpts.GetOutputFile() != "" {
displayOpts := &outputs.CSVOutputOptions{
Expand All @@ -126,8 +127,15 @@ func main() {
WriteTo: util.OpenFileOrStdDev(cmdLineOpts.GetOutputFile()),
}

outputer := outputs.NewCSVOutput(displayOpts)
outputer.Show(queryResults)
outputer = outputs.NewCSVOutput(displayOpts)
}

for _, sqlQuery := range sqlStrings {
queryResults := storage.ExecuteSQLString(sqlQuery)

if queryResults != nil && cmdLineOpts.GetOutputFile() != "" {
outputer.Show(queryResults)
}
}

if cmdLineOpts.GetSaveTo() != "" {
Expand Down
49 changes: 24 additions & 25 deletions outputs/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,42 @@ func NewCSVOutput(opts *CSVOutputOptions) *csvOutput {
return this
}

func (this *csvOutput) Show(queryResults []*sql.Rows) {
for _, rows := range queryResults {
cols, colsErr := rows.Columns()
func (this *csvOutput) Show(rows *sql.Rows) {
cols, colsErr := rows.Columns()

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

if this.options.WriteHeader {
if err := this.writer.Write(cols); err != nil {
log.Fatalln(err)
}
if this.options.WriteHeader {
if err := this.writer.Write(cols); err != nil {
log.Fatalln(err)
}
}

rawResult := make([][]byte, len(cols))
result := make([]string, len(cols))
rawResult := make([][]byte, len(cols))
result := make([]string, len(cols))

dest := make([]interface{}, len(cols))
dest := make([]interface{}, len(cols))

for i, _ := range cols {
dest[i] = &rawResult[i]
}
for i, _ := range cols {
dest[i] = &rawResult[i]
}

for rows.Next() {
rows.Scan(dest...)
for rows.Next() {
rows.Scan(dest...)

for i, raw := range rawResult {
result[i] = string(raw)
}
for i, raw := range rawResult {
result[i] = string(raw)
}

writeErr := this.writer.Write(result)
writeErr := this.writer.Write(result)

if writeErr != nil {
log.Fatalln(colsErr)
}
if writeErr != nil {
log.Fatalln(colsErr)
}
}

this.writer.Flush()
rows.Close()
}
2 changes: 1 addition & 1 deletion outputs/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package outputs
import "database/sql"

type Output interface {
Show([]*sql.Rows)
Show(*sql.Rows)
}
24 changes: 12 additions & 12 deletions storage/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"

"github.com/dinedal/textql/inputs"
"github.com/dinedal/textql/sqlparser"
sqlite3 "github.com/mattn/go-sqlite3"
)

Expand Down Expand Up @@ -178,20 +179,19 @@ func (this *sqlite3Storage) loadRow(tableName string, colCount int, values []str
return err
}

func (this *sqlite3Storage) ExecuteSQLStrings(sqlStrings []string) []*sql.Rows {
var results []*sql.Rows

for _, sqlQuery := range sqlStrings {
if strings.Trim(sqlQuery, " ") != "" {
//implictFromSql := sqlparser.AddImplictFrom(sqlQuery, this.firstTableName)
result, err := this.db.Query(sqlQuery)
if err != nil {
log.Fatalln(err)
}
results = append(results, result)
func (this *sqlite3Storage) ExecuteSQLString(sqlQuery string) *sql.Rows {
var result *sql.Rows
var err error

if strings.Trim(sqlQuery, " ") != "" {
implictFromSql := sqlparser.AddImplictFrom(sqlQuery, this.firstTableName)
result, err = this.db.Query(implictFromSql)
if err != nil {
log.Fatalln(err)
}
}
return results

return result
}

func (this *sqlite3Storage) SaveTo(path string) {
Expand Down
27 changes: 18 additions & 9 deletions storage/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestSQLiteStorageSaveTo(t *testing.T) {
}
}

func TestSQLiteStorageExecuteSQLStrings(t *testing.T) {
func TestSQLiteStorageExecuteSQLString(t *testing.T) {
storage := NewSQLite3Storage(storageOpts)
input, fp := NewTestCSVInput()
defer fp.Close()
Expand All @@ -94,17 +94,26 @@ func TestSQLiteStorageExecuteSQLStrings(t *testing.T) {

storage.LoadInput(input)

sqlStrings := []string{
"select count(*)",
}
sqlString := "select count(*)"

rows := storage.ExecuteSQLString(sqlString)

result := storage.ExecuteSQLStrings(sqlStrings)
cols, colsErr := rows.Columns()

if len(result) != len(sqlStrings) {
t.Fatalf("Failed to execute all sql statements, expected (%v) got (%v)", len(sqlStrings), len(result))
if colsErr != nil {
t.Fatalf(colsErr.Error())
}
}

func TestSQLiteStorageClose(t *testing.T) {
if len(cols) != 1 {
t.Fatalf("Expected 1 column, got (%v)", len(cols))
}

var dest int

for rows.Next() {
rows.Scan(&dest)
if dest != 2 {
t.Fatalf("Expected 2 rows counted, got (%v)", dest)
}
}
}
2 changes: 1 addition & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (
type Storage interface {
LoadInput(*inputs.Input)
SaveTo(string)
ExecuteSQLStrings([]string) []*sql.Rows
ExecuteSQLString(string) *sql.Rows
Close()
}

0 comments on commit 4470a0f

Please sign in to comment.