Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/fix limit in sql query (#27179)

Terry Sutton45998fd31a13
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
index 5b6e1862c0a..a8a3a4bafda 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
@@ -25,6 +25,24 @@ describe('SQLEditor.utils.ts:checkIfAppendLimitRequired', () => {
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
+ test('Should return false if query uses `FETCH FIRST` instead of limit ', () => {
+ const sql = 'select * from countries FETCH FIRST 5 rows only'
+ const limit = 100
+ const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
+ expect(appendAutoLimit).toBe(false)
+ })
+ test('Should return false if query uses `fetch first` instead of limit ', () => {
+ const sql = 'select * from countries fetch first 5 rows only'
+ const limit = 100
+ const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
+ expect(appendAutoLimit).toBe(false)
+ })
+ test('Should return false if query uses `fetch first` (with random spaces) instead of limit ', () => {
+ const sql = 'select * from countries FETCH FIRST 5 rows only'
+ const limit = 100
+ const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
+ expect(appendAutoLimit).toBe(false)
+ })
test('Should return false if query is not a select statement', () => {
const sql = 'create table test (id int8 primary key, name varchar);'
const limit = 100
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
index fe5e5a74047..c9c8b4a2a89 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
@@ -134,6 +134,7 @@ export const checkIfAppendLimitRequired = (sql: string, limit: number = 0) => {
!hasComments &&
!hasMultipleQueries &&
cleanedSql.toLowerCase().startsWith('select') &&
+ !cleanedSql.toLowerCase().match(/fetch\s+first/) &&
!cleanedSql.endsWith('limit') &&
!cleanedSql.endsWith('limit;') &&
!cleanedSql.match('limit [0-9]*[;]?$')