MCPcopy Create free account
hub / github.com/bytebase/bytebase / rewriteOracleFetchClause

Function rewriteOracleFetchClause

backend/plugin/db/oracle/query.go:278–306  ·  view source on GitHub ↗
(sql string, fetch *oracleast.FetchFirstClause, limitCount int)

Source from the content-addressed store, hash-verified

276}
277
278func rewriteOracleFetchClause(sql string, fetch *oracleast.FetchFirstClause, limitCount int) (string, error) {
279 if fetch.Count == nil {
280 if fetch.Loc.Start < 0 || fetch.Loc.End < fetch.Loc.Start || fetch.Loc.End > len(sql) {
281 return "", errors.Errorf("invalid FETCH position %d:%d", fetch.Loc.Start, fetch.Loc.End)
282 }
283 if hasOracleFetchKeyword(sql, fetch.Loc) {
284 return sql, nil
285 }
286 return sql[:fetch.Loc.End] + fmt.Sprintf(" FETCH NEXT %d ROWS ONLY", limitCount) + sql[fetch.Loc.End:], nil
287 }
288 if fetch.Percent {
289 return "", errors.Errorf("cannot rewrite PERCENT FETCH expression")
290 }
291
292 existingLimit := extractOracleFetchCount(fetch.Count)
293 if existingLimit > 0 && existingLimit <= limitCount {
294 return sql, nil
295 }
296
297 loc := oracleast.NodeLoc(fetch.Count)
298 loc = trimOracleLocSpace(sql, loc)
299 if loc.Start >= 0 && loc.End > loc.Start && loc.End <= len(sql) {
300 if existingLimit <= 0 {
301 return "", errors.Errorf("cannot rewrite non-constant FETCH expression")
302 }
303 return sql[:loc.Start] + fmt.Sprintf("%d", limitCount) + sql[loc.End:], nil
304 }
305 return "", errors.Errorf("cannot rewrite FETCH expression")
306}
307
308func hasOracleFetchKeyword(sql string, loc oracleast.Loc) bool {
309 segment := sql[loc.Start:loc.End]

Callers 1

rewriteOracleSelectFetchFunction · 0.85

Calls 4

hasOracleFetchKeywordFunction · 0.85
extractOracleFetchCountFunction · 0.85
trimOracleLocSpaceFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected