@@ -11,6 +11,7 @@ import (
11
11
"io/ioutil"
12
12
"log"
13
13
"os"
14
+ "path"
14
15
"path/filepath"
15
16
"regexp"
16
17
"sort"
@@ -97,6 +98,12 @@ func dumpJSONFile(file string, pretty bool, schema bool) error {
97
98
}
98
99
defer fd .Close ()
99
100
101
+ size := int64 (- 1 )
102
+ fi , err := fd .Stat ()
103
+ if err == nil {
104
+ size = fi .Size ()
105
+ }
106
+
100
107
if schema {
101
108
s , err := runner .ShapeFromFile (file , "doesn't-matter" , runner .DefaultShapeMaxBytesToRead , 100 )
102
109
if err != nil {
@@ -124,48 +131,57 @@ func dumpJSONFile(file string, pretty bool, schema bool) error {
124
131
return nil
125
132
}
126
133
127
- s , err := runner .ShapeFromFile (file , "doesn't-matter" , runner .DefaultShapeMaxBytesToRead , 100 )
128
- if err != nil {
129
- return err
130
- }
131
- var columns []string
132
- for name := range s .ArrayShape .Children .ObjectShape .Children {
133
- columns = append (columns , name )
134
- }
135
- sort .Strings (columns )
134
+ var rows []map [string ]interface {}
135
+ if size != 0 {
136
+ s , err := runner .ShapeFromFile (file , "doesn't-matter" , runner .DefaultShapeMaxBytesToRead , 100 )
137
+ if err != nil {
138
+ return err
139
+ }
140
+ var columns []string
141
+ for name := range s .ArrayShape .Children .ObjectShape .Children {
142
+ columns = append (columns , name )
143
+ }
144
+ sort .Strings (columns )
136
145
137
- table := tablewriter .NewWriter (os .Stdout )
138
- table .SetHeader (columns )
139
- table .SetAutoFormatHeaders (false )
146
+ table := tablewriter .NewWriter (os .Stdout )
147
+ table .SetHeader (columns )
148
+ table .SetAutoFormatHeaders (false )
140
149
141
- dec := json .NewDecoder (fd )
142
- var rows []map [string ]interface {}
143
- err = dec .Decode (& rows )
144
- if err != nil {
145
- return err
146
- }
150
+ dec := json .NewDecoder (fd )
151
+ err = dec .Decode (& rows )
152
+ if err != nil {
153
+ return err
154
+ }
147
155
148
- for _ , objRow := range rows {
149
- var row []string
150
- for _ , column := range columns {
151
- var cell string
152
- switch t := objRow [column ].(type ) {
153
- case bool , byte , complex64 , complex128 , error , float32 , float64 ,
154
- int , int8 , int16 , int32 , int64 ,
155
- uint , uint16 , uint32 , uint64 , uintptr :
156
- cell = fmt .Sprintf ("%#v" , t )
157
- case string :
158
- cell = t
159
- default :
160
- cellBytes , _ := json .Marshal (t )
161
- cell = string (cellBytes )
156
+ for _ , objRow := range rows {
157
+ var row []string
158
+ for _ , column := range columns {
159
+ var cell string
160
+ switch t := objRow [column ].(type ) {
161
+ case bool , byte , complex64 , complex128 , error , float32 , float64 ,
162
+ int , int8 , int16 , int32 , int64 ,
163
+ uint , uint16 , uint32 , uint64 , uintptr :
164
+ cell = fmt .Sprintf ("%#v" , t )
165
+ case string :
166
+ cell = t
167
+ default :
168
+ cellBytes , _ := json .Marshal (t )
169
+ cell = string (cellBytes )
170
+ }
171
+ row = append (row , cell )
162
172
}
163
- row = append (row , cell )
173
+ table . Append (row )
164
174
}
165
- table .Append (row )
175
+
176
+ table .Render ()
177
+ }
178
+
179
+ if len (rows ) == 1 {
180
+ fmt .Println ("(1 row)" )
181
+ } else {
182
+ fmt .Printf ("(%d rows)\n " , len (rows ))
166
183
}
167
184
168
- table .Render ()
169
185
return nil
170
186
}
171
187
@@ -251,13 +267,6 @@ func runQuery(queryRaw string, project *runner.ProjectState, ec *runner.EvalCont
251
267
}
252
268
253
269
func repl (project * runner.ProjectState , ec * runner.EvalContext , args * args , files []string ) error {
254
- tempfile , err := ioutil .TempFile ("" , "dsq-hist" )
255
- if err != nil {
256
- return err
257
- }
258
-
259
- defer os .Remove (tempfile .Name ())
260
-
261
270
completer := readline .NewPrefixCompleter (
262
271
readline .PcItem ("SELECT" ),
263
272
readline .PcItem ("FROM" ),
@@ -277,9 +286,10 @@ func repl(project *runner.ProjectState, ec *runner.EvalContext, args *args, file
277
286
return r , true
278
287
}
279
288
289
+ historyFile := path .Join (runner .HOME , "dsq_history" )
280
290
l , err := readline .NewEx (& readline.Config {
281
291
Prompt : "dsq> " ,
282
- HistoryFile : tempfile . Name () ,
292
+ HistoryFile : historyFile ,
283
293
InterruptPrompt : "^D" ,
284
294
EOFPrompt : "exit" ,
285
295
HistorySearchFold : true ,
0 commit comments