Skip to content

Commit 5980e05

Browse files
sql mixin: properly separate parameters from internal variables
parameters show not be changed during runtime, this prevents reloading the bot and other re-initializations (self.engine was made a class instance)
1 parent d915d4b commit 5980e05

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

intelmq/lib/mixins/sql.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,34 @@ class SQLMixin:
1919

2020
POSTGRESQL = "postgresql"
2121
SQLITE = "sqlite"
22-
default_engine = "postgresql"
22+
_default_engine = "postgresql"
2323
engine = None
2424
# overwrite the default value from the OutputBot
2525
message_jsondict_as_string = True
2626

2727
def __init__(self, *args, **kwargs):
2828
self.logger.debug("Running SQL Mixin initialization.")
29-
self.engine_name = getattr(self, 'engine', self.default_engine).lower()
29+
self._engine_name = getattr(self, 'engine', self._default_engine).lower()
3030
engines = {SQLMixin.POSTGRESQL: (self._init_postgresql, "%s"),
3131
SQLMixin.SQLITE: (self._init_sqlite, "?")}
3232
for key, val in engines.items():
33-
if self.engine_name == key:
33+
if self._engine_name == key:
3434
val[0]()
3535
self.format_char = val[1]
3636
break
3737
else:
38-
raise ValueError(f"Wrong parameter 'engine' {self.engine_name!r}, possible values are {engines}")
39-
40-
super().__init__()
38+
raise ValueError(f"Wrong parameter 'engine' {self._engine_name!r}, possible values are {engines}")
4139

4240
def _connect(self, engine, connect_args: dict, autocommitable: bool = False):
43-
self.engine = engine # imported external library that connects to the DB
41+
self._engine = engine # imported external library that connects to the DB
4442
self.logger.debug(f"Connecting to database with connect_args: {connect_args}.")
4543

4644
try:
47-
self.con = self.engine.connect(**connect_args)
45+
self.con = self._engine.connect(**connect_args)
4846
if autocommitable: # psycopg2 has it, sqlite3 has not
4947
self.con.autocommit = getattr(self, 'autocommit', True) # True prevents deadlocks
5048
self.cur = self.con.cursor()
51-
except (self.engine.Error, Exception):
49+
except (self._engine.Error, Exception):
5250
self.logger.exception('Failed to connect to database.')
5351
self.stop()
5452
self.logger.info("Connected to database.")
@@ -89,14 +87,14 @@ def execute(self, query: str, values: tuple, rollback=False):
8987
# note: this assumes, the DB was created with UTF-8 support!
9088
self.cur.execute(query, values)
9189
self.logger.debug('Done.')
92-
except (self.engine.InterfaceError, self.engine.InternalError,
93-
self.engine.OperationalError, AttributeError):
90+
except (self._engine.InterfaceError, self._engine.InternalError,
91+
self._engine.OperationalError, AttributeError):
9492
if rollback:
9593
try:
9694
self.con.rollback()
9795
self.logger.exception('Executed rollback command '
9896
'after failed query execution.')
99-
except self.engine.OperationalError:
97+
except self._engine.OperationalError:
10098
self.logger.exception('Executed rollback command '
10199
'after failed query execution.')
102100
self.init()

0 commit comments

Comments
 (0)