使用sqlparse添加动态sqllimi

2024-05-16 18:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能在sqlparse中增加一个N的限制?例如,假设用户输入以下三个查询(示例中使用1000作为限制):

1) Reformat if limit > 1000
SELECT * FROM mytable LIMIT 1e10
==> SELECT * FROM mytable LIMIT 1000

2) Reformat if no limit
SELECT * FROM mytable
==> SELECT * FROM mytable LIMIT 1000

3) Ignore if a limit <= 1000 already applied
SELECT * FROM mytable
==> [ ok ]

另外,sqlparse是否能够根据数据库引擎修改限制?例如,mysql中的limit与mssql中的top不同。示例:

MySQL:      SELECT * FROM table LIMIT 1000
PostgreSQL: SELECT * FROM table LIMIT 1000
Oracle:     SELECT * FROM table WHERE rownum <= 1000
MsSQL:      SELECT TOP 1000 FROM table

或者,如果无法在sqlparse中执行,是否有其他库可以执行此操作?你知道吗


Tags: no用户from示例ifmytabletableselect