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

Function ParseTransactionConfig

backend/plugin/parser/base/transaction_mode.go:33–94  ·  view source on GitHub ↗

ParseTransactionConfig extracts both transaction mode and isolation level directives from the SQL script. It scans comment lines at the top of the file for directives: - -- txn-mode = on|off - -- txn-isolation = READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE Directives can appear in an

(script string)

Source from the content-addressed store, hash-verified

31// Scanning stops at the first non-comment, non-empty line.
32// Returns the transaction configuration and the SQL script without the directives.
33func ParseTransactionConfig(script string) (common.TransactionConfig, string) {
34 config := common.TransactionConfig{
35 Mode: common.TransactionModeUnspecified,
36 Isolation: common.IsolationLevelDefault,
37 }
38
39 lines := strings.Split(script, "\n")
40 if len(lines) == 0 {
41 return config, script
42 }
43
44 directiveLines := make(map[int]bool)
45
46 // Scan lines from the top, stopping at first non-comment/non-empty line
47 for i, line := range lines {
48 trimmed := strings.TrimSpace(line)
49
50 // Skip empty lines
51 if trimmed == "" {
52 continue
53 }
54
55 // If it's not a comment, stop scanning for directives
56 if !strings.HasPrefix(trimmed, "--") {
57 break
58 }
59
60 // Check for transaction mode directive
61 if matches := txnModeDirectiveRegex.FindStringSubmatch(line); len(matches) == 2 {
62 mode := strings.ToLower(matches[1])
63 config.Mode = common.TransactionMode(mode)
64 directiveLines[i] = true
65 continue
66 }
67
68 // Check for isolation level directive
69 if matches := txnIsolationDirectiveRegex.FindStringSubmatch(line); len(matches) == 2 {
70 isolation := strings.ToUpper(matches[1])
71 // Normalize the spacing
72 isolation = strings.ReplaceAll(isolation, " ", " ")
73 // Note: We set the value as-is here. Invalid values will be caught
74 // by the specific database driver during execution, allowing each
75 // database to validate according to its supported levels.
76 config.Isolation = common.IsolationLevel(isolation)
77 directiveLines[i] = true
78 continue
79 }
80 }
81
82 // Remove directive lines from the script
83 if len(directiveLines) > 0 {
84 var remainingLines []string
85 for i, line := range lines {
86 if !directiveLines[i] {
87 remainingLines = append(remainingLines, line)
88 }
89 }
90 return config, strings.TrimSpace(strings.Join(remainingLines, "\n"))

Callers 12

ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92
ExecuteMethod · 0.92

Calls 3

TransactionModeTypeAlias · 0.92
IsolationLevelTypeAlias · 0.92
JoinMethod · 0.80

Tested by

no test coverage detected