Skip to content

Commit 6ba18da

Browse files
authored
Merge pull request #12 from hymkor/push-swumyroomwrx
Fix EOF handling and script execution issues - Fix: Correct handling of `io.EOF` during script execution (#12) - Suppress output of empty lines and leading/trailing spaces in script output (#12) - Fix: `CSVI` launched by `SELECT` in a script now terminates automatically with `>`, `q`, and `y` (#12)
2 parents 01b9d9f + 62d0c75 commit 6ba18da

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

commands.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sqlbless
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"io"
89
"strings"
@@ -14,7 +15,7 @@ import (
1415
"github.com/hymkor/sqlbless/internal/misc"
1516
)
1617

17-
func doSelect(ctx context.Context, ss *session, query string, v *spread.Viewer) error {
18+
func doSelect(ctx context.Context, ss *session, query string, v *spread.Viewer, pilot commandIn) error {
1819
var rows *sql.Rows
1920
var err error
2021
if ss.tx != nil {
@@ -34,9 +35,15 @@ func doSelect(ctx context.Context, ss *session, query string, v *spread.Viewer)
3435
v = newViewer(ss)
3536
}
3637
if ss.automatic() {
37-
v.Pilot = misc.CsviNoOperation{}
38+
v.Pilot = &misc.CsviNoOperation{}
39+
} else if a, ok := pilot.AutoPilotForCsvi(); ok {
40+
v.Pilot = a
3841
}
39-
return v.View(ctx, query, _rows, ss.termOut)
42+
err = v.View(ctx, query, _rows, ss.termOut)
43+
if errors.Is(err, io.EOF) {
44+
return nil
45+
}
46+
return err
4047
}
4148

4249
type canExec interface {
@@ -144,7 +151,7 @@ func doDescTables(ctx context.Context, ss *session, commandIn commandIn) error {
144151
rc, err := handler(e)
145152
if err == nil && rc.Quit && name != "" {
146153
action = func() error {
147-
return doDescColumns(ctx, ss, name)
154+
return doDescColumns(ctx, ss, name, commandIn)
148155
}
149156
}
150157
return rc, err
@@ -156,7 +163,7 @@ func doDescTables(ctx context.Context, ss *session, commandIn commandIn) error {
156163
if ss.Debug {
157164
fmt.Println(query)
158165
}
159-
err := doSelect(ctx, ss, query, v)
166+
err := doSelect(ctx, ss, query, v, commandIn)
160167
if err == nil && name != "" {
161168
fmt.Fprintln(ss.termErr)
162169
misc.Echo(ss.spool, name)
@@ -165,21 +172,21 @@ func doDescTables(ctx context.Context, ss *session, commandIn commandIn) error {
165172
return err
166173
}
167174

168-
func doDescColumns(ctx context.Context, ss *session, table string) error {
175+
func doDescColumns(ctx context.Context, ss *session, table string, commandIn commandIn) error {
169176
if ss.Dialect.SQLForColumns == "" {
170177
return fmt.Errorf("desc table: %w", ErrNotSupported)
171178
}
172179
query := ss.Dialect.BuildSQLForColumns(table)
173180
if ss.Debug {
174181
fmt.Println(query)
175182
}
176-
return doSelect(ctx, ss, query, newViewer(ss))
183+
return doSelect(ctx, ss, query, newViewer(ss), commandIn)
177184
}
178185

179186
func doDesc(ctx context.Context, ss *session, table string, commandIn commandIn) error {
180187
table = strings.TrimSpace(table)
181188
if table == "" {
182189
return doDescTables(ctx, ss, commandIn)
183190
}
184-
return doDescColumns(ctx, ss, table)
191+
return doDescColumns(ctx, ss, table, commandIn)
185192
}

internal/misc/csvinop.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,34 @@ type GetKeyAndSize interface {
1111
Size() (int, int, error)
1212
}
1313

14-
type CsviNoOperation struct{}
15-
16-
func (CsviNoOperation) Size() (int, int, error) {
17-
return 80, 25, nil
14+
type CsviNoOperation struct {
15+
text []string
1816
}
1917

20-
func (CsviNoOperation) Calibrate() error {
21-
return nil
18+
func (*CsviNoOperation) Size() (int, int, error) {
19+
return 80, 25, nil
2220
}
2321

24-
func (CsviNoOperation) GetKey() (string, error) {
25-
return "q", nil
22+
func (c *CsviNoOperation) GetKey() (string, error) {
23+
if len(c.text) <= 0 {
24+
c.text = []string{">", "q", "y", ""}
25+
}
26+
v := c.text[0]
27+
if v == "" {
28+
return "", io.EOF
29+
}
30+
c.text = c.text[1:]
31+
return v, nil
2632
}
2733

28-
func (CsviNoOperation) ReadLine(io.Writer, string, string, candidate.Candidate) (string, error) {
34+
func (*CsviNoOperation) ReadLine(io.Writer, string, string, candidate.Candidate) (string, error) {
2935
return "", nil
3036
}
3137

32-
func (CsviNoOperation) GetFilename(io.Writer, string, string) (string, error) {
38+
func (*CsviNoOperation) GetFilename(io.Writer, string, string) (string, error) {
3339
return "", nil
3440
}
3541

36-
func (CsviNoOperation) Close() error {
42+
func (*CsviNoOperation) Close() error {
3743
return nil
3844
}

loop.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (ss *session) Loop(ctx context.Context, commandIn commandIn) error {
9898
}
9999
lines, err := commandIn.Read(ctx)
100100
if err != nil {
101-
if err == io.EOF {
101+
if errors.Is(err, io.EOF) {
102102
if ss.tx != nil && !commandIn.CanCloseInTransaction() {
103103
fmt.Fprintln(ss.termErr, ErrTransactionIsNotClosed.Error())
104104
continue
@@ -161,7 +161,7 @@ func (ss *session) Loop(ctx context.Context, commandIn commandIn) error {
161161

162162
case "SELECT":
163163
misc.Echo(ss.spool, query)
164-
err = doSelect(ctx, ss, query, nil)
164+
err = doSelect(ctx, ss, query, nil, commandIn)
165165
case "ROLLBACK":
166166
misc.Echo(ss.spool, query)
167167
arg, _ = misc.CutField(arg)
@@ -216,7 +216,7 @@ func (ss *session) Loop(ctx context.Context, commandIn commandIn) error {
216216
default:
217217
misc.Echo(ss.spool, query)
218218
if q := ss.Dialect.IsQuerySQL; q != nil && q(query) {
219-
err = doSelect(ctx, ss, query, nil)
219+
err = doSelect(ctx, ss, query, nil, commandIn)
220220
} else {
221221
if ss.tx == nil {
222222
_, err = ss.conn.ExecContext(ctx, query)

release_note_en.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Support `SAVEPOINT` as a TCL command (#11)
66
- Support `ROLLBACK TO` (or `ROLLBACK TRANSACTION`) as a TCL command (#11)
77
- Require `;` after `ROLLBACK` to prevent accidental execution (#11)
8+
- Fix: Correct handling of `io.EOF` during script execution (#12)
9+
- Suppress output of empty lines and leading/trailing spaces in script output (#12)
10+
- Fix: `CSVI` launched by `SELECT` in a script now terminates automatically with `>`, `q`, and `y` (#12)
811

912
v0.25.0
1013
=======

release_note_ja.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- `SAVEPOINT` を TCL コマンドとしてサポート (#11)
66
- `ROLLBACK TO`(もしくは `ROLLBACK TRANSACTION`)を TCL コマンドとしてサポート (#11)
77
- 誤操作による実行を防ぐため、`ROLLBACK` には `;` を必須とした (#11)
8+
* スクリプト中の `SELECT` で起動した `CSVI` は、`>`, `q`, `y` の操作で自動的に終了するようにした。 (#12)
9+
* スクリプト実行時の出力で、空行や行頭・行末の空白が出力されないようにした。 (#12)
10+
* スクリプト実行時に `io.EOF` が誤ってエラーとして扱われていた問題を修正した。 (#12)
811

912
v0.25.0
1013
=======

script.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sqlbless
33
import (
44
"bufio"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -29,18 +30,25 @@ func (script *scriptIn) GetKey() (string, error) {
2930
}
3031

3132
func (script *scriptIn) AutoPilotForCsvi() (csvi.Pilot, bool) {
32-
return nil, false
33+
return &misc.CsviNoOperation{}, true
3334
}
3435

3536
func (script *scriptIn) Read(context.Context) ([]string, error) {
37+
if script.br == nil {
38+
return nil, io.EOF
39+
}
3640
var buffer strings.Builder
3741
quoted := 0
3842
for {
3943
ch, _, err := script.br.ReadRune()
40-
if err != nil {
44+
if errors.Is(err, io.EOF) {
4145
code := buffer.String()
42-
fmt.Fprintln(script.echo, code)
43-
return []string{code}, err
46+
fmt.Fprintln(script.echo, strings.TrimSpace(code))
47+
script.br = nil
48+
return []string{code}, nil
49+
}
50+
if err != nil {
51+
return nil, err
4452
}
4553
if ch == '\r' {
4654
continue
@@ -55,8 +63,7 @@ func (script *scriptIn) Read(context.Context) ([]string, error) {
5563
code := buffer.String()
5664
term := script.term
5765
if _, ok := misc.HasTerm(code, term); ok {
58-
println(code)
59-
fmt.Fprintln(script.echo, code)
66+
fmt.Fprintln(script.echo, strings.TrimSpace(code))
6067
return []string{code}, nil
6168
}
6269
}

0 commit comments

Comments
 (0)