Skip to content

Commit

Permalink
Introduce numerical sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Apr 29, 2024
1 parent a88f1f9 commit 4f81560
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
21 changes: 20 additions & 1 deletion infrastructure/sql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package infrastructure

import (
"strconv"
"strings"

"github.com/nao1215/sqly/domain/model"
Expand Down Expand Up @@ -39,7 +40,11 @@ func SingleQuote(s string) string {
func GenerateCreateTableStatement(t *model.Table) string {
ddl := "CREATE TABLE " + Quote(t.Name) + "("
for i, v := range t.Header {
ddl += Quote(v)
if isNumeric(t, i) {
ddl += Quote(v) + " INTEGER"
} else {
ddl += Quote(v) + " TEXT"
}
if i != len(t.Header)-1 {
ddl += ", "
} else {
Expand All @@ -49,6 +54,20 @@ func GenerateCreateTableStatement(t *model.Table) string {
return ddl
}

func isNumeric(t *model.Table, index int) bool {
if len(t.Records) == 0 {
return false
}

for _, record := range t.Records {
_, err := strconv.ParseFloat(record[index], 64)
if err != nil {
return false
}
}
return true
}

func GenerateInsertStatement(name string, record model.Record) string {
dml := "INSERT INTO " + Quote(name) + " VALUES ("
for i, v := range record {
Expand Down
19 changes: 17 additions & 2 deletions infrastructure/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@ func Test_generateCreateTableStatement(t *testing.T) {
want string
}{
{
name: "success to generate create table statement",
name: "success to generate create table statement with no records",
args: args{
t: &model.Table{
Name: "test",
Header: model.Header{"abc", "def", "ghj"},
},
},
want: "CREATE TABLE `test`(`abc`, `def`, `ghj`);",
want: "CREATE TABLE `test`(`abc` TEXT, `def` TEXT, `ghj` TEXT);",
},
{
name: "success to generate create table statement with records",
args: args{
t: &model.Table{
Name: "test",
Header: model.Header{"id", "name", "number_and_string"},
Records: []model.Record{
{"1", "name1", "1"},
{"2", "name2", "a"},
{"3", "name3", "3"},
},
},
},
want: "CREATE TABLE `test`(`id` INTEGER, `name` TEXT, `number_and_string` TEXT);",
},
}
for _, tt := range tests {
Expand Down
25 changes: 25 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ func Test_run(t *testing.T) {
g.Assert(t, "select_ltsv", got)
})

t.Run("Treat numbers as numeric types; support numerical sorting", func(t *testing.T) {
// SELECT * FROM numeric ORDER BY id
// [Previously Result]
// id,name
// 1,John
// 11,Ringo
// 12,Billy
// 2,Paul
// 3,George
//
// [Current Result]
// id,name
// 1,John
// 2,Paul
// 3,George
// 11,Ringo
// 12,Billy

args := []string{"sqly", "--sql", "SELECT * FROM numeric ORDER BY id", "--csv", "testdata/numeric.csv"}
got := getStdoutForRunFunc(t, run, args)
g := golden.New(t,
golden.WithFixtureDir(filepath.Join("testdata", "golden")))
g.Assert(t, "numeric", got)
})

t.Run("Fix Issue 42: Panic when json field is null", func(t *testing.T) {
args := []string{"sqly", "--sql", "select * from bug_issue42 limit 1", "--csv", "testdata/bug_issue42.json"}
got := getStdoutForRunFunc(t, run, args)
Expand Down
6 changes: 6 additions & 0 deletions testdata/golden/numeric.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id,name
1,John
2,Paul
3,George
11,Ringo
12,Billy
6 changes: 6 additions & 0 deletions testdata/numeric.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id,name
1,John
2,Paul
3,George
11,Ringo
12,Billy

0 comments on commit 4f81560

Please sign in to comment.