-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_library_db.py
274 lines (236 loc) · 9.93 KB
/
music_library_db.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
import psycopg2
joins = [
{'pair': {'has', 'Tag'},
'id': 'Tag_Id'},
{'pair': {'Track', 'Album'},
'id': 'Album_Id'},
{'pair': {'Album', 'has'},
'id': 'Album_Id'},
{'pair': {'Album', 'Artist'},
'id': 'Artist_Id'}
]
DATABASE_DATA = {'entities': [{'name': 'Artist',
'attributes': [{'name': 'Artist_Name',
'type': 'VARCHAR'},
{'name': 'Artist_Description',
'type': 'VARCHAR'},
{'name': 'Artist_Id',
'type': 'INT'}]},
{'name': 'Album',
'attributes': [{'name': 'Album_Name',
'type': 'VARCHAR'},
{'name': 'Album_Id',
'type': 'INT'},
{'name': 'Year',
'type': 'INT'},
{'name': 'Artist_Id',
'type': 'INT'},
]},
{'name': 'Tag',
'attributes': [{'name': 'Tag_Name',
'type': 'VARCHAR'},
{'name': 'Tag_Description',
'type': 'VARCHAR'},
{'name': 'Tag_Id',
'type': 'INT'}]},
{'name': 'has',
'attributes': [{'name': 'Album_Id',
'type': 'INT'},
{'name': 'Tag_Id',
'type': 'INT'}]},
{'name': 'Track',
'attributes': [{'name': 'Track_Name',
'type': 'VARCHAR'},
{'name': 'Track_Id',
'type': 'INT'},
{'name': 'Rank',
'type': 'INT'},
{'name': 'Duration',
'type': 'INT'},
{'name': 'Album_Id',
'type': 'INT'}]}]}
_SQL_CREATE_TABLE_SCRIPT = """
CREATE TABLE Artist
(
Artist_Name VARCHAR(128) NOT NULL,
Artist_Description VARCHAR(4096) NOT NULL,
Artist_Id SERIAL NOT NULL,
PRIMARY KEY (Artist_Id)
);
CREATE TABLE Album
(
Album_Name VARCHAR(128) NOT NULL,
Album_Id SERIAL NOT NULL,
Year INT NOT NULL,
Artist_Id SERIAL NOT NULL,
PRIMARY KEY (Album_Id),
FOREIGN KEY (Artist_Id) REFERENCES Artist(Artist_Id)
);
CREATE TABLE Tag
(
Tag_Id SERIAL NOT NULL,
Tag_Name VARCHAR(128) NOT NULL,
Tag_Description VARCHAR(4096) NOT NULL,
PRIMARY KEY (Tag_Id)
);
CREATE TABLE has
(
Album_Id SERIAL NOT NULL,
Tag_Id SERIAL NOT NULL,
PRIMARY KEY (Album_Id, Tag_Id),
FOREIGN KEY (Album_Id) REFERENCES Album(Album_Id),
FOREIGN KEY (Tag_Id) REFERENCES Tag(Tag_Id)
);
CREATE TABLE Track
(
Track_Name VARCHAR(128) NOT NULL,
Track_Id SERIAL NOT NULL,
Rank INT NOT NULL,
Duration INT NOT NULL,
Album_Id SERIAL NOT NULL,
PRIMARY KEY (Track_Id),
FOREIGN KEY (Album_Id) REFERENCES Album(Album_Id)
);
"""
class MusicLibraryDatabase:
_SQL_CREATE_SCRIPT = _SQL_CREATE_TABLE_SCRIPT
def __init__(self, user, host, password, database):
self.connector = psycopg2.connect(user=user, host=host, password=password, database=database)
# psycopg2.connect("dbname='db1' user='postgres' host='localhost' password='py'")
self.cursor = self.connector.cursor()
#try:
#self.cursor.execute(self._SQL_CREATE_SCRIPT)
#except:
# pass
#finally:
# pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.connector.commit()
self.cursor.close()
self.connector.close()
@staticmethod
def _get_what_where_query_format(row):
value_list = []
where_s = '('
what_sf = '('
for k, v in row.items():
where_s = where_s + k + ', '
what_sf = what_sf + '%s, '
value_list.append(v)
return where_s[:-2] + ')', what_sf[:-2] + ')', value_list
def _execute(self, query, entity, row):
where_s, what_sf, value_list = self._get_what_where_query_format(row)
self.cursor.execute((query % (entity, where_s, what_sf)), value_list)
def _fetch_all(self):
try:
return self.cursor.fetchall()
except psycopg2.ProgrammingError:
return []
def fetch_all(self):
return self._fetch_all()
def insert(self, into, row):
# self._execute("INSERT INTO %s %s VALUES %s", into, row)
def make_values(values):
# print(values)
return "(%s)" % (', '.join([("'%s'" %
value.replace('"', '\' || CHR(39) || \'').replace("'", ' '))
if isinstance(value, str)
else str(value)
for value in values]))
def make_keys(keys):
return "(%s)" % (', '.join(keys))
keys = [key for key, value in row.items()]
values = [value for key, value in row.items()]
query = "INSERT INTO %s %s VALUES %s" % (into, make_keys(keys), make_values(values))
# print(query)
self.cursor.execute(query)
def delete_all(self, entity):
self.cursor.execute("DELETE FROM %s;" % entity)
def drop_table(self, table):
pass
def drop_db(self):
pass
def fulltext_not_occurred(self, table, text):
"""mysql; not working"""
self.cursor.execute("""SELECT * FROM %s WHERE MATCH(col1, col2)
AGAINST('search terms' IN BOOLEAN MODE)""")
@staticmethod
def _kv_to_sql(k, v):
return ("%s=%s" if isinstance(v, int) else "%s='%s'") % (k, v)
@staticmethod
def _kv_to_sql_with_entity(e, k, v):
cond = (("%s.%s=%s" % (e, k, v)) if isinstance(v, int) else
("%s.%s='%s'" % (e, k, v)) if isinstance(v, str) else
("%s.%s >= %s AND %s.%s <= %s" % (e, k, v['lower'], e, k, v['upper']))
if isinstance(v, dict) else "")
return cond
def _row_to_condition(self, row, op=" OR "):
row = [[k, v] for k, v in row.items()]
condition = op.join(self._kv_to_sql(*kv) for kv in row)
return condition
def delete(self, entity, row):
self._execute("DELETE FROM %s WHERE CustomerName='Alfreds Futterkiste';", entity, row)
pass
def select_all(self, entity):
self.cursor.execute("SELECT * FROM %s;" % entity)
return self._fetch_all()
def select(self, entity, attribute, *values):
self.cursor.execute(("SELECT * FROM %s WHERE %s IN %%s;" % (entity, attribute)), *values)
@staticmethod
def _get_key(entity):
key_attrs = {
'Artist': {'Artist_Id'},
'Album': {'Album_Id'},
'Track': {'Track_Id'},
'Tag': {'Tag_Id'},
'has': {
'Album_Id',
'Tag_Id'
}
}
return key_attrs[entity]
def get_key(self, entity):
return self._get_key(entity)
def _dict_list_to_condition(self, entity, dict_list, op=' AND '):
return op.join(op.join(self._kv_to_sql_with_entity(entity, k, v) for k, v in d.items())
for d in dict_list)
def select_inner_join(self, entity1, entity2, key_attr, entity1_key_val, entity2_key_val):
condition_1 = self._dict_list_to_condition(entity1, entity1_key_val)
condition_2 = self._dict_list_to_condition(entity2, entity2_key_val)
condition = ' and '.join((condition_1, condition_2))
query = "SELECT * FROM %s INNER JOIN %s ON %s.%s = %s.%s WHERE %s;"\
% (entity1, entity2,
entity1, key_attr,
entity2, key_attr,
condition)
print('psql> '+query)
self.cursor.execute(query)
return self._fetch_all()
def fulltext_search_all_match(self, entity, attribute, key):
query = """select * from %s WHERE to_tsvector(%s) @@ to_tsquery('%s');"""
query = query % (entity, attribute, ' & '.join(key.split()))
print('psql> '+query)
self.cursor.execute(query, (entity, attribute, key))
return self._fetch_all()
def fulltext_search_all_match_query_from_plaintext(self, entity, attribute, key):
query = """select * from %s WHERE to_tsvector(%s) @@ plainto_tsquery('%s');"""
query = query % (entity, attribute, key)
print('psql> '+query)
self.cursor.execute(query, (entity, attribute, key))
return self._fetch_all()
def fulltext_search_one_not(self, entity, attribute, key):
query = """select * from %s WHERE to_tsvector(%s) @@ to_tsquery('!%s');"""
query = query % (entity, attribute, key)
print('psql> '+query)
self.cursor.execute(query, (entity, attribute, key))
return self._fetch_all()
def update(self, entity, key, row):
query = "UPDATE %s SET %s WHERE %s;"
assignment = self._row_to_condition(row, op=", ")
condition = self._row_to_condition(key, op=' AND ')
query = query % (entity, assignment, condition)
print('psql> '+query)
self.cursor.execute(query)
return self._fetch_all()