(sql string, stmt *ast.SetOpStmt, limitCount int)
| 201 | } |
| 202 | |
| 203 | func rewriteSetOpLimit(sql string, stmt *ast.SetOpStmt, limitCount int) (string, error) { |
| 204 | if stmt.Limit != nil { |
| 205 | limitLoc := literalLoc(stmt.Limit) |
| 206 | if limitLoc.Start < 0 { |
| 207 | return "", errors.New("cannot rewrite non-constant LIMIT expression") |
| 208 | } |
| 209 | |
| 210 | if countStart, countEnd, ok := findCommaLimitCount(sql, limitLoc); ok { |
| 211 | existingCount, _ := strconv.Atoi(sql[countStart:countEnd]) |
| 212 | if existingCount > 0 && existingCount <= limitCount { |
| 213 | return sql, nil |
| 214 | } |
| 215 | return sql[:countStart] + fmt.Sprintf("%d", limitCount) + sql[countEnd:], nil |
| 216 | } |
| 217 | |
| 218 | existingLimit := extractLimitValue(stmt.Limit) |
| 219 | if existingLimit >= 0 && existingLimit <= limitCount { |
| 220 | return sql, nil |
| 221 | } |
| 222 | return sql[:limitLoc.Start] + fmt.Sprintf("%d", limitCount) + sql[limitLoc.End:], nil |
| 223 | } |
| 224 | if hasUnparsedTail(sql, stmt.Loc.End) { |
| 225 | return "", errors.New("statement has unparsed tail content") |
| 226 | } |
| 227 | return sql[:stmt.Loc.End] + fmt.Sprintf(" LIMIT %d", limitCount) + sql[stmt.Loc.End:], nil |
| 228 | } |
| 229 | |
| 230 | func extractLimitValue(node ast.Node) int { |
| 231 | lit, ok := node.(*ast.Literal) |
no test coverage detected