(sql string, stmt *ast.SelectStmt, limitCount int)
| 170 | } |
| 171 | |
| 172 | func rewriteSelectLimit(sql string, stmt *ast.SelectStmt, limitCount int) (string, error) { |
| 173 | if stmt.Limit != nil { |
| 174 | limitLoc := literalLoc(stmt.Limit) |
| 175 | if limitLoc.Start < 0 { |
| 176 | return "", errors.New("cannot rewrite non-constant LIMIT expression") |
| 177 | } |
| 178 | |
| 179 | if countStart, countEnd, ok := findCommaLimitCount(sql, limitLoc); ok { |
| 180 | existingCount, _ := strconv.Atoi(sql[countStart:countEnd]) |
| 181 | if existingCount > 0 && existingCount <= limitCount { |
| 182 | return sql, nil |
| 183 | } |
| 184 | return sql[:countStart] + fmt.Sprintf("%d", limitCount) + sql[countEnd:], nil |
| 185 | } |
| 186 | |
| 187 | existingLimit := extractLimitValue(stmt.Limit) |
| 188 | if existingLimit >= 0 && existingLimit <= limitCount { |
| 189 | return sql, nil |
| 190 | } |
| 191 | return sql[:limitLoc.Start] + fmt.Sprintf("%d", limitCount) + sql[limitLoc.End:], nil |
| 192 | } |
| 193 | |
| 194 | if stmt.Into != nil { |
| 195 | return sql[:stmt.Into.Loc.Start] + fmt.Sprintf("LIMIT %d ", limitCount) + sql[stmt.Into.Loc.Start:], nil |
| 196 | } |
| 197 | if hasUnparsedTail(sql, stmt.Loc.End) { |
| 198 | return "", errors.New("statement has unparsed tail content") |
| 199 | } |
| 200 | return sql[:stmt.Loc.End] + fmt.Sprintf(" LIMIT %d", limitCount) + sql[stmt.Loc.End:], nil |
| 201 | } |
| 202 | |
| 203 | func rewriteSetOpLimit(sql string, stmt *ast.SetOpStmt, limitCount int) (string, error) { |
| 204 | if stmt.Limit != nil { |
no test coverage detected