def _insertContents(self, parent, insertions): if not all(element.isInDb() for _, element in insertions): raise levels.ConsistencyError( "Elements must be in the DB before being added to a container." ) with db.transaction(): db.multiQuery( "INSERT INTO {p}contents (container_id, position, element_id) VALUES (?,?,?)", [(parent.id, pos, child.id) for pos, child in insertions]) db.updateElementsCounter((parent.id, )) super()._insertContents(parent, insertions)
def _changeStickers(self, changes): if not all(element.isInDb() for element in changes.keys()): raise levels.ConsistencyError( "Elements on real must be added to the DB before adding stickers." ) with db.transaction(): for element, diff in changes.items(): for type, (a, b) in diff.diffs.items(): if a is not None: db.query( "DELETE FROM {p}stickers WHERE type=? AND element_id=?", type, element.id) if b is not None: db.multiQuery( "INSERT INTO {p}stickers (element_id, type, sort, data) VALUES (?,?,?,?)", [(element.id, type, i, val) for i, val in enumerate(b)]) super()._changeStickers(changes)
def _changeFlags(self, changes): if not all(element.isInDb() for element in changes.keys()): raise levels.ConsistencyError( "Elements on real must be added to the DB before adding tags.") with db.transaction(): dbRemovals = [(el.id, flag.id) for el, diff in changes.items() for flag in diff.getRemovals()] if len(dbRemovals): db.multiQuery( "DELETE FROM {p}flags WHERE element_id = ? AND flag_id = ?", dbRemovals) dbAdditions = [(el.id, flag.id) for el, diff in changes.items() for flag in diff.getAdditions()] if len(dbAdditions): db.multiQuery( "INSERT INTO {p}flags (element_id, flag_id) VALUES(?,?)", dbAdditions) super()._changeFlags(changes)
def _setStickers(self, type, elementToStickers): if not all(element.isInDb() for element in elementToStickers.keys()): raise levels.ConsistencyError( "Elements on real must be added to the DB before adding stickers." ) values = [] for element, stickers in elementToStickers.items(): if stickers is not None: values.extend( (element.id, type, i, s) for i, s in enumerate(stickers)) with db.transaction(): db.query( "DELETE FROM {}stickers WHERE type = ? AND element_id IN ({})". format(db.prefix, db.csIdList(elementToStickers.keys())), type) if len(values) > 0: db.multiQuery( "INSERT INTO {p}stickers (element_id, type, sort, data) VALUES (?,?,?,?)", values) super()._setStickers(type, elementToStickers)
def _setContents(self, parent, contents): with db.transaction(): db.query("DELETE FROM {p}contents WHERE container_id = ?", parent.id) #Note: This checks skips elements which are not loaded on real. This should rarely happen and # due to foreign key constraints... if not all(self[childId].isInDb() for childId in contents if childId in self): raise levels.ConsistencyError( "Elements must be in the DB before being added to a container." ) if len(contents) > 0: # ...the following query will fail anyway (but with a DBException) # if some contents are not in the database yet. db.multiQuery( "INSERT INTO {p}contents (container_id, position, element_id) VALUES (?,?,?)", [(parent.id, pos, childId) for pos, childId in contents.items()]) db.updateElementsCounter((parent.id, )) super()._setContents(parent, contents)