forked from kennethreitz/records
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecords.py
344 lines (260 loc) · 9.91 KB
/
records.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -*- coding: utf-8 -*-
import os
from code import interact
from datetime import datetime
import tablib
import psycopg2
from docopt import docopt
from psycopg2.extras import register_hstore, NamedTupleCursor
from psycopg2.extensions import cursor as _cursor
DATABASE_URL = os.environ.get('DATABASE_URL')
PG_INTERNAL_TABLES_QUERY = "SELECT * FROM pg_catalog.pg_tables"
PG_TABLES_QUERY = """
SELECT
*
FROM
pg_catalog.pg_tables
WHERE
schemaname != 'pg_catalog' AND
schemaname != 'information_schema'
"""
class RecordsCursor(NamedTupleCursor):
"""An enhanced cursor that generates Records."""
try:
from collections import namedtuple
except ImportError as _exc:
def _make_nt(self):
raise self._exc
else:
def _make_nt(self, namedtuple=namedtuple):
RecordBase = namedtuple("Record", [d[0] for d in self.description or ()])
# Extend the RecordsBase namedtuple, for enhanced API functionality.
class Record(RecordBase):
__slots__ = ()
def keys(self):
return self._fields
def __getitem__(self, key):
if isinstance(key, int):
return super(RecordBase, self).__getitem__(key)
if key in self.keys():
return getattr(self, key)
raise KeyError("Record contains no '{}' field.".format(key))
@property
def dataset(self):
"""A Tablib Dataset containing the row."""
data = tablib.Dataset()
data.headers = self._fields
row = _reduce_datetimes(self)
data.append(row)
return data
def export(self, format, **kwargs):
"""Exports the row to the given format."""
return self.dataset.export(format, **kwargs)
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
return Record
class ResultSet(object):
"""A set of results from a query."""
def __init__(self, rows):
self._rows = rows
self._all_rows = []
self.pending = True
def __repr__(self):
r = '<ResultSet size={} pending={}>'.format(len(self), self.pending)
return r
def __iter__(self):
"""Iterate over all rows, consuming the underlying generator
only when necessary."""
i = 0
while True:
# Other code may have iterated between yields,
# so always check the cache.
if i < len(self):
yield self[i]
else:
# Throws StopIteration when done.
yield next(self)
i += 1
def next(self):
return self.__next__()
def __next__(self):
try:
nextrow = next(self._rows)
self._all_rows.append(nextrow)
return nextrow
except StopIteration:
self.pending = False
raise StopIteration('ResultSet contains no more rows.')
def __getitem__(self, key):
is_int = isinstance(key, int)
# Convert ResultSet[1] into slice.
if is_int:
key = slice(key, key + 1)
while len(self) < key.stop or key.stop is None:
try:
next(self)
except StopIteration:
break
rows = self._all_rows[key]
if is_int:
return rows[0]
else:
return ResultSet(iter(rows))
def __len__(self):
return len(self._all_rows)
def export(self, format, **kwargs):
"""Export the ResultSet to a given format (courtesy of Tablib)."""
return self.dataset.export(format, **kwargs)
@property
def dataset(self):
"""A Tablib Dataset representation of the ResultSet."""
# Create a new Tablib Dataset.
data = tablib.Dataset()
# Set the column names as headers on Tablib Dataset.
first = self[0]
data.headers = first._fields
for row in self.all():
row = _reduce_datetimes(row)
data.append(row)
return data
def all(self):
"""Returns a list of all rows for the ResultSet. If they haven't
been fetched yet, consume the iterator and cache the results."""
# By calling list it calls the __iter__ method
return list(self)
class Database(object):
"""A Database connection."""
def __init__(self, db_url=None):
# If no db_url was provided, fallback to $DATABASE_URL.
self.db_url = db_url or DATABASE_URL
if not self.db_url:
raise ValueError('You must provide a db_url.')
# Connect to the database.
self.db = psycopg2.connect(self.db_url, cursor_factory=RecordsCursor)
# Enable hstore if it's available.
self._enable_hstore()
self.open = True
def close(self):
"""Closes the connection to the Database."""
self.db.close()
self.open = False
def __enter__(self):
return self
def __exit__(self, exc, val, traceback):
self.close()
def __repr__(self):
return '<Database open={}>'.format(self.open)
def _enable_hstore(self):
"""Enables HSTORE support, if available."""
try:
register_hstore(self.db)
except psycopg2.ProgrammingError:
pass
def get_table_names(self, internal=False):
"""Returns a list of table names for the connected database."""
# Support listing internal table names as well.
query = PG_INTERNAL_TABLES_QUERY if internal else PG_TABLES_QUERY
# Return a list of tablenames.
return [r['tablename'] for r in self.query(query)]
def query(self, query, params=None, fetchall=False):
"""Executes the given SQL query against the Database. Parameters
can, optionally, be provided. Returns a ResultSet, which can be
iterated over to get result rows as dictionaries.
"""
# Execute the given query.
c = self.db.cursor()
c.execute(query, params)
# Row-by-row result generator.
row_gen = (r for r in c)
# Convert psycopg2 results to ResultSet.
results = ResultSet(row_gen)
# Fetch all results if desired.
if fetchall:
results.all()
return results
def query_file(self, path, params=None, fetchall=False):
"""Like Database.query, but takes a filename to load a query from."""
# If path doesn't exists
if not os.path.exists(path):
raise FileNotFoundError
# If it's a directory
if os.path.isdir(path):
raise IsADirectoryError
# Read the given .sql file into memory.
with open(path) as f:
query = f.read()
# Defer processing to self.query method.
return self.query(query=query, params=params, fetchall=fetchall)
def _reduce_datetimes(row):
"""Receives a row, converts datetimes to strings."""
for i in range(len(row)):
if hasattr(row[i], 'isoformat'):
row = row._replace(**{row._fields[0]: row[i].isoformat()})
return row
def cli():
cli_docs ="""Records: SQL for Humans™
A Kenneth Reitz project.
Usage:
records <query> <format> [-i] [--params <params>...] [--url=<url>]
records (-h | --help)
Options:
-h --help Show this screen.
--url=<url> The database URL to use. Defaults to $DATABASE_URL.
--params Prameterized query. Subsequent arguments are treated as
parameters to the query.
-i --interactive An interactive interpreter.
Supported Formats:
csv, tsv, json, yaml, html, xls, xlsx, dbf, latex, ods
Note: xls, xlsx, dbf, and ods formats are binary, and should only be
used with redirected output e.g. '$ records sql xls > sql.xls'.
Notes:
- While you may specify a Postgres connection string with --url, records
will automatically default to the value of $DATABASE_URL, if available.
- Query is intended to be the path of a SQL file, however a query string
can be provided instead. Use this feature discernfully; it's dangerous.
- Records is intended for report-style exports of database queries, and
has not yet been optimized for extremely large data dumps.
- Interactive mode is experimental and may be removed at any time.
Feedback, as always, is much appreciated! [email protected]
Cake:
✨ 🍰 ✨
"""
supported_formats = 'csv tsv json yaml html xls xlsx dbf latex ods'.split()
# Parse the command-line arguments.
arguments = docopt(cli_docs)
# print arguments
# exit()
# Cleanup docopt parsing errors.
if arguments['--params'] and arguments['<format>'] not in supported_formats:
arguments['<params>'].insert(0, arguments['<format>'])
arguments['<format>'] = None
# Create the Database.
db = Database(arguments['--url'])
query = arguments['<query>']
params = arguments['<params>']
# Execute the query, if it is a found file.
if os.path.isfile(query):
rows = db.query_file(query, params)
# Execute the query, if it appears to be a query string.
elif len(query.split()) > 2:
rows = db.query(query, params)
# Otherwise, say the file wasn't found.
else:
print('The given query could not be found.')
exit(66)
# Interactive mode.
if arguments['--interactive']:
interactive_env = {'db': db, 'rows': rows}
interact(interactive_env, local=interactive_env)
exit()
# Print results in desired format.
if arguments['<format>']:
print(rows.export(arguments['<format>']))
else:
print(rows.dataset)
# Run the CLI when executed directly.
if __name__ == '__main__':
cli()