Skip to content

Commit

Permalink
Adding usql_trim option to odbc driver to enable trimming end ;
Browse files Browse the repository at this point in the history
Closes #436.
  • Loading branch information
kenshaw committed Jan 17, 2024
1 parent 4d071aa commit fda6939
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 26 deletions.
3 changes: 2 additions & 1 deletion drivers/athena/athena.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"regexp"

_ "github.com/uber/athenadriver/go" // DRIVER: awsathena
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
)

func init() {
endRE := regexp.MustCompile(`;?\s*$`)
drivers.Register("awsathena", drivers.Driver{
AllowMultilineComments: true,
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
Process: func(_ *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
Expand Down
3 changes: 2 additions & 1 deletion drivers/csvq/csvq.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (

"github.com/mithrandie/csvq-driver" // DRIVER
"github.com/mithrandie/csvq/lib/query"
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
)

func init() {
csvq.SetStdout(query.NewDiscard())
drivers.Register("csvq", drivers.Driver{
AllowMultilineComments: true,
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
Process: func(_ *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
typ, q := drivers.QueryExecType(prefix, sqlstr)
if strings.HasPrefix(prefix, "SHOW") {
csvq.SetStdout(os.Stdout)
Expand Down
4 changes: 2 additions & 2 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Driver struct {
// IsPasswordErr will be used by IsPasswordErr if defined.
IsPasswordErr func(error) bool
// Process will be used by Process if defined.
Process func(string, string) (string, string, bool, error)
Process func(*dburl.URL, string, string) (string, string, bool, error)
// RowsAffected will be used by RowsAffected if defined.
RowsAffected func(sql.Result) (int64, error)
// Err will be used by Error.Error if defined.
Expand Down Expand Up @@ -246,7 +246,7 @@ func User(ctx context.Context, u *dburl.URL, db DB) (string, error) {
// Process processes the sql query for a driver.
func Process(u *dburl.URL, prefix, sqlstr string) (string, string, bool, error) {
if d, ok := drivers[u.Driver]; ok && d.Process != nil {
a, b, c, err := d.Process(prefix, sqlstr)
a, b, c, err := d.Process(u, prefix, sqlstr)
return a, b, c, WrapErr(u.Driver, err)
}
typ, q := QueryExecType(prefix, sqlstr)
Expand Down
15 changes: 15 additions & 0 deletions drivers/odbc/odbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@
package odbc

import (
"regexp"
"strings"

"github.com/alexbrainman/odbc" // DRIVER
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
)

func init() {
dburl.OdbcIgnoreQueryPrefixes = []string{"usql_"}
endRE := regexp.MustCompile(`;?\s*$`)
endAnchorRE := regexp.MustCompile(`(?i)\send\s*;\s*$`)
drivers.Register("odbc", drivers.Driver{
LexerName: "tsql",
Process: func(u *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
// trim last ; but only when not END;
if s := strings.ToLower(u.Query().Get("usql_trim")); s != "" && s != "off" && s != "0" && s != "false" {
if !endAnchorRE.MatchString(sqlstr) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
}
}
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
},
IsPasswordErr: func(err error) bool {
if e, ok := err.(*odbc.Error); ok {
msg := strings.ToLower(e.Error())
Expand Down
2 changes: 1 addition & 1 deletion drivers/oracle/orshared/orshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Register(name string, err func(error) (string, string), isPasswordErr func(
},
Err: err,
IsPasswordErr: isPasswordErr,
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
Process: func(_ *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
if !endAnchorRE.MatchString(sqlstr) {
// trim last ; but only when not END;
sqlstr = endRE.ReplaceAllString(sqlstr, "")
Expand Down
3 changes: 2 additions & 1 deletion drivers/presto/presto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"regexp"

_ "github.com/prestodb/presto-go-client/presto" // DRIVER
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
)

func init() {
endRE := regexp.MustCompile(`;?\s*$`)
drivers.Register("presto", drivers.Driver{
AllowMultilineComments: true,
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
Process: func(_ *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
Expand Down
3 changes: 2 additions & 1 deletion drivers/sapase/sapase.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/thda/tds" // DRIVER: tds
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func init() {
IsPasswordErr: func(err error) bool {
return strings.Contains(err.Error(), "Login failed")
},
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
Process: func(_ *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
Expand Down
3 changes: 2 additions & 1 deletion drivers/trino/trino.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"

_ "github.com/trinodb/trino-go-client/trino" // DRIVER
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
"github.com/xo/usql/drivers/metadata"
infos "github.com/xo/usql/drivers/metadata/informationschema"
Expand Down Expand Up @@ -39,7 +40,7 @@ func init() {
}
drivers.Register("trino", drivers.Driver{
AllowMultilineComments: true,
Process: func(prefix string, sqlstr string) (string, string, bool, error) {
Process: func(_ *dburl.URL, prefix string, sqlstr string) (string, string, bool, error) {
sqlstr = endRE.ReplaceAllString(sqlstr, "")
typ, q := drivers.QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ClickHouse/clickhouse-go/v2 v2.17.1
github.com/IBM/nzgo/v12 v12.0.8
github.com/MichaelS11/go-cql-driver v0.1.1
github.com/SAP/go-hdb v1.7.2
github.com/SAP/go-hdb v1.7.4
github.com/VoltDB/voltdb-client-go v1.0.15
github.com/alecthomas/chroma/v2 v2.12.0
github.com/alecthomas/kingpin/v2 v2.4.0
Expand All @@ -32,7 +32,7 @@ require (
github.com/google/goexpect v0.0.0-20210430020637-ab937bf7fd6f
github.com/googleapis/go-sql-spanner v1.1.1
github.com/jackc/pgconn v1.14.1
github.com/jackc/pgx/v5 v5.5.1
github.com/jackc/pgx/v5 v5.5.2
github.com/jeandeaual/go-locale v0.0.0-20220711133428-7de61946b173
github.com/jmrobles/h2go v0.5.0
github.com/kenshaw/rasterm v0.1.8
Expand All @@ -48,14 +48,14 @@ require (
github.com/ory/dockertest/v3 v3.10.0
github.com/prestodb/presto-go-client v0.0.0-20230524183650-a1a0bac0f63e
github.com/proullon/ramsql v0.1.3
github.com/sijms/go-ora/v2 v2.8.5
github.com/sijms/go-ora/v2 v2.8.6
github.com/snowflakedb/gosnowflake v1.7.1
github.com/thda/tds v0.1.7
github.com/trinodb/trino-go-client v0.313.0
github.com/uber/athenadriver v1.1.14
github.com/vertica/vertica-sql-go v1.3.3
github.com/xo/dburl v0.20.2
github.com/xo/tblfmt v0.10.3
github.com/xo/dburl v0.21.0
github.com/xo/tblfmt v0.10.4
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e
github.com/ydb-platform/ydb-go-sdk/v3 v3.54.3
github.com/yookoala/realpath v1.0.0
Expand Down Expand Up @@ -287,7 +287,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ github.com/Microsoft/hcsshim v0.11.1/go.mod h1:nFJmaO4Zr5Y7eADdFOpYswDDlNVbvcIJJ
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/SAP/go-hdb v1.7.2 h1:Jq9kA9GHIOyFntvYjOA+JLOxzaJAVdHkrBFttuQWQxo=
github.com/SAP/go-hdb v1.7.2/go.mod h1:jzZ114xBER7cPp4ey+xfAYxUGFkujBKIB9fKBZww/2M=
github.com/SAP/go-hdb v1.7.4 h1:sLjur7axWA9gAfovQswlZeicnPxWm6WOEza+oScK9f0=
github.com/SAP/go-hdb v1.7.4/go.mod h1:v38PtG76h9ScNGJzPNdGhqBgwAdyeC9ICwDYRPwRbhw=
github.com/UNO-SOFT/zlog v0.8.1 h1:TEFkGJHtUfTRgMkLZiAjLSHALjwSBdw6/zByMC5GJt4=
github.com/UNO-SOFT/zlog v0.8.1/go.mod h1:yqFOjn3OhvJ4j7ArJqQNA+9V+u6t9zSAyIZdWdMweWc=
github.com/VoltDB/voltdb-client-go v1.0.15 h1:G7rZxKiemYkaYZLoLamhRnAOGyq5wlyqPefCAAflB/0=
Expand Down Expand Up @@ -769,8 +769,8 @@ github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrU
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI=
github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/pgx/v5 v5.5.2 h1:iLlpgp4Cp/gC9Xuscl7lFL1PhhW+ZLtXZcrfCt4C3tA=
github.com/jackc/pgx/v5 v5.5.2/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
Expand Down Expand Up @@ -990,8 +990,8 @@ github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9Nz
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sijms/go-ora/v2 v2.8.5 h1:8y20J75NWuZ3+pWfm04j+QZIra2olIFwagTa0gHbm5Q=
github.com/sijms/go-ora/v2 v2.8.5/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
github.com/sijms/go-ora/v2 v2.8.6 h1:sq907Z5cno0YIXidOYM85tiQFFVhhgssw09IJPG0WG4=
github.com/sijms/go-ora/v2 v2.8.6/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
Expand Down Expand Up @@ -1063,11 +1063,11 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/xo/dburl v0.20.2 h1:59zqIzahtfQ/X9E6fyp1ziHwjYEFy65opjpyQPmZC7w=
github.com/xo/dburl v0.20.2/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
github.com/xo/dburl v0.21.0 h1:PsA4R49rGVWMMR41nuLOclipbvMib71ht1uy6UMdv+M=
github.com/xo/dburl v0.21.0/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
github.com/xo/tblfmt v0.0.0-20190609041254-28c54ec42ce8/go.mod h1:3U5kKQdIhwACye7ml3acccHmjGExY9WmUGU7rnDWgv0=
github.com/xo/tblfmt v0.10.3 h1:Fb2Le5wvzBjOwA/KFRpP5bZo+iR9cZnzSAN3tULoUPI=
github.com/xo/tblfmt v0.10.3/go.mod h1:W6oqhxtqDOEMkrS2GPO0zmcww7ozM2E6BRv3kNfXd24=
github.com/xo/tblfmt v0.10.4 h1:CAVf3/Q2tOKmzLSBxb4k15z+vTehgQYjWT9G/wkKigM=
github.com/xo/tblfmt v0.10.4/go.mod h1:RUXY5LkdeXT2PGzzD3+nC5BYUjUsc/s9pFbqr2ddhgE=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
Expand Down Expand Up @@ -1177,8 +1177,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e h1:723BNChdd0c2Wk6WOE320qGBiPtYx0F0Bbm1kriShfE=
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
Expand Down

0 comments on commit fda6939

Please sign in to comment.