|
@@ -1,5 +1,4 @@
|
|
|
import json
|
|
|
-from datetime import datetime
|
|
|
|
|
|
from applications.config import Chunk
|
|
|
|
|
@@ -21,32 +20,29 @@ class BaseMySQLClient(TaskConst):
|
|
|
class Dataset(BaseMySQLClient):
|
|
|
async def update_dataset_status(self, dataset_id, ori_status, new_status):
|
|
|
query = """
|
|
|
- UPDATE dataset set status = %s where id = %s and status = %s;
|
|
|
+ UPDATE dataset SET status = %s WHERE id = %s AND status = %s;
|
|
|
"""
|
|
|
return await self.pool.async_save(
|
|
|
- query=query,
|
|
|
- params=(new_status, dataset_id, ori_status),
|
|
|
+ query=query, params=(new_status, dataset_id, ori_status)
|
|
|
)
|
|
|
|
|
|
async def select_dataset(self, status=1):
|
|
|
query = """
|
|
|
- select * from dataset where status = %s;
|
|
|
+ SELECT * FROM dataset WHERE status = %s;
|
|
|
"""
|
|
|
return await self.pool.async_fetch(query=query, params=(status,))
|
|
|
|
|
|
async def add_dataset(self, name):
|
|
|
query = """
|
|
|
- insert into dataset (name, created_at, updated_at, status) values (%s, %s, %s, %s);
|
|
|
+ INSERT INTO dataset (name) VALUES (%s);
|
|
|
"""
|
|
|
- return await self.pool.async_save(
|
|
|
- query=query, params=(name, datetime.now(), datetime.now(), 1)
|
|
|
- )
|
|
|
+ return await self.pool.async_save(query=query, params=(name,))
|
|
|
|
|
|
- async def select_dataset_by_id(self, id, status=1):
|
|
|
+ async def select_dataset_by_id(self, id_, status: int = 1):
|
|
|
query = """
|
|
|
- select * from dataset where id = %s and status = %s;
|
|
|
+ SELECT * FROM dataset WHERE id = %s AND status = %s;
|
|
|
"""
|
|
|
- return await self.pool.async_fetch(query=query, params=(id, status))
|
|
|
+ return await self.pool.async_fetch(query=query, params=(id_, status))
|
|
|
|
|
|
|
|
|
class Contents(BaseMySQLClient):
|
|
@@ -89,8 +85,7 @@ class Contents(BaseMySQLClient):
|
|
|
:return:
|
|
|
"""
|
|
|
query = """
|
|
|
- UPDATE contents
|
|
|
- SET doc_status = %s WHERE doc_id = %s and doc_status = %s;
|
|
|
+ UPDATE contents SET doc_status = %s WHERE doc_id = %s AND doc_status = %s;
|
|
|
"""
|
|
|
return await self.pool.async_save(
|
|
|
query=query, params=(new_status, doc_id, ori_status)
|
|
@@ -98,22 +93,20 @@ class Contents(BaseMySQLClient):
|
|
|
|
|
|
async def select_count(self, dataset_id, doc_status=1):
|
|
|
query = """
|
|
|
- select count(*) as count from contents where dataset_id = %s and doc_status = %s;
|
|
|
+ SELECT count(*) AS count FROM contents WHERE dataset_id = %s AND doc_status = %s;
|
|
|
"""
|
|
|
rows = await self.pool.async_fetch(query=query, params=(dataset_id, doc_status))
|
|
|
return rows[0]["count"] if rows else 0
|
|
|
|
|
|
async def select_content_by_doc_id(self, doc_id):
|
|
|
- query = """
|
|
|
- select * from contents where doc_id = %s;
|
|
|
- """
|
|
|
+ query = """SELECT * FROM contents WHERE doc_id = %s;"""
|
|
|
return await self.pool.async_fetch(query=query, params=(doc_id,))
|
|
|
|
|
|
async def select_contents(
|
|
|
self,
|
|
|
page_num: int,
|
|
|
page_size: int,
|
|
|
- order_by: dict = {"id": "desc"},
|
|
|
+ order_by=None,
|
|
|
dataset_id: int = None,
|
|
|
doc_status: int = 1,
|
|
|
):
|
|
@@ -126,6 +119,8 @@ class Contents(BaseMySQLClient):
|
|
|
:param doc_status: 文档状态(默认 1)
|
|
|
:return: dict,包含 entities、total_count、page、page_size、total_pages
|
|
|
"""
|
|
|
+ if order_by is None:
|
|
|
+ order_by = {"id": "desc"}
|
|
|
offset = (page_num - 1) * page_size
|
|
|
|
|
|
# 动态拼接 where 条件
|
|
@@ -273,10 +268,8 @@ class ContentChunks(BaseMySQLClient):
|
|
|
query=query, params=(new_status, dataset_id, ori_status)
|
|
|
)
|
|
|
|
|
|
- async def select_chunk_content(self, doc_id, chunk_id, status=1):
|
|
|
+ async def select_chunk_content(self, doc_id, chunk_id):
|
|
|
query = """
|
|
|
- select * from content_chunks where doc_id = %s and chunk_id = %s and status = %s;
|
|
|
+ SELECT * FROM content_chunks WHERE doc_id = %s AND chunk_id = %s;
|
|
|
"""
|
|
|
- return await self.pool.async_fetch(
|
|
|
- query=query, params=(doc_id, chunk_id, status)
|
|
|
- )
|
|
|
+ return await self.pool.async_fetch(query=query, params=(doc_id, chunk_id))
|