-
Notifications
You must be signed in to change notification settings - Fork 345
/
sapase.go
57 lines (54 loc) · 1.47 KB
/
sapase.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
// Package sapase defines and registers usql's SAP ASE driver.
//
// See: https://github.com/thda/tds
package sapase
import (
"context"
"errors"
"regexp"
"strconv"
"strings"
"github.com/thda/tds" // DRIVER: tds
"github.com/xo/usql/drivers"
)
func init() {
endRE := regexp.MustCompile(`;?\s*$`)
drivers.Register("tds", drivers.Driver{
AllowMultilineComments: true,
RequirePreviousPassword: true,
LexerName: "tsql",
Version: func(ctx context.Context, db drivers.DB) (string, error) {
var ver string
err := db.QueryRowContext(ctx, `SELECT @@version`).Scan(&ver)
if err != nil {
return "", err
}
return ver, nil
},
ChangePassword: func(db drivers.DB, user, newpw, oldpw string) error {
if user != "" {
return errors.New("Cannot change password for another user")
}
_, err := db.Exec(`exec sp_password '` + oldpw + `', '` + newpw + `'`)
return err
},
Err: func(err error) (string, string) {
if e, ok := err.(tds.SybError); ok {
return strconv.Itoa(int(e.MsgNumber)), e.Message
}
msg := err.Error()
if i := strings.LastIndex(msg, "tds:"); i != -1 {
msg = msg[i:]
}
return "", msg
},
IsPasswordErr: func(err error) bool {
return strings.Contains(err.Error(), "Login failed")
},
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
},
})
}