Skip to content

Commit

Permalink
Dealing with namespaces in update attributes function
Browse files Browse the repository at this point in the history
  • Loading branch information
npalacioescat committed Sep 17, 2024
1 parent 683d014 commit 8467591
Showing 1 changed file with 54 additions and 31 deletions.
85 changes: 54 additions & 31 deletions cache_manager/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,40 +1103,63 @@ def update(

_log(f'Updating attributes in attr_{actual_typ}')

for k, v in update.get('attrs', {}).items():

typ = self._sqlite_type(v)

if k not in main_fields and typ == actual_typ.upper():

# Updating current attr
qval = self._quotes(v, typ)
val = f'value = {qval}'
name_where = where + f' AND name = {self._quotes(k)}'
q = f'UPDATE attr_{actual_typ} SET {val} {name_where}'
self._execute(q)

# Adding new attr
q = f'SELECT id FROM attr_{actual_typ} {name_where}'
self._execute(q)
existing_attr_ids = set(i[0] for i in self.cur.fetchall())
useattrs = [
(
0,
self._quotes(group)
if isinstance(vals, dict)
else 'NULL',
k,
v
)
for group, vals in update.get('attrs', {}).items()
for k, v in (
vals
if isinstance(vals, dict) else
{group: vals}
).items()
if (
self._sqlite_type(v) == actual_typ.upper()
and k not in main_fields
)
]

new_attr_ids = set(ids) - existing_attr_ids
for keyvar, group, k, v in useattrs:

if not new_attr_ids:
continue
typ = self._sqlite_type(v)

# Insert new attr to DB
new_values = ",".join(
f'{i}, NULL, 0, "{k}", {qval}'
for i in new_attr_ids
)
new_q = (
f'INSERT INTO attr_{actual_typ} '
'(id, namespace, keyvar, name, value) '
f'VALUES ({new_values})'
)
self._execute(new_q)
# Updating current attr
qval = self._quotes(v, typ)
val = f'value = {qval}'
name_where = (
where +
f' AND name = "{k}" AND namespace = {group}'
)
q = f'UPDATE attr_{actual_typ} SET {val} {name_where}'
self._execute(q)

# Adding new attr
q = f'SELECT id FROM attr_{actual_typ} {name_where}'
self._execute(q)
existing_attr_ids = set(i[0] for i in self.cur.fetchall())

new_attr_ids = set(ids) - existing_attr_ids

if not new_attr_ids:

continue

# Insert new attr to DB
new_values = ",".join(
f'{i}, {group}, {keyvar}, "{k}", {qval}'
for i in new_attr_ids
)
new_q = (
f'INSERT INTO attr_{actual_typ} '
'(id, namespace, keyvar, name, value) '
f'VALUES ({new_values})'
)
self._execute(new_q)

_log(f'Finished updating attributes')

Expand Down

0 comments on commit 8467591

Please sign in to comment.