MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / parseIntervalExpression

Method parseIntervalExpression

pkg/sql/parser/expressions_complex.go:243–270  ·  view source on GitHub ↗

parseIntervalExpression parses an INTERVAL expression: INTERVAL 'value' Examples: INTERVAL '1 day' INTERVAL '2 hours' INTERVAL '1 year 2 months 3 days' INTERVAL '30 days'

()

Source from the content-addressed store, hash-verified

241// INTERVAL '1 year 2 months 3 days'
242// INTERVAL '30 days'
243func (p *Parser) parseIntervalExpression() (*ast.IntervalExpression, error) {
244 // Consume INTERVAL keyword
245 p.advance()
246
247 // Support both PostgreSQL style: INTERVAL '1 day'
248 // and MySQL style: INTERVAL 30 DAY, INTERVAL 1 HOUR
249 if p.isStringLiteral() {
250 value := p.currentToken.Token.Value
251 p.advance()
252 return &ast.IntervalExpression{Value: value}, nil
253 }
254
255 // MySQL style: INTERVAL <number> <unit>
256 if p.isNumericLiteral() {
257 numStr := p.currentToken.Token.Value
258 p.advance()
259 // Expect a unit keyword (DAY, HOUR, MINUTE, SECOND, MONTH, YEAR, WEEK, etc.)
260 unit := strings.ToUpper(p.currentToken.Token.Value)
261 p.advance()
262 return &ast.IntervalExpression{Value: numStr + " " + unit}, nil
263 }
264
265 return nil, goerrors.InvalidSyntaxError(
266 "expected string literal or number after INTERVAL keyword",
267 p.currentLocation(),
268 "Use INTERVAL '1 day' or INTERVAL 1 DAY syntax",
269 )
270}
271
272// parseArrayConstructor parses PostgreSQL ARRAY constructor syntax.
273// Supports both ARRAY[...] (square bracket) and ARRAY(...) (subquery) forms.

Callers 1

Calls 4

advanceMethod · 0.95
isStringLiteralMethod · 0.95
isNumericLiteralMethod · 0.95
currentLocationMethod · 0.95

Tested by

no test coverage detected