You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
6.4 KiB
209 lines
6.4 KiB
3 years ago
|
#!/usr/bin/env python
|
||
|
# -*- encoding: utf-8 -*-
|
||
|
'''
|
||
3 years ago
|
@Date:2022/07/18 15:37:28
|
||
3 years ago
|
'''
|
||
3 years ago
|
import traceback
|
||
3 years ago
|
from sqlalchemy import create_engine
|
||
|
from sqlalchemy.orm import sessionmaker
|
||
3 years ago
|
from sqlalchemy_utils import database_exists, create_database
|
||
3 years ago
|
from sqlalchemy.sql.elements import BinaryExpression, BooleanClauseList
|
||
3 years ago
|
from sqlalchemy.pool import NullPool
|
||
3 years ago
|
from sqlalchemy.sql import func
|
||
|
import paginate_sqlalchemy
|
||
3 years ago
|
from config.SystemConfig import SystemConfig
|
||
|
from models.models_base import Base
|
||
|
from common.log_utlis import logger
|
||
|
|
||
|
|
||
|
"""数据仓储类"""
|
||
3 years ago
|
class Repository(object):
|
||
|
|
||
|
# 初始化数据库连接
|
||
3 years ago
|
#engine = create_engine(SystemConfig.getConfig('DBConntion'),pool_size=100,max_overflow=3,pool_timeout=10,pool_recycle=3600)
|
||
|
engine = create_engine(SystemConfig.getConfig(
|
||
|
'dbconntion'), poolclass=NullPool)
|
||
|
if not database_exists(engine.url):
|
||
|
create_database(engine.url)
|
||
3 years ago
|
# 创建session工厂
|
||
3 years ago
|
DBSession = sessionmaker(bind=engine, expire_on_commit=False)
|
||
3 years ago
|
# 创建session对象
|
||
|
session = DBSession()
|
||
|
# 事务标识
|
||
|
transFlag = False
|
||
|
|
||
|
def __init__(self, entityType):
|
||
|
super().__init__()
|
||
|
# 对象类型
|
||
|
self.entityType = entityType
|
||
|
|
||
3 years ago
|
@classmethod
|
||
|
def createTables(cls):
|
||
|
Base.metadata.create_all(cls.engine)
|
||
|
|
||
3 years ago
|
# 开始事务
|
||
|
def beginTrans(self):
|
||
|
self.session = self.DBSession()
|
||
|
self.transFlag = True
|
||
|
|
||
|
# 提交事务
|
||
|
def commitTrans(self):
|
||
|
try:
|
||
|
self.transFlag = False
|
||
|
self.session.commit()
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 新增数据
|
||
|
def insert(self, entity):
|
||
|
try:
|
||
3 years ago
|
if(not self.transFlag):
|
||
3 years ago
|
self.session = self.DBSession()
|
||
|
self.session.add(entity)
|
||
|
if(not self.transFlag):
|
||
|
self.session.commit()
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 新增多行数据
|
||
|
def insert_many(self, entity_list):
|
||
|
try:
|
||
|
if(not self.transFlag):
|
||
|
self.session = self.DBSession()
|
||
|
self.session.bulk_save_objects(entity_list)
|
||
|
if (not self.transFlag):
|
||
|
self.session.commit()
|
||
|
return True
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 更新数据
|
||
|
def update(self, entity):
|
||
|
try:
|
||
3 years ago
|
if(not self.transFlag):
|
||
|
self.session = self.DBSession()
|
||
3 years ago
|
self.session.merge(entity)
|
||
|
if(not self.transFlag):
|
||
|
self.session.commit()
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 删除操作
|
||
|
def delete(self, where):
|
||
|
try:
|
||
3 years ago
|
if(not self.transFlag):
|
||
3 years ago
|
self.session = self.DBSession()
|
||
|
self.session.query(self.entityType).filter(where).delete()
|
||
|
if(not self.transFlag):
|
||
|
self.session.commit()
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 查询单个实体
|
||
|
def findEntity(self, *where):
|
||
|
try:
|
||
|
self.session = self.DBSession()
|
||
|
if (type(*where) is BinaryExpression or type(*where) is BooleanClauseList):
|
||
|
return self.session.query(self.entityType).filter(*where).first()
|
||
|
else:
|
||
|
return self.session.query(self.entityType).get(where)
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 查询实体列表
|
||
|
def findList(self, *where):
|
||
|
try:
|
||
|
self.session = self.DBSession()
|
||
|
return self.session.query(self.entityType).filter(*where)
|
||
3 years ago
|
except Exception as e:
|
||
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 查询分页
|
||
|
def queryPage(self, orm_query, pageParam):
|
||
|
try:
|
||
|
page = paginate_sqlalchemy.SqlalchemyOrmPage(
|
||
|
orm_query, page=pageParam.curPage, items_per_page=pageParam.pageRows, db_session=self.DBSession)
|
||
|
pageParam.totalRecords = page.item_count
|
||
|
return page.items
|
||
3 years ago
|
except Exception as e:
|
||
|
logger.debug(traceback.format_exc())
|
||
|
raise e
|
||
3 years ago
|
|
||
|
# 查询数量
|
||
|
def findCount(self, *where):
|
||
|
try:
|
||
|
self.session = self.DBSession()
|
||
|
return self.session.query(func.count('*')).select_from(self.entityType).filter(*where).scalar()
|
||
3 years ago
|
except Exception as e:
|
||
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 查询最大数
|
||
|
def findMax(self, prop, *where):
|
||
|
try:
|
||
|
self.session = self.DBSession()
|
||
|
return self.session.query(func.max(prop)).select_from(self.entityType).filter(*where).scalar()
|
||
3 years ago
|
except Exception as e:
|
||
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 执行Sql语句
|
||
|
def execute(self, sql, *agrs):
|
||
|
try:
|
||
|
self.session = self.DBSession()
|
||
|
return self.session.execute(sql, *agrs)
|
||
3 years ago
|
except Exception as e:
|
||
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|
||
|
|
||
|
# 执行Sql语句
|
||
|
def executeNoParam(self, sql):
|
||
|
try:
|
||
|
self.session = self.DBSession()
|
||
|
self.session.execute(sql)
|
||
|
if(not self.transFlag):
|
||
|
self.session.commit()
|
||
3 years ago
|
except Exception as e:
|
||
|
logger.debug(traceback.format_exc())
|
||
3 years ago
|
self.session.rollback()
|
||
3 years ago
|
raise e
|
||
3 years ago
|
finally:
|
||
|
self.session.close()
|