fuck orm
This commit is contained in:
parent
d75fcf3507
commit
c87c517217
5 changed files with 140 additions and 213 deletions
43
db/base.py
43
db/base.py
|
|
@ -1,15 +1,5 @@
|
|||
import os
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
engine = create_engine("sqlite:///:memory:", echo=False)
|
||||
session = sessionmaker(bind=engine, autoflush=False, autocommit=False)()
|
||||
session.flush = lambda: None
|
||||
import sqlite3
|
||||
|
||||
DATABASES = {
|
||||
"QuestMst.db": "Quest",
|
||||
|
|
@ -19,11 +9,28 @@ DATABASES = {
|
|||
}
|
||||
|
||||
|
||||
def init(path):
|
||||
for d in DATABASES:
|
||||
if not os.path.isfile(os.path.join(path, d)):
|
||||
raise FileNotFoundError("Database {} not found".format(d))
|
||||
class BaseDB(object):
|
||||
conn = None
|
||||
cursor = None
|
||||
|
||||
for d in DATABASES:
|
||||
t = text("attach database :path as :schema")
|
||||
engine.execute(t, path=os.path.join(path, d), schema=DATABASES[d])
|
||||
def __init__(self, path, databases):
|
||||
self.conn = sqlite3.connect(":memory:")
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
self.cursor = self.conn.cursor()
|
||||
self.attach_dbs(databases, path)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self.quest_conn.close()
|
||||
|
||||
def attach_dbs(self, databases, path):
|
||||
for d in databases:
|
||||
if not os.path.isfile(os.path.join(path, d)):
|
||||
raise FileNotFoundError(f"Database {d} not found")
|
||||
|
||||
for d in databases.items():
|
||||
self.cursor.execute(
|
||||
"ATTACH DATABASE ? AS ?", (os.path.join(path, d[0]), d[1])
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue