def test_atomicTransaction_raises(experiment): conn = experiment.conn bad_sql = '""' with pytest.raises(OperationalError): mut.atomicTransaction(conn, bad_sql)
def test_database_upgrade(empty_temp_db): connection = connect(qc.config["core"]["db_location"], qc.config["core"]["db_debug"]) userversion = get_user_version(connection) if userversion != 0: raise RuntimeError("trying to upgrade from version 0" " but your database is version" " {}".format(userversion)) sql = 'ALTER TABLE "runs" ADD COLUMN "quality"' atomicTransaction(connection, sql) set_user_version(connection, 1)
def __init__(self, dataSet, sub_id: str, callback: Callable[..., None], state: Optional[Any] = None, min_wait: int = 100, min_count: int = 1, callback_kwargs: Optional[Dict[str, Any]] = None) -> None: self.sub_id = sub_id # whether or not this is actually thread safe I am not sure :P self.dataSet = dataSet self.table_name = dataSet.table_name self.conn = dataSet.conn self.log = logging.getLogger(f"Subscriber {self.sub_id}") self.state = state self.min_wait = min_wait self.min_count = min_count self._send_queue: int = 0 if callback_kwargs is None: self.callback = callback else: self.callback = functools.partial(callback, **callback_kwargs) self._stop_signal: bool = False parameters = dataSet.get_parameters() param_sql = ",".join([f"NEW.{p.name}" for p in parameters]) self.callbackid = f"callback{self.sub_id}" self.conn.create_function(self.callbackid, -1, self.cache) sql = f""" CREATE TRIGGER sub{self.sub_id} AFTER INSERT ON '{self.table_name}' BEGIN SELECT {self.callbackid}({param_sql}); END;""" atomicTransaction(self.conn, sql) self.data: Queue = Queue() self._data_set_len = len(dataSet) super().__init__()
def unsubscribe_all(self): """ Remove all subscribers """ sql = "select * from sqlite_master where type = 'trigger';" triggers = atomicTransaction(self.conn, sql).fetchall() with atomic(self.conn): for trigger in triggers: self._remove_trigger(trigger['name']) for sub in self.subscribers.values(): sub.schedule_stop() sub.join() self.subscribers.clear()
def number_of_results(self): tabnam = self.table_name # TODO: is it better/faster to use the max index? sql = f'SELECT COUNT(*) FROM "{tabnam}"' cursor = atomicTransaction(self.conn, sql) return one(cursor, 'COUNT(*)')