68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
from sqlalchemy import Column, DateTime,Integer, String, create_engine, Boolean, Text
|
||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||
|
||
# 申明基类对象
|
||
Base = declarative_base()
|
||
from decouple import config
|
||
|
||
DB_PATH = config('DB_PATH', default='E://pyptoject//sqlbot_agent//main.sqlite3')
|
||
|
||
|
||
|
||
|
||
|
||
class QuestionFeedBack(Base):
|
||
# 定义表名
|
||
__tablename__ = 'db_question_feedback'
|
||
__table_args__ = {'extend_existing': True}
|
||
# 定义字段
|
||
id = Column(String(255), primary_key=True)
|
||
create_time = Column(DateTime, nullable=False, )
|
||
question = Column(String(255), nullable=False)
|
||
sql = Column(String(500), nullable=False)
|
||
user_id = Column(String(100), nullable=False, default='1')
|
||
# 用户意见反馈
|
||
user_comment = Column(Text, nullable=True)
|
||
# 用户点赞,点踩 0代表未点赞,1代表点赞,-1代表点踩
|
||
user_praise = Column(Integer, nullable=False, default=0)
|
||
# 该数据是否被认为梳理过
|
||
is_process = Column(Boolean, nullable=False, default=False)
|
||
def to_dict(self):
|
||
return {"id":self.id, "question":self.question, "user_id":self.user_id, "user_praise":self.user_praise}
|
||
|
||
|
||
class Conversation(Base):
|
||
__tablename__ = 'db_conversation'
|
||
id = Column(String(255), primary_key=True)
|
||
create_time = Column(DateTime, nullable=False, )
|
||
question = Column(String(500), nullable=False)
|
||
sql = Column(String(500), nullable=True)
|
||
user_id = Column(String(100), nullable=False)
|
||
cvs_id = Column(String(100), nullable=False)
|
||
meta = Column(Text, nullable=True)
|
||
|
||
|
||
class PredefinedQuestion(Base):
|
||
# 定义表名,预制问题表
|
||
__tablename__ = 'db_predefined_question'
|
||
__table_args__ = {'extend_existing': True}
|
||
# 定义字段
|
||
id = Column(String(255), primary_key=True)
|
||
question = Column(String(255), nullable=False)
|
||
user_id = Column(String(100), nullable=False, default='1')
|
||
# 该数据是否被认为梳理过
|
||
enable = Column(Boolean, nullable=False, default=True)
|
||
|
||
def to_dict(self):
|
||
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
|
||
|
||
|
||
class SqliteSqlalchemy(object):
|
||
def __init__(self):
|
||
# 创建sqlite连接引擎
|
||
engine = create_engine(f'sqlite:///{DB_PATH}', echo=True)
|
||
# 创建表
|
||
Base.metadata.create_all(engine, checkfirst=True)
|
||
# 创建sqlite的session连接对象
|
||
self.session = sessionmaker(bind=engine)()
|