Skip to content

Commit a63cd74

Browse files
committed
Add ALTER DATABASE SET statement support, enable 2 more tests (114→116)
New features: - ALTER DATABASE ... SET statement with database options - ACCELERATED_DATABASE_RECOVERY option support - AlterDatabaseSetStatement and AcceleratedDatabaseRecoveryDatabaseOption AST types
1 parent f57f947 commit a63cd74

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ast
2+
3+
// AlterDatabaseSetStatement represents ALTER DATABASE ... SET statement
4+
type AlterDatabaseSetStatement struct {
5+
DatabaseName *Identifier
6+
UseCurrent bool
7+
WithManualCutover bool
8+
Options []DatabaseOption
9+
}
10+
11+
func (a *AlterDatabaseSetStatement) node() {}
12+
func (a *AlterDatabaseSetStatement) statement() {}
13+
14+
// DatabaseOption is an interface for database options
15+
type DatabaseOption interface {
16+
Node
17+
databaseOption()
18+
}
19+
20+
// AcceleratedDatabaseRecoveryDatabaseOption represents ACCELERATED_DATABASE_RECOVERY option
21+
type AcceleratedDatabaseRecoveryDatabaseOption struct {
22+
OptionKind string // "AcceleratedDatabaseRecovery"
23+
OptionState string // "On" or "Off"
24+
}
25+
26+
func (a *AcceleratedDatabaseRecoveryDatabaseOption) node() {}
27+
func (a *AcceleratedDatabaseRecoveryDatabaseOption) databaseOption() {}

parser/parser.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,65 @@ func (p *Parser) parseAlterDatabaseStatement() (ast.Statement, error) {
376376
}
377377
}
378378

379+
// Parse database name followed by SET
380+
if p.curTok.Type == TokenIdent || p.curTok.Type == TokenLBracket {
381+
dbName := p.parseIdentifier()
382+
383+
// Expect SET
384+
if p.curTok.Type == TokenSet {
385+
return p.parseAlterDatabaseSetStatement(dbName)
386+
}
387+
}
388+
379389
return nil, fmt.Errorf("unexpected token after ALTER DATABASE: %s", p.curTok.Literal)
380390
}
381391

392+
func (p *Parser) parseAlterDatabaseSetStatement(dbName *ast.Identifier) (*ast.AlterDatabaseSetStatement, error) {
393+
// Consume SET
394+
p.nextToken()
395+
396+
stmt := &ast.AlterDatabaseSetStatement{
397+
DatabaseName: dbName,
398+
}
399+
400+
// Parse options
401+
for {
402+
optionName := strings.ToUpper(p.curTok.Literal)
403+
p.nextToken()
404+
405+
if p.curTok.Type != TokenEquals {
406+
return nil, fmt.Errorf("expected = after %s, got %s", optionName, p.curTok.Literal)
407+
}
408+
p.nextToken()
409+
410+
// Parse option value
411+
optionValue := strings.ToUpper(p.curTok.Literal)
412+
p.nextToken()
413+
414+
switch optionName {
415+
case "ACCELERATED_DATABASE_RECOVERY":
416+
opt := &ast.AcceleratedDatabaseRecoveryDatabaseOption{
417+
OptionKind: "AcceleratedDatabaseRecovery",
418+
OptionState: capitalizeFirst(optionValue),
419+
}
420+
stmt.Options = append(stmt.Options, opt)
421+
}
422+
423+
if p.curTok.Type == TokenComma {
424+
p.nextToken()
425+
} else {
426+
break
427+
}
428+
}
429+
430+
// Skip optional semicolon
431+
if p.curTok.Type == TokenSemicolon {
432+
p.nextToken()
433+
}
434+
435+
return stmt, nil
436+
}
437+
382438
func (p *Parser) parseAlterDatabaseScopedCredentialStatement() (*ast.AlterCredentialStatement, error) {
383439
// Consume CREDENTIAL
384440
p.nextToken()
@@ -5869,6 +5925,8 @@ func statementToJSON(stmt ast.Statement) jsonNode {
58695925
return createPartitionSchemeStatementToJSON(s)
58705926
case *ast.AlterCredentialStatement:
58715927
return alterCredentialStatementToJSON(s)
5928+
case *ast.AlterDatabaseSetStatement:
5929+
return alterDatabaseSetStatementToJSON(s)
58725930
case *ast.RevertStatement:
58735931
return revertStatementToJSON(s)
58745932
case *ast.DropCredentialStatement:
@@ -6172,6 +6230,38 @@ func alterCredentialStatementToJSON(s *ast.AlterCredentialStatement) jsonNode {
61726230
return node
61736231
}
61746232

6233+
func alterDatabaseSetStatementToJSON(s *ast.AlterDatabaseSetStatement) jsonNode {
6234+
node := jsonNode{
6235+
"$type": "AlterDatabaseSetStatement",
6236+
"WithManualCutover": s.WithManualCutover,
6237+
"UseCurrent": s.UseCurrent,
6238+
}
6239+
if s.DatabaseName != nil {
6240+
node["DatabaseName"] = identifierToJSON(s.DatabaseName)
6241+
}
6242+
if len(s.Options) > 0 {
6243+
opts := make([]jsonNode, len(s.Options))
6244+
for i, opt := range s.Options {
6245+
opts[i] = databaseOptionToJSON(opt)
6246+
}
6247+
node["Options"] = opts
6248+
}
6249+
return node
6250+
}
6251+
6252+
func databaseOptionToJSON(opt ast.DatabaseOption) jsonNode {
6253+
switch o := opt.(type) {
6254+
case *ast.AcceleratedDatabaseRecoveryDatabaseOption:
6255+
return jsonNode{
6256+
"$type": "AcceleratedDatabaseRecoveryDatabaseOption",
6257+
"OptionKind": o.OptionKind,
6258+
"OptionState": o.OptionState,
6259+
}
6260+
default:
6261+
return jsonNode{"$type": "UnknownDatabaseOption"}
6262+
}
6263+
}
6264+
61756265
func indexDefinitionToJSON(idx *ast.IndexDefinition) jsonNode {
61766266
node := jsonNode{
61776267
"$type": "IndexDefinition",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"skip": true}
1+
{"skip": false}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"skip": true}
1+
{"skip": false}

0 commit comments

Comments
 (0)