def sql_db_connect(): """Connection to DB """ try: uri_params = config.get_sql_db_uri_params() # TODO args* style argument SQLDatabase.connect(uri_params[0], uri_params[1], uri_params[2], uri_params[3], uri_params[4], uri_params[5]) except Exception as e: msg = "SQL: can't connect" log.info("msg", exc_info=True) raise IrmaDatabaseError(msg)
def sql_db_connect(): """Connection to DB """ try: uri_params = config.get_sql_db_uri_params() # TODO args* style argument SQLDatabase.connect(uri_params[0], uri_params[1], uri_params[2], uri_params[3], uri_params[4], uri_params[5], debug=config.sql_debug_enabled()) except Exception as e: log.exception(e) msg = "SQL: can't connect" raise IrmaDatabaseError(msg)
def session_query(): """Provide a transactional scope around a series of operations.""" sql_db_connect() session = SQLDatabase.get_session() try: yield session except IrmaDatabaseError: raise
def session_transaction(): """Provide a transactional scope around a series of operations.""" # TODO: when used with 'with', session is not commited and usage of vars # such as object.id could not be initialized (None) sql_db_connect() session = SQLDatabase.get_session() try: yield session session.commit() except IrmaDatabaseError: session.rollback() raise finally: session.close()
# SQLite fix for auto increment on ids # see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html if config.get_sql_db_uri_params()[0] == "sqlite": __table_args__ = {"sqlite_autoincrement": True} # Fields id = Column(Integer, autoincrement=True, nullable=False, primary_key=True, name="id") filename = Column(String, nullable=False, index=True, name="filename") probename = Column(String, nullable=False, index=True, name="probename") status = Column(Integer, nullable=False, name="status") ts_start = Column(Integer, nullable=False, name="ts_start") ts_end = Column(Integer, name="ts_end") task_id = Column(String, name="task_id") # Many to one Job <-> Scan scan_id = Column(Integer, ForeignKey("{0}scan.id".format(tables_prefix)), index=True, nullable=False) def __init__(self, filename, probename, scanid, taskid): self.filename = filename self.probename = probename self.ts_start = timestamp() self.status = self.running self.scan_id = scanid self.task_id = taskid def finished(self): return self.status != self.running Base.metadata.create_all(SQLDatabase.get_engine())