|
1 | | -from pathlib import Path |
2 | | -from sqlalchemy.orm import sessionmaker, scoped_session |
3 | | -from sqlalchemy import MetaData, Table |
| 1 | +from sqlalchemy import Table |
4 | 2 | from sqlalchemy.exc import ( |
5 | | - IllegalStateChangeError, |
6 | 3 | NoInspectionAvailable, |
7 | 4 | NoSuchTableError, |
8 | 5 | ) |
9 | | -from nxc.logger import nxc_logger |
| 6 | +from nxc.database import BaseDB |
10 | 7 | import sys |
11 | 8 |
|
12 | 9 |
|
13 | | -class database: |
| 10 | +class database(BaseDB): |
14 | 11 | def __init__(self, db_engine): |
15 | 12 | self.CredentialsTable = None |
16 | 13 | self.HostsTable = None |
17 | 14 | self.LoggedinRelationsTable = None |
18 | 15 | self.SharesTable = None |
19 | 16 |
|
20 | | - self.db_engine = db_engine |
21 | | - self.db_path = self.db_engine.url.database |
22 | | - self.protocol = Path(self.db_path).stem.upper() |
23 | | - self.metadata = MetaData() |
24 | | - self.reflect_tables() |
25 | | - |
26 | | - session_factory = sessionmaker(bind=self.db_engine, expire_on_commit=True) |
27 | | - Session = scoped_session(session_factory) |
28 | | - self.sess = Session() |
| 17 | + super().__init__(db_engine) |
29 | 18 |
|
30 | 19 | @staticmethod |
31 | 20 | def db_schema(db_conn): |
@@ -79,16 +68,3 @@ def reflect_tables(self): |
79 | 68 | [-] Then remove the {self.protocol} DB (`rm -f {self.db_path}`) and run nxc to initialize the new DB""" |
80 | 69 | ) |
81 | 70 | sys.exit() |
82 | | - |
83 | | - def shutdown_db(self): |
84 | | - try: |
85 | | - self.sess.close() |
86 | | - # due to the async nature of nxc, sometimes session state is a bit messy and this will throw: |
87 | | - # Method 'close()' can't be called here; method '_connection_for_bind()' is already in progress and |
88 | | - # this would cause an unexpected state change to <SessionTransactionState.CLOSED: 5> |
89 | | - except IllegalStateChangeError as e: |
90 | | - nxc_logger.debug(f"Error while closing session db object: {e}") |
91 | | - |
92 | | - def clear_database(self): |
93 | | - for table in self.metadata.sorted_tables: |
94 | | - self.sess.execute(table.delete()) |
0 commit comments