|
@@ -13,6 +13,7 @@ from common.DBgetdata import dbgetdata
|
|
|
from pprint import pprint
|
|
|
from datetime import datetime
|
|
|
# import pandas as pd
|
|
|
+from pydantic import BaseModel
|
|
|
import sqlalchemy
|
|
|
import pymysql
|
|
|
import json,time,io
|
|
@@ -327,169 +328,15 @@ async def v1(request: Request, background_tasks: BackgroundTasks, db: Session =
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-# @router.get('/v2')
|
|
|
-@router.post('/v2')
|
|
|
-# @router.post('/v2/{servicecode}')
|
|
|
-async def v2(servicecode:str,request: Request, db: Session = Depends(get_db)):
|
|
|
- # 获取请求头 servicecode
|
|
|
- # if service_code is None:
|
|
|
- service_code = request.headers.get('servicecode')
|
|
|
- if service_code is None:
|
|
|
- return JSONResponse(status_code=410, content= {
|
|
|
- 'code': 410,
|
|
|
- 'msg': "请求头servicecode未传参"
|
|
|
- })
|
|
|
- # 判断请求接口是否存在
|
|
|
- service_info = db.query(ApiServiceEntity).filter(ApiServiceEntity.id == service_code).first()
|
|
|
- if service_info is None:
|
|
|
- return JSONResponse(status_code=410, content={
|
|
|
- 'code': 410,
|
|
|
- 'msg': 'servicecode服务不存在'
|
|
|
- })
|
|
|
- # print(service_code)
|
|
|
-
|
|
|
-
|
|
|
- # 判断请求接口对应数据库是否存在
|
|
|
- command_info = db.query(CommandEntity).filter(CommandEntity.scope == service_code).first()
|
|
|
- if command_info is None:
|
|
|
- return JSONResponse(status_code=410, content={
|
|
|
- 'code': 410,
|
|
|
- 'msg': '查询命令没配置'
|
|
|
- })
|
|
|
-
|
|
|
- # 判断请求接口对应数据库是否存在
|
|
|
- database_info = db.query(DatasourceEntity).filter(DatasourceEntity.id == command_info.datasource).first()
|
|
|
- if database_info is None:
|
|
|
- return JSONResponse(status_code=410, content={
|
|
|
- 'errcode': 410,
|
|
|
- 'errmsg': '数据库没配置'
|
|
|
- })
|
|
|
-
|
|
|
- # 获取接口对应的sql
|
|
|
- sql = command_info.sqltext
|
|
|
- print('sql ==== << ', sql)
|
|
|
-
|
|
|
- # 从params和body获取参数
|
|
|
- # query_list = parse.parse_qsl(str(request.query_params))
|
|
|
- # print(query_list)
|
|
|
- data = await request.body()
|
|
|
- body = data.decode(encoding='utf-8')
|
|
|
- if len(body) > 0:
|
|
|
- body = json.loads(body)
|
|
|
-
|
|
|
- # 分页器 页数和页码的设置
|
|
|
- size = 10
|
|
|
- current = 1
|
|
|
- if "size" in body:
|
|
|
- if isinstance(body['size'], str):
|
|
|
- size = int(body['size'])
|
|
|
- elif isinstance(body['size'], int):
|
|
|
- size = body['size']
|
|
|
- if size >100:
|
|
|
- size = 100
|
|
|
-
|
|
|
- if "current" in body:
|
|
|
- if isinstance(body['current'], str):
|
|
|
- current = int(body['current'])
|
|
|
- elif isinstance(body['current'], int):
|
|
|
- current = body['current']
|
|
|
- if current<=0:
|
|
|
- current=1
|
|
|
- # 接口sql的参数替换
|
|
|
- if 'query' in body:
|
|
|
- for (key, val) in body['query'].items():
|
|
|
- if isinstance(val, int):
|
|
|
- val = str(val)
|
|
|
- if contains_special_characters(val):
|
|
|
- return JSONResponse(status_code=411, content={
|
|
|
- 'code': 411,
|
|
|
- 'msg': f'参数{key}含特殊符号:;、&、$、#、\'、\\t、@、空格等'
|
|
|
- })
|
|
|
- sql = sql.replace("{" + key + "}", val)
|
|
|
-
|
|
|
-
|
|
|
- print('sql ==== >> ', sql)
|
|
|
-
|
|
|
-
|
|
|
- data = []
|
|
|
-
|
|
|
- # 数据库类型为mysql情况下
|
|
|
- if database_info.dbtype == 'pymysql':
|
|
|
- # 数据库连接
|
|
|
-
|
|
|
- conn = pymysql.connect(host=database_info.host,
|
|
|
- user=database_info.user,
|
|
|
- password=database_info.password,
|
|
|
- database=database_info.database,
|
|
|
- port=database_info.port,
|
|
|
- charset=database_info.charset)
|
|
|
- cur = conn.cursor()
|
|
|
-
|
|
|
- # 查总数据量,分页数据处理
|
|
|
- totalsql = f'select count(*) from ({sql})t'
|
|
|
- print(totalsql)
|
|
|
- cur.execute(totalsql)
|
|
|
- total = cur.fetchone()[0]
|
|
|
-
|
|
|
- pages,pagesmod = divmod(total, size)
|
|
|
- print(total,pages,pagesmod)
|
|
|
- if pagesmod!=0:
|
|
|
- pages+=1
|
|
|
- print(pages,pagesmod)
|
|
|
- if total <size :
|
|
|
- size = total
|
|
|
-
|
|
|
-
|
|
|
- # 正式查询
|
|
|
- sql = sql+f' limit {size*(current-1)}, {size};'
|
|
|
- print(sql,size)
|
|
|
- cur.execute(sql)
|
|
|
- rows = cur.fetchall()
|
|
|
- colnames = [desc[0] for desc in cur.description]
|
|
|
- for row in range(size):
|
|
|
- item = {}
|
|
|
- for col in range(len(colnames)):
|
|
|
- field_name = colnames[col]
|
|
|
- item[field_name] = rows[row][col]
|
|
|
- data.append(item)
|
|
|
-
|
|
|
- # 数据库关闭
|
|
|
- cur.close()
|
|
|
- conn.close()
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- else:
|
|
|
- return JSONResponse(status_code=410, content={
|
|
|
- 'code': 410,
|
|
|
- 'msg': '接口对应数据库暂不支持'
|
|
|
- })
|
|
|
-
|
|
|
- return {
|
|
|
- 'code': 0,
|
|
|
- 'msg': 'success',
|
|
|
- 'data': {"list": data,
|
|
|
- 'pages': pages, # 总页数
|
|
|
- 'currentPage': current, # 当前页数
|
|
|
- # 'current':current,
|
|
|
- # 'total' : total,
|
|
|
- 'total': total, # 总数据量
|
|
|
- # 'size':size,
|
|
|
- 'pageSize': size # 页码
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else:
|
|
|
- return JSONResponse(status_code=410, content={
|
|
|
- 'code': 410,
|
|
|
- 'msg': 'body不能为空'
|
|
|
- })
|
|
|
-
|
|
|
-
|
|
|
+class CreateApiServiceFrom(BaseModel):
|
|
|
+ datasource:str
|
|
|
+ name:str
|
|
|
+ sqltext:str
|
|
|
+ serviceid:str=None
|
|
|
|
|
|
@router.post('/v2/create_api_service')
|
|
|
async def create_api_service_v2(
|
|
|
+ form_data: CreateApiServiceFrom,
|
|
|
request: Request,
|
|
|
db: Session = Depends(get_db)
|
|
|
):
|
|
@@ -506,7 +353,7 @@ async def create_api_service_v2(
|
|
|
return f'{param}为必填参数,不能不存在'
|
|
|
|
|
|
# 数据库信息校验
|
|
|
- datasource = body['datasource']
|
|
|
+ datasource = form_data.datasource
|
|
|
database_info = db.query(DatasourceEntity).filter(DatasourceEntity.id == datasource).first()
|
|
|
if database_info is None:
|
|
|
return JSONResponse(status_code=410, content={
|
|
@@ -515,10 +362,13 @@ async def create_api_service_v2(
|
|
|
})
|
|
|
|
|
|
# 接口信息生成及获取
|
|
|
- serviceid = str(uuid.uuid4()).replace("-","")
|
|
|
- serviname = body['name']
|
|
|
+ if form_data.serviceid is None:
|
|
|
+ serviceid = str(uuid.uuid4()).replace("-","")
|
|
|
+ else:
|
|
|
+ serviceid = form_data.serviceid
|
|
|
+ serviname = form_data.name
|
|
|
id = str(uuid.uuid4())
|
|
|
- sqltext = body['sqltext']
|
|
|
+ sqltext = form_data.sqltext
|
|
|
|
|
|
# 接口执行校验
|
|
|
if database_info.dbtype == 'pymysql':
|
|
@@ -780,3 +630,163 @@ async def create_update_datasource_v2(
|
|
|
return [datasource,testresult]
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+@router.get('/v2/{serviceid}')
|
|
|
+@router.post('/v2/{serviceid}')
|
|
|
+# @router.post('/v2/{servicecode}')
|
|
|
+async def v2(serviceid:str,request: Request, db: Session = Depends(get_db)):
|
|
|
+ # 获取请求头 servicecode
|
|
|
+ # if service_code is None:
|
|
|
+ # service_code = request.headers.get('servicecode')
|
|
|
+ # print(serviceid)
|
|
|
+ if serviceid is None:
|
|
|
+ return JSONResponse(status_code=410, content= {
|
|
|
+ 'code': 410,
|
|
|
+ 'msg': "请求头servicecode未传参"
|
|
|
+ })
|
|
|
+ # 判断请求接口是否存在
|
|
|
+ service_info = db.query(ApiServiceEntity).filter(ApiServiceEntity.id == serviceid).first()
|
|
|
+ if service_info is None:
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'code': 410,
|
|
|
+ 'msg': 'servicecode服务不存在'
|
|
|
+ })
|
|
|
+ # print(service_code)
|
|
|
+
|
|
|
+
|
|
|
+ # 判断请求接口对应数据库是否存在
|
|
|
+ command_info = db.query(CommandEntity).filter(CommandEntity.scope == serviceid).first()
|
|
|
+ if command_info is None:
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'code': 410,
|
|
|
+ 'msg': '查询命令没配置'
|
|
|
+ })
|
|
|
+
|
|
|
+ # 判断请求接口对应数据库是否存在
|
|
|
+ database_info = db.query(DatasourceEntity).filter(DatasourceEntity.id == command_info.datasource).first()
|
|
|
+ if database_info is None:
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'errcode': 410,
|
|
|
+ 'errmsg': '数据库没配置'
|
|
|
+ })
|
|
|
+
|
|
|
+ # 获取接口对应的sql
|
|
|
+ sql = command_info.sqltext
|
|
|
+ print('sql ==== << ', sql)
|
|
|
+
|
|
|
+ # 从params和body获取参数
|
|
|
+ # query_list = parse.parse_qsl(str(request.query_params))
|
|
|
+ # print(query_list)
|
|
|
+ data = await request.body()
|
|
|
+ body = data.decode(encoding='utf-8')
|
|
|
+ if len(body) > 0:
|
|
|
+ body = json.loads(body)
|
|
|
+
|
|
|
+ # 分页器 页数和页码的设置
|
|
|
+ size = 10
|
|
|
+ current = 1
|
|
|
+ if "size" in body:
|
|
|
+ if isinstance(body['size'], str):
|
|
|
+ size = int(body['size'])
|
|
|
+ elif isinstance(body['size'], int):
|
|
|
+ size = body['size']
|
|
|
+ if size >100:
|
|
|
+ size = 100
|
|
|
+
|
|
|
+ if "current" in body:
|
|
|
+ if isinstance(body['current'], str):
|
|
|
+ current = int(body['current'])
|
|
|
+ elif isinstance(body['current'], int):
|
|
|
+ current = body['current']
|
|
|
+ if current<=0:
|
|
|
+ current=1
|
|
|
+ # 接口sql的参数替换
|
|
|
+ if 'query' in body:
|
|
|
+ for (key, val) in body['query'].items():
|
|
|
+ if isinstance(val, int):
|
|
|
+ val = str(val)
|
|
|
+ if contains_special_characters(val):
|
|
|
+ return JSONResponse(status_code=411, content={
|
|
|
+ 'code': 411,
|
|
|
+ 'msg': f'参数{key}含特殊符号:;、&、$、#、\'、\\t、@、空格等'
|
|
|
+ })
|
|
|
+ sql = sql.replace("{" + key + "}", val)
|
|
|
+
|
|
|
+
|
|
|
+ print('sql ==== >> ', sql)
|
|
|
+
|
|
|
+
|
|
|
+ data = []
|
|
|
+
|
|
|
+ # 数据库类型为mysql情况下
|
|
|
+ if database_info.dbtype == 'pymysql':
|
|
|
+ # 数据库连接
|
|
|
+
|
|
|
+ conn = pymysql.connect(host=database_info.host,
|
|
|
+ user=database_info.user,
|
|
|
+ password=database_info.password,
|
|
|
+ database=database_info.database,
|
|
|
+ port=database_info.port,
|
|
|
+ charset=database_info.charset)
|
|
|
+ cur = conn.cursor()
|
|
|
+
|
|
|
+ # 查总数据量,分页数据处理
|
|
|
+ totalsql = f'select count(*) from ({sql})t'
|
|
|
+ print(totalsql)
|
|
|
+ cur.execute(totalsql)
|
|
|
+ total = cur.fetchone()[0]
|
|
|
+
|
|
|
+ pages,pagesmod = divmod(total, size)
|
|
|
+ print(total,pages,pagesmod)
|
|
|
+ if pagesmod!=0:
|
|
|
+ pages+=1
|
|
|
+ print(pages,pagesmod)
|
|
|
+ if total <size :
|
|
|
+ size = total
|
|
|
+
|
|
|
+
|
|
|
+ # 正式查询
|
|
|
+ sql = sql+f' limit {size*(current-1)}, {size};'
|
|
|
+ print(sql,size)
|
|
|
+ cur.execute(sql)
|
|
|
+ rows = cur.fetchall()
|
|
|
+ colnames = [desc[0] for desc in cur.description]
|
|
|
+ for row in range(size):
|
|
|
+ item = {}
|
|
|
+ for col in range(len(colnames)):
|
|
|
+ field_name = colnames[col]
|
|
|
+ item[field_name] = rows[row][col]
|
|
|
+ data.append(item)
|
|
|
+
|
|
|
+ # 数据库关闭
|
|
|
+ cur.close()
|
|
|
+ conn.close()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ else:
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'code': 410,
|
|
|
+ 'msg': '接口对应数据库暂不支持'
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'code': 0,
|
|
|
+ 'msg': 'success',
|
|
|
+ 'data': {"list": data,
|
|
|
+ 'pages': pages, # 总页数
|
|
|
+ 'currentPage': current, # 当前页数
|
|
|
+ # 'current':current,
|
|
|
+ # 'total' : total,
|
|
|
+ 'total': total, # 总数据量
|
|
|
+ # 'size':size,
|
|
|
+ 'pageSize': size # 页码
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else:
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'code': 410,
|
|
|
+ 'msg': 'body不能为空'
|
|
|
+ })
|