|
5 | 5 | from sqlalchemy import Row, text |
6 | 6 | from sqlalchemy.ext.asyncio import AsyncSession |
7 | 7 |
|
| 8 | +from backend.core.conf import settings |
| 9 | + |
8 | 10 |
|
9 | 11 | class CRUDGen: |
10 | 12 | @staticmethod |
11 | 13 | async def get_all_tables(db: AsyncSession, table_schema: str) -> Sequence[str]: |
12 | | - stmt = text( |
13 | | - 'select table_name as table_name ' |
14 | | - 'from information_schema.tables ' |
15 | | - 'where table_name not like "sys_gen_%" ' |
16 | | - 'and table_schema = :table_schema;' |
17 | | - ).bindparams(table_schema=table_schema) |
| 14 | + if settings.DATABASE_TYPE == 'mysql': |
| 15 | + sql = """ |
| 16 | + SELECT table_name AS table_name FROM information_schema.tables |
| 17 | + WHERE table_name NOT LIKE 'sys_gen_%' |
| 18 | + AND table_schema = :table_schema; |
| 19 | + """ |
| 20 | + else: |
| 21 | + sql = """ |
| 22 | + SELECT table_name AS table_name FROM information_schema.tables |
| 23 | + WHERE table_name NOT LIKE 'sys_gen_%' |
| 24 | + AND table_catalog = :table_schema |
| 25 | + AND table_schema = 'public'; -- schema 通常是 'public' |
| 26 | + """ |
| 27 | + stmt = text(sql).bindparams(table_schema=table_schema) |
18 | 28 | result = await db.execute(stmt) |
19 | 29 | return result.scalars().all() |
20 | 30 |
|
21 | 31 | @staticmethod |
22 | 32 | async def get_table(db: AsyncSession, table_name: str) -> Row[tuple]: |
23 | | - stmt = text( |
24 | | - 'select table_name as table_name, table_comment as table_comment ' |
25 | | - 'from information_schema.tables ' |
26 | | - 'where table_name not like "sys_gen_%" ' |
27 | | - 'and table_name = :table_name;' |
28 | | - ).bindparams(table_name=table_name) |
| 33 | + if settings.DATABASE_TYPE == 'mysql': |
| 34 | + sql = """ |
| 35 | + SELECT table_name AS table_name, table_comment AS table_comment FROM information_schema.tables |
| 36 | + WHERE table_name NOT LIKE 'sys_gen_%' |
| 37 | + AND table_name = :table_name; |
| 38 | + """ |
| 39 | + else: |
| 40 | + sql = """ |
| 41 | + SELECT t.tablename AS table_name, |
| 42 | + pg_catalog.obj_description(t.tablename::regclass, 'pg_class') AS table_comment |
| 43 | + FROM pg_tables t |
| 44 | + WHERE t.tablename NOT LIKE 'sys_gen_%' |
| 45 | + AND t.tablename = :table_name |
| 46 | + AND t.schemaname = 'public'; -- schema 通常是 'public' |
| 47 | + """ |
| 48 | + stmt = text(sql).bindparams(table_name=table_name) |
29 | 49 | result = await db.execute(stmt) |
30 | 50 | return result.fetchone() |
31 | 51 |
|
32 | 52 | @staticmethod |
33 | 53 | async def get_all_columns(db: AsyncSession, table_schema: str, table_name: str) -> Sequence[Row[tuple]]: |
34 | | - stmt = text( |
35 | | - 'select column_name AS column_name, ' |
36 | | - 'case when column_key = "PRI" then 1 else 0 end as is_pk, ' |
37 | | - 'case when is_nullable = "NO" or column_key = "PRI" then 0 else 1 end as is_nullable, ' |
38 | | - 'ordinal_position as sort, ' |
39 | | - 'column_comment as column_comment, ' |
40 | | - 'column_type as column_type ' |
41 | | - 'from information_schema.columns ' |
42 | | - 'where table_schema = :table_schema ' |
43 | | - 'and table_name = :table_name ' |
44 | | - 'and column_name != "id" ' |
45 | | - 'and column_name != "created_time" ' |
46 | | - 'and column_name != "updated_time" ' |
47 | | - 'order by sort;' |
48 | | - ).bindparams(table_schema=table_schema, table_name=table_name) |
| 54 | + if settings.DATABASE_TYPE == 'mysql': |
| 55 | + sql = """ |
| 56 | + SELECT column_name AS column_name, |
| 57 | + CASE WHEN column_key = 'PRI' THEN 1 ELSE 0 END AS is_pk, |
| 58 | + CASE WHEN is_nullable = 'NO' OR column_key = 'PRI' THEN 0 ELSE 1 END AS is_nullable, |
| 59 | + ordinal_position AS sort, column_comment AS column_comment, |
| 60 | + column_type AS column_type FROM information_schema.columns |
| 61 | + WHERE table_schema = :table_schema |
| 62 | + AND table_name = :table_name |
| 63 | + AND column_name <> 'id' |
| 64 | + AND column_name <> 'created_time' |
| 65 | + AND column_name <> 'updated_time' |
| 66 | + ORDER BY sort; |
| 67 | + """ |
| 68 | + stmt = text(sql).bindparams(table_schema=table_schema, table_name=table_name) |
| 69 | + else: |
| 70 | + sql = """ |
| 71 | + SELECT a.attname AS column_name, |
| 72 | + CASE WHEN EXISTS ( |
| 73 | + SELECT 1 |
| 74 | + FROM pg_constraint c |
| 75 | + WHERE c.conrelid = t.oid |
| 76 | + AND c.contype = 'p' |
| 77 | + AND a.attnum = ANY(c.conkey) |
| 78 | + ) THEN 1 ELSE 0 END AS is_pk, |
| 79 | + CASE WHEN a.attnotnull OR EXISTS ( |
| 80 | + SELECT 1 |
| 81 | + FROM pg_constraint c |
| 82 | + WHERE c.conrelid = t.oid |
| 83 | + AND c.contype = 'p' |
| 84 | + AND a.attnum = ANY(c.conkey) |
| 85 | + ) THEN 0 ELSE 1 END AS is_nullable, |
| 86 | + a.attnum AS sort, |
| 87 | + col_description(t.oid, a.attnum) AS column_comment, |
| 88 | + pg_catalog.format_type(a.atttypid, a.atttypmod) AS column_type |
| 89 | + FROM pg_attribute a |
| 90 | + JOIN pg_class t ON a.attrelid = t.oid |
| 91 | + JOIN pg_namespace n ON n.oid = t.relnamespace |
| 92 | + WHERE n.nspname = 'public' -- 根据你的实际情况修改 schema 名称,通常是 'public' |
| 93 | + AND t.relname = :table_name |
| 94 | + AND a.attnum > 0 |
| 95 | + AND NOT a.attisdropped |
| 96 | + AND a.attname <> 'id' |
| 97 | + AND a.attname <> 'created_time' |
| 98 | + AND a.attname <> 'updated_time' |
| 99 | + ORDER BY sort; |
| 100 | + """ |
| 101 | + stmt = text(sql).bindparams(table_name=table_name) |
49 | 102 | result = await db.execute(stmt) |
50 | 103 | return result.fetchall() |
51 | 104 |
|
|
0 commit comments