Updated DB backend for new schemas

Update the DB backend to work with an updated schema.
Also fixed a few UI bugs caused by that.
dev
Peery 3 years ago
parent 99069ba77d
commit b5e226bc3a

@ -131,7 +131,7 @@ class ArtNetManager:
if len(result) != 0: # tag is implied by some other tag if len(result) != 0: # tag is implied by some other tag
continue continue
result = self.db_connection.get_tag_aliases(tag) result = self.db_connection.get_tag_aliases_by_name(tag)
if len(result) != 0: # tag is already tagged by an alias if len(result) != 0: # tag is already tagged by an alias
continue continue

@ -149,10 +149,10 @@ class DBAdapter:
presence = self.get_presence(name, domain) presence = self.get_presence(name, domain)
d = {"name": name, "domain": domain, "artist": artist_ID, "link": link} d = {"name": name, "domain": domain, "artist": artist_ID, "link": link}
if len(presence) == 0: # presence does not exist yet if len(presence) == 0: # presence does not exist yet
self.db_cursor.execute("INSERT INTO presence (name, domain, artist, link) " + self.db_cursor.execute("INSERT INTO presence (name, domain, artist_ID, link) " +
"VALUES (%(name)s, %(domain)s, %(artist)s, %(link)s)", d) "VALUES (%(name)s, %(domain)s, %(artist)s, %(link)s)", d)
else: # presence exists, update it else: # presence exists, update it
self.db_cursor.execute("UPDATE presence SET artist = %(artist)s, link = %(link)s" + self.db_cursor.execute("UPDATE presence SET artist_ID = %(artist)s, link = %(link)s" +
"WHERE name = %(name)s and domain = %(domain)s", d) "WHERE name = %(name)s and domain = %(domain)s", d)
self.db.commit() self.db.commit()
@ -176,7 +176,7 @@ class DBAdapter:
:return: :return:
""" """
d = {"name": name, "domain": domain} d = {"name": name, "domain": domain}
self.db_cursor.execute("SELECT name, domain, artist, link FROM presence " + self.db_cursor.execute("SELECT name, domain, artist_ID, link FROM presence " +
"WHERE name = %(name)s and domain = %(domain)s", "WHERE name = %(name)s and domain = %(domain)s",
d) d)
rows = self.db_cursor.fetchall() rows = self.db_cursor.fetchall()
@ -239,6 +239,42 @@ class DBAdapter:
self.db.commit() self.db.commit()
def get_category_by_ID(self, id: int):
"""
Queries the database for the category by its ID
:param id:
:return: (id, "name")
"""
d = {"id": id}
self.db_cursor.execute("SELECT category_id, name FROM tag_category WHERE category_id = %(id)s", d)
r = self.db_cursor.fetchall()
if len(r) == 0:
return None
nr = []
for i in range(len(r[0])):
if isinstance(r[0][i], str):
nr.append(r[0][i].strip())
else:
nr.append(r[0][i])
return nr
def get_category_by_name(self, name: str):
"""
Queries the database for the category by its name
:return:
"""
d = {"name": name}
self.db_cursor.execute("SELECT category_id, name FROM tag_category WHERE name = %(name)s", d)
r = self.db_cursor.fetchall()
if len(r) == 0:
return None
return r[0]
def get_artist(self, ID: int): def get_artist(self, ID: int):
""" """
Queries the database for the artist (not presence) and returns the result Queries the database for the artist (not presence) and returns the result
@ -381,13 +417,13 @@ class DBAdapter:
Search a list of presences fitting the (name, domain) fuzzy. Search a list of presences fitting the (name, domain) fuzzy.
:param name: :param name:
:param domain: :param domain:
:param all_if_empty: :param all_if_empty: return all presences (or a large selection) if an empty name and domain is given
:return: :return:
""" """
if all_if_empty and (name is None or len(name) == 0) and (domain is None or len(domain) == 0): if all_if_empty and (name is None or len(name) == 0) and (domain is None or len(domain) == 0):
self.db_cursor.execute("SELECT name, domain, artist FROM presence") self.db_cursor.execute("SELECT name, domain, artist_id FROM presence")
else: else:
self.db_cursor.execute("SELECT name, domain, artist FROM presence WHERE LOWER(name) LIKE LOWER('%{0}%')".format(name) + self.db_cursor.execute("SELECT name, domain, artist_id FROM presence WHERE LOWER(name) LIKE LOWER('%{0}%')".format(name) +
" AND domain LIKE '%{0}%'".format(domain)) " AND domain LIKE '%{0}%'".format(domain))
rows = self.db_cursor.fetchall() rows = self.db_cursor.fetchall()
@ -444,9 +480,9 @@ class DBAdapter:
:return: :return:
""" """
if all_if_empty and len(name) == 0: if all_if_empty and len(name) == 0:
self.db_cursor.execute("SELECT name, description, category FROM tag") self.db_cursor.execute("SELECT name, description, category_id FROM tag")
else: else:
self.db_cursor.execute("SELECT name, description, category FROM tag WHERE LOWER(name) LIKE LOWER('%{0}%')".format(name)) self.db_cursor.execute("SELECT name, description, category_id FROM tag WHERE LOWER(name) LIKE LOWER('%{0}%')".format(name))
rows = self.db_cursor.fetchall() rows = self.db_cursor.fetchall()
new_rows = [] new_rows = []
@ -455,8 +491,10 @@ class DBAdapter:
for j in range(len(rows[i])): for j in range(len(rows[i])):
if rows[i][j] is None: if rows[i][j] is None:
new_rows[i].append("") new_rows[i].append("")
else: elif isinstance(rows[i][j], str):
new_rows[i].append(rows[i][j].strip()) new_rows[i].append(rows[i][j].strip())
else:
new_rows[i].append(rows[i][j])
return new_rows return new_rows
@ -486,136 +524,176 @@ class DBAdapter:
aliases = [] aliases = []
if implications is None: if implications is None:
implications = [] implications = []
d = {"name": name, "description": description, "category": category} category_id = self.get_category_by_name(category)[0]
self.db_cursor.execute("INSERT INTO tag (name, description, category) " + d = {"name": name, "description": description, "category_id": category_id}
"VALUES (LOWER(%(name)s), %(description)s, %(category)s)", d) self.db_cursor.execute("INSERT INTO tag (name, description, category_id) " +
"VALUES (LOWER(%(name)s), %(description)s, %(category_id)s)", d)
for alias in aliases: for alias in aliases:
self.add_alias(name, alias) self.add_alias_by_name(name, alias)
for implicant in implications: for implicant in implications:
self.add_implication(name, implicant) self.add_implication_by_name(name, implicant)
self.db.commit() self.db.commit()
def edit_tag(self, name: str, description: str, aliases: list=None, implications: list=None, category: str = None, def edit_tag(self, tag_id: int, name: str, description: str, aliases: list = None, implications: list = None,
old_tag: str = None): category_id: int = None):
""" """
Edit a tag with the new given data Edit a tag with the new given data
:param tag_id: tag id to uniquely identify the tag
:param name: unique tag name to apply edits to (distinct from old_tag) :param name: unique tag name to apply edits to (distinct from old_tag)
:param description: new description to apply, set to None to omit :param description: new description to apply, set to None to omit
:param aliases: list of tag names to be the alias of this tag, set to None to omit :param aliases: list of tag names to be the alias of this tag, set to None to omit
:param implications: list of tag names to be implied by this tag, set to None to omit :param implications: list of tag names to be implied by this tag, set to None to omit
:param category: :param category_id:
:param old_tag: old tag name from which to transfer all data
:return: :return:
""" """
name = name.strip().lower() name = name.strip().lower()
d = { d = {
"id": tag_id,
"name": name, "name": name,
"description": description, "description": description,
"alias": aliases, "alias": aliases,
"implications": implications, "implications": implications,
"category": category, "category_id": category_id,
} }
if old_tag is not None and len(old_tag) > 0: self.db_cursor.execute("UPDATE tag SET description = %(description)s, category_id = %(category_id)s, name = %(name)s WHERE tag_ID = %(id)s", d)
# tag name changed, need to transfer all data and remove old tag
old_tag = self.get_tag_by_name(old_tag)
d["ID"] = old_tag[0][-1]
self.db_cursor.execute("UPDATE tag SET name = %(name)s WHERE tag_ID = %(ID)s", d)
if description is not None:
self.db_cursor.execute("UPDATE tag SET description = %(description)s, category = %(category)s " +
"WHERE name = %(name)s", d)
if aliases is not None: if aliases is not None:
old_aliases = self.get_tag_aliases(name) old_aliases = self.get_tag_aliases_by_ID(tag_id)
for alias in aliases: for alias in aliases:
if alias in old_aliases: # is this already set? if alias in old_aliases: # is this already set?
continue continue
self.add_alias(name, alias) self.add_alias_by_name(name, alias)
for old_alias in old_aliases: for old_alias in old_aliases:
if old_alias not in aliases: # got to delete an alias? if old_alias not in aliases: # got to delete an alias?
self.remove_alias(name, old_alias) self.remove_alias_by_name(name, old_alias)
if implications is not None: if implications is not None:
old_implicants = self.get_tag_implications(name) old_implicants = self.get_tag_implications(name)
for implicant in implications: for implicant in implications:
if implicant in old_implicants: # is this already set? if implicant in old_implicants: # is this already set?
continue continue
self.add_implication(name, implicant) self.add_implication_by_name(name, implicant)
for old_implicant in old_implicants: for old_implicant in old_implicants:
if old_implicant not in implications: # got to delete an implicant? if old_implicant not in implications: # got to delete an implicant?
self.remove_implication(name, old_implicant) self.remove_implication_by_name(name, old_implicant)
def add_alias(self, name: str, alias: str): def add_alias_by_name(self, name: str, alias: str):
""" """
Add the alias pair to the database Add the alias pair to the database
:param name: :param name:
:param alias: :param alias:
:return: :return:
""" """
d = { self.add_alias_by_ID(self.get_tag_ID(name), self.get_tag_ID(alias))
"name": self.get_tag_ID(name),
"alias": self.get_tag_ID(alias)
}
self.db_cursor.execute("INSERT INTO tag_alias (tag1, tag2) VALUES (%(name)s, %(alias)s)", d)
def remove_alias(self, name: str, alias: str): def add_alias_by_ID(self, tag1: int, tag2: int):
"""
Add the alias pair to the database
:return:
"""
d = {
"tag1": tag1,
"tag2": tag2
}
self.db_cursor.execute("INSERT INTO tag_alias (tag1, tag2) VALUES (%(tag1)s, %(tag2)s)", d)
self.db.commit()
def remove_alias_by_ID(self, tag1: int, tag2: int):
"""
Remove alias pair from the database
:param tag1:
:param tag2:
:return:
"""
d = {
"tag1": tag1,
"tag2": tag2
}
self.db_cursor.execute("DELETE FROM tag_alias WHERE (tag1 = %(tag1)s and tag2 = %(tag2)s) or " +
"(tag1 = %(tag2)s and tag2 = %(tag1)s)", d)
self.db.commit()
def remove_alias_by_name(self, name: str, alias: str):
""" """
Remove alias pair from the database Remove alias pair from the database
:param name: :param name:
:param alias: :param alias:
:return: :return:
""" """
d = { self.remove_alias_by_ID(self.get_tag_ID(name), self.get_tag_ID(alias))
"ID": self.get_tag_ID(name),
"alias": self.get_tag_ID(alias)
}
self.db_cursor.execute("DELETE FROM tag_alias WHERE (tag1 = %(ID)s and tag2 = %(alias)s) " +
"or (tag1 = %(alias)s and tag2 = %(ID)s)", d)
def add_implication(self, name: str, implicant: str): def add_implication_by_ID(self, tag_id: int, implicant: int):
"""
Add the implication to the database
:param tag_id:
:param implicant:
:return:
"""
d = {
"tag": tag_id,
"implicant": implicant
}
self.db_cursor.execute("INSERT INTO tag_implication (root_tag, implicate) VALUES (%(tag)s, %(implicant)s)",
d)
self.db.commit()
def add_implication_by_name(self, name: str, implicant: str):
""" """
Add the implication to the database Add the implication to the database
:param name: :param name:
:param implicant: :param implicant:
:return: :return:
""" """
self.add_implication_by_ID(self.get_tag_ID(name), self.get_tag_ID(implicant))
def remove_implication_by_ID(self, tag: int, implicant: int):
"""
Remove the implication pair from the database
:param tag:
:param implicant:
:return:
"""
d = { d = {
"name": self.get_tag_ID(name), "tag": tag,
"implicant": self.get_tag_ID(implicant) "implicant": implicant
} }
self.db_cursor.execute("INSERT INTO tag_implication (root_tag, implicate) VALUES (%(name)s, %(implicant)s)", self.db_cursor.execute("DELETE FROM tag_implication WHERE root_tag = %(name)s " +
d) "and implicate = %(implicant)s", d)
self.db.commit() self.db.commit()
def remove_implication(self, name: str, implicant: str): def remove_implication_by_name(self, name: str, implicant: str):
""" """
Remove the implication pair from the database Remove the implication pair from the database
:param name: :param name:
:param implicant: :param implicant:
:return: :return:
""" """
d = {"name": self.get_tag_ID(name), self.remove_implication_by_ID(self.get_tag_ID(name), self.get_tag_ID(implicant))
"implicant": self.get_tag_ID(implicant)}
self.db_cursor.execute("DELETE FROM tag_implication WHERE root_tag = %(name)s " +
"and implicate = %(implicant)s", d)
self.db.commit()
def remove_tag(self, name: str): def remove_tag_by_ID(self, tag: int):
""" """
Remove the given tag from the database Remove the given tag from the database
:param str: :param tag:
:return: :return:
""" """
d = {"name": name} d = {"tag": tag}
self.db_cursor.execute("DELETE FROM tag WHERE name = %(name)s", d) self.db_cursor.execute("DELETE FROM tag WHERE tag_id = %(tag)s", d)
self.db.commit() self.db.commit()
def remove_tag_by_name(self, name: str):
"""
Remove the given tag from the database
:param name:
:return:
"""
self.remove_tag_by_ID(self.get_tag_ID(name))
def get_tag_ID(self, name: str) -> int: def get_tag_ID(self, name: str) -> int:
""" """
Get the tag ID by querying it by its unique name Get the tag ID by querying it by its unique name
@ -623,14 +701,14 @@ class DBAdapter:
:return: :return:
""" """
d = {"name": name} d = {"name": name}
self.db_cursor.execute("SELECT tag_ID, name FROM tag where LOWER(name) = LOWER(%(name)s)", d) self.db_cursor.execute("SELECT tag_ID FROM tag where LOWER(name) = LOWER(%(name)s)", d)
rows = [] rows = []
for row in self.db_cursor.fetchall(): for row in self.db_cursor.fetchall():
rows.append(row[0]) rows.append(row[0])
if len(rows) > 1: if len(rows) > 1:
exit(1) # something went horribly horribly wrong! raise Exception("Found multiple tags by the same name!")
elif len(rows) == 0: elif len(rows) == 0:
return None return None
@ -645,7 +723,7 @@ class DBAdapter:
:return: Returns the tag's name, description, category, tag ID :return: Returns the tag's name, description, category, tag ID
""" """
d = {"name": name} d = {"name": name}
self.db_cursor.execute("SELECT name, description, category, tag_ID FROM tag where LOWER(name) = LOWER(%(name)s)", d) self.db_cursor.execute("SELECT name, description, category_id, tag_ID FROM tag where LOWER(name) = LOWER(%(name)s)", d)
rows = [] rows = []
for row in self.db_cursor.fetchall(): for row in self.db_cursor.fetchall():
@ -669,11 +747,11 @@ class DBAdapter:
:return: Returns the tag's ID, name, description, category :return: Returns the tag's ID, name, description, category
""" """
d = {"ID": ID} d = {"ID": ID}
self.db_cursor.execute("SELECT tag_ID, name, description, category FROM tag where tag_ID = %(ID)s", d) self.db_cursor.execute("SELECT tag_ID, name, description, category_id FROM tag where tag_ID = %(ID)s", d)
return self.db_cursor.fetchall() return self.db_cursor.fetchall()
def get_tag_aliases(self, name: str) -> list: def get_tag_aliases_by_name(self, name: str) -> list:
""" """
Search for the tag's aliases and the tag's aliases Search for the tag's aliases and the tag's aliases
:param name: :param name:
@ -690,7 +768,7 @@ class DBAdapter:
def get_tag_aliases_by_ID(self, tag_ID: int) -> list: def get_tag_aliases_by_ID(self, tag_ID: int) -> list:
""" """
Search for the tag's aliases and the tag's aliases Search for the tag's aliases and the tag's aliases
:param name: :param tag_ID:
:return: List of tag IDs that are aliases of this one :return: List of tag IDs that are aliases of this one
""" """
marked = [] marked = []
@ -707,7 +785,7 @@ class DBAdapter:
marked.remove(tag_ID) marked.remove(tag_ID)
return marked return marked
def get_all_tag_implications(self, name: str) -> list: def get_all_tag_implications_by_name(self, name: str) -> list:
""" """
Search for the tag's implications and those that are implied by them. Search for the tag's implications and those that are implied by them.
:param name: :param name:
@ -826,28 +904,3 @@ class DBAdapter:
raise Exception("Something went terribly wrong!") raise Exception("Something went terribly wrong!")
return r return r
if __name__ == "__main__":
db = DBAdapter(user="artnet_editor", password="G606Rm9sFEXe6wfTLxVu",
database="artnet", host="localhost", port=5432)
search = 'Ajin/66699b4e41f533982db1766e8f72494a.gif2222'
print("Art search result:", search, db.get_art_by_path('Ajin/66699b4e41f533982db1766e8f72494a.gif'))
search = "tag"
print("Fuzzy Search Result:", search, db.search_fuzzy_tag(search))
search = "tag1"
print("Search Result:", search, db.get_tag_by_name(search))
search = "tag1"
print("Alias Search Result:", search, db.get_tag_aliases(search))
search = "tag1"
print("Implication Search Result:", search, db.get_tag_implications(search))
target = "tag1"
db.edit_tag(target, "new description! ;D", aliases=["tag1_alias"], implications=["tag1_implicated"])
print("Editing:", target, db.get_tag_by_name(target), db.get_tag_aliases(target), db.get_tag_implications(target))

@ -72,6 +72,6 @@ class TagSelectDialog(QtWidgets.QDialog):
"description": tag[1], "description": tag[1],
"aliases": self.parent.get_tag_aliases(tag[0]), "aliases": self.parent.get_tag_aliases(tag[0]),
"implications": self.parent.get_tag_implications(tag[0]), "implications": self.parent.get_tag_implications(tag[0]),
"category": tag[2] "category_id": tag[2]
} }
return tag_data return tag_data

@ -396,7 +396,7 @@ class Window(QtWidgets.QMainWindow):
:param name: :param name:
:return: :return:
""" """
return self.__main.db_connection.get_tag_aliases(name) return self.__main.db_connection.get_tag_aliases_by_name(name)
def get_tag_implications(self, name: str) -> list: def get_tag_implications(self, name: str) -> list:
""" """
@ -465,12 +465,12 @@ class Window(QtWidgets.QMainWindow):
implied_tags = [] implied_tags = []
for x in self.curr_tags: for x in self.curr_tags:
# collect all implied tags into a list # collect all implied tags into a list
implied_tags += self.__main.db_connection.get_all_tag_implications(x) implied_tags += self.__main.db_connection.get_all_tag_implications_by_name(x)
self.set_implied_list(implied_tags) self.set_implied_list(implied_tags)
self.curr_tag_aliases = list() self.curr_tag_aliases = list()
for tag in tags+implied_tags: for tag in tags+implied_tags:
self.curr_tag_aliases += self.__main.db_connection.get_tag_aliases(tag) self.curr_tag_aliases += self.__main.db_connection.get_tag_aliases_by_name(tag)
self.data_changed = True self.data_changed = True
@ -815,7 +815,7 @@ class Window(QtWidgets.QMainWindow):
if confirmation_reply == QtWidgets.QMessageBox.No: if confirmation_reply == QtWidgets.QMessageBox.No:
return return
self.__main.db_connection.remove_tag(tag["name"]) self.__main.db_connection.remove_tag_by_name(tag["name"])
self.on_tag_search_change() self.on_tag_search_change()
def force_edit_tag_dialog(self, name: str): def force_edit_tag_dialog(self, name: str):
@ -858,7 +858,7 @@ class Window(QtWidgets.QMainWindow):
if tag is None or len(tag) == 0: if tag is None or len(tag) == 0:
return return
tag['aliases'] = self.__main.db_connection.get_tag_aliases(tag["name"]) tag['aliases'] = self.__main.db_connection.get_tag_aliases_by_name(tag["name"])
tag['implications'] = self.__main.db_connection.get_tag_implications(tag["name"]) tag['implications'] = self.__main.db_connection.get_tag_implications(tag["name"])
edit_dialog = TagModifyDialog(self, create_tag=False) edit_dialog = TagModifyDialog(self, create_tag=False)
@ -868,7 +868,7 @@ class Window(QtWidgets.QMainWindow):
edit_dialog.alias_selection = tag["aliases"] edit_dialog.alias_selection = tag["aliases"]
edit_dialog.set_selected_implicated_tags(tag["implications"], set_checked=True) edit_dialog.set_selected_implicated_tags(tag["implications"], set_checked=True)
edit_dialog.implication_selection = tag["implications"] edit_dialog.implication_selection = tag["implications"]
edit_dialog.category_selection = tag["category"] edit_dialog.category_selection = self.__main.db_connection.get_category_by_ID(tag["category_id"])[1]
edit_dialog.set_all_categories() edit_dialog.set_all_categories()
tag_data = edit_dialog.exec_() tag_data = edit_dialog.exec_()
@ -879,9 +879,10 @@ class Window(QtWidgets.QMainWindow):
if "old_tag_name" not in tag_data.keys(): if "old_tag_name" not in tag_data.keys():
tag_data["old_tag_name"] = None tag_data["old_tag_name"] = None
self.__main.db_connection.edit_tag(name=tag_data["name"], description=tag_data["description"], self.__main.db_connection.edit_tag(tag_id=tag["ID"],
name=tag_data["name"], description=tag_data["description"],
aliases=tag_data["aliases"], implications=tag_data["implications"], aliases=tag_data["aliases"], implications=tag_data["implications"],
category=tag_data["category"], old_tag=tag_data["old_tag_name"]) category_id=self.__main.db_connection.get_category_by_name(tag_data["category"])[0])
self.on_tag_search_change() self.on_tag_search_change()
def on_tag_search_item_changed(self, item: QStandardItem): def on_tag_search_item_changed(self, item: QStandardItem):

Loading…
Cancel
Save