Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: fix query limit in Oracle 11 (#5881)

Danny Xu190d4d5163d7
diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.go
index cc8869caf4..7018cde902 100644
--- a/backend/plugin/db/util/driverutil.go
+++ b/backend/plugin/db/util/driverutil.go
@@ -126,16 +126,20 @@ func Query(ctx context.Context, dbType db.Type, conn *sql.Conn, statement string
if !readOnly {
return queryAdmin(ctx, dbType, conn, statement, limit)
}
+
+ statement = strings.TrimRight(statement, " \n\t;")
// Limit SQL query result size.
- if dbType == db.MySQL || dbType == db.MariaDB {
- // MySQL 5.7 doesn't support WITH clause.
- statement = getMySQLStatementWithResultLimit(statement, limit)
- } else if dbType == db.Oracle {
- statement = getOracleStatementWithResultLimit(statement, limit)
- } else if dbType == db.MSSQL {
- statement = getMSSQLStatementWithResultLimit(statement, limit)
- } else {
- statement = getStatementWithResultLimit(statement, limit)
+ if !strings.HasPrefix(statement, "EXPLAIN") && limit > 0 {
+ if dbType == db.MySQL || dbType == db.MariaDB {
+ // MySQL 5.7 doesn't support WITH clause.
+ statement = getMySQLStatementWithResultLimit(statement, limit)
+ } else if dbType == db.Oracle {
+ statement = getOracleStatementWithResultLimit(statement, limit)
+ } else if dbType == db.MSSQL {
+ statement = getMSSQLStatementWithResultLimit(statement, limit)
+ } else {
+ statement = getStatementWithResultLimit(statement, limit)
+ }
}
// TiDB doesn't support READ ONLY transactions. We have to skip the flag for it.
@@ -313,52 +317,20 @@ func readRows(rows *sql.Rows, dbType db.Type, columnTypes []*sql.ColumnType, col
}
func getStatementWithResultLimit(stmt string, limit int) string {
- stmt = strings.TrimRight(stmt, " \n\t;")
- if !strings.HasPrefix(stmt, "EXPLAIN") {
- limitPart := ""
- if limit > 0 {
- limitPart = fmt.Sprintf(" LIMIT %d", limit)
- }
- return fmt.Sprintf("WITH result AS (%s) SELECT * FROM result%s;", stmt, limitPart)
- }
- return stmt
+ return fmt.Sprintf("WITH result AS (%s) SELECT * FROM result LIMIT %d;", stmt, limit)
}
func getMySQLStatementWithResultLimit(stmt string, limit int) string {
- stmt = strings.TrimRight(stmt, " \n\t;")
- if !strings.HasPrefix(stmt, "EXPLAIN") {
- limitPart := ""
- if limit > 0 {
- limitPart = fmt.Sprintf(" LIMIT %d", limit)
- }
- return fmt.Sprintf("SELECT * FROM (%s) result%s;", stmt, limitPart)
- }
- return stmt
+ return fmt.Sprintf("SELECT * FROM (%s) result LIMIT %d;", stmt, limit)
}
func getOracleStatementWithResultLimit(stmt string, limit int) string {
- stmt = strings.TrimRight(stmt, " \n\t;")
- if !strings.HasPrefix(stmt, "EXPLAIN") {
- limitPart := ""
- if limit > 0 {
- limitPart = fmt.Sprintf(" FETCH NEXT %d ROWS ONLY", limit)
- }
- return fmt.Sprintf("%s%s", stmt, limitPart)
- }
- return stmt
+ return fmt.Sprintf("SELECT * FROM (%s) WHERE ROWNUM <= %d", stmt, limit)
}
func getMSSQLStatementWithResultLimit(stmt string, limit int) string {
// TODO(d): support SELECT 1 (mssql: No column name was specified for column 1 of 'result').
- stmt = strings.TrimRight(stmt, " \n\t;")
- if !strings.HasPrefix(stmt, "EXPLAIN") {
- limitPart := ""
- if limit > 0 {
- limitPart = fmt.Sprintf(" TOP %d", limit)
- }
- return fmt.Sprintf("WITH result AS (%s) SELECT%s * FROM result;", stmt, limitPart)
- }
- return stmt
+ return fmt.Sprintf("WITH result AS (%s) SELECT TOP %d * FROM result;", stmt, limit)
}
// FindMigrationHistoryList will find the list of migration history.
diff --git a/backend/plugin/db/util/driverutil_test.go b/backend/plugin/db/util/driverutil_test.go
index 1c0ed675dd..934416c8ea 100644
--- a/backend/plugin/db/util/driverutil_test.go
+++ b/backend/plugin/db/util/driverutil_test.go
@@ -85,40 +85,10 @@ func TestGetStatementWithResultLimit(t *testing.T) {
want string
}{
{
- sqlStatement: " seLeCT * FROM test;",
+ sqlStatement: " seLeCT * FROM test",
limit: 123,
want: "WITH result AS ( seLeCT * FROM test) SELECT * FROM result LIMIT 123;",
},
- {
- sqlStatement: " seLeCT * FROM test;",
- limit: 0,
- want: "WITH result AS ( seLeCT * FROM test) SELECT * FROM result;",
- },
- {
- sqlStatement: " \n \r SELEct * from test ",
- limit: 100,
- want: "WITH result AS ( \n \r SELEct * from test) SELECT * FROM result LIMIT 100;",
- },
- {
- sqlStatement: "SELECT\n*\nFROM\ntest ;\n",
- limit: 100,
- want: "WITH result AS (SELECT\n*\nFROM\ntest) SELECT * FROM result LIMIT 100;",
- },
- {
- sqlStatement: "SELECT\n*\nFROM\ntest ;;;\n",
- limit: 100,
- want: "WITH result AS (SELECT\n*\nFROM\ntest) SELECT * FROM result LIMIT 100;",
- },
- {
- sqlStatement: "SELECT\n*\nFROM\n`test;` ;;;\n",
- limit: 100,
- want: "WITH result AS (SELECT\n*\nFROM\n`test;`) SELECT * FROM result LIMIT 100;",
- },
- {
- sqlStatement: "EXPLAIN \n \r SELEct * from test ",
- limit: 0,
- want: "EXPLAIN \n \r SELEct * from test",
- },
}
for _, test := range tests {
@@ -136,40 +106,10 @@ func TestGetMySQLStatementWithResultLimit(t *testing.T) {
want string
}{
{
- sqlStatement: " seLeCT * FROM test;",
+ sqlStatement: " seLeCT * FROM test",
limit: 123,
want: "SELECT * FROM ( seLeCT * FROM test) result LIMIT 123;",
},
- {
- sqlStatement: " seLeCT * FROM test;",
- limit: 0,
- want: "SELECT * FROM ( seLeCT * FROM test) result;",
- },
- {
- sqlStatement: " \n \r SELEct * from test ",
- limit: 100,
- want: "SELECT * FROM ( \n \r SELEct * from test) result LIMIT 100;",
- },
- {
- sqlStatement: "SELECT\n*\nFROM\ntest ;\n",
- limit: 100,
- want: "SELECT * FROM (SELECT\n*\nFROM\ntest) result LIMIT 100;",
- },
- {
- sqlStatement: "SELECT\n*\nFROM\ntest ;;;\n",
- limit: 100,
- want: "SELECT * FROM (SELECT\n*\nFROM\ntest) result LIMIT 100;",
- },
- {
- sqlStatement: "SELECT\n*\nFROM\n`test;` ;;;\n",
- limit: 100,
- want: "SELECT * FROM (SELECT\n*\nFROM\n`test;`) result LIMIT 100;",
- },
- {
- sqlStatement: "EXPLAIN \n \r SELEct * from test ",
- limit: 0,
- want: "EXPLAIN \n \r SELEct * from test",
- },
}
for _, test := range tests {