2020-08-23 13:42:15 +03:00
|
|
|
// Package json implements the sq driver for JSON. There are three
|
|
|
|
// supported types:
|
|
|
|
// - JSON: plain old JSON
|
|
|
|
// - JSONA: JSON Array, where each record is an array of JSON values on its own line.
|
|
|
|
// - JSONL: JSON Lines, where each record a JSON object on its own line.
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"golang.org/x/exp/slog"
|
2020-08-23 13:42:15 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/cleanup"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TypeJSON is the plain-old JSON driver type.
|
2023-04-22 06:36:32 +03:00
|
|
|
TypeJSON = source.DriverType("json")
|
2020-08-23 13:42:15 +03:00
|
|
|
|
|
|
|
// TypeJSONA is the JSON Array driver type.
|
2023-04-22 06:36:32 +03:00
|
|
|
TypeJSONA = source.DriverType("jsona")
|
2020-08-23 13:42:15 +03:00
|
|
|
|
|
|
|
// TypeJSONL is the JSON Lines driver type.
|
2023-04-22 06:36:32 +03:00
|
|
|
TypeJSONL = source.DriverType("jsonl")
|
2020-08-23 13:42:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Provider implements driver.Provider.
|
|
|
|
type Provider struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
Log *slog.Logger
|
2020-08-23 13:42:15 +03:00
|
|
|
Scratcher driver.ScratchDatabaseOpener
|
|
|
|
Files *source.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverFor implements driver.Provider.
|
2023-04-22 06:36:32 +03:00
|
|
|
func (d *Provider) DriverFor(typ source.DriverType) (driver.Driver, error) {
|
2020-08-23 13:42:15 +03:00
|
|
|
var importFn importFunc
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
switch typ { //nolint:exhaustive
|
2020-08-23 13:42:15 +03:00
|
|
|
case TypeJSON:
|
|
|
|
importFn = importJSON
|
|
|
|
case TypeJSONA:
|
|
|
|
importFn = importJSONA
|
|
|
|
case TypeJSONL:
|
|
|
|
importFn = importJSONL
|
|
|
|
default:
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Errorf("unsupported driver type {%s}", typ)
|
2020-08-23 13:42:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &driveri{
|
|
|
|
log: d.Log,
|
|
|
|
typ: typ,
|
|
|
|
scratcher: d.Scratcher,
|
|
|
|
files: d.Files,
|
|
|
|
importFn: importFn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Driver implements driver.Driver.
|
|
|
|
type driveri 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-23 13:42:15 +03:00
|
|
|
importFn importFunc
|
|
|
|
scratcher driver.ScratchDatabaseOpener
|
|
|
|
files *source.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverMetadata implements driver.Driver.
|
|
|
|
func (d *driveri) DriverMetadata() driver.Metadata {
|
|
|
|
md := driver.Metadata{Type: d.typ, Monotable: true}
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
switch d.typ { //nolint:exhaustive
|
2020-08-23 13:42:15 +03:00
|
|
|
case TypeJSON:
|
|
|
|
md.Description = "JSON"
|
|
|
|
md.Doc = "https://en.wikipedia.org/wiki/JSON"
|
|
|
|
case TypeJSONA:
|
|
|
|
md.Description = "JSON Array: LF-delimited JSON arrays"
|
|
|
|
md.Doc = "https://en.wikipedia.org/wiki/JSON"
|
|
|
|
case TypeJSONL:
|
|
|
|
md.Description = "JSON Lines: LF-delimited JSON objects"
|
|
|
|
md.Doc = "https://en.wikipedia.org/wiki/JSON_streaming#Line-delimited_JSON"
|
|
|
|
}
|
|
|
|
|
|
|
|
return md
|
|
|
|
}
|
|
|
|
|
2023-04-08 21:09:27 +03:00
|
|
|
// Open implements driver.DatabaseOpener.
|
2020-08-23 13:42:15 +03:00
|
|
|
func (d *driveri) Open(ctx context.Context, src *source.Source) (driver.Database, error) {
|
|
|
|
dbase := &database{log: d.log, src: src, clnup: cleanup.New(), files: d.files}
|
|
|
|
|
|
|
|
r, err := d.files.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbase.impl, err = d.scratcher.OpenScratch(ctx, src.Handle)
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfCloseError(d.log, lgm.CloseFileReader, r)
|
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDB, dbase.clnup.Run)
|
2020-08-23 13:42:15 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:05:43 +03:00
|
|
|
job := importJob{
|
|
|
|
fromSrc: src,
|
|
|
|
openFn: d.files.OpenFunc(src),
|
|
|
|
destDB: dbase.impl,
|
|
|
|
sampleSize: driver.Tuning.SampleSize,
|
|
|
|
flatten: true, // TODO: Should come from src.Options
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
err = d.importFn(ctx, job)
|
2020-08-23 13:42:15 +03:00
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfCloseError(d.log, lgm.CloseFileReader, r)
|
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDB, dbase.clnup.Run)
|
2020-08-23 13:42:15 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate implements driver.Driver.
|
2023-04-01 11:38:32 +03:00
|
|
|
func (d *driveri) Truncate(_ context.Context, _ *source.Source, _ string, _ bool) (int64, error) {
|
2020-08-23 13:42:15 +03:00
|
|
|
return 0, errz.Errorf("truncate not supported for %s", d.DriverMetadata().Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateSource implements driver.Driver.
|
|
|
|
func (d *driveri) ValidateSource(src *source.Source) (*source.Source, error) {
|
|
|
|
if src.Type != d.typ {
|
2023-04-22 06:36:32 +03:00
|
|
|
return nil, errz.Errorf("expected driver type {%s} but got {%s}", d.typ, src.Type)
|
2020-08-23 13:42:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping implements driver.Driver.
|
2023-04-01 11:38:32 +03:00
|
|
|
func (d *driveri) Ping(_ context.Context, src *source.Source) error {
|
2023-04-02 22:49:45 +03:00
|
|
|
d.log.Debug("Ping source", lga.Src, src)
|
2020-08-23 13:42:15 +03:00
|
|
|
|
|
|
|
r, err := d.files.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfCloseError(d.log, lgm.CloseFileReader, r)
|
2020-08-23 13:42:15 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// database implements driver.Database.
|
|
|
|
type database struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2020-08-23 13:42:15 +03:00
|
|
|
src *source.Source
|
|
|
|
impl driver.Database
|
|
|
|
clnup *cleanup.Cleanup
|
|
|
|
files *source.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB implements driver.Database.
|
|
|
|
func (d *database) DB() *sql.DB {
|
|
|
|
return d.impl.DB()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if tblName != source.MonotableName {
|
|
|
|
return nil, errz.Errorf("table name should be %s for CSV/TSV etc., but got: %s",
|
|
|
|
source.MonotableName, tblName)
|
|
|
|
}
|
|
|
|
|
|
|
|
srcMeta, err := d.SourceMetadata(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// There will only ever be one table for CSV.
|
|
|
|
return srcMeta.Tables[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SourceMetadata implements driver.Database.
|
|
|
|
func (d *database) SourceMetadata(ctx context.Context) (*source.Metadata, error) {
|
|
|
|
md, err := d.impl.SourceMetadata(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md.Handle = d.src.Handle
|
|
|
|
md.Location = d.src.Location
|
|
|
|
md.SourceType = d.src.Type
|
|
|
|
|
|
|
|
md.Name, err = source.LocationFileName(d.src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md.Size, err = d.files.Size(d.src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md.FQName = md.Name
|
|
|
|
return md, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Database.
|
|
|
|
func (d *database) Close() error {
|
2023-04-02 22:49:45 +03:00
|
|
|
d.log.Debug(lgm.CloseDB, lga.Src, d.src)
|
2020-08-23 13:42:15 +03:00
|
|
|
|
|
|
|
return errz.Combine(d.impl.Close(), d.clnup.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-04-22 06:36:32 +03:00
|
|
|
_ source.DriverDetectFunc = DetectJSON
|
|
|
|
_ source.DriverDetectFunc = DetectJSONA
|
|
|
|
_ source.DriverDetectFunc = DetectJSONL
|
2020-08-23 13:42:15 +03:00
|
|
|
)
|