Skip to content

Commit 01b9d9f

Browse files
authored
Merge pull request #11 from hymkor/push-tknomrnlswvt
Add support for SAVEPOINT and ROLLBACK TO - Support `SAVEPOINT` as a TCL command (#11) - Support `ROLLBACK TO` (or `ROLLBACK TRANSACTION`) as a TCL command (#11) - Require `;` after `ROLLBACK` to prevent accidental execution (#11)
2 parents 10fbe0e + 24ec887 commit 01b9d9f

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

commands.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ func doDML(ctx context.Context, conn canExec, query string, args []any, w io.Wri
5656
return count, nil
5757
}
5858

59+
func doTCL(ctx context.Context, ss *session, query string) error {
60+
if ss.tx == nil {
61+
return ErrNoActiveTransaction
62+
}
63+
_, err := ss.tx.ExecContext(ctx, query)
64+
if err == nil {
65+
fmt.Fprintln(ss.stdErr, "Ok")
66+
}
67+
return err
68+
}
69+
5970
func (ss *session) commit() error {
6071
var err error
6172
if ss.tx != nil {

interactive.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,17 @@ func newReservedWordPattern(list ...string) reserveWordPattern {
4949
var o = struct{}{}
5050

5151
var oneLineCommands = map[string]struct{}{
52-
`COMMIT`: o,
53-
`DESC`: o,
54-
`EDIT`: o,
55-
`EXIT`: o,
56-
`HISTORY`: o,
57-
`HOST`: o,
58-
`QUIT`: o,
59-
`REM`: o,
60-
`ROLLBACK`: o,
61-
`SPOOL`: o,
62-
`START`: o,
63-
`\D`: o,
52+
`COMMIT`: o,
53+
`DESC`: o,
54+
`EDIT`: o,
55+
`EXIT`: o,
56+
`HISTORY`: o,
57+
`HOST`: o,
58+
`QUIT`: o,
59+
`REM`: o,
60+
`SPOOL`: o,
61+
`START`: o,
62+
`\D`: o,
6463
}
6564

6665
func isOneLineCommand(cmdLine string) bool {
@@ -122,7 +121,7 @@ func (ss *session) newInteractiveIn() *interactiveIn {
122121
editor.ResetColor = "\x1B[0m"
123122
editor.DefaultColor = "\x1B[39;49;1m"
124123
editor.Highlight = []readline.Highlight{
125-
{Pattern: newReservedWordPattern("HOST", "ALTER", "COMMIT", "CREATE", "DELETE", "DESC", "DROP", "EXIT", "HISTORY", "INSERT", "QUIT", "REM", "ROLLBACK", "SELECT", "SPOOL", "START", "TRUNCATE", "UPDATE", "AND", "FROM", "INTO", "OR", "WHERE"), Sequence: "\x1B[36;49;1m"},
124+
{Pattern: newReservedWordPattern("HOST", "ALTER", "COMMIT", "CREATE", "DELETE", "DESC", "DROP", "EXIT", "HISTORY", "INSERT", "QUIT", "REM", "ROLLBACK", "SELECT", "SPOOL", "START", "TRUNCATE", "UPDATE", "AND", "FROM", "INTO", "OR", "WHERE", "SAVEPOINT", "TO", "TRANSACTION"), Sequence: "\x1B[36;49;1m"},
126125
{Pattern: regexp.MustCompile(`[0-9]+`), Sequence: "\x1B[35;49;1m"},
127126
{Pattern: regexp.MustCompile(`/\*.*?\*/`), Sequence: "\x1B[33;49;22m"},
128127
{Pattern: regexp.MustCompile(`"[^"]*"|"[^"]*$`), Sequence: "\x1B[31;49;1m"},

internal/sqlcompletion/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func getSqlCommands() []string {
3030
"quit",
3131
"rem",
3232
"rollback",
33+
"savepoint",
3334
"select",
3435
"spool",
3536
"start",
@@ -108,6 +109,13 @@ func (C *completeType) getCandidates(ctx context.Context, fields []string) ([]st
108109
v, _ := completion.PathComplete(fields[:i+1])
109110
return v
110111
}
112+
} else if strings.EqualFold(word, "rollback") {
113+
tableListNow = false
114+
lastKeywordAt = i
115+
nextKeyword = nil
116+
candidates = func() []string {
117+
return []string{"to", "transaction"}
118+
}
111119
} else {
112120
if tableListNow && i < len(fields)-1 {
113121
tableNameInline = append(tableNameInline, word)

loop.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ var (
7272
ErrBeginIsNotSupported = errors.New("'BEGIN' is not supported; transactions are managed automatically")
7373
ErrNoDataFound = errors.New("no data found")
7474
ErrNotSupported = errors.New("not supported")
75+
ErrInvalidRollback = errors.New("invalid ROLLBACK syntax: expected 'TO' or 'TRANSACTION'")
76+
ErrNoActiveTransaction = errors.New("no active transaction")
7577
)
7678

7779
func (ss *session) prompt(w io.Writer, i int) (int, error) {
@@ -160,6 +162,19 @@ func (ss *session) Loop(ctx context.Context, commandIn commandIn) error {
160162
case "SELECT":
161163
misc.Echo(ss.spool, query)
162164
err = doSelect(ctx, ss, query, nil)
165+
case "ROLLBACK":
166+
misc.Echo(ss.spool, query)
167+
arg, _ = misc.CutField(arg)
168+
if arg == "" {
169+
err = ss.rollback()
170+
} else if strings.EqualFold(arg, "TO") || strings.EqualFold(arg, "TRANSACTION") {
171+
err = doTCL(ctx, ss, query)
172+
} else {
173+
err = ErrInvalidRollback
174+
}
175+
case "SAVEPOINT":
176+
misc.Echo(ss.spool, query)
177+
doTCL(ctx, ss, query)
163178
case "DELETE", "INSERT", "UPDATE", "MERGE":
164179
misc.Echo(ss.spool, query)
165180
isNewTx := (ss.tx == nil)
@@ -174,9 +189,6 @@ func (ss *session) Loop(ctx context.Context, commandIn commandIn) error {
174189
case "COMMIT":
175190
misc.Echo(ss.spool, query)
176191
err = ss.commit()
177-
case "ROLLBACK":
178-
misc.Echo(ss.spool, query)
179-
err = ss.rollback()
180192
case "EXIT", "QUIT":
181193
if ss.tx == nil || commandIn.CanCloseInTransaction() {
182194
return nil

release_note_en.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Refactor `dialect` subpackage: rename fields and methods for clarity (#8)
44
- Updated `go-readline-ny` to v1.12.2 and `go-ttyadapter` to v0.2.0, and switched API calls to use `go-ttyadapter`.(#9)
5+
- Support `SAVEPOINT` as a TCL command (#11)
6+
- Support `ROLLBACK TO` (or `ROLLBACK TRANSACTION`) as a TCL command (#11)
7+
- Require `;` after `ROLLBACK` to prevent accidental execution (#11)
58

69
v0.25.0
710
=======

release_note_ja.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- サブパッケージ `dialect` をリファクタリング: フィールド・メソッドを改名 (#8)
44
- `go-readline-ny` を v1.12.2、`go-ttyadapter` を v0.2.0 に更新し、対応する API 呼び出しを `go-ttyadapter` 側に切り替えた。(#9)
5+
- `SAVEPOINT` を TCL コマンドとしてサポート (#11)
6+
- `ROLLBACK TO`(もしくは `ROLLBACK TRANSACTION`)を TCL コマンドとしてサポート (#11)
7+
- 誤操作による実行を防ぐため、`ROLLBACK` には `;` を必須とした (#11)
58

69
v0.25.0
710
=======

0 commit comments

Comments
 (0)