(sql string, fetch *oracleast.FetchFirstClause, limitCount int)
| 276 | } |
| 277 | |
| 278 | func 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 | |
| 308 | func hasOracleFetchKeyword(sql string, loc oracleast.Loc) bool { |
| 309 | segment := sql[loc.Start:loc.End] |
no test coverage detected