diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-13 23:21:39 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-13 23:21:39 -0500 |
| commit | 2212743dcf34fa34aeebbb2292152cc4d9d6dcdf (patch) | |
| tree | b3b2370325f62685102ba6be01ff985ddab30b73 /internal/sqlparse | |
| parent | Update configuration format and env names (diff) | |
| download | cli-2212743dcf34fa34aeebbb2292152cc4d9d6dcdf.zip | |
Add support for turning off transaction execution
Diffstat (limited to '')
| -rw-r--r-- | internal/sqlparse/sqlparse.go | 44 | ||||
| -rw-r--r-- | internal/sqlparse/sqlparse_test.go | 56 |
2 files changed, 100 insertions, 0 deletions
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go new file mode 100644 index 0000000..9ded618 --- /dev/null +++ b/internal/sqlparse/sqlparse.go @@ -0,0 +1,44 @@ +package sqlparse + +import ( + "bufio" + "io" + "strings" +) + +type SqlParser struct { + reader io.Reader +} + +type ExecutionOptions struct { + UseTransaction bool +} + +func NewSqlParser(reader io.Reader) *SqlParser { + return &SqlParser{reader: reader} +} + +func (sp *SqlParser) Parse() (*ExecutionOptions, error) { + options := &ExecutionOptions{UseTransaction: true} + + scanner := bufio.NewScanner(sp.reader) + if scanner.Scan() { + firstLine := strings.ToLower(strings.TrimSpace(scanner.Text())) + if strings.HasPrefix(firstLine, "-- bolt:") { + parts := strings.Split(firstLine, " ") + // Skip the first two parts ("--" and "bolt:") + for _, part := range parts[2:] { + if part == "no-transaction" { + options.UseTransaction = false + break + } + } + } + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return options, nil +} diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go new file mode 100644 index 0000000..51b93b1 --- /dev/null +++ b/internal/sqlparse/sqlparse_test.go @@ -0,0 +1,56 @@ +package sqlparse_test + +import ( + "strings" + "testing" + + "github.com/eugenetriguba/bolt/internal/sqlparse" + "gotest.tools/v3/assert" +) + +func TestSqlParser_Parse(t *testing.T) { + type test struct { + buffer string + expectedExecutionOptions *sqlparse.ExecutionOptions + } + + tests := []test{ + { + buffer: "-- bolt: no-transaction", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, + }, + { + buffer: "-- BOLT: no-transaction", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, + }, + { + buffer: "\n-- bolt: no-transaction", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + }, + { + buffer: "", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + }, + { + buffer: "--bolt: no-transaction", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + }, + { + buffer: "-- bolt : no-transaction", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + }, + { + buffer: "-- bolt: no-transaction no-transaction", + expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, + }, + } + + for _, tc := range tests { + sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer)) + + execOptions, err := sqlParser.Parse() + assert.NilError(t, err) + + assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions) + } +} |