-
Notifications
You must be signed in to change notification settings - Fork 9
/
shelldb.go
98 lines (80 loc) · 2.35 KB
/
shelldb.go
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
package shelldb
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"os/exec"
log "github.com/sirupsen/logrus"
)
type d struct {
}
func init() {
sql.Register("shelldb", &d{})
}
// Open a new connection to the shell driver.
func (d *d) Open(name string) (driver.Conn, error) {
return &conn{
name: name,
}, nil
}
type conn struct {
name string
}
// Exec a query against the shell driver. The query represents the command to
// run. The args are passed to the command as arguments.
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
return c.ExecContext(context.Background(), query, args)
}
type logWriter struct {
logger *log.Entry
}
func (lw logWriter) Write(p []byte) (n int, err error) {
lw.logger.Infof("%s", p)
return len(p), nil
}
// ExecContext a query against the shell driver. The query represents the command to
// run. The args are passed to the command as arguments. The context is used to
// cancel the command if the context is canceled.
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Value) (driver.Result, error) {
var cmd *exec.Cmd
if len(args) == 0 {
cmd = exec.CommandContext(ctx, query)
} else {
var argsStr []string
for _, v := range args {
argsStr = append(argsStr, fmt.Sprintf("%s", v))
}
cmd = exec.Command(query, argsStr...)
}
log := log.WithFields(log.Fields{
"shelldb_destination": c.name,
})
cmd.Stdout = logWriter{logger: log}
cmd.Stderr = logWriter{logger: log}
if err := cmd.Run(); err != nil {
return nil, err
}
return &result{}, nil
}
type result struct{}
// LastInsertId is not supported by the shell driver.
func (r *result) LastInsertId() (int64, error) {
return 0, fmt.Errorf("unsupported LastInsertId in shell driver")
}
// RowsAffected is not supported by the shell driver.
func (r *result) RowsAffected() (int64, error) {
return 0, fmt.Errorf("unsupported RowsAffected in shell driver")
}
// Prepare is not supported by the shell driver.
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return nil, fmt.Errorf("unsupported Prepare in shell driver")
}
// Begin is not supported by the shell driver.
func (c *conn) Begin() (driver.Tx, error) {
return nil, fmt.Errorf("unsupported Begin in shell driver")
}
// Close implements the driver.Conn interface. This is a no-op for the shell.
func (c *conn) Close() error {
return nil
}