|
|
|
@ -50,32 +50,6 @@ class SqlSessionCache(object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SqlsessionFactory(object):
|
|
|
|
|
"""
|
|
|
|
|
工厂模式创建sqlSession
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, engine: Engine):
|
|
|
|
|
self._engine = engine
|
|
|
|
|
self.cache = SqlSessionCache(self.create_session)
|
|
|
|
|
|
|
|
|
|
def open_session(self):
|
|
|
|
|
"""
|
|
|
|
|
从缓存获取,如果没有会自动调用create_session创建
|
|
|
|
|
"""
|
|
|
|
|
return self.cache()
|
|
|
|
|
|
|
|
|
|
def create_session(self):
|
|
|
|
|
try:
|
|
|
|
|
conn = self._engine.connect()
|
|
|
|
|
tx_factory = TransactionFactory(conn)
|
|
|
|
|
tx = tx_factory.create_transaction()
|
|
|
|
|
executor = SQLExecutor(tx)
|
|
|
|
|
return Sqlsession(executor, self.cache)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise CreateSessionError(msg=f'创建sqlSession异常,e={e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Sqlsession(object):
|
|
|
|
|
def __init__(self, executor: SQLExecutor, session_cache: SqlSessionCache):
|
|
|
|
|
self.executor = executor
|
|
|
|
@ -90,6 +64,19 @@ class Sqlsession(object):
|
|
|
|
|
def select_all(self, sql):
|
|
|
|
|
return self.executor.query(sql).fetchall()
|
|
|
|
|
|
|
|
|
|
def select_by_stream(self, sql, stream_size):
|
|
|
|
|
"""
|
|
|
|
|
流式查询,循环取数据
|
|
|
|
|
:param sql:
|
|
|
|
|
:param stream_size: 每次返回量
|
|
|
|
|
for partition in result.partitions():
|
|
|
|
|
# partition is an iterable that will be at most stream_size items
|
|
|
|
|
for row in partition:
|
|
|
|
|
print(f"{row}")
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
return self.executor.query_by_stream(sql, stream_size)
|
|
|
|
|
|
|
|
|
|
def insert(self, sql):
|
|
|
|
|
return self._execute_with_tx(sql)
|
|
|
|
|
|
|
|
|
@ -152,3 +139,29 @@ class Sqlsession(object):
|
|
|
|
|
"""
|
|
|
|
|
self.executor.get_connection().close()
|
|
|
|
|
self.session_cache.remove_session()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SqlsessionFactory(object):
|
|
|
|
|
"""
|
|
|
|
|
工厂模式创建sqlSession
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, engine: Engine):
|
|
|
|
|
self._engine = engine
|
|
|
|
|
self.cache = SqlSessionCache(self.create_session)
|
|
|
|
|
|
|
|
|
|
def open_session(self) -> Sqlsession:
|
|
|
|
|
"""
|
|
|
|
|
从缓存获取,如果没有会自动调用create_session创建
|
|
|
|
|
"""
|
|
|
|
|
return self.cache()
|
|
|
|
|
|
|
|
|
|
def create_session(self) -> Sqlsession:
|
|
|
|
|
try:
|
|
|
|
|
conn = self._engine.connect()
|
|
|
|
|
tx_factory = TransactionFactory(conn)
|
|
|
|
|
tx = tx_factory.create_transaction()
|
|
|
|
|
executor = SQLExecutor(tx)
|
|
|
|
|
return Sqlsession(executor, self.cache)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise CreateSessionError(msg=f'创建sqlSession异常,e={e}')
|
|
|
|
|