MCPcopy Create free account
hub / github.com/bytebase/bytebase / GenerateRestoreSQL

Function GenerateRestoreSQL

backend/plugin/parser/tsql/restore.go:25–65  ·  view source on GitHub ↗
(ctx context.Context, rCtx base.RestoreContext, statement string, backupItem *storepb.PriorBackupDetail_Item)

Source from the content-addressed store, hash-verified

23}
24
25func GenerateRestoreSQL(ctx context.Context, rCtx base.RestoreContext, statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error) {
26 originalSQL, err := extractStatement(statement, backupItem)
27 if err != nil {
28 return "", errors.Errorf("failed to extract single SQL: %v", err)
29 }
30
31 if len(originalSQL) == 0 {
32 return "", errors.Errorf("no original SQL")
33 }
34
35 parsedStatements, err := parseTSQLStatements(statement)
36 if err != nil {
37 return "", err
38 }
39
40 // Find the AST that contains the statement at the backup position
41 var targetResult ast.Node
42 for _, parsedStatement := range parsedStatements {
43 node, ok := GetOmniNode(parsedStatement.AST)
44 if !ok || node == nil || !isRestorableDML(node) {
45 continue
46 }
47 start, end := statementPositions(parsedStatement.Start, parsedStatement.Text, dmlNodeLoc(node))
48 if inRange(start, end, backupItem.StartPosition, backupItem.EndPosition) {
49 targetResult = node
50 break
51 }
52 }
53
54 if targetResult == nil {
55 return "", errors.Errorf("could not find statement at position (line %d:%d - %d:%d)",
56 backupItem.StartPosition.Line, backupItem.StartPosition.Column,
57 backupItem.EndPosition.Line, backupItem.EndPosition.Column)
58 }
59
60 sqlForComment, truncated := common.TruncateString(originalSQL, maxCommentLength)
61 if truncated {
62 sqlForComment += "..."
63 }
64 return doGenerate(ctx, rCtx, sqlForComment, targetResult, backupItem)
65}
66
67func doGenerate(ctx context.Context, rCtx base.RestoreContext, sqlForComment string, node ast.Node, backupItem *storepb.PriorBackupDetail_Item) (string, error) {
68 _, sourceDatabase, err := common.GetInstanceDatabaseID(backupItem.SourceTable.Database)

Callers 3

TestRestoreFunction · 0.70

Calls 10

TruncateStringFunction · 0.92
parseTSQLStatementsFunction · 0.85
isRestorableDMLFunction · 0.85
statementPositionsFunction · 0.85
dmlNodeLocFunction · 0.85
ErrorfMethod · 0.80
extractStatementFunction · 0.70
GetOmniNodeFunction · 0.70
inRangeFunction · 0.70
doGenerateFunction · 0.70

Tested by 3

TestRestoreFunction · 0.56