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

Function extractStatement

backend/plugin/parser/mysql/restore.go:274–321  ·  view source on GitHub ↗
(statement string, backupItem *storepb.PriorBackupDetail_Item)

Source from the content-addressed store, hash-verified

272}
273
274func extractStatement(statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error) {
275 if backupItem == nil {
276 return "", errors.Errorf("backup item is nil")
277 }
278
279 list, err := SplitSQL(statement)
280 if err != nil {
281 return "", errors.Wrap(err, "failed to split sql")
282 }
283
284 start := 0
285 end := len(list) - 1
286 for i, item := range list {
287 if equalOrLess(item.Start, backupItem.StartPosition) {
288 start = i
289 }
290 }
291
292 for i := len(list) - 1; i >= 0; i-- {
293 if equalOrGreater(list[i].Start, backupItem.EndPosition) {
294 end = i
295 }
296 }
297
298 _, sourceDatabase, err := common.GetInstanceDatabaseID(backupItem.SourceTable.Database)
299 if err != nil {
300 return "", errors.Wrapf(err, "failed to get source database ID for %s", backupItem.SourceTable.Database)
301 }
302
303 var result []string
304 for i := start; i <= end; i++ {
305 stmtList, err := ParseMySQLOmni(list[i].Text)
306 if err != nil {
307 return "", errors.Wrap(err, "failed to parse sql")
308 }
309 containsSourceTable := false
310 for _, node := range stmtList.Items {
311 if containsTable(node, sourceDatabase, backupItem.SourceTable.Table) {
312 containsSourceTable = true
313 break
314 }
315 }
316 if containsSourceTable {
317 result = append(result, list[i].Text)
318 }
319 }
320 return strings.Join(result, ""), nil
321}
322
323// containsTable checks whether the given AST node references the specified table.
324func containsTable(node ast.Node, database, table string) bool {

Callers 1

GenerateRestoreSQLFunction · 0.70

Calls 8

GetInstanceDatabaseIDFunction · 0.92
ParseMySQLOmniFunction · 0.85
ErrorfMethod · 0.80
JoinMethod · 0.80
SplitSQLFunction · 0.70
equalOrLessFunction · 0.70
equalOrGreaterFunction · 0.70
containsTableFunction · 0.70

Tested by

no test coverage detected