forked from gvanrossum/patma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_tutorial.py
208 lines (170 loc) · 6.96 KB
/
sqlite_tutorial.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
import argparse
from dataclasses import dataclass
import os
import sqlite3
import tempfile
from tempfile import NamedTemporaryFile, TemporaryDirectory
from urllib import request
from zipfile import ZipFile
# The following classes are used as signals from within the
# db query method
class FileClosed:
pass
class QueryIssue:
def __init__(self, query):
self.query = query
def __str__(self):
return f"There was a problem executing {self.query}"
class InvalidTable:
def __str__(self):
return "The selected table is not part of this demo"
class Results:
__match_args__ = ("size", "results")
def __init__(self, results):
self.results = results
@property
def size(self):
return len(self.results)
# These classes represent results of a database query
@dataclass
class Artist:
name: str
def __str__(self):
return self.name
@dataclass
class Album:
name: str
artist: str
def __str__(self):
return f"{self.name} by {self.artist}"
@dataclass
class Track:
name: str
album: str
artist: str
def __str__(self):
return f"{self.name} on {self.album} by {self.artist}"
# Temporary database accessor
class TempDB:
"""This class is a wrapper around an sqlite database. It fetches the
database from a remote source and extracts it into a temporary file.
A connection to this database is then created and stored.
"""
file_url = ("https://cdn.sqlitetutorial.net/wp-content/uploads/2018/03/"
"chinook.zip")
def __init__(self, temp_dir):
self._temp_dir = temp_dir
self._populate_db()
self._connect()
self._valid_tables = ("tracks", "albums", "artists")
def _populate_db(self):
# fetch the database into a local file
with tempfile.NamedTemporaryFile() as tmp_zip_file:
request.urlretrieve(self.file_url, filename=tmp_zip_file.name)
with ZipFile(tmp_zip_file, 'r') as zip_obj:
zip_obj.extract('chinook.db', self._temp_dir)
def _connect(self):
# build the connections to the database
self._connection = sqlite3.connect(os.path.join(self._temp_dir,
"chinook.db"))
self._cursor = self._connection.cursor()
def execute_query(self, query: str):
"""Executes a query against the stored db connection. This method
supports a single query string which it used to generate resulting
rows which are then matched against known row patterns. The results
are used to construct new objects and are returned inside a results
object.
This function will never raise a database exception. All returns are
instances of signalling classes. Any exceptions will be truly
exceptional behavior.
Additionally the method checks against a few other conditions,
and returns various signals if appropriate.
"""
if not os.path.exists(self._temp_dir):
# Verify the temp directory has not been destroyed
return FileClosed()
if not sum((table in query for table in self._valid_tables)):
# only query against a sub set of tables
return InvalidTable()
results = []
try:
for row in self._cursor.execute(query):
match row:
# this represents rows from the artist table
case (_, artist):
results.append(Artist(artist))
# This case is rows from album joined with artist
case (_, title, _, _, artist):
results.append(Album(title, artist))
# This case is rows from track joined with album
# joined with artist
case (_, name, *_, album, _, _, artist):
results.append(Track(name, album, artist))
return Results(results)
except sqlite3.Error as e:
# Return a query signal if there was a failure
return QueryIssue(query)
class Runner:
def __init__(self, args, temp_dir):
self.args = args
self.db = TempDB(temp_dir)
self._recursion = 0
def _run_impl(self, query):
# Run the query and respond appropriately to the returned signal
match self.db.execute_query(query):
case FileClosed(_):
# The file was closed some how, create a new temp file and
# recreate the db and re-execute
with TemporaryDirectory as temp_dir:
self.db = TempDB(temp_dir)
self._recursion += 1
if self._recursion < 2:
self._run_impl(query)
self._recursion = 0
else:
print("Too many layers of recursion, problem with db object")
return QueryIssue(query)
case InvalidTable(return_value):
print(return_value)
case Results(0, _):
print("There are no results to return")
case Results(_, results):
for r in results:
print(r)
case QueryIssue(issue):
print(issue)
def run(self):
# Generate the query and call the implementation
if self.args.tracks is not None:
if self.args.tracks:
clause = f"artists.Name = '{self.args.tracks}'"
else:
clause = 1
self._run_impl("select * from tracks join albums "
"on tracks.albumid = albums.albumid join artists "
"on albums.artistid = artists.artistid where "
f"{clause};")
if self.args.artists:
self._run_impl("select * from artists;")
if self.args.albums is not None:
if self.args.albums:
clause = f"artists.Name = '{self.args.albums}'"
else:
clause = 1
self._run_impl("select * from albums join artists "
"on albums.artistid = artists.artistid where "
f"{clause};")
if __name__ == "__main__":
msg = "Lookup info from a collection of music"
parser = argparse.ArgumentParser(description=msg)
parser.add_argument("--tracks", metavar="artist",
help='List all the tracks from an artist in the'
' database, use "" for all artists')
parser.add_argument("--artists", action="store_const", const=True,
help="List all the artists in the database")
parser.add_argument("--albums", metavar="artist",
help='List the albums from an artist in the database,'
' use "" for all artists')
args = parser.parse_args()
with tempfile.TemporaryDirectory() as temp_dir:
Runner(args, temp_dir).run()