55import json
66import sys
77import csv as csv_std
8+ import tabulate
89import sqlite3
910
1011
@@ -23,8 +24,17 @@ def output_options(fn):
2324 is_flag = True ,
2425 default = False ,
2526 ),
26- click .option ("--csv" , is_flag = True , help = "Output CSV" ),
27+ click .option ("-c" , "- -csv" , is_flag = True , help = "Output CSV" ),
2728 click .option ("--no-headers" , is_flag = True , help = "Omit CSV headers" ),
29+ click .option ("-t" , "--table" , is_flag = True , help = "Output as a table" ),
30+ click .option (
31+ "-f" ,
32+ "--fmt" ,
33+ help = "Table format - one of {}" .format (
34+ ", " .join (tabulate .tabulate_formats )
35+ ),
36+ default = "simple" ,
37+ ),
2838 )
2939 ):
3040 fn = decorator (fn )
@@ -60,7 +70,7 @@ def cli():
6070 is_flag = True ,
6171 default = False ,
6272)
63- def tables (path , fts4 , fts5 , counts , nl , arrays , csv , no_headers , columns ):
73+ def tables (path , fts4 , fts5 , counts , nl , arrays , csv , no_headers , table , fmt , columns ):
6474 """List the tables in the database"""
6575 db = sqlite_utils .Database (path )
6676 headers = ["table" ]
@@ -82,7 +92,9 @@ def _iter():
8292 row .append (cols )
8393 yield row
8494
85- if csv :
95+ if table :
96+ print (tabulate .tabulate (_iter (), headers = headers , tablefmt = fmt ))
97+ elif csv :
8698 writer = csv_std .writer (sys .stdout )
8799 if not no_headers :
88100 writer .writerow (headers )
@@ -175,7 +187,7 @@ def insert_upsert_options(fn):
175187 click .argument ("json_file" , type = click .File (), required = True ),
176188 click .option ("--pk" , help = "Column to use as the primary key, e.g. id" ),
177189 click .option ("--nl" , is_flag = True , help = "Expect newline-delimited JSON" ),
178- click .option ("--csv" , is_flag = True , help = "Expect CSV" ),
190+ click .option ("-c" , "- -csv" , is_flag = True , help = "Expect CSV" ),
179191 click .option (
180192 "--batch-size" , type = int , default = 100 , help = "Commit every X records"
181193 ),
@@ -244,12 +256,14 @@ def upsert(path, table, json_file, pk, nl, csv, batch_size):
244256)
245257@click .argument ("sql" )
246258@output_options
247- def query (path , sql , nl , arrays , csv , no_headers ):
259+ def query (path , sql , nl , arrays , csv , no_headers , table , fmt ):
248260 "Execute SQL query and return the results as JSON"
249261 db = sqlite_utils .Database (path )
250262 cursor = iter (db .conn .execute (sql ))
251263 headers = [c [0 ] for c in cursor .description ]
252- if csv :
264+ if table :
265+ print (tabulate .tabulate (list (cursor ), headers = headers , tablefmt = fmt ))
266+ elif csv :
253267 writer = csv_std .writer (sys .stdout )
254268 if not no_headers :
255269 writer .writerow ([c [0 ] for c in cursor .description ])
@@ -266,19 +280,21 @@ def query(path, sql, nl, arrays, csv, no_headers):
266280 type = click .Path (file_okay = True , dir_okay = False , allow_dash = False ),
267281 required = True ,
268282)
269- @click .argument ("table " )
283+ @click .argument ("dbtable " )
270284@output_options
271285@click .pass_context
272- def rows (ctx , path , table , nl , arrays , csv , no_headers ):
286+ def rows (ctx , path , dbtable , nl , arrays , csv , no_headers , table , fmt ):
273287 "Output all rows in the specified table"
274288 ctx .invoke (
275289 query ,
276290 path = path ,
277- sql = "select * from [{}]" .format (table ),
291+ sql = "select * from [{}]" .format (dbtable ),
278292 nl = nl ,
279293 arrays = arrays ,
280294 csv = csv ,
281295 no_headers = no_headers ,
296+ table = table ,
297+ fmt = fmt ,
282298 )
283299
284300
0 commit comments