Skip to content

Commit cea4a10

Browse files
insistencegitee-org
authored andcommitted
!29 RuoYi-Vue3-FastAPI v1.6.1
Merge pull request !29 from insistence/develop
2 parents a5a4099 + b708d86 commit cea4a10

File tree

11 files changed

+61
-27
lines changed

11 files changed

+61
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<p align="center">
22
<img alt="logo" src="https://oscimg.oschina.net/oscnet/up-d3d0a9303e11d522a06cd263f3079027715.png">
33
</p>
4-
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">RuoYi-Vue3-FastAPI v1.6.0</h1>
4+
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">RuoYi-Vue3-FastAPI v1.6.1</h1>
55
<h4 align="center">基于RuoYi-Vue3+FastAPI前后端分离的快速开发框架</h4>
66
<p align="center">
77
<a href="https://gitee.com/insistence2022/RuoYi-Vue3-FastAPI/stargazers"><img src="https://gitee.com/insistence2022/RuoYi-Vue3-FastAPI/badge/star.svg?theme=dark"></a>
88
<a href="https://github.com/insistence/RuoYi-Vue3-FastAPI"><img src="https://img.shields.io/github/stars/insistence/RuoYi-Vue3-FastAPI?style=social"></a>
9-
<a href="https://gitee.com/insistence2022/RuoYi-Vue3-FastAPI"><img src="https://img.shields.io/badge/RuoYiVue3FastAPI-v1.6.0-brightgreen.svg"></a>
9+
<a href="https://gitee.com/insistence2022/RuoYi-Vue3-FastAPI"><img src="https://img.shields.io/badge/RuoYiVue3FastAPI-v1.6.1-brightgreen.svg"></a>
1010
<a href="https://gitee.com/insistence2022/RuoYi-Vue3-FastAPI/blob/master/LICENSE"><img src="https://img.shields.io/github/license/mashape/apistatus.svg"></a>
1111
<img src="https://img.shields.io/badge/python-≥3.9-blue">
1212
<img src="https://img.shields.io/badge/MySQL-≥5.7-blue">

ruoyi-fastapi-backend/.env.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ APP_HOST = '0.0.0.0'
1010
# 应用端口
1111
APP_PORT = 9099
1212
# 应用版本
13-
APP_VERSION= '1.6.0'
13+
APP_VERSION= '1.6.1'
1414
# 应用是否开启热重载
1515
APP_RELOAD = true
1616
# 应用是否开启IP归属区域查询

ruoyi-fastapi-backend/.env.prod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ APP_HOST = '0.0.0.0'
1010
# 应用端口
1111
APP_PORT = 9099
1212
# 应用版本
13-
APP_VERSION= '1.6.0'
13+
APP_VERSION= '1.6.1'
1414
# 应用是否开启热重载
1515
APP_RELOAD = false
1616
# 应用是否开启IP归属区域查询

ruoyi-fastapi-backend/config/env.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
from dotenv import load_dotenv
55
from functools import lru_cache
6+
from pydantic import computed_field
67
from pydantic_settings import BaseSettings
78
from typing import Literal
89

@@ -51,6 +52,13 @@ class DataBaseSettings(BaseSettings):
5152
db_pool_recycle: int = 3600
5253
db_pool_timeout: int = 30
5354

55+
@computed_field
56+
@property
57+
def sqlglot_parse_dialect(self) -> str:
58+
if self.db_type == 'postgresql':
59+
return 'postgres'
60+
return self.db_type
61+
5462

5563
class RedisSettings(BaseSettings):
5664
"""

ruoyi-fastapi-backend/module_admin/annotation/pydantic_annotation.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
from fastapi import Form, Query
33
from pydantic import BaseModel
44
from pydantic.fields import FieldInfo
5-
from typing import Type
5+
from typing import Type, TypeVar
66

77

8-
def as_query(cls: Type[BaseModel]):
8+
BaseModelVar = TypeVar('BaseModelVar', bound=BaseModel)
9+
10+
11+
def as_query(cls: Type[BaseModelVar]) -> Type[BaseModelVar]:
912
"""
1013
pydantic模型查询参数装饰器,将pydantic模型用于接收查询参数
1114
"""
@@ -43,7 +46,7 @@ async def as_query_func(**data):
4346
return cls
4447

4548

46-
def as_form(cls: Type[BaseModel]):
49+
def as_form(cls: Type[BaseModelVar]) -> Type[BaseModelVar]:
4750
"""
4851
pydantic模型表单参数装饰器,将pydantic模型用于接收表单参数
4952
"""

ruoyi-fastapi-backend/module_generator/dao/gen_dao.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sqlalchemy import delete, func, select, text, update
33
from sqlalchemy.ext.asyncio import AsyncSession
44
from sqlalchemy.orm import selectinload
5+
from sqlglot.expressions import Expression
56
from typing import List
67
from config.env import DataBaseConfig
78
from module_generator.entity.do.gen_do import GenTable, GenTableColumn
@@ -75,15 +76,17 @@ async def get_gen_table_all(cls, db: AsyncSession):
7576
return gen_table_all
7677

7778
@classmethod
78-
async def create_table_by_sql_dao(cls, db: AsyncSession, sql: str):
79+
async def create_table_by_sql_dao(cls, db: AsyncSession, sql_statements: List[Expression]):
7980
"""
8081
根据sql语句创建表结构
8182
8283
:param db: orm对象
83-
:param sql: sql语句
84+
:param sql_statements: sql语句的ast列表
8485
:return:
8586
"""
86-
await db.execute(text(sql))
87+
for sql_statement in sql_statements:
88+
sql = sql_statement.sql(dialect=DataBaseConfig.sqlglot_parse_dialect)
89+
await db.execute(text(sql))
8790

8891
@classmethod
8992
async def get_gen_table_list(cls, db: AsyncSession, query_object: GenTablePageQueryModel, is_page: bool = False):

ruoyi-fastapi-backend/module_generator/service/gen_service.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import io
22
import json
33
import os
4-
import re
54
import zipfile
65
from datetime import datetime
76
from sqlalchemy.ext.asyncio import AsyncSession
7+
from sqlglot import parse as sqlglot_parse
8+
from sqlglot.expressions import Add, Alter, Create, Delete, Drop, Expression, Insert, Table, TruncateTable, Update
89
from typing import List
910
from config.constant import GenConstant
10-
from config.env import GenConfig
11+
from config.env import DataBaseConfig, GenConfig
1112
from exceptions.exception import ServiceException
1213
from module_admin.entity.vo.common_vo import CrudResponseModel
1314
from module_admin.entity.vo.user_vo import CurrentUserModel
@@ -197,10 +198,11 @@ async def create_table_services(cls, query_db: AsyncSession, sql: str, current_u
197198
:param current_user: 当前用户信息对象
198199
:return: 创建表结构结果
199200
"""
200-
if cls.__is_valid_create_table(sql):
201+
sql_statements = sqlglot_parse(sql, dialect=DataBaseConfig.sqlglot_parse_dialect)
202+
if cls.__is_valid_create_table(sql_statements):
201203
try:
202-
table_names = re.findall(r'create\s+table\s+(\w+)', sql, re.IGNORECASE)
203-
await GenTableDao.create_table_by_sql_dao(query_db, sql)
204+
table_names = cls.__get_table_names(sql_statements)
205+
await GenTableDao.create_table_by_sql_dao(query_db, sql_statements)
204206
gen_table_list = await cls.get_gen_db_table_list_by_name_services(query_db, table_names)
205207
await cls.import_gen_table_services(query_db, gen_table_list, current_user)
206208

@@ -211,22 +213,39 @@ async def create_table_services(cls, query_db: AsyncSession, sql: str, current_u
211213
raise ServiceException(message='建表语句不合法')
212214

213215
@classmethod
214-
def __is_valid_create_table(cls, sql: str):
216+
def __is_valid_create_table(cls, sql_statements: List[Expression]):
215217
"""
216218
校验sql语句是否为合法的建表语句
217219
218-
:param sql: sql语句
220+
:param sql_statements: sql语句的ast列表
219221
:return: 校验结果
220222
"""
221-
create_table_pattern = r'^\s*CREATE\s+TABLE\s+'
222-
if not re.search(create_table_pattern, sql, re.IGNORECASE):
223+
validate_create = [isinstance(sql_statement, Create) for sql_statement in sql_statements]
224+
validate_forbidden_keywords = [
225+
isinstance(
226+
sql_statement,
227+
(Add, Alter, Delete, Drop, Insert, TruncateTable, Update),
228+
)
229+
for sql_statement in sql_statements
230+
]
231+
if not any(validate_create) or any(validate_forbidden_keywords):
223232
return False
224-
forbidden_keywords = ['INSERT', 'UPDATE', 'DELETE', 'DROP', 'ALTER', 'TRUNCATE']
225-
for keyword in forbidden_keywords:
226-
if re.search(rf'\b{keyword}\b', sql, re.IGNORECASE):
227-
return False
228233
return True
229234

235+
@classmethod
236+
def __get_table_names(cls, sql_statements: List[Expression]):
237+
"""
238+
获取sql语句中所有的建表表名
239+
240+
:param sql_statements: sql语句的ast列表
241+
:return: 建表表名列表
242+
"""
243+
table_names = []
244+
for sql_statement in sql_statements:
245+
if isinstance(sql_statement, Create):
246+
table_names.append(sql_statement.find(Table).name)
247+
return table_names
248+
230249
@classmethod
231250
async def preview_code_services(cls, query_db: AsyncSession, table_id: int):
232251
"""

ruoyi-fastapi-backend/module_generator/templates/python/dao.py.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class {{ BusinessName }}Dao:
7070
await db.execute(
7171
select({{ ClassName }}).where(
7272
{% for column in columns %}
73-
{% if column.required %}
73+
{% if column.unique %}
7474
{{ ClassName }}.{{ column.python_field | camel_to_snake }} == {{ businessName }}.{{ column.python_field | camel_to_snake }} if {{ businessName }}.{{ column.python_field | camel_to_snake }} else True,
7575
{% endif %}
7676
{% endfor %}

ruoyi-fastapi-backend/module_generator/templates/python/vo.py.jinja2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
{% set vo_field_required.has_required = True %}
99
{% endif %}
1010
{% endfor %}
11-
{% if table.sub %}
1211
{% set sub_vo_field_required = namespace(has_required=False) %}
12+
{% if table.sub %}
1313
{% for sub_column in subTable.columns %}
1414
{% if sub_column.required %}
1515
{% set sub_vo_field_required.has_required = True %}
@@ -21,7 +21,7 @@
2121
{% endfor %}
2222
from pydantic import BaseModel, ConfigDict, Field
2323
from pydantic.alias_generators import to_camel
24-
{% if vo_field_required.has_required %}
24+
{% if vo_field_required.has_required or sub_vo_field_required.has_required %}
2525
from pydantic_validation_decorator import NotBlank
2626
{% endif %}
2727
{% if table.sub %}

ruoyi-fastapi-backend/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ PyMySQL==1.1.1
1414
redis==5.2.1
1515
requests==2.32.3
1616
SQLAlchemy[asyncio]==2.0.38
17+
sqlglot[rs]==26.6.0
1718
user-agents==2.2.0

ruoyi-fastapi-frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vfadmin",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "vfadmin管理系统",
55
"author": "insistence",
66
"license": "MIT",

0 commit comments

Comments
 (0)