Skip to content

Commit

Permalink
Handle 0 rows (#57)
Browse files Browse the repository at this point in the history
* Handle 0 rows

* Fix for tests

* Update readme
  • Loading branch information
eatonphil authored Apr 26, 2022
1 parent 8f88b40 commit e07058e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 45 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,24 @@ file into SQLite.
So even if you change the query, as long as the file doesn't change,
the cache is effective.
### Interactive REPL
Use the `-i` or `--interactive` flag to enter an interactive REPL
where you can run multiple SQL queries.
```
$ dsq some-large-file.json -i
dsq> SELECT COUNT(1) FROM {};
+----------+
| COUNT(1) |
+----------+
| 1000 |
+----------+
(1 row)
dsq> SELECT * FROM {} WHERE NAME = 'Kevin';
(0 rows)
```
## Supported Data Types
| Name | File Extension(s) | Mime Type | Notes |
Expand Down
96 changes: 53 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -97,6 +98,12 @@ func dumpJSONFile(file string, pretty bool, schema bool) error {
}
defer fd.Close()

size := int64(-1)
fi, err := fd.Stat()
if err == nil {
size = fi.Size()
}

if schema {
s, err := runner.ShapeFromFile(file, "doesn't-matter", runner.DefaultShapeMaxBytesToRead, 100)
if err != nil {
Expand Down Expand Up @@ -124,48 +131,57 @@ func dumpJSONFile(file string, pretty bool, schema bool) error {
return nil
}

s, err := runner.ShapeFromFile(file, "doesn't-matter", runner.DefaultShapeMaxBytesToRead, 100)
if err != nil {
return err
}
var columns []string
for name := range s.ArrayShape.Children.ObjectShape.Children {
columns = append(columns, name)
}
sort.Strings(columns)
var rows []map[string]interface{}
if size != 0 {
s, err := runner.ShapeFromFile(file, "doesn't-matter", runner.DefaultShapeMaxBytesToRead, 100)
if err != nil {
return err
}
var columns []string
for name := range s.ArrayShape.Children.ObjectShape.Children {
columns = append(columns, name)
}
sort.Strings(columns)

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(columns)
table.SetAutoFormatHeaders(false)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(columns)
table.SetAutoFormatHeaders(false)

dec := json.NewDecoder(fd)
var rows []map[string]interface{}
err = dec.Decode(&rows)
if err != nil {
return err
}
dec := json.NewDecoder(fd)
err = dec.Decode(&rows)
if err != nil {
return err
}

for _, objRow := range rows {
var row []string
for _, column := range columns {
var cell string
switch t := objRow[column].(type) {
case bool, byte, complex64, complex128, error, float32, float64,
int, int8, int16, int32, int64,
uint, uint16, uint32, uint64, uintptr:
cell = fmt.Sprintf("%#v", t)
case string:
cell = t
default:
cellBytes, _ := json.Marshal(t)
cell = string(cellBytes)
for _, objRow := range rows {
var row []string
for _, column := range columns {
var cell string
switch t := objRow[column].(type) {
case bool, byte, complex64, complex128, error, float32, float64,
int, int8, int16, int32, int64,
uint, uint16, uint32, uint64, uintptr:
cell = fmt.Sprintf("%#v", t)
case string:
cell = t
default:
cellBytes, _ := json.Marshal(t)
cell = string(cellBytes)
}
row = append(row, cell)
}
row = append(row, cell)
table.Append(row)
}
table.Append(row)

table.Render()
}

if len(rows) == 1 {
fmt.Println("(1 row)")
} else {
fmt.Printf("(%d rows)\n", len(rows))
}

table.Render()
return nil
}

Expand Down Expand Up @@ -251,13 +267,6 @@ func runQuery(queryRaw string, project *runner.ProjectState, ec *runner.EvalCont
}

func repl(project *runner.ProjectState, ec *runner.EvalContext, args *args, files []string) error {
tempfile, err := ioutil.TempFile("", "dsq-hist")
if err != nil {
return err
}

defer os.Remove(tempfile.Name())

completer := readline.NewPrefixCompleter(
readline.PcItem("SELECT"),
readline.PcItem("FROM"),
Expand All @@ -277,9 +286,10 @@ func repl(project *runner.ProjectState, ec *runner.EvalContext, args *args, file
return r, true
}

historyFile := path.Join(runner.HOME, "dsq_history")
l, err := readline.NewEx(&readline.Config{
Prompt: "dsq> ",
HistoryFile: tempfile.Name(),
HistoryFile: historyFile,
InterruptPrompt: "^D",
EOFPrompt: "exit",
HistorySearchFold: true,
Expand Down
6 changes: 4 additions & 2 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def test(name, to_run, want, fail=False, sort=False, winSkip=False, within_secon
+----+-------+
| 1 | Corah |
| 3 | Minh |
+----+-------+"""
+----+-------+
(2 rows)"""
test("Pretty column order alphabetical", to_run, want)

# Pretty without query
Expand All @@ -192,7 +193,8 @@ def test(name, to_run, want, fail=False, sort=False, winSkip=False, within_secon
| a | b | c |
+---+---+-------+
| 1 | 2 | [1,2] |
+---+---+-------+"""
+---+---+-------+
(1 row)"""
test("Pretty works even without query", to_run, want)

# Prints schema pretty
Expand Down

0 comments on commit e07058e

Please sign in to comment.