2020-08-06 20:58:47 +03:00
|
|
|
// Package userdriver implements the "user-driver" functionality
|
|
|
|
// that allows users to define source driver types declaratively.
|
|
|
|
// Note pkg userdriver itself is the framework: an actual
|
|
|
|
// implementation for each genre (such as XML) must be defined
|
|
|
|
// separately as in the "xmlud" sub-package.
|
|
|
|
package userdriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"io"
|
2023-08-12 21:54:14 +03:00
|
|
|
"log/slog"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgm"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/cleanup"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImportFunc is a function that can import
|
|
|
|
// data (as defined in def) to destDB.
|
2023-04-02 22:49:45 +03:00
|
|
|
type ImportFunc func(ctx context.Context, def *DriverDef,
|
|
|
|
data io.Reader, destDB driver.Database) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Provider implements driver.Provider for a DriverDef.
|
|
|
|
type Provider struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
Log *slog.Logger
|
2020-08-06 20:58:47 +03:00
|
|
|
DriverDef *DriverDef
|
|
|
|
Scratcher driver.ScratchDatabaseOpener
|
|
|
|
Files *source.Files
|
|
|
|
ImportFn ImportFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverFor implements driver.Provider.
|
2023-04-22 06:36:32 +03:00
|
|
|
func (p *Provider) DriverFor(typ source.DriverType) (driver.Driver, error) {
|
|
|
|
if typ != source.DriverType(p.DriverDef.Name) {
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Errorf("unsupported driver type {%s}", typ)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &drvr{
|
|
|
|
log: p.Log,
|
|
|
|
typ: typ,
|
|
|
|
def: p.DriverDef,
|
|
|
|
scratcher: p.Scratcher,
|
|
|
|
importFn: p.ImportFn,
|
|
|
|
files: p.Files,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
// Detectors returns funcs that can detect the driver type.
|
|
|
|
func (p *Provider) Detectors() []source.DriverDetectFunc {
|
2020-08-06 20:58:47 +03:00
|
|
|
// TODO: it should be possible to return type detectors that
|
|
|
|
// can detect based upon the DriverDef. So, as of right
|
|
|
|
// now these detectors do nothing.
|
2023-04-22 06:36:32 +03:00
|
|
|
return []source.DriverDetectFunc{}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Driver implements driver.Driver.
|
|
|
|
type drvr struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2023-04-22 06:36:32 +03:00
|
|
|
typ source.DriverType
|
2020-08-06 20:58:47 +03:00
|
|
|
def *DriverDef
|
|
|
|
files *source.Files
|
|
|
|
scratcher driver.ScratchDatabaseOpener
|
|
|
|
importFn ImportFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverMetadata implements driver.Driver.
|
|
|
|
func (d *drvr) DriverMetadata() driver.Metadata {
|
|
|
|
return driver.Metadata{
|
2023-04-22 06:36:32 +03:00
|
|
|
Type: source.DriverType(d.def.Name),
|
2020-08-06 20:58:47 +03:00
|
|
|
Description: d.def.Title,
|
|
|
|
Doc: d.def.Doc,
|
|
|
|
UserDefined: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 21:09:27 +03:00
|
|
|
// Open implements driver.DatabaseOpener.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (d *drvr) Open(ctx context.Context, src *source.Source) (driver.Database, error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
lg.FromContext(ctx).Debug(lgm.OpenSrc, lga.Src, src)
|
2023-04-30 17:18:56 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
clnup := cleanup.New()
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
r, err := d.files.Open(src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfCloseError(d.log, lgm.CloseFileReader, r)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
scratchDB, err := d.scratcher.OpenScratch(ctx, src.Handle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clnup.AddE(scratchDB.Close)
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
err = d.importFn(ctx, d.def, r, scratchDB)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDB, clnup.Run)
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, errz.Wrap(err, d.def.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &database{log: d.log, src: src, impl: scratchDB, clnup: clnup}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate implements driver.Driver.
|
2023-04-01 11:38:32 +03:00
|
|
|
func (d *drvr) Truncate(_ context.Context, _ *source.Source, _ string, _ bool) (int64, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
return 0, errz.Errorf("truncate not supported for %s", d.DriverMetadata().Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateSource implements driver.Driver.
|
|
|
|
func (d *drvr) ValidateSource(src *source.Source) (*source.Source, error) {
|
2023-04-02 22:49:45 +03:00
|
|
|
d.log.Debug("Validating source", lga.Src, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if string(src.Type) != d.def.Name {
|
2023-04-22 06:36:32 +03:00
|
|
|
return nil, errz.Errorf("expected driver type {%s} but got {%s}", d.def.Name, src.Type)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping implements driver.Driver.
|
2023-04-01 11:38:32 +03:00
|
|
|
func (d *drvr) Ping(_ context.Context, src *source.Source) error {
|
2023-04-02 22:49:45 +03:00
|
|
|
d.log.Debug("Ping source",
|
|
|
|
lga.Driver, d.typ,
|
|
|
|
lga.Src, src,
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
r, err := d.files.Open(src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: possibly do something more useful than just
|
|
|
|
// getting the reader?
|
|
|
|
|
|
|
|
return r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// database implements driver.Database.
|
|
|
|
type database struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2020-08-06 20:58:47 +03:00
|
|
|
src *source.Source
|
|
|
|
impl driver.Database
|
|
|
|
|
|
|
|
// clnup will ultimately invoke impl.Close to dispose of
|
|
|
|
// the scratch DB.
|
|
|
|
clnup *cleanup.Cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB implements driver.Database.
|
2023-07-08 18:21:27 +03:00
|
|
|
func (d *database) DB(ctx context.Context) (*sql.DB, error) {
|
|
|
|
return d.impl.DB(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SQLDriver implements driver.Database.
|
|
|
|
func (d *database) SQLDriver() driver.SQLDriver {
|
|
|
|
return d.impl.SQLDriver()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source implements driver.Database.
|
|
|
|
func (d *database) Source() *source.Source {
|
|
|
|
return d.src
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableMetadata implements driver.Database.
|
|
|
|
func (d *database) TableMetadata(ctx context.Context, tblName string) (*source.TableMetadata, error) {
|
|
|
|
return d.impl.TableMetadata(ctx, tblName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SourceMetadata implements driver.Database.
|
2023-06-22 08:48:58 +03:00
|
|
|
func (d *database) SourceMetadata(ctx context.Context, noSchema bool) (*source.Metadata, error) {
|
|
|
|
meta, err := d.impl.SourceMetadata(ctx, noSchema)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.Handle = d.src.Handle
|
|
|
|
meta.Location = d.src.Location
|
|
|
|
meta.Name, err = source.LocationFileName(d.src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.FQName = meta.Name
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Database.
|
|
|
|
func (d *database) Close() error {
|
2023-05-03 15:36:10 +03:00
|
|
|
d.log.Debug(lgm.CloseDB, lga.Handle, d.src.Handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// We don't need to explicitly invoke c.impl.Close
|
|
|
|
// because that's already been added to c.cleanup.
|
|
|
|
return d.clnup.Run()
|
|
|
|
}
|