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

Method parseConnectByCondition

pkg/sql/parser/mariadb.go:364–436  ·  view source on GitHub ↗

parseConnectByCondition parses the condition expression for CONNECT BY. It handles the PRIOR prefix operator in either position: CONNECT BY PRIOR id = parent_id (PRIOR on left) CONNECT BY id = PRIOR parent_id (PRIOR on right) CONNECT BY PRIOR id = parent_id AND active = 1 (c

()

Source from the content-addressed store, hash-verified

362// PRIOR references the value from the parent row in the hierarchy.
363// It is modeled as UnaryExpression{Operator: ast.Prior, Expr: <column>}.
364func (p *Parser) parseConnectByCondition() (ast.Expression, error) {
365 var base ast.Expression
366
367 // Case 1: PRIOR col op col
368 if strings.EqualFold(p.currentToken.Token.Value, "PRIOR") {
369 p.advance()
370 priorIdent := p.parseIdent()
371 if priorIdent == nil || priorIdent.Name == "" {
372 return nil, p.expectedError("column name after PRIOR")
373 }
374 priorExpr := &ast.UnaryExpression{Operator: ast.Prior, Expr: priorIdent}
375
376 if p.isType(models.TokenTypeEq) || p.isType(models.TokenTypeNeq) ||
377 p.isType(models.TokenTypeLt) || p.isType(models.TokenTypeGt) ||
378 p.isType(models.TokenTypeLtEq) || p.isType(models.TokenTypeGtEq) {
379 op := p.currentToken.Token.Value
380 p.advance()
381 right, err := p.parsePrimaryExpression()
382 if err != nil {
383 return nil, err
384 }
385 base = &ast.BinaryExpression{Left: priorExpr, Operator: op, Right: right}
386 } else {
387 base = priorExpr
388 }
389 } else {
390 // Case 2: col op PRIOR col (PRIOR on the right-hand side)
391 // or plain expression (no PRIOR)
392 left, err := p.parsePrimaryExpression()
393 if err != nil {
394 return nil, err
395 }
396 if p.isType(models.TokenTypeEq) || p.isType(models.TokenTypeNeq) ||
397 p.isType(models.TokenTypeLt) || p.isType(models.TokenTypeGt) ||
398 p.isType(models.TokenTypeLtEq) || p.isType(models.TokenTypeGtEq) {
399 op := p.currentToken.Token.Value
400 p.advance()
401 // Check for PRIOR on the right side
402 if strings.EqualFold(p.currentToken.Token.Value, "PRIOR") {
403 p.advance()
404 priorIdent := p.parseIdent()
405 if priorIdent == nil || priorIdent.Name == "" {
406 return nil, p.expectedError("column name after PRIOR")
407 }
408 priorExpr := &ast.UnaryExpression{Operator: ast.Prior, Expr: priorIdent}
409 base = &ast.BinaryExpression{Left: left, Operator: op, Right: priorExpr}
410 } else {
411 right, err := p.parsePrimaryExpression()
412 if err != nil {
413 return nil, err
414 }
415 base = &ast.BinaryExpression{Left: left, Operator: op, Right: right}
416 }
417 } else {
418 base = left
419 }
420 }
421

Callers 1

parseSelectStatementMethod · 0.95

Calls 5

advanceMethod · 0.95
parseIdentMethod · 0.95
expectedErrorMethod · 0.95
isTypeMethod · 0.95

Tested by

no test coverage detected