2020-08-06 20:58:47 +03:00
|
|
|
// Package sqlite3 implements the sq driver for SQLite.
|
|
|
|
// The backing SQL driver is mattn/sqlite3.
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import "C"
|
2022-12-18 11:35:59 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-01-01 06:17:44 +03:00
|
|
|
"net/url"
|
2020-08-18 22:13:25 +03:00
|
|
|
"os"
|
2020-08-06 20:58:47 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-12-30 21:57:58 +03:00
|
|
|
"sync"
|
2022-12-24 07:05:26 +03:00
|
|
|
"time"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver/dialect"
|
|
|
|
|
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-06 20:58:47 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"golang.org/x/exp/slog"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3" // Import for side effect of loading the driver
|
2023-04-07 11:00:49 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/ast/render"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
2020-08-23 22:00:13 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlmodel"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Type is the sqlite3 source driver type.
|
2023-04-22 06:36:32 +03:00
|
|
|
Type source.DriverType = "sqlite3"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// dbDrvr is the backing sqlite3 SQL driver impl name.
|
|
|
|
dbDrvr = "sqlite3"
|
2020-08-07 22:51:30 +03:00
|
|
|
|
|
|
|
// Prefix is the scheme+separator value "sqlite3://".
|
|
|
|
Prefix = "sqlite3://"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2020-08-20 23:19:16 +03:00
|
|
|
var _ driver.Provider = (*Provider)(nil)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Provider is the SQLite3 implementation of driver.Provider.
|
|
|
|
type Provider struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
Log *slog.Logger
|
2023-04-08 21:09:27 +03:00
|
|
|
|
|
|
|
// NOTE: Unlike other driver.SQLDriver impls, sqlite doesn't
|
|
|
|
// seem to benefit from applying a driver.SQLConfig to
|
|
|
|
// its sql.DB.
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DriverFor implements driver.Provider.
|
2023-04-22 06:36:32 +03:00
|
|
|
func (p *Provider) DriverFor(typ source.DriverType) (driver.Driver, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if typ != Type {
|
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
|
|
|
}
|
|
|
|
|
2023-04-08 21:09:27 +03:00
|
|
|
return &driveri{log: p.Log}, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-20 23:19:16 +03:00
|
|
|
var _ driver.Driver = (*driveri)(nil)
|
|
|
|
|
2020-08-13 06:40:03 +03:00
|
|
|
// driveri is the SQLite3 implementation of driver.Driver.
|
|
|
|
type driveri struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DriverMetadata implements driver.Driver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) DriverMetadata() driver.Metadata {
|
2020-08-06 20:58:47 +03:00
|
|
|
return driver.Metadata{
|
|
|
|
Type: Type,
|
|
|
|
Description: "SQLite",
|
|
|
|
Doc: "https://github.com/mattn/go-sqlite3",
|
|
|
|
IsSQL: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 21:09:27 +03:00
|
|
|
// Open implements driver.DatabaseOpener.
|
2023-04-30 17:18:56 +03:00
|
|
|
func (d *driveri) 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)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
db, err := d.doOpen(ctx, src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &database{log: d.log, db: db, src: src, drvr: d}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driveri) doOpen(ctx context.Context, src *source.Source) (*sql.DB, error) {
|
2020-08-07 22:51:30 +03:00
|
|
|
dsn, err := PathFromLocation(src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
db, err := sql.Open(dbDrvr, dsn)
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Wrapf(err, "failed to open sqlite3 source with DSN: %s", dsn)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
driver.ConfigureDB(ctx, db, src.Options)
|
|
|
|
return db, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate implements driver.Driver.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) Truncate(ctx context.Context, src *source.Source, tbl string, reset bool) (affected int64,
|
2022-12-18 11:35:59 +03:00
|
|
|
err error,
|
|
|
|
) {
|
2023-04-30 17:18:56 +03:00
|
|
|
db, err := d.doOpen(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfFuncError(d.log, lgm.CloseDB, db.Close)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
affected, err = sqlz.ExecAffected(ctx, tx, fmt.Sprintf("DELETE FROM %q", tbl))
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return affected, errz.Append(err, errz.Err(tx.Rollback()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if reset {
|
2021-02-22 10:37:00 +03:00
|
|
|
// First check that the sqlite_sequence table event exists. It
|
|
|
|
// may not exist if there are no auto-increment columns?
|
|
|
|
const q = `SELECT COUNT(name) FROM sqlite_master WHERE type='table' AND name='sqlite_sequence'`
|
|
|
|
var count int64
|
|
|
|
err = tx.QueryRowContext(ctx, q).Scan(&count)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Append(err, errz.Err(tx.Rollback()))
|
|
|
|
}
|
2021-02-22 10:37:00 +03:00
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
_, err = tx.ExecContext(ctx, "UPDATE sqlite_sequence SET seq = 0 WHERE name = ?", tbl)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Append(err, errz.Err(tx.Rollback()))
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return affected, errz.Err(tx.Commit())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateSource implements driver.Driver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) ValidateSource(src *source.Source) (*source.Source, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if src.Type != Type {
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Errorf("expected driver type {%s} but got {%s}", Type, src.Type)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping implements driver.Driver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) Ping(ctx context.Context, src *source.Source) error {
|
2020-08-07 22:51:30 +03:00
|
|
|
dbase, err := d.Open(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfCloseError(d.log, lgm.CloseDB, dbase)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
return dbase.DB().Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dialect implements driver.SQLDriver.
|
2023-04-07 11:00:49 +03:00
|
|
|
func (d *driveri) Dialect() dialect.Dialect {
|
|
|
|
return dialect.Dialect{
|
2020-08-12 21:24:01 +03:00
|
|
|
Type: Type,
|
|
|
|
Placeholders: placeholders,
|
2023-04-07 11:00:49 +03:00
|
|
|
IdentQuote: '"',
|
|
|
|
Enquote: stringz.DoubleQuote,
|
2020-08-12 21:24:01 +03:00
|
|
|
MaxBatchValues: 500,
|
2023-04-07 11:00:49 +03:00
|
|
|
Ops: dialect.DefaultOps(),
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
func placeholders(numCols, numRows int) string {
|
|
|
|
rows := make([]string, numRows)
|
|
|
|
for i := 0; i < numRows; i++ {
|
|
|
|
rows[i] = "(" + stringz.RepeatJoin("?", numCols, driver.Comma) + ")"
|
|
|
|
}
|
|
|
|
return strings.Join(rows, driver.Comma)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-08 21:09:27 +03:00
|
|
|
// Renderer implements driver.SQLDriver.
|
2023-04-07 11:00:49 +03:00
|
|
|
func (d *driveri) Renderer() *render.Renderer {
|
2023-04-08 21:09:27 +03:00
|
|
|
return render.NewDefaultRenderer()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTable implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) CopyTable(ctx context.Context, db sqlz.DB, fromTable, toTable string, copyData bool) (int64, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// Per https://stackoverflow.com/questions/12730390/copy-table-structure-to-new-table-in-sqlite3
|
|
|
|
// It is possible to copy the table structure with a simple statement:
|
|
|
|
// CREATE TABLE copied AS SELECT * FROM mytable WHERE 0
|
|
|
|
// However, this does not keep the type information as desired. Thus
|
|
|
|
// we need to do something more complicated.
|
|
|
|
|
|
|
|
var originTblCreateStmt string
|
2022-12-18 05:43:53 +03:00
|
|
|
err := db.QueryRowContext(ctx, fmt.Sprintf("SELECT sql FROM sqlite_master WHERE type='table' AND name='%s'",
|
|
|
|
fromTable)).Scan(&originTblCreateStmt)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple replace of the table name should work to mutate the
|
2020-08-09 16:46:46 +03:00
|
|
|
// above CREATE stmt to use toTable instead of fromTable.
|
2020-08-06 20:58:47 +03:00
|
|
|
destTblCreateStmt := strings.Replace(originTblCreateStmt, fromTable, toTable, 1)
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, destTblCreateStmt)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !copyData {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt := fmt.Sprintf("INSERT INTO %q SELECT * FROM %q", toTable, fromTable)
|
2021-02-22 10:37:00 +03:00
|
|
|
affected, err := sqlz.ExecAffected(ctx, db, stmt)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordMeta implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (sqlz.RecordMeta, driver.NewRecordFunc, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
recMeta, err := recordMetaFromColumnTypes(d.log, colTypes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
2022-12-17 02:34:33 +03:00
|
|
|
mungeFn := func(vals []any) (sqlz.Record, error) {
|
2022-12-24 07:05:26 +03:00
|
|
|
rec := newRecordFromScanRow(recMeta, vals)
|
2020-08-06 20:58:47 +03:00
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return recMeta, mungeFn, nil
|
|
|
|
}
|
|
|
|
|
2022-12-24 07:05:26 +03:00
|
|
|
// newRecordFromScanRow iterates over the elements of the row slice
|
|
|
|
// from rows.Scan, and returns a new (record) slice, replacing any
|
|
|
|
// wrapper types such as sql.NullString with the unboxed value,
|
|
|
|
// and other similar sanitization. For example, it will
|
|
|
|
// make a copy of any sql.RawBytes. The row slice
|
|
|
|
// can be reused by rows.Scan after this function returns.
|
|
|
|
//
|
|
|
|
// Note that this function can modify the kind of the RecordMeta elements
|
|
|
|
// if the kind is currently unknown. That is, if meta[0].Kind() returns
|
|
|
|
// kind.Unknown, but this function detects that row[0] is an *int64, then
|
|
|
|
// the kind will be set to kind.Int.
|
2022-12-25 08:00:28 +03:00
|
|
|
func newRecordFromScanRow(meta sqlz.RecordMeta, row []any) (rec sqlz.Record) { //nolint:funlen,gocognit,gocyclo,cyclop
|
2022-12-24 07:05:26 +03:00
|
|
|
rec = make([]any, len(row))
|
|
|
|
|
|
|
|
for i := 0; i < len(row); i++ {
|
|
|
|
if row[i] == nil {
|
|
|
|
rec[i] = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dereference *any before the switch
|
|
|
|
col := row[i]
|
|
|
|
if ptr, ok := col.(*any); ok {
|
|
|
|
col = *ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
switch col := col.(type) {
|
|
|
|
default:
|
|
|
|
// Shouldn't happen
|
|
|
|
// TODO: We really should log here
|
|
|
|
rec[i] = col
|
|
|
|
continue
|
|
|
|
case nil:
|
|
|
|
rec[i] = nil
|
|
|
|
case *int64:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
v := *col
|
|
|
|
rec[i] = &v
|
|
|
|
case int64:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
rec[i] = &col
|
|
|
|
case *float64:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Float)
|
|
|
|
v := *col
|
|
|
|
rec[i] = &v
|
|
|
|
case float64:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Float)
|
|
|
|
rec[i] = &col
|
|
|
|
case *bool:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Bool)
|
|
|
|
v := *col
|
|
|
|
rec[i] = &v
|
|
|
|
case bool:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Bool)
|
|
|
|
rec[i] = &col
|
|
|
|
case *string:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Text)
|
|
|
|
v := *col
|
|
|
|
rec[i] = &v
|
|
|
|
case string:
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Text)
|
|
|
|
rec[i] = &col
|
|
|
|
case *[]byte:
|
|
|
|
if col == nil || *col == nil {
|
|
|
|
rec[i] = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta[i].Kind() != kind.Bytes {
|
|
|
|
// We only want to use []byte for kind.Bytes. Otherwise
|
|
|
|
// switch to a string.
|
|
|
|
s := string(*col)
|
|
|
|
rec[i] = &s
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Text)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*col) == 0 {
|
|
|
|
v := []byte{}
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
dest := make([]byte, len(*col))
|
|
|
|
copy(dest, *col)
|
|
|
|
rec[i] = &dest
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Bytes)
|
|
|
|
case *sql.NullInt64:
|
|
|
|
if col.Valid {
|
|
|
|
v := col.Int64
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
case *sql.NullString:
|
|
|
|
if col.Valid {
|
|
|
|
v := col.String
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Text)
|
|
|
|
case *sql.RawBytes:
|
|
|
|
if col == nil || *col == nil {
|
|
|
|
// Explicitly set rec[i] so that its type becomes nil
|
|
|
|
rec[i] = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
knd := meta[i].Kind()
|
|
|
|
|
|
|
|
// If RawBytes is of length zero, there's no
|
|
|
|
// need to copy.
|
|
|
|
if len(*col) == 0 {
|
|
|
|
if knd == kind.Bytes {
|
|
|
|
v := []byte{}
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
// Else treat it as an empty string
|
|
|
|
var s string
|
|
|
|
rec[i] = &s
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := make([]byte, len(*col))
|
|
|
|
copy(dest, *col)
|
|
|
|
|
|
|
|
if knd == kind.Bytes {
|
|
|
|
rec[i] = &dest
|
|
|
|
} else {
|
|
|
|
str := string(dest)
|
|
|
|
rec[i] = &str
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *sql.NullFloat64:
|
|
|
|
if col.Valid {
|
|
|
|
v := col.Float64
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Float)
|
|
|
|
case *sql.NullBool:
|
|
|
|
if col.Valid {
|
|
|
|
v := col.Bool
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Bool)
|
|
|
|
case *sqlz.NullBool:
|
|
|
|
// This custom NullBool type is only used by sqlserver at this time.
|
|
|
|
// Possibly this code should skip this item, and allow
|
|
|
|
// the sqlserver munge func handle the conversion?
|
|
|
|
if col.Valid {
|
|
|
|
v := col.Bool
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Bool)
|
|
|
|
case *sql.NullTime:
|
|
|
|
if col.Valid {
|
|
|
|
v := col.Time
|
|
|
|
rec[i] = &v
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Datetime)
|
|
|
|
case *time.Time:
|
|
|
|
v := *col
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Datetime)
|
|
|
|
case time.Time:
|
|
|
|
v := col
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Datetime)
|
|
|
|
|
|
|
|
// REVISIT: We probably don't need any of the below cases
|
|
|
|
// for sqlite?
|
|
|
|
case *int:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case int:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *int8:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case int8:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *int16:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case int16:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *int32:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case int32:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *uint:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case uint:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *uint8:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case uint8:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *uint16:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case uint16:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *uint32:
|
|
|
|
v := int64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case uint32:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case *float32:
|
|
|
|
v := float64(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
|
|
|
|
case float32:
|
|
|
|
v := int64(col)
|
|
|
|
rec[i] = &v
|
|
|
|
sqlz.SetKindIfUnknown(meta, i, kind.Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec[i] != nil && meta[i].Kind() == kind.Decimal {
|
|
|
|
// Drivers use varying types for numeric/money/decimal.
|
|
|
|
// We want to standardize on string.
|
|
|
|
switch col := rec[i].(type) {
|
|
|
|
case *string:
|
|
|
|
// Do nothing, it's already string
|
|
|
|
|
|
|
|
case *[]byte:
|
|
|
|
v := string(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
|
|
|
|
case *float64:
|
|
|
|
v := stringz.FormatFloat(*col)
|
|
|
|
rec[i] = &v
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Shouldn't happen
|
|
|
|
v := fmt.Sprintf("%v", col)
|
|
|
|
rec[i] = &v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2020-08-09 16:46:46 +03:00
|
|
|
// DropTable implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) DropTable(ctx context.Context, db sqlz.DB, tbl string, ifExists bool) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
var stmt string
|
|
|
|
|
|
|
|
if ifExists {
|
|
|
|
stmt = fmt.Sprintf("DROP TABLE IF EXISTS %q", tbl)
|
|
|
|
} else {
|
|
|
|
stmt = fmt.Sprintf("DROP TABLE %q", tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := db.ExecContext(ctx, stmt)
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTable implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error {
|
2022-12-18 03:51:33 +03:00
|
|
|
query := buildCreateTableStmt(tblDef)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
stmt, err := db.PrepareContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.ExecContext(ctx)
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfCloseError(d.log, lgm.CloseDBStmt, stmt)
|
2020-08-06 20:58:47 +03:00
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errz.Err(stmt.Close())
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// CurrentSchema implements driver.SQLDriver.
|
|
|
|
func (d *driveri) CurrentSchema(ctx context.Context, db sqlz.DB) (string, error) {
|
|
|
|
const q = `SELECT name FROM pragma_database_list ORDER BY seq limit 1`
|
|
|
|
var name string
|
|
|
|
if err := db.QueryRowContext(ctx, q).Scan(&name); err != nil {
|
|
|
|
return "", errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlterTableRename implements driver.SQLDriver.
|
|
|
|
func (d *driveri) AlterTableRename(ctx context.Context, db sqlz.DB, tbl, newName string) error {
|
|
|
|
q := fmt.Sprintf(`ALTER TABLE %q RENAME TO %q`, tbl, newName)
|
|
|
|
_, err := db.ExecContext(ctx, q)
|
|
|
|
return errz.Wrapf(err, "alter table: failed to rename table {%s} to {%s}", tbl, newName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlterTableRenameColumn implements driver.SQLDriver.
|
|
|
|
func (d *driveri) AlterTableRenameColumn(ctx context.Context, db sqlz.DB, tbl, col, newName string) error {
|
|
|
|
q := fmt.Sprintf("ALTER TABLE %q RENAME COLUMN %q TO %q", tbl, col, newName)
|
|
|
|
_, err := db.ExecContext(ctx, q)
|
|
|
|
return errz.Wrapf(err, "alter table: failed to rename column {%s.%s} to {%s}", tbl, col, newName)
|
|
|
|
}
|
|
|
|
|
2020-08-21 06:08:59 +03:00
|
|
|
// AlterTableAddColumn implements driver.SQLDriver.
|
2023-03-22 09:17:34 +03:00
|
|
|
func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl, col string, knd kind.Kind) error {
|
|
|
|
q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + DBTypeForKind(knd)
|
2020-08-21 06:08:59 +03:00
|
|
|
|
|
|
|
_, err := db.ExecContext(ctx, q)
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
return errz.Wrapf(err, "alter table: failed to add column {%s} to table {%s}", col, tbl)
|
2020-08-21 06:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 05:56:22 +03:00
|
|
|
// TableExists implements driver.SQLDriver.
|
|
|
|
func (d *driveri) TableExists(ctx context.Context, db sqlz.DB, tbl string) (bool, error) {
|
2023-03-22 09:17:34 +03:00
|
|
|
const query = `SELECT COUNT(*) FROM sqlite_master WHERE name = ? AND type='table'`
|
2021-01-04 05:56:22 +03:00
|
|
|
|
|
|
|
var count int64
|
|
|
|
err := db.QueryRowContext(ctx, query, tbl).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return false, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return count == 1, nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// PrepareInsertStmt implements driver.SQLDriver.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string,
|
2022-12-18 11:35:59 +03:00
|
|
|
numRows int,
|
|
|
|
) (*driver.StmtExecer, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
destColsMeta, err := d.getTableRecordMeta(ctx, db, destTbl, destColNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
stmt, err := driver.PrepareInsertStmt(ctx, d, db, destTbl, destColsMeta.Names(), numRows)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
execer := driver.NewStmtExecer(stmt, driver.DefaultInsertMungeFunc(destTbl, destColsMeta),
|
|
|
|
newStmtExecFunc(stmt), destColsMeta)
|
2020-08-06 20:58:47 +03:00
|
|
|
return execer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareUpdateStmt implements driver.SQLDriver.
|
2022-12-18 03:51:33 +03:00
|
|
|
func (d *driveri) PrepareUpdateStmt(ctx context.Context, db sqlz.DB, destTbl string,
|
2022-12-18 11:35:59 +03:00
|
|
|
destColNames []string, where string,
|
|
|
|
) (*driver.StmtExecer, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
destColsMeta, err := d.getTableRecordMeta(ctx, db, destTbl, destColNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query, err := buildUpdateStmt(destTbl, destColNames, where)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.PrepareContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
execer := driver.NewStmtExecer(stmt, driver.DefaultInsertMungeFunc(destTbl, destColsMeta),
|
|
|
|
newStmtExecFunc(stmt), destColsMeta)
|
2020-08-06 20:58:47 +03:00
|
|
|
return execer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStmtExecFunc(stmt *sql.Stmt) driver.StmtExecFunc {
|
2022-12-17 02:34:33 +03:00
|
|
|
return func(ctx context.Context, args ...any) (int64, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
res, err := stmt.ExecContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
return affected, errz.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableColumnTypes implements driver.SQLDriver.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName string,
|
2022-12-18 11:35:59 +03:00
|
|
|
colNames []string,
|
|
|
|
) ([]*sql.ColumnType, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// Given the dynamic behavior of sqlite's rows.ColumnTypes,
|
|
|
|
// this query selects a single row, as that'll give us more
|
|
|
|
// accurate column type info than no rows. For other db
|
|
|
|
// impls, LIMIT can be 0.
|
|
|
|
const queryTpl = "SELECT %s FROM %s LIMIT 1"
|
|
|
|
|
|
|
|
dialect := d.Dialect()
|
2023-04-07 11:00:49 +03:00
|
|
|
quote := string(dialect.IdentQuote)
|
2020-08-06 20:58:47 +03:00
|
|
|
tblNameQuoted := stringz.Surround(tblName, quote)
|
|
|
|
|
2022-12-18 11:35:59 +03:00
|
|
|
colsClause := "*"
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(colNames) > 0 {
|
|
|
|
colNamesQuoted := stringz.SurroundSlice(colNames, quote)
|
|
|
|
colsClause = strings.Join(colNamesQuoted, driver.Comma)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf(queryTpl, colsClause, tblNameQuoted)
|
|
|
|
rows, err := db.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We invoke rows.ColumnTypes twice.
|
|
|
|
// The first time is to cover the scenario where the table
|
|
|
|
// is empty (no rows), so that we at least get some
|
|
|
|
// column type info.
|
|
|
|
colTypes, err := rows.ColumnTypes()
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDBRows, rows.Close)
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the table does have rows, we invoke rows.ColumnTypes again,
|
|
|
|
// as on this invocation the column type info will be more
|
|
|
|
// accurate (col nullability will be reported etc).
|
|
|
|
if rows.Next() {
|
|
|
|
colTypes, err = rows.ColumnTypes()
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDBRows, rows.Close)
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDBRows, rows.Close)
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return colTypes, nil
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName string,
|
2022-12-18 11:35:59 +03:00
|
|
|
colNames []string,
|
|
|
|
) (sqlz.RecordMeta, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
colTypes, err := d.TableColumnTypes(ctx, db, tblName, colNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
destCols, _, err := d.RecordMeta(colTypes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return destCols, nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 22:13:25 +03:00
|
|
|
// database implements driver.Database.
|
|
|
|
type database struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2020-08-18 22:13:25 +03:00
|
|
|
db *sql.DB
|
|
|
|
src *source.Source
|
|
|
|
drvr *driveri
|
2020-12-30 21:57:58 +03:00
|
|
|
|
|
|
|
// DEBUG: closeMu and closed exist while debugging close behavior
|
|
|
|
closeMu sync.Mutex
|
|
|
|
closed bool
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DB implements driver.Database.
|
|
|
|
func (d *database) DB() *sql.DB {
|
|
|
|
return d.db
|
|
|
|
}
|
|
|
|
|
|
|
|
// SQLDriver implements driver.Database.
|
|
|
|
func (d *database) SQLDriver() driver.SQLDriver {
|
|
|
|
return d.drvr
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2023-04-02 22:49:45 +03:00
|
|
|
return getTableMetadata(ctx, d.DB(), tblName)
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SourceMetadata implements driver.Database.
|
|
|
|
func (d *database) SourceMetadata(ctx context.Context) (*source.Metadata, error) {
|
|
|
|
// https://stackoverflow.com/questions/9646353/how-to-find-sqlite-database-file-version
|
|
|
|
|
|
|
|
meta := &source.Metadata{Handle: d.src.Handle, SourceType: Type, DBDriverType: dbDrvr}
|
|
|
|
|
|
|
|
dsn, err := PathFromLocation(d.src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
const q = "SELECT sqlite_version(), (SELECT name FROM pragma_database_list ORDER BY seq limit 1);"
|
2020-08-18 22:13:25 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
err = d.DB().QueryRowContext(ctx, q).Scan(&meta.DBVersion, &meta.Schema)
|
2020-08-18 22:13:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.DBProduct = "SQLite3 v" + meta.DBVersion
|
|
|
|
|
|
|
|
fi, err := os.Stat(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.Size = fi.Size()
|
|
|
|
meta.Name = fi.Name()
|
2023-03-22 09:17:34 +03:00
|
|
|
meta.FQName = fi.Name() + "/" + meta.Schema
|
2020-08-18 22:13:25 +03:00
|
|
|
meta.Location = d.src.Location
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
meta.Tables, err = getAllTblMeta(ctx, d.db)
|
2020-08-18 22:13:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Database.
|
|
|
|
func (d *database) Close() error {
|
2020-12-30 21:57:58 +03:00
|
|
|
d.closeMu.Lock()
|
|
|
|
defer d.closeMu.Unlock()
|
|
|
|
|
|
|
|
if d.closed {
|
2023-04-02 22:49:45 +03:00
|
|
|
d.log.Warn("SQLite DB already closed", lga.Src, d.src)
|
2020-12-30 21:57:58 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-18 22:13:25 +03:00
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
d.log.Debug(lgm.CloseDB, lga.Handle, d.src.Handle)
|
2020-12-30 21:57:58 +03:00
|
|
|
err := errz.Err(d.db.Close())
|
|
|
|
d.closed = true
|
|
|
|
return err
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 21:57:58 +03:00
|
|
|
// NewScratchSource returns a new scratch src. Effectively this
|
|
|
|
// function creates a new sqlite db file in the temp dir, and
|
|
|
|
// src points at this file. The returned clnup func closes that
|
|
|
|
// db file and deletes it.
|
2023-04-30 17:18:56 +03:00
|
|
|
func NewScratchSource(ctx context.Context, name string) (src *source.Source, clnup func() error, err error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
name = stringz.SanitizeAlphaNumeric(name, '_')
|
|
|
|
_, f, cleanFn, err := source.TempDirFile(name + ".sqlite")
|
|
|
|
if err != nil {
|
|
|
|
return nil, cleanFn, err
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
log.Debug("Created sqlite3 scratchdb data file", lga.Path, f.Name())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
src = &source.Source{
|
|
|
|
Type: Type,
|
|
|
|
Handle: source.ScratchHandle,
|
2020-08-07 22:51:30 +03:00
|
|
|
Location: Prefix + f.Name(),
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-10 04:29:13 +03:00
|
|
|
fn := func() error {
|
|
|
|
log.Debug("Deleting sqlite3 scratchdb data file", lga.Src, src, lga.Path, f.Name())
|
|
|
|
if cleanFn != nil {
|
|
|
|
return cleanFn()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return src, fn, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:51:30 +03:00
|
|
|
// PathFromLocation returns the absolute file path
|
|
|
|
// from the source location, which should have the "sqlite3://" prefix.
|
|
|
|
func PathFromLocation(src *source.Source) (string, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if src.Type != Type {
|
2023-04-02 22:49:45 +03:00
|
|
|
return "", errz.Errorf("driver {%s} does not support {%s}", Type, src.Type)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:51:30 +03:00
|
|
|
if !strings.HasPrefix(src.Location, Prefix) {
|
2023-04-02 22:49:45 +03:00
|
|
|
return "", errz.Errorf("sqlite3 source location must begin with {%s} but was: %s", Prefix, src.RedactedLocation())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:51:30 +03:00
|
|
|
loc := strings.TrimPrefix(src.Location, Prefix)
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(loc) < 2 {
|
|
|
|
return "", errz.Errorf("sqlite3 source location is too short: %s", src.RedactedLocation())
|
|
|
|
}
|
|
|
|
|
|
|
|
loc = filepath.Clean(loc)
|
|
|
|
return loc, nil
|
|
|
|
}
|
2023-01-01 06:17:44 +03:00
|
|
|
|
|
|
|
// MungeLocation takes a location argument (as received from the user)
|
|
|
|
// and builds a sqlite3 location URL. Each of these forms are allowed:
|
|
|
|
//
|
|
|
|
// sqlite3:///path/to/sakila.db --> sqlite3:///path/to/sakila.db
|
|
|
|
// sqlite3:sakila.db --> sqlite3:///current/working/dir/sakila.db
|
|
|
|
// sqlite3:/sakila.db --> sqlite3:///sakila.db
|
|
|
|
// sqlite3:./sakila.db --> sqlite3:///current/working/dir/sakila.db
|
|
|
|
// sqlite3:sakila.db --> sqlite3:///current/working/dir/sakila.db
|
|
|
|
// sakila.db --> sqlite3:///current/working/dir/sakila.db
|
|
|
|
// /path/to/sakila.db --> sqlite3:///path/to/sakila.db
|
|
|
|
//
|
|
|
|
// The final form is particularly nice for shell completion etc.
|
|
|
|
func MungeLocation(loc string) (string, error) {
|
|
|
|
loc2 := strings.TrimSpace(loc)
|
|
|
|
if loc2 == "" {
|
|
|
|
return "", errz.New("location must not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
loc2 = strings.TrimPrefix(loc2, "sqlite3://")
|
|
|
|
loc2 = strings.TrimPrefix(loc2, "sqlite3:")
|
|
|
|
|
|
|
|
// Now we should be left with just a path, which could be
|
|
|
|
// relative or absolute.
|
|
|
|
u, err := url.Parse(loc2)
|
|
|
|
if err != nil {
|
|
|
|
return "", errz.Wrapf(err, "invalid location: %s", loc)
|
|
|
|
}
|
|
|
|
|
2023-01-01 08:35:33 +03:00
|
|
|
fp, err := filepath.Abs(u.Path)
|
2023-01-01 06:17:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errz.Wrapf(err, "invalid location: %s", loc)
|
|
|
|
}
|
|
|
|
|
2023-01-01 08:35:33 +03:00
|
|
|
fp = filepath.ToSlash(fp)
|
|
|
|
return "sqlite3://" + fp, nil
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|