Skip to content

Commit df0eafb

Browse files
committed
Rename "print" to "view"
Add print Implement limit for view
1 parent 8a7024f commit df0eafb

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

README.rst

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ Save the result of the last select to a csv file:
103103
104104
$export file.csv
105105
106+
If the result was already fetched, the query is rerun.
107+
106108
``def``
107109
-------
108110
To use a custom python function in the script:
@@ -113,9 +115,28 @@ To use a custom python function in the script:
113115
...
114116
return ret
115117
118+
``view``
119+
--------
120+
Print the result of the last select on the terminal:
121+
122+
.. code:: sql
123+
124+
$view [limit]
125+
126+
An optional argument ``limit`` sets the maximum number of rows to display. If ``limit`` is omitted, the its value is ``100``. If ``limit == -1``, then no limit is set.
127+
128+
If the result was already fetched, the query is rerun.
129+
130+
``print``
131+
---------
132+
Print a string or a list of string on the terminal
133+
134+
.. code:: sql
135+
136+
$print something
137+
116138
TODO
117139
====
118140
* A ``script`` instruction that stores the session
119-
* A ``print`` instruction that prints the result of the last query
120141
* A ``dump`` instruction to dump the in memory database
121142
* An ``aggregate`` to create aggregate functions

examples/example.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ ORDER BY SUM(amount) DESC
5353

5454
$export "by_state.csv"
5555

56-
$print
56+
$print "The content of by_state.csv is:"
57+
58+
$view -1
59+
60+
$print "The first 10 lines of by_state.csv are:"
61+
62+
$view 10
5763

5864
DROP VIEW IF EXISTS detailed

sqliteondbf.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __init__(self, script, logger):
102102
else:
103103
self.__script = script.read()
104104
self.__logger = logger
105-
self.__ex = {"connect":self.__connect, "convert":self.__convert, "export":self.__export, "def":self.__def, "print":self.__print}
105+
self.__ex = {"connect":self.__connect, "convert":self.__convert, "export":self.__export, "def":self.__def, "print":self.__print, "view":self.__view}
106106

107107
def __connect(self, e, t, fpath, encoding="cp850"):
108108
self.__logger.info("set source to {} ({})".format(fpath, t))
@@ -138,19 +138,17 @@ def __def(self, e, *args):
138138

139139
self.__connection.create_function(name, len(params), func)
140140

141-
def __print(self, e, *args):
141+
def __view(self, e, *args):
142142
self.__ensure_cursor()
143143

144144
if args:
145-
limit = args[0]
145+
limit = int(args[0])
146146
else:
147147
limit = 100
148+
view(self.__cursor, limit, self.__logger)
148149

149-
column_names = [description[0] for description in self.__cursor.description]
150-
rows = [r for _, r in zip(range(limit), self.__cursor)]
151-
ws = [max(len(str(y)) for y in col) for col in zip(column_names, *rows)]
152-
for zs in (column_names, *rows):
153-
print ("\t".join([str(z).rjust(w) if type(z) in (int, float) else str(z).ljust(w) for z, w in zip(zs, ws)]))
150+
def __print(self, e, *args):
151+
print (*args)
154152

155153
def __ensure_cursor(self):
156154
if self.__cursor_fetched:
@@ -237,6 +235,19 @@ def export(cursor, csv_path, logger=logging.getLogger("sqliteondbf")):
237235
for row in cursor:
238236
writer.writerow(row)
239237

238+
def view(cursor, limit, logger=logging.getLogger("sqliteondbf")):
239+
logger.debug("display data on terminal")
240+
column_names = [description[0] for description in cursor.description]
241+
if limit >= 0:
242+
rows = [r for _, r in zip(range(limit), cursor)]
243+
else:
244+
rows = [r for r in cursor]
245+
ws = [max(len(str(y)) for y in col) for col in zip(column_names, *rows)]
246+
for zs in (column_names, *rows):
247+
print ("\t".join([str(z).rjust(w) if type(z) in (int, float) else str(z).ljust(w) for z, w in zip(zs, ws)]))
248+
if cursor.fetchone():
249+
print ("...")
250+
240251

241252
if __name__ == '__main__':
242253
main()

0 commit comments

Comments
 (0)