Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Jan 6, 2021
1 parent d6cc163 commit af85899
Show file tree
Hide file tree
Showing 57 changed files with 108 additions and 898 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-2020 Kenneth Shaw
Copyright (c) 2016-2021 Kenneth Shaw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 1 addition & 3 deletions drivers/adodb/adodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package adodb
import (
"database/sql"

// DRIVER: adodb
_ "github.com/mattn/go-adodb"

_ "github.com/mattn/go-adodb" // DRIVER: adodb
"github.com/xo/usql/drivers"
)

Expand Down
4 changes: 1 addition & 3 deletions drivers/athena/athena.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package athena
import (
"regexp"

// DRIVER: athena
_ "github.com/uber/athenadriver/go"

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

Expand Down
5 changes: 1 addition & 4 deletions drivers/avatica/avatica.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package avatica
import (
"strconv"

// DRIVER: avatica
_ "github.com/apache/calcite-avatica-go/v5"

_ "github.com/apache/calcite-avatica-go/v5" // DRIVER: avatica
avaticaerrors "github.com/apache/calcite-avatica-go/v5/errors"

"github.com/xo/usql/drivers"
)

Expand Down
4 changes: 1 addition & 3 deletions drivers/bigquery/bigquery.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package bigquery

import (
// DRIVER: bigquery
_ "gorm.io/driver/bigquery/driver"

"github.com/xo/usql/drivers"
_ "gorm.io/driver/bigquery/driver" // DRIVER: bigquery
)

func init() {
Expand Down
10 changes: 4 additions & 6 deletions drivers/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import (
"regexp"
"strings"

// DRIVER: cql
cql "github.com/MichaelS11/go-cql-driver"

cql "github.com/MichaelS11/go-cql-driver" // DRIVER: cql
"github.com/gocql/gocql"
"github.com/xo/dburl"

"github.com/xo/usql/drivers"
)

Expand All @@ -32,16 +29,19 @@ func (l *logger) Print(v ...interface{}) {
log.Print(v...)
}
}

func (l *logger) Printf(s string, v ...interface{}) {
if l.debug {
log.Printf(s, v...)
}
}

func (l *logger) Println(v ...interface{}) {
if l.debug {
log.Println(v...)
}
}

func (l *logger) Write(buf []byte) (int, error) {
if l.debug {
log.Printf("WRITE: %s", string(buf))
Expand All @@ -56,11 +56,9 @@ func init() {
log.Printf("ENABLING DEBUGGING FOR CQL")
debug = true
}

// error regexp's
authReqRE := regexp.MustCompile(`authentication required`)
passwordErrRE := regexp.MustCompile(`Provided username (.*)and/or password are incorrect`)

var l *logger
drivers.Register("cql", drivers.Driver{
AllowDollar: true,
Expand Down
4 changes: 1 addition & 3 deletions drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package clickhouse
import (
"database/sql"

// DRIVER: clickhouse
_ "github.com/ClickHouse/clickhouse-go"

_ "github.com/ClickHouse/clickhouse-go" // DRIVER: clickhouse
"github.com/xo/usql/drivers"
)

Expand Down
4 changes: 1 addition & 3 deletions drivers/cosmos/cosmos.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cosmos

import (
// DRIVER: cosmos
_ "github.com/btnguyen2k/gocosmos"

_ "github.com/btnguyen2k/gocosmos" // DRIVER: cosmos
"github.com/xo/usql/drivers"
)

Expand Down
4 changes: 1 addition & 3 deletions drivers/couchbase/couchbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package couchbase
import (
"strings"

// DRIVER: n1ql
_ "github.com/couchbase/go_n1ql"

_ "github.com/couchbase/go_n1ql" // DRIVER: n1ql
"github.com/xo/usql/drivers"
)

Expand Down
47 changes: 0 additions & 47 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/lexers"
"github.com/xo/dburl"

"github.com/xo/usql/stmt"
"github.com/xo/usql/text"
)
Expand All @@ -27,80 +26,58 @@ type DB interface {
type Driver struct {
// Name is a name to override the driver name with.
Name string

// AllowDollar will be passed to query buffers to enable dollar ($$) style
// strings.
AllowDollar bool

// AllowMultilineComments will be passed to query buffers to enable
// multiline (/**/) style comments.
AllowMultilineComments bool

// AllowCComments will be passed to query buffers to enable C (//) style
// comments.
AllowCComments bool

// AllowHashComments will be passed to query buffers to enable hash (#)
// style comments.
AllowHashComments bool

// RequirePreviousPassword will be used by RequirePreviousPassword.
RequirePreviousPassword bool

// LexerName is the name of the syntax lexer to use.
LexerName string

// ForceParams will be used to force parameters if defined.
ForceParams func(*dburl.URL)

// Open will be used by Open if defined.
Open func(*dburl.URL) (func(string, string) (*sql.DB, error), error)

// Version will be used by Version if defined.
Version func(DB) (string, error)

// User will be used by User if defined.
User func(DB) (string, error)

// ChangePassword will be used by ChangePassword if defined.
ChangePassword func(DB, string, string, string) error

// 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)

// Columns will be used to retrieve the columns for the rows if
// defined.
Columns func(*sql.Rows) ([]string, error)

// RowsAffected will be used by RowsAffected if defined.
RowsAffected func(sql.Result) (int64, error)

// Err will be used by Error.Error if defined.
Err func(error) (string, string)

// ConvertBytes will be used by ConvertBytes to convert a raw []byte
// slice to a string if defined.
ConvertBytes func([]byte, string) (string, error)

// ConvertMap will be used by ConvertMap to convert a map[string]interface{}
// to a string if defined.
ConvertMap func(map[string]interface{}) (string, error)

// ConvertSlice will be used by ConvertSlice to convert a []interface{} to
// a string if defined.
ConvertSlice func([]interface{}) (string, error)

// ConvertDefault will be used by ConvertDefault to convert a interface{}
// to a string if defined.
ConvertDefault func(interface{}) (string, error)

// BatchAsTransaction will cause batched queries to be done in a
// transaction block.
BatchAsTransaction bool

// BatchQueryPrefixes will be used by BatchQueryPrefixes if defined.
BatchQueryPrefixes map[string]string
}
Expand All @@ -122,14 +99,11 @@ func Register(name string, d Driver, aliases ...string) {
if _, ok := drivers[name]; ok {
panic(fmt.Sprintf("driver %s is already registered", name))
}

drivers[name] = d

for _, alias := range aliases {
if _, ok := drivers[alias]; ok {
panic(fmt.Sprintf("alias %s is already registered", name))
}

drivers[alias] = d
}
}
Expand All @@ -151,25 +125,21 @@ func ForceParams(u *dburl.URL) {
// Open opens a sql.DB connection for the registered driver.
func Open(u *dburl.URL) (*sql.DB, error) {
var err error

d, ok := drivers[u.Driver]
if !ok {
return nil, WrapErr(u.Driver, text.ErrDriverNotAvailable)
}

f := sql.Open
if d.Open != nil {
f, err = d.Open(u)
if err != nil {
return nil, WrapErr(u.Driver, err)
}
}

db, err := f(u.Driver, u.DSN)
if err != nil {
return nil, WrapErr(u.Driver, err)
}

return db, nil
}

Expand All @@ -185,7 +155,6 @@ func stmtOpts(u *dburl.URL) []stmt.Option {
}
}
}

return []stmt.Option{
stmt.AllowDollar(true),
stmt.AllowMultilineComments(true),
Expand Down Expand Up @@ -216,7 +185,6 @@ func Version(u *dburl.URL, db DB) (string, error) {
ver, err := d.Version(db)
return ver, WrapErr(u.Driver, err)
}

var ver string
err := db.QueryRow(`select version();`).Scan(&ver)
if err != nil || ver == "" {
Expand All @@ -231,7 +199,6 @@ func User(u *dburl.URL, db DB) (string, error) {
user, err := d.User(db)
return user, WrapErr(u.Driver, err)
}

var user string
db.QueryRow(`select current_user`).Scan(&user)
return user, nil
Expand All @@ -243,7 +210,6 @@ func Process(u *dburl.URL, prefix, sqlstr string) (string, string, bool, error)
a, b, c, err := d.Process(prefix, sqlstr)
return a, b, c, WrapErr(u.Driver, err)
}

typ, q := QueryExecType(prefix, sqlstr)
return typ, sqlstr, q, nil
}
Expand All @@ -255,7 +221,6 @@ func IsPasswordErr(u *dburl.URL, err error) bool {
if e, ok := err.(*Error); ok {
drv, err = e.Driver, e.Err
}

if d, ok := drivers[drv]; ok && d.IsPasswordErr != nil {
return d.IsPasswordErr(err)
}
Expand Down Expand Up @@ -292,7 +257,6 @@ func ChangePassword(u *dburl.URL, db DB, user, new, old string) (string, error)
return "", err
}
}

return user, d.ChangePassword(db, user, new, old)
}
return "", text.ErrPasswordNotSupportedByDriver
Expand All @@ -303,23 +267,19 @@ func ChangePassword(u *dburl.URL, db DB, user, new, old string) (string, error)
func Columns(u *dburl.URL, rows *sql.Rows) ([]string, error) {
var cols []string
var err error

if d, ok := drivers[u.Driver]; ok && d.Columns != nil {
cols, err = d.Columns(rows)
} else {
cols, err = rows.Columns()
}

if err != nil {
return nil, WrapErr(u.Driver, err)
}

for i, c := range cols {
if strings.TrimSpace(c) == "" {
cols[i] = fmt.Sprintf("col%d", i)
}
}

return cols, nil
}

Expand Down Expand Up @@ -390,12 +350,10 @@ func BatchAsTransaction(u *dburl.URL) bool {
func IsBatchQueryPrefix(u *dburl.URL, prefix string) (string, string, bool) {
// normalize
typ, q := QueryExecType(prefix, "")

d, ok := drivers[u.Driver]
if q || !ok || d.BatchQueryPrefixes == nil {
return typ, "", false
}

end, ok := d.BatchQueryPrefixes[typ]
return typ, end, ok
}
Expand All @@ -410,15 +368,12 @@ func RowsAffected(u *dburl.URL, res sql.Result) (int64, error) {
} else {
count, err = res.RowsAffected()
}

if err != nil && err.Error() == "no RowsAffected available after DDL statement" {
return 0, nil
}

if err != nil {
return 0, WrapErr(u.Driver, err)
}

return count, nil
}

Expand All @@ -438,9 +393,7 @@ func Lexer(u *dburl.URL) chroma.Lexer {
if l == nil {
l = lexers.Get("sql")
}

l.Config().EnsureNL = false

return l
}

Expand Down
Loading

0 comments on commit af85899

Please sign in to comment.