Skip to content
Snippets Groups Projects

Retrieve children elements from SQLite cache in list_element_children

Merged Eva Bardou requested to merge use-local-cache into master
All threads resolved!
4 files
+ 178
3
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 33
0
@@ -21,6 +21,13 @@ CachedElement = namedtuple(
)
def convert_table_tuple(table):
if table == "elements":
return CachedElement
else:
raise NotImplementedError
class LocalDB(object):
def __init__(self, path):
self.db = sqlite3.connect(path)
@@ -41,3 +48,29 @@ class LocalDB(object):
f"INSERT INTO {table} ({columns}) VALUES ({placeholders})", values
)
self.db.commit()
def fetch(self, table, where=[]):
"""
where parameter is a list containing 3-values tuples defining an SQL WHERE condition.
e.g: where=[("id", "LIKE", "%0000%"), ("id", "NOT LIKE", "%1111%")]
stands for "WHERE id LIKE '%0000%' AND id NOT LIKE '%1111%'" in SQL.
This method only supports 'AND' SQL conditions.
"""
sql = f"SELECT * FROM {table}"
if where:
assert isinstance(where, list), "where should be a list"
assert all(
isinstance(condition, tuple) and len(condition) == 3
for condition in where
), "where conditions should be tuples of 3 values"
sql += " WHERE "
sql += " AND ".join(
[f"{field} {operator} (?)" for field, operator, _ in where]
)
self.cursor.execute(sql, [value for _, _, value in where])
tuple_type = convert_table_tuple(table)
return [tuple_type(**dict(row)) for row in self.cursor.fetchall()]
Loading