Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_delete(db):
db.update(delete('int'), where('char') == 'a')
assert 'int' not in db.get(where('char') == 'a')
table_name (str): Name of the table to operate on. See :any:`AbstractDatabase.table`.
match_any (bool): Only applies if `keys` is a dictionary. If True then any key
in `keys` may match or if False then all keys in `keys` must match.
Raises:
ValueError: ``bool(keys) == False`` or invalid value for `keys`.
"""
table = self.table(table_name)
if isinstance(keys, self.Record.eid_type):
for field in fields:
#LOGGER.debug("%s: unset(%s, eid=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=[keys])
elif isinstance(keys, dict):
for field in fields:
#LOGGER.debug("%s: unset(%s, keys=%r)", table_name, field, keys)
table.update(operations.delete(field), self._query(keys, match_any))
elif isinstance(keys, (list, tuple)):
for field in fields:
#LOGGER.debug("%s: unset(%s, eids=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=keys)
else:
raise ValueError(keys)
Args:
fields (list): Names of fields to remove from matching records.
keys: Fields or element identifiers to match.
table_name (str): Name of the table to operate on. See :any:`AbstractDatabase.table`.
match_any (bool): Only applies if `keys` is a dictionary. If True then any key
in `keys` may match or if False then all keys in `keys` must match.
Raises:
ValueError: ``bool(keys) == False`` or invalid value for `keys`.
"""
table = self.table(table_name)
if isinstance(keys, self.Record.eid_type):
for field in fields:
#LOGGER.debug("%s: unset(%s, eid=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=[keys])
elif isinstance(keys, dict):
for field in fields:
#LOGGER.debug("%s: unset(%s, keys=%r)", table_name, field, keys)
table.update(operations.delete(field), self._query(keys, match_any))
elif isinstance(keys, (list, tuple)):
for field in fields:
#LOGGER.debug("%s: unset(%s, eids=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=keys)
else:
raise ValueError(keys)
Args:
fields (list): Names of fields to remove from matching records.
keys: Fields or element identifiers to match.
table_name (str): Name of the table to operate on. See :any:`AbstractDatabase.table`.
match_any (bool): Only applies if `keys` is a dictionary. If True then any key
in `keys` may match or if False then all keys in `keys` must match.
Raises:
ValueError: ``bool(keys) == False`` or invaild value for `keys`.
"""
table = self.table(table_name)
if isinstance(keys, self.Record.eid_type):
for field in fields:
LOGGER.debug("%s: unset(%s, eid=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=[keys])
elif isinstance(keys, dict):
for field in fields:
LOGGER.debug("%s: unset(%s, keys=%r)", table_name, field, keys)
table.update(operations.delete(field), self._query(keys, match_any))
elif isinstance(keys, (list, tuple)):
for field in fields:
LOGGER.debug("%s: unset(%s, eids=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=keys)
else:
raise ValueError(keys)
def upsert(self, note, cond):
orig = self.select(cond)
if orig:
#for key in (set(note.keys()) - set(orig.keys())):
# self.db.table('notes').update(delete(key), cond)
for key in (set(orig.keys()) - set(note.keys())):
self.db.table('notes').update(delete(key), doc_ids=[orig.doc_id])
return self.db.table('notes').upsert(note, cond)
fields (list): Fields to unset.
keys: Fields or element identifiers to match.
match_any (bool): If True then any key may match or if False then all keys must match.
"""
table = self.database.table(table_name)
try:
eid = int(keys)
except TypeError:
if isinstance(keys, list):
LOGGER.debug("%s: unset(%r, eids=%r)", table_name, fields, keys)
for field in fields:
table.update(tinydb.operations.delete(field), eids=keys)
elif isinstance(keys, dict):
LOGGER.debug("%s: unset(%r, keys=%r)", table_name, fields, keys)
for field in fields:
table.update(tinydb.operations.delete(field), self._query(keys, match_any))
else:
LOGGER.debug("%s: unset(%r, eid=%r)", table_name, fields, eid)
for field in fields:
table.update(tinydb.operations.delete(field), eids=[eid])
Raises:
ValueError: ``bool(keys) == False`` or invaild value for `keys`.
"""
table = self.table(table_name)
if isinstance(keys, self.Record.eid_type):
for field in fields:
LOGGER.debug("%s: unset(%s, eid=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=[keys])
elif isinstance(keys, dict):
for field in fields:
LOGGER.debug("%s: unset(%s, keys=%r)", table_name, field, keys)
table.update(operations.delete(field), self._query(keys, match_any))
elif isinstance(keys, (list, tuple)):
for field in fields:
LOGGER.debug("%s: unset(%s, eids=%r)", table_name, field, keys)
table.update(operations.delete(field), eids=keys)
else:
raise ValueError(keys)
If `keys` is a dictionary, update records with attributes matching `keys`.
Args:
table_name (str): Name of the table to operate on.
fields (list): Fields to unset.
keys: Fields or element identifiers to match.
match_any (bool): If True then any key may match or if False then all keys must match.
"""
table = self.database.table(table_name)
try:
eid = int(keys)
except TypeError:
if isinstance(keys, list):
LOGGER.debug("%s: unset(%r, eids=%r)", table_name, fields, keys)
for field in fields:
table.update(tinydb.operations.delete(field), eids=keys)
elif isinstance(keys, dict):
LOGGER.debug("%s: unset(%r, keys=%r)", table_name, fields, keys)
for field in fields:
table.update(tinydb.operations.delete(field), self._query(keys, match_any))
else:
LOGGER.debug("%s: unset(%r, eid=%r)", table_name, fields, eid)
for field in fields:
table.update(tinydb.operations.delete(field), eids=[eid])
with open("cli.config.json") as f:
config = json.load(f)
dbman = database.DatabaseManager(config["database"]["filename"], Log())
# Update items.
users = dbman.users.all()
i = 0
q = Query()
if len(users):
for u in users:
if "online" in u:
i += 1
print(u["name"])
dbman.users.update(delete("online"), q.name==u["name"])
print("Updated {0} records.".format(i))
dbman._unlock()