Skip to content

Commit

Permalink
Make all readers logging readers
Browse files Browse the repository at this point in the history
  • Loading branch information
nineinchnick committed Mar 6, 2021
1 parent 0909b3e commit 1e733c6
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 259 deletions.
14 changes: 7 additions & 7 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ type Driver struct {
// BatchQueryPrefixes will be used by BatchQueryPrefixes if defined.
BatchQueryPrefixes map[string]string
// NewMetadataReader returns a db metadata introspector.
NewMetadataReader func(db DB) metadata.Reader
NewMetadataReader func(db DB, opts ...metadata.ReaderOption) metadata.Reader
// NewMetadataWriter returns a db metadata printer.
NewMetadataWriter func(db DB, w io.Writer) metadata.Writer
NewMetadataWriter func(db DB, w io.Writer, opts ...metadata.ReaderOption) metadata.Writer
}

// drivers is the map of drivers funcs.
Expand Down Expand Up @@ -427,26 +427,26 @@ func NextResultSet(q *sql.Rows) bool {
}

// NewMetadataReader wraps creating a new database introspector for the specified driver.
func NewMetadataReader(u *dburl.URL, db DB, w io.Writer) (metadata.Reader, error) {
func NewMetadataReader(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 d.NewMetadataReader(db), nil
return d.NewMetadataReader(db, opts...), nil
}

// NewMetadataWriter wraps creating a new database metadata printer for the specified driver.
func NewMetadataWriter(u *dburl.URL, db DB, w io.Writer) (metadata.Writer, error) {
func NewMetadataWriter(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`)
}
if d.NewMetadataWriter != nil {
return d.NewMetadataWriter(db, w), nil
return d.NewMetadataWriter(db, w, opts...), nil
}
if d.NewMetadataReader == nil {
return nil, fmt.Errorf(text.NotSupportedByDriver, `describe commands`)
}
newMetadataWriter := metadata.NewDefaultWriter(d.NewMetadataReader(db))
newMetadataWriter := metadata.NewDefaultWriter(d.NewMetadataReader(db, opts...))
return newMetadataWriter(db, w), nil
}
19 changes: 3 additions & 16 deletions drivers/godror/godror.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"database/sql"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -111,22 +109,11 @@ func init() {
return typ, sqlstr, q, nil
},
NewMetadataReader: orameta.NewReader(),
NewMetadataWriter: func(db drivers.DB, w io.Writer) metadata.Writer {
// TODO if options would be common to all readers, this could be moved
// to the caller and passed in an argument
envs := env.All()
opts := []orameta.Option{}
if envs["ECHO_HIDDEN"] == "on" || envs["ECHO_HIDDEN"] == "noexec" {
if envs["ECHO_HIDDEN"] == "noexec" {
opts = append(opts, orameta.WithDryRun(true))
}
opts = append(opts, orameta.WithLogger(log.New(os.Stdout, "DEBUG: ", log.LstdFlags)))
}
newReader := orameta.NewReader(opts...)
writerOpts := []metadata.Option{
NewMetadataWriter: func(db drivers.DB, w io.Writer, opts ...metadata.ReaderOption) metadata.Writer {
writerOpts := []metadata.WriterOption{
metadata.WithSystemSchemas([]string{"ctxsys", "flows_files", "mdsys", "outln", "sys", "system", "xdb", "xs$null"}),
}
return metadata.NewDefaultWriter(newReader(db), writerOpts...)(db, w)
return metadata.NewDefaultWriter(orameta.NewReader()(db, opts...), writerOpts...)(db, w)
},
})
}
96 changes: 33 additions & 63 deletions drivers/metadata/informationschema/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ import (

// InformationSchema metadata reader
type InformationSchema struct {
db drivers.DB
logger Logger
dryRun bool
pf func(int) string
hasTypeDetails bool
hasFunctions bool
hasSequences bool
hasIndexes bool
colExpr map[ColumnName]string
metadata.LoggingReader
pf func(int) string
hasFunctions bool
hasSequences bool
hasIndexes bool
colExpr map[ColumnName]string
}

var _ metadata.BasicReader = &InformationSchema{}
Expand All @@ -48,7 +45,7 @@ const (
)

// New InformationSchema reader
func New(opts ...Option) func(db drivers.DB) metadata.Reader {
func New(opts ...metadata.ReaderOption) func(drivers.DB, ...metadata.ReaderOption) metadata.Reader {
s := &InformationSchema{
pf: func(n int) string { return fmt.Sprintf("$%d", n) },
hasFunctions: true,
Expand All @@ -66,68 +63,52 @@ func New(opts ...Option) func(db drivers.DB) metadata.Reader {
FunctionsSecurityType: "security_type",
},
}
// aply InformationSchema specific options
for _, o := range opts {
o(s)
}

return func(db drivers.DB) metadata.Reader {
s.db = db
return func(db drivers.DB, opts ...metadata.ReaderOption) metadata.Reader {
s.LoggingReader = metadata.NewLoggingReader(db, opts...)
return s
}
}

// Option to configure the InformationSchema reader
type Option func(*InformationSchema)

// WithLogger used to log queries before executing them
func WithLogger(l Logger) Option {
return func(s *InformationSchema) {
s.logger = l
}
}

// WithDryRun allows to avoid running any queries
func WithDryRun(d bool) Option {
return func(s *InformationSchema) {
s.dryRun = d
}
}

// WithPlaceholder generator function, that usually returns either `?` or `$n`,
// where `n` is the argument.
func WithPlaceholder(pf func(int) string) Option {
return func(s *InformationSchema) {
s.pf = pf
func WithPlaceholder(pf func(int) string) metadata.ReaderOption {
return func(r metadata.Reader) {
r.(*InformationSchema).pf = pf
}
}

// WithCustomColumns to use different expressions for some columns
func WithCustomColumns(cols map[ColumnName]string) Option {
return func(s *InformationSchema) {
func WithCustomColumns(cols map[ColumnName]string) metadata.ReaderOption {
return func(r metadata.Reader) {
for k, v := range cols {
s.colExpr[k] = v
r.(*InformationSchema).colExpr[k] = v
}
}
}

// WithFunctions when the `routines` and `parameters` tables exists
func WithFunctions(fun bool) Option {
return func(s *InformationSchema) {
s.hasFunctions = fun
func WithFunctions(fun bool) metadata.ReaderOption {
return func(r metadata.Reader) {
r.(*InformationSchema).hasFunctions = fun
}
}

// WithIndexes when the `statistics` table exists
func WithIndexes(ind bool) Option {
return func(s *InformationSchema) {
s.hasIndexes = ind
func WithIndexes(ind bool) metadata.ReaderOption {
return func(r metadata.Reader) {
r.(*InformationSchema).hasIndexes = ind
}
}

// WithSequences when the `sequences` table exists
func WithSequences(seq bool) Option {
return func(s *InformationSchema) {
s.hasSequences = seq
func WithSequences(seq bool) metadata.ReaderOption {
return func(r metadata.Reader) {
r.(*InformationSchema).hasSequences = seq
}
}

Expand Down Expand Up @@ -168,7 +149,7 @@ func (s InformationSchema) Columns(catalog, schemaPattern, tablePattern string)
}
qstr += `
ORDER BY table_catalog, table_schema, table_name, ordinal_position`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewColumnSet([]metadata.Column{}), nil
Expand Down Expand Up @@ -274,7 +255,7 @@ FROM information_schema.sequences
}
qstr += `
ORDER BY table_catalog, table_schema, table_type, table_name`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewTableSet([]metadata.Table{}), nil
Expand Down Expand Up @@ -320,7 +301,7 @@ FROM information_schema.schemata
}
qstr += `
ORDER BY catalog_name, schema_name`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewSchemaSet([]metadata.Schema{}), nil
Expand Down Expand Up @@ -393,7 +374,7 @@ func (s InformationSchema) Functions(catalog, schemaPattern, namePattern string,
}
qstr += `
ORDER BY routine_catalog, routine_schema, routine_name, COALESCE(routine_type, '')`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewFunctionSet([]metadata.Function{}), nil
Expand Down Expand Up @@ -469,7 +450,7 @@ func (s InformationSchema) FunctionColumns(catalog, schemaPattern, functionPatte
}
qstr += `
ORDER BY specific_catalog, specific_schema, specific_name, ordinal_position, COALESCE(parameter_name, '')`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewFunctionColumnSet([]metadata.FunctionColumn{}), nil
Expand Down Expand Up @@ -549,7 +530,7 @@ GROUP BY table_catalog, index_schema, table_name, index_name,
index_type
ORDER BY table_catalog, index_schema, table_name, index_name
`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewIndexSet([]metadata.Index{}), nil
Expand Down Expand Up @@ -618,7 +599,7 @@ JOIN information_schema.columns c ON
}
qstr += `
ORDER BY i.table_catalog, index_schema, table_name, index_name, seq_in_index`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewIndexColumnSet([]metadata.IndexColumn{}), nil
Expand Down Expand Up @@ -679,7 +660,7 @@ FROM information_schema.sequences
}
qstr += `
ORDER BY sequence_catalog, sequence_schema, sequence_name`
rows, err := s.query(qstr, vals...)
rows, err := s.Query(qstr, vals...)
if err != nil {
if err == sql.ErrNoRows {
return metadata.NewSequenceSet([]metadata.Sequence{}), nil
Expand All @@ -702,14 +683,3 @@ ORDER BY sequence_catalog, sequence_schema, sequence_name`
}
return metadata.NewSequenceSet(results), nil
}

func (s InformationSchema) query(q string, v ...interface{}) (*sql.Rows, error) {
if s.logger != nil {
s.logger.Println(q)
s.logger.Println(v)
}
if s.dryRun {
return nil, sql.ErrNoRows
}
return s.db.Query(q, v...)
}
4 changes: 2 additions & 2 deletions drivers/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
infos.FunctionColumnsColumnSize: "COALESCE(character_maximum_length, numeric_precision, datetime_precision, interval_precision, 0)",
}),
},
WriterOpts: []metadata.Option{
WriterOpts: []metadata.WriterOption{
metadata.WithSystemSchemas([]string{"pg_catalog", "pg_toast", "information_schema"}),
},
},
Expand All @@ -86,7 +86,7 @@ var (
infos.FunctionColumnsNumericPrecRadix: "10",
}),
},
WriterOpts: []metadata.Option{
WriterOpts: []metadata.WriterOption{
metadata.WithSystemSchemas([]string{"mysql", "performance_schema", "information_schema"}),
},
},
Expand Down
Loading

0 comments on commit 1e733c6

Please sign in to comment.