-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.go
More file actions
192 lines (175 loc) · 4.4 KB
/
commands.go
File metadata and controls
192 lines (175 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package sqlbless
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"strings"
"github.com/hymkor/csvi"
"github.com/hymkor/sqlbless/spread"
"github.com/hymkor/sqlbless/internal/misc"
)
func doSelect(ctx context.Context, ss *session, query string, v *spread.Viewer, pilot commandIn, args ...any) error {
var rows *sql.Rows
var err error
if ss.tx != nil {
rows, err = ss.tx.QueryContext(ctx, query, args...)
} else {
rows, err = ss.conn.QueryContext(ctx, query, args...)
}
if err != nil {
return fmt.Errorf("query: %[1]w (%[1]T)", err)
}
_rows, ok := misc.RowsHasNext(rows)
if !ok {
rows.Close()
return ErrNoDataFound
}
if v == nil {
v = newViewer(ss)
}
if ss.automatic() {
v.Pilot = &misc.CsviNoOperation{}
} else if a, ok := pilot.AutoPilotForCsvi(); ok {
v.Pilot = a
}
err = v.View(ctx, query, _rows, ss.termOut)
if errors.Is(err, io.EOF) {
return nil
}
return err
}
type canExec interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
func doDML(ctx context.Context, conn canExec, query string, args []any, w io.Writer) (int64, error) {
result, err := conn.ExecContext(ctx, query, args...)
if err != nil {
return 0, fmt.Errorf("exec: %[1]w (%[1]T)", err)
}
count, err := result.RowsAffected()
if err != nil {
return 0, fmt.Errorf("RowsAffected: %[1]w (%[1]T)", err)
}
fmt.Fprintf(w, "%d record(s) updated.\n", count)
return count, nil
}
func doTCL(ctx context.Context, ss *session, query string) error {
if ss.tx == nil {
return ErrNoActiveTransaction
}
_, err := ss.tx.ExecContext(ctx, query)
if err == nil {
fmt.Fprintln(ss.stdErr, "Ok")
}
return err
}
func (ss *session) commit() error {
var err error
if ss.tx != nil {
err = ss.tx.Commit()
ss.tx = nil
}
if err == nil {
fmt.Fprintln(ss.stdErr, "Commit complete.")
}
return err
}
func (ss *session) rollback() error {
var err error
if ss.tx != nil {
err = ss.tx.Rollback()
ss.tx = nil
}
if err == nil {
fmt.Fprintln(ss.stdErr, "Rollback complete.")
}
return err
}
func (ss *session) beginTx(ctx context.Context, w io.Writer) error {
if ss.tx != nil {
return nil
}
fmt.Fprintln(w, "Starts a transaction")
var err error
ss.tx, err = ss.conn.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("BeginTx: %[1]w (%[1]T)", err)
}
return nil
}
func doDescTables(ctx context.Context, ss *session, commandIn commandIn) error {
if ss.Dialect.SQLForTables == "" {
return fmt.Errorf("desc: %w", ErrNotSupported)
}
query := ss.Dialect.BuildSQLForTables()
var name string
handler := func(e *csvi.KeyEventArgs) (*csvi.CommandResult, error) {
if e.CurrentRow().Index() == 0 {
return &csvi.CommandResult{}, nil
}
header := e.Front()
for i, c := range header.Cell {
if strings.EqualFold(c.Text(), ss.Dialect.TableNameField) {
name = e.CurrentRow().Cell[i].Text()
return &csvi.CommandResult{Quit: true}, nil
}
}
return &csvi.CommandResult{}, nil
}
action := func() error { return nil }
rKey := spread.KeyBinding{
Key: "r",
Handler: func(e *csvi.KeyEventArgs) (*csvi.CommandResult, error) {
rc, err := handler(e)
if err == nil && rc.Quit && name != "" {
action = func() error {
return doEdit(ctx, ss, "edit "+ss.Dialect.EncloseIdentifier(name), commandIn)
}
}
return rc, err
},
}
enterKey := spread.KeyBinding{
Key: "\r",
Handler: func(e *csvi.KeyEventArgs) (*csvi.CommandResult, error) {
rc, err := handler(e)
if err == nil && rc.Quit && name != "" {
action = func() error {
return doDescColumns(ctx, ss, name, commandIn)
}
}
return rc, err
},
}
v := newViewer(ss)
v.OnEvents = append(v.OnEvents, rKey, enterKey)
if ss.Debug {
fmt.Println(query)
}
err := doSelect(ctx, ss, query, v, commandIn)
if err == nil && name != "" {
fmt.Fprintln(ss.termErr)
misc.Echo(ss.spool, name)
err = action()
}
return err
}
func doDescColumns(ctx context.Context, ss *session, table string, commandIn commandIn) error {
if ss.Dialect.SQLForColumns == "" {
return fmt.Errorf("desc table: %w", ErrNotSupported)
}
query := ss.Dialect.BuildSQLForColumns(table)
if ss.Debug {
fmt.Println(query)
}
return doSelect(ctx, ss, query, newViewer(ss), commandIn, table)
}
func doDesc(ctx context.Context, ss *session, table string, commandIn commandIn) error {
table = strings.TrimSpace(table)
if table == "" {
return doDescTables(ctx, ss, commandIn)
}
return doDescColumns(ctx, ss, table, commandIn)
}