|
|
|
@ -149,10 +149,10 @@ class DBAdapter:
|
|
|
|
|
presence = self.get_presence(name, domain)
|
|
|
|
|
d = {"name": name, "domain": domain, "artist": artist_ID, "link": link}
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
self.db.commit()
|
|
|
|
@ -176,7 +176,7 @@ class DBAdapter:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
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",
|
|
|
|
|
d)
|
|
|
|
|
rows = self.db_cursor.fetchall()
|
|
|
|
@ -239,6 +239,42 @@ class DBAdapter:
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
"""
|
|
|
|
|
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.
|
|
|
|
|
:param name:
|
|
|
|
|
: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:
|
|
|
|
|
"""
|
|
|
|
|
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:
|
|
|
|
|
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))
|
|
|
|
|
rows = self.db_cursor.fetchall()
|
|
|
|
|
|
|
|
|
@ -444,9 +480,9 @@ class DBAdapter:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
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:
|
|
|
|
|
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()
|
|
|
|
|
new_rows = []
|
|
|
|
@ -455,8 +491,10 @@ class DBAdapter:
|
|
|
|
|
for j in range(len(rows[i])):
|
|
|
|
|
if rows[i][j] is None:
|
|
|
|
|
new_rows[i].append("")
|
|
|
|
|
else:
|
|
|
|
|
elif isinstance(rows[i][j], str):
|
|
|
|
|
new_rows[i].append(rows[i][j].strip())
|
|
|
|
|
else:
|
|
|
|
|
new_rows[i].append(rows[i][j])
|
|
|
|
|
|
|
|
|
|
return new_rows
|
|
|
|
|
|
|
|
|
@ -486,136 +524,176 @@ class DBAdapter:
|
|
|
|
|
aliases = []
|
|
|
|
|
if implications is None:
|
|
|
|
|
implications = []
|
|
|
|
|
d = {"name": name, "description": description, "category": category}
|
|
|
|
|
self.db_cursor.execute("INSERT INTO tag (name, description, category) " +
|
|
|
|
|
"VALUES (LOWER(%(name)s), %(description)s, %(category)s)", d)
|
|
|
|
|
category_id = self.get_category_by_name(category)[0]
|
|
|
|
|
d = {"name": name, "description": description, "category_id": category_id}
|
|
|
|
|
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:
|
|
|
|
|
self.add_alias(name, alias)
|
|
|
|
|
self.add_alias_by_name(name, alias)
|
|
|
|
|
for implicant in implications:
|
|
|
|
|
self.add_implication(name, implicant)
|
|
|
|
|
self.add_implication_by_name(name, implicant)
|
|
|
|
|
|
|
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
|
|
def edit_tag(self, name: str, description: str, aliases: list=None, implications: list=None, category: str = None,
|
|
|
|
|
old_tag: str = None):
|
|
|
|
|
def edit_tag(self, tag_id: int, name: str, description: str, aliases: list = None, implications: list = None,
|
|
|
|
|
category_id: int = None):
|
|
|
|
|
"""
|
|
|
|
|
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 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 implications: list of tag names to be implied by this tag, set to None to omit
|
|
|
|
|
:param category:
|
|
|
|
|
:param old_tag: old tag name from which to transfer all data
|
|
|
|
|
:param category_id:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
name = name.strip().lower()
|
|
|
|
|
d = {
|
|
|
|
|
"id": tag_id,
|
|
|
|
|
"name": name,
|
|
|
|
|
"description": description,
|
|
|
|
|
"alias": aliases,
|
|
|
|
|
"implications": implications,
|
|
|
|
|
"category": category,
|
|
|
|
|
"category_id": category_id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if old_tag is not None and len(old_tag) > 0:
|
|
|
|
|
# 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)
|
|
|
|
|
self.db_cursor.execute("UPDATE tag SET description = %(description)s, category_id = %(category_id)s, name = %(name)s WHERE tag_ID = %(id)s", d)
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
if alias in old_aliases: # is this already set?
|
|
|
|
|
continue
|
|
|
|
|
self.add_alias(name, alias)
|
|
|
|
|
self.add_alias_by_name(name, alias)
|
|
|
|
|
|
|
|
|
|
for old_alias in old_aliases:
|
|
|
|
|
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:
|
|
|
|
|
old_implicants = self.get_tag_implications(name)
|
|
|
|
|
for implicant in implications:
|
|
|
|
|
if implicant in old_implicants: # is this already set?
|
|
|
|
|
continue
|
|
|
|
|
self.add_implication(name, implicant)
|
|
|
|
|
self.add_implication_by_name(name, implicant)
|
|
|
|
|
|
|
|
|
|
for old_implicant in old_implicants:
|
|
|
|
|
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
|
|
|
|
|
:param name:
|
|
|
|
|
:param alias:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
d = {
|
|
|
|
|
"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)
|
|
|
|
|
self.add_alias_by_ID(self.get_tag_ID(name), self.get_tag_ID(alias))
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
:param name:
|
|
|
|
|
:param alias:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
d = {
|
|
|
|
|
"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)
|
|
|
|
|
self.remove_alias_by_ID(self.get_tag_ID(name), self.get_tag_ID(alias))
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
:param name:
|
|
|
|
|
:param implicant:
|
|
|
|
|
: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 = {
|
|
|
|
|
"name": self.get_tag_ID(name),
|
|
|
|
|
"implicant": self.get_tag_ID(implicant)
|
|
|
|
|
"tag": tag,
|
|
|
|
|
"implicant": implicant
|
|
|
|
|
}
|
|
|
|
|
self.db_cursor.execute("INSERT INTO tag_implication (root_tag, implicate) VALUES (%(name)s, %(implicant)s)",
|
|
|
|
|
d)
|
|
|
|
|
self.db_cursor.execute("DELETE FROM tag_implication WHERE root_tag = %(name)s " +
|
|
|
|
|
"and implicate = %(implicant)s", d)
|
|
|
|
|
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
|
|
|
|
|
:param name:
|
|
|
|
|
:param implicant:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
d = {"name": self.get_tag_ID(name),
|
|
|
|
|
"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()
|
|
|
|
|
self.remove_implication_by_ID(self.get_tag_ID(name), self.get_tag_ID(implicant))
|
|
|
|
|
|
|
|
|
|
def remove_tag(self, name: str):
|
|
|
|
|
def remove_tag_by_ID(self, tag: int):
|
|
|
|
|
"""
|
|
|
|
|
Remove the given tag from the database
|
|
|
|
|
:param str:
|
|
|
|
|
:param tag:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
d = {"name": name}
|
|
|
|
|
self.db_cursor.execute("DELETE FROM tag WHERE name = %(name)s", d)
|
|
|
|
|
|
|
|
|
|
d = {"tag": tag}
|
|
|
|
|
self.db_cursor.execute("DELETE FROM tag WHERE tag_id = %(tag)s", d)
|
|
|
|
|
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:
|
|
|
|
|
"""
|
|
|
|
|
Get the tag ID by querying it by its unique name
|
|
|
|
@ -623,14 +701,14 @@ class DBAdapter:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
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 = []
|
|
|
|
|
for row in self.db_cursor.fetchall():
|
|
|
|
|
rows.append(row[0])
|
|
|
|
|
|
|
|
|
|
if len(rows) > 1:
|
|
|
|
|
exit(1) # something went horribly horribly wrong!
|
|
|
|
|
raise Exception("Found multiple tags by the same name!")
|
|
|
|
|
elif len(rows) == 0:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
@ -645,7 +723,7 @@ class DBAdapter:
|
|
|
|
|
:return: Returns the tag's name, description, category, tag ID
|
|
|
|
|
"""
|
|
|
|
|
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 = []
|
|
|
|
|
for row in self.db_cursor.fetchall():
|
|
|
|
@ -669,11 +747,11 @@ class DBAdapter:
|
|
|
|
|
:return: Returns the tag's ID, name, description, category
|
|
|
|
|
"""
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
:param name:
|
|
|
|
@ -690,7 +768,7 @@ class DBAdapter:
|
|
|
|
|
def get_tag_aliases_by_ID(self, tag_ID: int) -> list:
|
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
"""
|
|
|
|
|
marked = []
|
|
|
|
@ -707,7 +785,7 @@ class DBAdapter:
|
|
|
|
|
marked.remove(tag_ID)
|
|
|
|
|
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.
|
|
|
|
|
:param name:
|
|
|
|
@ -826,28 +904,3 @@ class DBAdapter:
|
|
|
|
|
raise Exception("Something went terribly wrong!")
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|