mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
Optimize naming and preview in code generation (#764)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import Select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -32,6 +33,15 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
"""
|
||||
return await self.select_model_by_column(db, table_name=name)
|
||||
|
||||
async def get_all(self, db: AsyncSession) -> Sequence[GenBusiness]:
|
||||
"""
|
||||
获取所有代码生成业务
|
||||
|
||||
:param db: 数据库会话
|
||||
:return:
|
||||
"""
|
||||
return await self.select_models(db)
|
||||
|
||||
async def get_list(self, table_name: str | None) -> Select:
|
||||
"""
|
||||
获取所有代码生成业务
|
||||
@@ -44,7 +54,7 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
if table_name is not None:
|
||||
filters['table_name__like'] = f'%{table_name}%'
|
||||
|
||||
return await self.select_order('id', 'desc', **filters)
|
||||
return await self.select_order('id', 'desc', load_strategies={'gen_column': 'noload'}, **filters)
|
||||
|
||||
async def create(self, db: AsyncSession, obj: CreateGenBusinessParam) -> None:
|
||||
"""
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import Row, text
|
||||
from sqlalchemy import Row, RowMapping, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from backend.core.conf import settings
|
||||
@@ -12,7 +12,7 @@ class CRUDGen:
|
||||
"""代码生成 CRUD 类"""
|
||||
|
||||
@staticmethod
|
||||
async def get_all_tables(db: AsyncSession, table_schema: str) -> Sequence[str]:
|
||||
async def get_all_tables(db: AsyncSession, table_schema: str) -> Sequence[RowMapping]:
|
||||
"""
|
||||
获取所有表名
|
||||
|
||||
@@ -22,20 +22,23 @@ class CRUDGen:
|
||||
"""
|
||||
if settings.DATABASE_TYPE == 'mysql':
|
||||
sql = """
|
||||
SELECT table_name AS table_name FROM information_schema.tables
|
||||
WHERE table_name NOT LIKE 'sys_gen_%'
|
||||
SELECT table_name AS table_name, table_comment AS table_comment
|
||||
FROM information_schema.tables
|
||||
WHERE table_name NOT LIKE 'sys_gen_%'
|
||||
AND table_schema = :table_schema;
|
||||
"""
|
||||
else:
|
||||
sql = """
|
||||
SELECT table_name AS table_name FROM information_schema.tables
|
||||
WHERE table_name NOT LIKE 'sys_gen_%'
|
||||
AND table_catalog = :table_schema
|
||||
AND table_schema = 'public'; -- schema 通常是 'public'
|
||||
SELECT c.relname AS table_name, obj_description(c.oid) AS table_comment
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relkind = 'r'
|
||||
AND n.nspname = 'public' -- schema 通常是 'public'
|
||||
AND c.relname NOT LIKE 'sys_gen_%';
|
||||
"""
|
||||
stmt = text(sql).bindparams(table_schema=table_schema)
|
||||
result = await db.execute(stmt)
|
||||
return result.scalars().all()
|
||||
return result.mappings().all()
|
||||
|
||||
@staticmethod
|
||||
async def get_table(db: AsyncSession, table_name: str) -> Row[tuple]:
|
||||
@@ -48,18 +51,19 @@ class CRUDGen:
|
||||
"""
|
||||
if settings.DATABASE_TYPE == 'mysql':
|
||||
sql = """
|
||||
SELECT table_name AS table_name, table_comment AS table_comment FROM information_schema.tables
|
||||
SELECT table_name AS table_name, table_comment AS table_comment
|
||||
FROM information_schema.tables
|
||||
WHERE table_name NOT LIKE 'sys_gen_%'
|
||||
AND table_name = :table_name;
|
||||
"""
|
||||
else:
|
||||
sql = """
|
||||
SELECT t.tablename AS table_name,
|
||||
pg_catalog.obj_description(t.tablename::regclass, 'pg_class') AS table_comment
|
||||
FROM pg_tables t
|
||||
WHERE t.tablename NOT LIKE 'sys_gen_%'
|
||||
AND t.tablename = :table_name
|
||||
AND t.schemaname = 'public'; -- schema 通常是 'public'
|
||||
SELECT c.relname AS table_name, obj_description(c.oid) AS table_comment
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relkind = 'r'
|
||||
AND n.nspname = 'public' -- schema 通常是 'public'
|
||||
AND c.relname NOT LIKE 'sys_gen_%';
|
||||
"""
|
||||
stmt = text(sql).bindparams(table_name=table_name)
|
||||
result = await db.execute(stmt)
|
||||
|
||||
@@ -6,11 +6,11 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy_crud_plus import CRUDPlus
|
||||
|
||||
from backend.plugin.code_generator.model import GenColumn
|
||||
from backend.plugin.code_generator.schema.column import CreateGenModelParam, UpdateGenModelParam
|
||||
from backend.plugin.code_generator.schema.column import CreateGenColumnParam, UpdateGenColumnParam
|
||||
|
||||
|
||||
class CRUDGenModel(CRUDPlus[GenColumn]):
|
||||
"""代码生成模型 CRUD 类"""
|
||||
class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
"""代码生成模型列 CRUD 类"""
|
||||
|
||||
async def get(self, db: AsyncSession, pk: int) -> GenColumn | None:
|
||||
"""
|
||||
@@ -32,24 +32,24 @@ class CRUDGenModel(CRUDPlus[GenColumn]):
|
||||
"""
|
||||
return await self.select_models_order(db, sort_columns='sort', gen_business_id=business_id)
|
||||
|
||||
async def create(self, db: AsyncSession, obj: CreateGenModelParam, pd_type: str | None) -> None:
|
||||
async def create(self, db: AsyncSession, obj: CreateGenColumnParam, pd_type: str | None) -> None:
|
||||
"""
|
||||
创建代码生成模型
|
||||
创建代码生成模型列
|
||||
|
||||
:param db: 数据库会话
|
||||
:param obj: 创建代码生成模型参数
|
||||
:param obj: 创建代码生成模型列参数
|
||||
:param pd_type: Pydantic 类型
|
||||
:return:
|
||||
"""
|
||||
await self.create_model(db, obj, pd_type=pd_type)
|
||||
|
||||
async def update(self, db: AsyncSession, pk: int, obj: UpdateGenModelParam, pd_type: str | None) -> int:
|
||||
async def update(self, db: AsyncSession, pk: int, obj: UpdateGenColumnParam, pd_type: str | None) -> int:
|
||||
"""
|
||||
更新代码生成模型
|
||||
更新代码生成模型列
|
||||
|
||||
:param db: 数据库会话
|
||||
:param pk: 代码生成模型 ID
|
||||
:param obj: 更新代码生成模型参数
|
||||
:param pk: 代码生成模型列 ID
|
||||
:param obj: 更新代码生成模型列参数
|
||||
:param pd_type: Pydantic 类型
|
||||
:return:
|
||||
"""
|
||||
@@ -57,13 +57,13 @@ class CRUDGenModel(CRUDPlus[GenColumn]):
|
||||
|
||||
async def delete(self, db: AsyncSession, pk: int) -> int:
|
||||
"""
|
||||
删除代码生成模型
|
||||
删除代码生成模型列
|
||||
|
||||
:param db: 数据库会话
|
||||
:param pk: 代码生成模型 ID
|
||||
:param pk: 代码生成模型列 ID
|
||||
:return:
|
||||
"""
|
||||
return await self.delete_model(db, pk)
|
||||
|
||||
|
||||
gen_model_dao: CRUDGenModel = CRUDGenModel(GenColumn)
|
||||
gen_column_dao: CRUDGenColumn = CRUDGenColumn(GenColumn)
|
||||
|
||||
Reference in New Issue
Block a user