Skip to content

Commit

Permalink
Cleaning up errors from metadata reader/writer
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Nov 17, 2021
1 parent 9619d33 commit 670ea82
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 69 deletions.
3 changes: 2 additions & 1 deletion drivers/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gohxs/readline"
"github.com/xo/usql/drivers/metadata"
"github.com/xo/usql/env"
"github.com/xo/usql/text"
)

const (
Expand Down Expand Up @@ -856,7 +857,7 @@ func qualifiedIdentifier(filter metadata.Filter, catalog, schema, name string) s
func (c completer) getNames(query func() (iterator, error), mapper func(interface{}) string) []string {
res, err := query()
if err != nil {
if err != metadata.ErrNotSupported {
if err != text.ErrNotSupported {
c.logger.Println("Error getting selectables", err)
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func ForceQueryParameters(params []string) func(*dburl.URL) {
func NewMetadataReader(ctx context.Context, u *dburl.URL, db DB, w io.Writer, opts ...metadata.ReaderOption) (metadata.Reader, error) {
d, ok := drivers[u.Driver]
if !ok || d.NewMetadataReader == nil {
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`)
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`, u.Driver)
}
return d.NewMetadataReader(db, opts...), nil
}
Expand All @@ -458,13 +458,13 @@ func NewMetadataReader(ctx context.Context, u *dburl.URL, db DB, w io.Writer, op
func NewMetadataWriter(ctx context.Context, u *dburl.URL, db DB, w io.Writer, opts ...metadata.ReaderOption) (metadata.Writer, error) {
d, ok := drivers[u.Driver]
if !ok {
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`)
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`, u.Driver)
}
if d.NewMetadataWriter != nil {
return d.NewMetadataWriter(db, w, opts...), nil
}
if d.NewMetadataReader == nil {
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`)
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`, u.Driver)
}
newMetadataWriter := metadata.NewDefaultWriter(d.NewMetadataReader(db, opts...))
return newMetadataWriter(db, w), nil
Expand Down Expand Up @@ -503,7 +503,7 @@ func Copy(ctx context.Context, u *dburl.URL, stdout, stderr func() io.Writer, ro
return 0, WrapErr(u.Driver, text.ErrDriverNotAvailable)
}
if d.Copy == nil {
return 0, fmt.Errorf(text.NotSupportedByDriver, "copy")
return 0, fmt.Errorf(text.NotSupportedByDriver, "copy", u.Driver)
}
db, err := Open(u, stdout, stderr)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions drivers/metadata/informationschema/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

// InformationSchema metadata reader
Expand Down Expand Up @@ -331,7 +332,7 @@ FROM information_schema.schemata
// Functions from selected catalog (or all, if empty), matching schemas, names and types
func (s InformationSchema) Functions(f metadata.Filter) (*metadata.FunctionSet, error) {
if !s.hasFunctions {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

columns := []string{
Expand Down Expand Up @@ -393,7 +394,7 @@ func (s InformationSchema) Functions(f metadata.Filter) (*metadata.FunctionSet,
// FunctionColumns (arguments) from selected catalog (or all, if empty), matching schemas and functions
func (s InformationSchema) FunctionColumns(f metadata.Filter) (*metadata.FunctionColumnSet, error) {
if !s.hasFunctions {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

columns := []string{
Expand Down Expand Up @@ -457,7 +458,7 @@ func (s InformationSchema) FunctionColumns(f metadata.Filter) (*metadata.Functio
// Indexes from selected catalog (or all, if empty), matching schemas and names
func (s InformationSchema) Indexes(f metadata.Filter) (*metadata.IndexSet, error) {
if !s.hasIndexes {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

qstr := `SELECT
Expand Down Expand Up @@ -512,7 +513,7 @@ GROUP BY table_catalog, index_schema, table_name, index_name,
// IndexColumns from selected catalog (or all, if empty), matching schemas and indexes
func (s InformationSchema) IndexColumns(f metadata.Filter) (*metadata.IndexColumnSet, error) {
if !s.hasIndexes {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

qstr := `SELECT
Expand Down Expand Up @@ -565,7 +566,7 @@ JOIN information_schema.columns c ON
// Constraintes from selected catalog (or all, if empty), matching schemas and names
func (s InformationSchema) Constraints(f metadata.Filter) (*metadata.ConstraintSet, error) {
if !s.hasConstraints {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

columns := []string{
Expand Down Expand Up @@ -654,7 +655,7 @@ LEFT JOIN information_schema.check_constraints c ON t.constraint_catalog = c.con
// ConstraintColumns from selected catalog (or all, if empty), matching schemas and constraints
func (s InformationSchema) ConstraintColumns(f metadata.Filter) (*metadata.ConstraintColumnSet, error) {
if !s.hasConstraints {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

vals := []interface{}{}
Expand Down Expand Up @@ -761,9 +762,8 @@ LEFT JOIN information_schema.key_column_usage f ON r.unique_constraint_catalog =
// Sequences from selected catalog (or all, if empty), matching schemas and names
func (s InformationSchema) Sequences(f metadata.Filter) (*metadata.SequenceSet, error) {
if !s.hasSequences {
return nil, metadata.ErrNotSupported
return nil, text.ErrNotSupported
}

columns := []string{
"sequence_catalog",
"sequence_schema",
Expand Down
24 changes: 10 additions & 14 deletions drivers/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package metadata

import (
"errors"
)

var (
ErrNotSupported = errors.New("error: not supported")
ErrScanArgsCount = errors.New("error: wrong number of arguments for Scan()")
"github.com/xo/dburl"
"github.com/xo/usql/text"
)

// ExtendedReader of all database metadata in a structured format.
Expand Down Expand Up @@ -140,19 +136,19 @@ type Filter struct {
// Writer of database metadata in a human readable format.
type Writer interface {
// DescribeFunctions \df, \dfa, \dfn, \dft, \dfw, etc.
DescribeFunctions(string, string, bool, bool) error
DescribeFunctions(*dburl.URL, string, string, bool, bool) error
// DescribeTableDetails \d foo
DescribeTableDetails(string, bool, bool) error
DescribeTableDetails(*dburl.URL, string, bool, bool) error
// ListAllDbs \l
ListAllDbs(string, bool) error
ListAllDbs(*dburl.URL, string, bool) error
// ListTables \dt, \dv, \dm, etc.
ListTables(string, string, bool, bool) error
ListTables(*dburl.URL, string, string, bool, bool) error
// ListSchemas \dn
ListSchemas(string, bool, bool) error
ListSchemas(*dburl.URL, string, bool, bool) error
// ListIndexes \di
ListIndexes(string, bool, bool) error
ListIndexes(*dburl.URL, string, bool, bool) error
// ShowStats \ss
ShowStats(string, string, bool, int) error
ShowStats(*dburl.URL, string, string, bool, int) error
}

type CatalogSet struct {
Expand Down Expand Up @@ -898,7 +894,7 @@ func (r resultSet) Scan(dest ...interface{}) error {
v = r.scanValues(r.results[r.current-1])
}
if len(v) != len(dest) {
return ErrScanArgsCount
return text.ErrWrongNumberOfArguments
}
for i, d := range dest {
p := d.(*interface{})
Expand Down
28 changes: 15 additions & 13 deletions drivers/metadata/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"time"

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

// PluginReader allows to be easily composed from other readers
Expand Down Expand Up @@ -74,91 +76,91 @@ func NewPluginReader(readers ...Reader) Reader {

func (p PluginReader) Catalogs(f Filter) (*CatalogSet, error) {
if p.catalogs == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.catalogs(f)
}

func (p PluginReader) Schemas(f Filter) (*SchemaSet, error) {
if p.schemas == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.schemas(f)
}

func (p PluginReader) Tables(f Filter) (*TableSet, error) {
if p.tables == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.tables(f)
}

func (p PluginReader) Columns(f Filter) (*ColumnSet, error) {
if p.columns == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.columns(f)
}

func (p PluginReader) ColumnStats(f Filter) (*ColumnStatSet, error) {
if p.columnStats == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.columnStats(f)
}

func (p PluginReader) Indexes(f Filter) (*IndexSet, error) {
if p.indexes == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.indexes(f)
}

func (p PluginReader) IndexColumns(f Filter) (*IndexColumnSet, error) {
if p.indexColumns == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.indexColumns(f)
}

func (p PluginReader) Triggers(f Filter) (*TriggerSet, error) {
if p.triggers == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.triggers(f)
}

func (p PluginReader) Constraints(f Filter) (*ConstraintSet, error) {
if p.constraints == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.constraints(f)
}

func (p PluginReader) ConstraintColumns(f Filter) (*ConstraintColumnSet, error) {
if p.constraintColumns == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.constraintColumns(f)
}

func (p PluginReader) Functions(f Filter) (*FunctionSet, error) {
if p.functions == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.functions(f)
}

func (p PluginReader) FunctionColumns(f Filter) (*FunctionColumnSet, error) {
if p.functionColumns == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.functionColumns(f)
}

func (p PluginReader) Sequences(f Filter) (*SequenceSet, error) {
if p.sequences == nil {
return nil, ErrNotSupported
return nil, text.ErrNotSupported
}
return p.sequences(f)
}
Expand Down
Loading

0 comments on commit 670ea82

Please sign in to comment.