MCPcopy Index your code
hub / github.com/bytebase/bytebase / extractSQL

Function extractSQL

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

Source from the content-addressed store, hash-verified

337}
338
339func extractSQL(statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error) {
340 if backupItem == nil {
341 return "", errors.New("backup item is nil")
342 }
343
344 list, err := SplitSQL(statement)
345 if err != nil {
346 return "", errors.Wrapf(err, "failed to split SQL")
347 }
348
349 start := 0
350 end := len(list) - 1
351 for i, item := range list {
352 if equalOrLess(item.Start, backupItem.StartPosition) {
353 start = i
354 }
355 }
356
357 for i := len(list) - 1; i >= 0; i-- {
358 if equalOrGreater(list[i].Start, backupItem.EndPosition) {
359 end = i
360 }
361 }
362
363 _, sourceDatabase, err := common.GetInstanceDatabaseID(backupItem.SourceTable.Database)
364 if err != nil {
365 return "", errors.Wrapf(err, "failed to get source database ID for %s", backupItem.SourceTable.Database)
366 }
367
368 var result []string
369 for i := start; i <= end; i++ {
370 containsSourceTable := false
371 tables, err := prepareTransformation(sourceDatabase, list[i].Text)
372 if err != nil {
373 if err == errNoBackupableDML {
374 continue
375 }
376 return "", errors.Wrap(err, "failed to prepare transformation")
377 }
378 for _, table := range tables {
379 if table.table.Schema == sourceDatabase && table.table.Table == backupItem.SourceTable.Table {
380 containsSourceTable = true
381 break
382 }
383 }
384 if containsSourceTable {
385 result = append(result, normalizeExtractedRestoreSQL(list[i].Text))
386 }
387 }
388 return strings.Join(result, "\n"), nil
389}
390
391func normalizeExtractedRestoreSQL(statement string) string {
392 return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(statement), ";"))

Callers 1

GenerateRestoreSQLFunction · 0.85

Calls 7

GetInstanceDatabaseIDFunction · 0.92
JoinMethod · 0.80
SplitSQLFunction · 0.70
equalOrLessFunction · 0.70
equalOrGreaterFunction · 0.70
prepareTransformationFunction · 0.70

Tested by

no test coverage detected