Upgraded sqlservice to 2.0.x

This commit is contained in:
Marcus Lindvall 2023-04-13 10:55:42 +02:00
parent 229ea6fb5b
commit 6c1eb903f7
8 changed files with 95 additions and 91 deletions

View file

@ -12,45 +12,11 @@ from sqlalchemy.orm import sessionmaker, scoped_session
# from sqlalchemy.orm.exc import UnmappedClassError
from pymssql import OperationalError
from sqlservice import SQLClient, SQLQuery
from sqlservice import Database
logger = logging.getLogger("PyJeeves." + __name__)
class BaseFilterQuery(SQLQuery):
def get(self, ident):
# Override get() so that the flag is always checked in the
# DB as opposed to pulling from the identity map. - this is optional.
return SQLQuery.get(self.populate_existing(), ident)
def __iter__(self):
return SQLQuery.__iter__(self.private())
def from_self(self, *ent):
# Override from_self() to automatically apply
# the criterion to. this works with count() and
# others.
return SQLQuery.from_self(self.private(), *ent)
def private(self):
# Fetch the model name and column list and apply model-specific base filters
mzero = self._mapper_zero()
if mzero:
# Sometimes a plain model class will be fetched instead of mzero
try:
model = mzero.class_
obj = mzero.class_
except Exception:
model = mzero.__class__
obj = mzero
if hasattr(model, '_base_filters'):
return self.enable_assertions(False).filter(model._base_filters(obj))
return self
class DBConnector(object):
"""This class is used to control the SQLAlchemy integration"""
@ -82,9 +48,9 @@ class DBConnector(object):
return retval
def execute(self, operation=""):
conn = self.raw_engine.raw_connection()
conn = self.raw_engine
with conn.cursor(as_dict=True) as cursor:
with conn.connection.cursor(as_dict=True) as cursor:
try:
cursor.execute(operation)
results = cursor.fetchall()
@ -100,14 +66,13 @@ class DBConnector(object):
uri = 'mssql+pymssql://{user}:{pw}@{host}:{port}/{db}?charset=utf8'.format(
**config.config['databases']['raw'])
sql_client_config = {'SQL_DATABASE_URI': uri}
db = SQLClient(sql_client_config, query_class=BaseFilterQuery)
return db, db.session, db.engine
db = Database(uri, echo=False)
return db, db.session(), db.connect()
def set_model_class(self, model_class):
self.raw_db.model_class = model_class
self.raw_db.update_models_registry()
def meta_session(self):