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-08-12 21:54:14 +03:00
|
|
|
"log/slog"
|
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-11-20 04:06:36 +03:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Import for side effect of loading the driver
|
2023-11-22 00:49:52 +03:00
|
|
|
"github.com/shopspring/decimal"
|
2023-11-19 03:05:48 +03:00
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/sqlite3/internal/sqlparser"
|
2023-11-19 03:05:48 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/ast"
|
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"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/jointype"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgm"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/loz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/record"
|
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"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/tablefq"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver/dialect"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/drivertype"
|
|
|
|
"github.com/neilotoole/sq/libsq/source/metadata"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Type is the sqlite3 source driver type.
|
2023-11-21 00:42:38 +03:00
|
|
|
Type drivertype.Type = "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
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DriverFor implements driver.Provider.
|
2023-11-21 00:42:38 +03:00
|
|
|
func (p *Provider) DriverFor(typ drivertype.Type) (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
|
|
|
}
|
|
|
|
|
2023-06-13 19:06:18 +03:00
|
|
|
var _ driver.SQLDriver = (*driveri)(nil)
|
2020-08-20 23:19:16 +03:00
|
|
|
|
2023-06-13 19:06:18 +03:00
|
|
|
// driveri is the SQLite3 implementation of driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
type driveri struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 19:06:18 +03:00
|
|
|
// ConnParams implements driver.SQLDriver.
|
|
|
|
// See: https://github.com/mattn/go-sqlite3#connection-string.
|
|
|
|
func (d *driveri) ConnParams() map[string][]string {
|
|
|
|
return map[string][]string{
|
|
|
|
"_auth": nil,
|
|
|
|
"_auth_crypt": {"SHA1", "SSHA1", "SHA256", "SSHA256", "SHA384", "SSHA384", "SHA512", "SSHA512"},
|
|
|
|
"_auth_pass": nil,
|
|
|
|
"_auth_salt": nil,
|
|
|
|
"_auth_user": nil,
|
|
|
|
"_auto_vacuum": {"none", "full", "incremental"},
|
|
|
|
"_busy_timeout": nil,
|
|
|
|
"_cache_size": {"-2000"},
|
|
|
|
"_case_sensitive_like": {"true", "false"},
|
|
|
|
"_defer_foreign_keys": {"true", "false"},
|
|
|
|
"_foreign_keys": {"true", "false"},
|
|
|
|
"_ignore_check_constraints": {"true", "false"},
|
|
|
|
"_journal_mode": {"DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF"},
|
|
|
|
"_loc": nil,
|
|
|
|
"_locking_mode": {"NORMAL", "EXCLUSIVE"},
|
|
|
|
"_mutex": {"no", "full"},
|
|
|
|
"_query_only": {"true", "false"},
|
|
|
|
"_recursive_triggers": {"true", "false"},
|
|
|
|
"_secure_delete": {"true", "false", "FAST"},
|
|
|
|
"_synchronous": {"OFF", "NORMAL", "FULL", "EXTRA"},
|
|
|
|
"_txlock": {"immediate", "deferred", "exclusive"},
|
|
|
|
"cache": {"true", "false", "FAST"},
|
|
|
|
"mode": {"ro", "rw", "rwc", "memory"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// ErrWrapFunc implements driver.SQLDriver.
|
|
|
|
func (d *driveri) ErrWrapFunc() func(error) error {
|
|
|
|
return errw
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// DBProperties implements driver.SQLDriver.
|
|
|
|
func (d *driveri) DBProperties(ctx context.Context, db sqlz.DB) (map[string]any, error) {
|
|
|
|
return getDBProperties(ctx, db)
|
|
|
|
}
|
|
|
|
|
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-11-19 05:21:14 +03:00
|
|
|
// Open implements driver.PoolOpener.
|
|
|
|
func (d *driveri) Open(ctx context.Context, src *source.Source) (driver.Pool, 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
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
if err = driver.OpeningPing(ctx, src, db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
return &pool{log: d.log, db: db, src: src, drvr: d}, nil
|
2023-04-30 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driveri) doOpen(ctx context.Context, src *source.Source) (*sql.DB, error) {
|
2023-11-19 03:05:48 +03:00
|
|
|
fp, err := PathFromLocation(src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-19 03:05:48 +03:00
|
|
|
db, err := sql.Open(dbDrvr, fp)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-11-19 03:05:48 +03:00
|
|
|
return nil, errz.Wrapf(errw(err), "failed to open sqlite3 source with DSN: %s", fp)
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return affected, errz.Append(err, errw(tx.Rollback()))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errz.Append(err, errw(tx.Rollback()))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errz.Append(err, errw(tx.Rollback()))
|
2021-02-22 10:37:00 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
return affected, errw(tx.Commit())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
db, err := d.doOpen(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
defer lg.WarnIfCloseError(d.log, lgm.CloseDB, db)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
if err = db.PingContext(ctx); err != nil {
|
|
|
|
err = errz.Wrapf(err, "ping %s: %s", src.Handle, src.Location)
|
|
|
|
lg.FromContext(ctx).Warn("ping failed",
|
|
|
|
lga.Src, src,
|
|
|
|
lga.Err, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
Enquote: stringz.DoubleQuote,
|
2020-08-12 21:24:01 +03:00
|
|
|
MaxBatchValues: 500,
|
2023-04-07 11:00:49 +03:00
|
|
|
Ops: dialect.DefaultOps(),
|
2023-07-03 18:34:19 +03:00
|
|
|
Joins: jointype.All(),
|
2023-11-19 03:05:48 +03:00
|
|
|
Catalog: false,
|
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-11-19 03:05:48 +03:00
|
|
|
r := render.NewDefaultRenderer()
|
2023-11-20 09:44:36 +03:00
|
|
|
const schemaFrag = `(SELECT name FROM pragma_database_list ORDER BY seq limit 1)`
|
|
|
|
r.FunctionOverrides[ast.FuncNameSchema] = render.FuncOverrideString(schemaFrag)
|
|
|
|
|
|
|
|
// SQLite doesn't support catalogs, so we just return the string "default".
|
|
|
|
// We could return empty string, but that may be even more confusing, and would
|
|
|
|
// make SQLite the odd man out, as the other SQL drivers (even MySQL)
|
|
|
|
// have a value for catalog.
|
|
|
|
const catalogFrag = `(SELECT 'default')`
|
|
|
|
r.FunctionOverrides[ast.FuncNameCatalog] = render.FuncOverrideString(catalogFrag)
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
return r
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTable implements driver.SQLDriver.
|
2023-11-19 03:05:48 +03:00
|
|
|
func (d *driveri) CopyTable(ctx context.Context, db sqlz.DB,
|
|
|
|
fromTbl, toTbl tablefq.T, 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.
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// First we get the original CREATE TABLE statement.
|
|
|
|
masterTbl := tablefq.T{Schema: fromTbl.Schema, Table: "sqlite_master"}
|
|
|
|
q := fmt.Sprintf("SELECT sql FROM %s WHERE type='table' AND name=%s",
|
|
|
|
masterTbl.Render(stringz.DoubleQuote),
|
|
|
|
stringz.SingleQuote(fromTbl.Table))
|
|
|
|
var ogTblCreateStmt string
|
|
|
|
err := db.QueryRowContext(ctx, q).Scan(&ogTblCreateStmt)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// Next, we extract the table identifier from the CREATE TABLE statement.
|
|
|
|
// For example, "main"."actor". Note that the schema part may be empty.
|
|
|
|
ogSchema, ogTbl, err := sqlparser.ExtractTableIdentFromCreateTableStmt(ogTblCreateStmt,
|
|
|
|
false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errw(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know what text to replace in ogTblCreateStmt.
|
|
|
|
replaceTarget := ogTbl
|
|
|
|
if ogSchema != "" {
|
|
|
|
replaceTarget = ogSchema + "." + ogTbl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the old table identifier with the new one, and, voila,
|
|
|
|
// we have our new CREATE TABLE statement.
|
|
|
|
destTblCreateStmt := strings.Replace(
|
|
|
|
ogTblCreateStmt,
|
|
|
|
replaceTarget,
|
|
|
|
toTbl.Render(stringz.DoubleQuote),
|
|
|
|
1,
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, destTblCreateStmt)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !copyData {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
stmt := fmt.Sprintf("INSERT INTO %s SELECT * FROM %s", toTbl, fromTbl)
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordMeta implements driver.SQLDriver.
|
2023-07-09 04:34:53 +03:00
|
|
|
func (d *driveri) RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (
|
|
|
|
record.Meta, driver.NewRecordFunc, error,
|
2023-07-03 18:34:19 +03:00
|
|
|
) {
|
|
|
|
recMeta, err := recordMetaFromColumnTypes(ctx, colTypes)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, nil, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
mungeFn := func(vals []any) (record.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.
|
|
|
|
//
|
2023-05-22 18:08:14 +03:00
|
|
|
// Note that this function can modify the kind of the record.Meta elements
|
2022-12-24 07:05:26 +03:00
|
|
|
// 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.
|
2023-05-22 18:08:14 +03:00
|
|
|
//
|
|
|
|
//nolint:funlen,gocognit,gocyclo,cyclop
|
|
|
|
func newRecordFromScanRow(meta record.Meta, row []any) (rec record.Record) {
|
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:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = *col
|
2022-12-24 07:05:26 +03:00
|
|
|
case int64:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col
|
2022-12-24 07:05:26 +03:00
|
|
|
case *float64:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Float)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = *col
|
2022-12-24 07:05:26 +03:00
|
|
|
case float64:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Float)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col
|
2023-11-22 00:49:52 +03:00
|
|
|
case decimal.Decimal:
|
|
|
|
record.SetKindIfUnknown(meta, i, kind.Decimal)
|
|
|
|
rec[i] = col
|
|
|
|
case *decimal.Decimal:
|
|
|
|
record.SetKindIfUnknown(meta, i, kind.Decimal)
|
|
|
|
rec[i] = *col
|
|
|
|
|
2022-12-24 07:05:26 +03:00
|
|
|
case *bool:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Bool)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = *col
|
2022-12-24 07:05:26 +03:00
|
|
|
case bool:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Bool)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col
|
2022-12-24 07:05:26 +03:00
|
|
|
case *string:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Text)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = *col
|
2022-12-24 07:05:26 +03:00
|
|
|
case string:
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Text)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col
|
2022-12-24 07:05:26 +03:00
|
|
|
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)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = s
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Text)
|
2022-12-24 07:05:26 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*col) == 0 {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = []byte{}
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
dest := make([]byte, len(*col))
|
|
|
|
copy(dest, *col)
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = dest
|
2022-12-24 07:05:26 +03:00
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Bytes)
|
2022-12-24 07:05:26 +03:00
|
|
|
case *sql.NullInt64:
|
|
|
|
if col.Valid {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col.Int64
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2023-11-22 00:49:52 +03:00
|
|
|
case *decimal.NullDecimal:
|
|
|
|
if col.Valid {
|
|
|
|
rec[i] = col.Decimal
|
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2022-12-24 07:05:26 +03:00
|
|
|
case *sql.NullString:
|
|
|
|
if col.Valid {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col.String
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Text)
|
2022-12-24 07:05:26 +03:00
|
|
|
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 {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = []byte{}
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
// Else treat it as an empty string
|
|
|
|
var s string
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = s
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Text)
|
2022-12-24 07:05:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := make([]byte, len(*col))
|
|
|
|
copy(dest, *col)
|
|
|
|
|
|
|
|
if knd == kind.Bytes {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = dest
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
2023-05-27 16:57:07 +03:00
|
|
|
s := string(dest)
|
|
|
|
rec[i] = s
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Text)
|
2022-12-24 07:05:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case *sql.NullFloat64:
|
|
|
|
if col.Valid {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col.Float64
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Float)
|
2022-12-24 07:05:26 +03:00
|
|
|
case *sql.NullBool:
|
|
|
|
if col.Valid {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col.Bool
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Bool)
|
2022-12-24 07:05:26 +03:00
|
|
|
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 {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col.Bool
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Bool)
|
2022-12-24 07:05:26 +03:00
|
|
|
case *sql.NullTime:
|
|
|
|
if col.Valid {
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col.Time
|
2022-12-24 07:05:26 +03:00
|
|
|
} else {
|
|
|
|
rec[i] = nil
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Datetime)
|
2022-12-24 07:05:26 +03:00
|
|
|
case *time.Time:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = *col
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Datetime)
|
2022-12-24 07:05:26 +03:00
|
|
|
case time.Time:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = col
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Datetime)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
// REVISIT: We probably don't need any of the below cases
|
|
|
|
// for sqlite?
|
|
|
|
case *int:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case int:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *int8:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case int8:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *int16:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case int16:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *int32:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case int32:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *uint:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case uint:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *uint8:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case uint8:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *uint16:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case uint16:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *uint32:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case uint32:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case *float32:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = float64(*col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
case float32:
|
2023-05-27 16:57:07 +03:00
|
|
|
rec[i] = int64(col)
|
2023-05-22 18:08:14 +03:00
|
|
|
record.SetKindIfUnknown(meta, i, kind.Int)
|
2022-12-24 07:05:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2020-08-09 16:46:46 +03:00
|
|
|
// DropTable implements driver.SQLDriver.
|
2023-11-19 03:05:48 +03:00
|
|
|
func (d *driveri) DropTable(ctx context.Context, db sqlz.DB, tbl tablefq.T, ifExists bool) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
var stmt string
|
|
|
|
|
|
|
|
if ifExists {
|
2023-11-19 03:05:48 +03:00
|
|
|
stmt = fmt.Sprintf("DROP TABLE IF EXISTS %s", tbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
} else {
|
2023-11-19 03:05:48 +03:00
|
|
|
stmt = fmt.Sprintf("DROP TABLE %s", tbl)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := db.ExecContext(ctx, stmt)
|
2023-05-22 18:08:14 +03:00
|
|
|
return errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// CreateSchema implements driver.SQLDriver. This is implemented for SQLite
|
|
|
|
// by attaching a new database to db, using ATTACH DATABASE. This attached
|
|
|
|
// database is only available on the connection on which the ATTACH DATABASE
|
|
|
|
// command was issued. Thus, db must be a *sql.Conn or *sql.Tx, as
|
|
|
|
// per sqlz.RequireSingleConn. The same constraint applies to DropSchema.
|
|
|
|
//
|
|
|
|
// See: https://www.sqlite.org/lang_attach.html
|
|
|
|
func (d *driveri) CreateSchema(ctx context.Context, db sqlz.DB, schemaName string) error {
|
|
|
|
if err := sqlz.RequireSingleConn(db); err != nil {
|
|
|
|
return errz.Wrapf(err, "create schema {%s}: ATTACH DATABASE requires single connection", schemaName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Empty string for DATABASE creates a temporary database.
|
|
|
|
// We may want to change this to create a more permanent database, perhaps
|
|
|
|
// in the same directory as the existing db?
|
|
|
|
stmt := `ATTACH DATABASE "" AS ` + stringz.DoubleQuote(schemaName)
|
|
|
|
if _, err := db.ExecContext(ctx, stmt); err != nil {
|
|
|
|
return errz.Wrapf(err, "create schema {%s}", schemaName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DropSchema implements driver.SQLDriver. As per CreateSchema, db must
|
|
|
|
// be a *sql.Conn or *sql.Tx.
|
|
|
|
//
|
|
|
|
// See https://www.sqlite.org/lang_detach.html
|
|
|
|
func (d *driveri) DropSchema(ctx context.Context, db sqlz.DB, schemaName string) error {
|
|
|
|
if err := sqlz.RequireSingleConn(db); err != nil {
|
|
|
|
return errz.Wrapf(err, "drop schema {%s}: DETACH DATABASE requires single connection", schemaName)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt := `DETACH DATABASE ` + stringz.DoubleQuote(schemaName)
|
|
|
|
if _, err := db.ExecContext(ctx, stmt); err != nil {
|
|
|
|
return errz.Wrapf(err, "drop schema {%s}", schemaName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// 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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.ExecContext(ctx)
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfCloseError(d.log, lgm.CloseDBStmt, stmt)
|
2023-05-22 18:08:14 +03:00
|
|
|
return errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
return errw(stmt.Close())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return "", errw(err)
|
2023-03-22 09:17:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// ListSchemas implements driver.SQLDriver.
|
|
|
|
func (d *driveri) ListSchemas(ctx context.Context, db sqlz.DB) ([]string, error) {
|
|
|
|
log := lg.FromContext(ctx)
|
|
|
|
|
|
|
|
const q = `SELECT name FROM pragma_database_list ORDER BY name`
|
|
|
|
var schemas []string
|
|
|
|
rows, err := db.QueryContext(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var schema string
|
|
|
|
if err = rows.Scan(&schema); err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
schemas = append(schemas, schema)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return schemas, nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
// ListSchemaMetadata implements driver.SQLDriver.
|
|
|
|
// The returned metadata.Schema instances will have a Catalog
|
|
|
|
// value of "default", and an empty Owner value.
|
|
|
|
func (d *driveri) ListSchemaMetadata(ctx context.Context, db sqlz.DB) ([]*metadata.Schema, error) {
|
|
|
|
names, err := d.ListSchemas(ctx, db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
schemas := make([]*metadata.Schema, len(names))
|
|
|
|
for i, name := range names {
|
|
|
|
schemas[i] = &metadata.Schema{
|
|
|
|
Name: name,
|
|
|
|
Catalog: "default",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return schemas, nil
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// CurrentCatalog implements driver.SQLDriver. SQLite does not support catalogs,
|
|
|
|
// so this method returns an error.
|
|
|
|
func (d *driveri) CurrentCatalog(_ context.Context, _ sqlz.DB) (string, error) {
|
|
|
|
return "", errz.New("sqlite3: catalog mechanism not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListCatalogs implements driver.SQLDriver. SQLite does not support catalogs,
|
|
|
|
// so this method returns an error.
|
|
|
|
func (d *driveri) ListCatalogs(_ context.Context, _ sqlz.DB) ([]string, error) {
|
|
|
|
return nil, errz.New("sqlite3: catalog mechanism not supported")
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// 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)
|
2023-05-22 18:08:14 +03:00
|
|
|
return errz.Wrapf(errw(err), "alter table: failed to rename table {%s} to {%s}", tbl, newName)
|
2023-03-22 09:17:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2023-05-22 18:08:14 +03:00
|
|
|
return errz.Wrapf(errw(err), "alter table: failed to rename column {%s.%s} to {%s}", tbl, col, newName)
|
2023-03-22 09:17:34 +03:00
|
|
|
}
|
|
|
|
|
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-05-22 18:08:14 +03:00
|
|
|
return errz.Wrapf(errw(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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return false, errw(err)
|
2021-01-04 05:56:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-05-22 18:08:14 +03:00
|
|
|
return 0, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
affected, err := res.RowsAffected()
|
2023-05-22 18:08:14 +03:00
|
|
|
return affected, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
enquote := d.Dialect().Enquote
|
|
|
|
tblNameQuoted := enquote(tblName)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2022-12-18 11:35:59 +03:00
|
|
|
colsClause := "*"
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(colNames) > 0 {
|
2023-07-03 18:34:19 +03:00
|
|
|
colNamesQuoted := loz.Apply(colNames, enquote)
|
2020-08-06 20:58:47 +03:00
|
|
|
colsClause = strings.Join(colNamesQuoted, driver.Comma)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf(queryTpl, colsClause, tblNameQuoted)
|
|
|
|
rows, err := db.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
2023-04-02 22:49:45 +03:00
|
|
|
lg.WarnIfFuncError(d.log, lgm.CloseDBRows, rows.Close)
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Close()
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return colTypes, nil
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName string, colNames []string) (
|
|
|
|
record.Meta, error,
|
|
|
|
) {
|
2020-08-06 20:58:47 +03:00
|
|
|
colTypes, err := d.TableColumnTypes(ctx, db, tblName, colNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
destCols, _, err := d.RecordMeta(ctx, colTypes)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return destCols, nil
|
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// pool implements driver.Pool.
|
|
|
|
type pool 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
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// DB implements driver.Pool.
|
|
|
|
func (p *pool) DB(context.Context) (*sql.DB, error) {
|
|
|
|
return p.db, nil
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// SQLDriver implements driver.Pool.
|
|
|
|
func (p *pool) SQLDriver() driver.SQLDriver {
|
|
|
|
return p.drvr
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// Source implements driver.Pool.
|
|
|
|
func (p *pool) Source() *source.Source {
|
|
|
|
return p.src
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// TableMetadata implements driver.Pool.
|
2023-11-21 00:42:38 +03:00
|
|
|
func (p *pool) TableMetadata(ctx context.Context, tblName string) (*metadata.Table, error) {
|
2023-11-19 05:21:14 +03:00
|
|
|
db, err := p.DB(ctx)
|
2023-07-08 18:21:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTableMetadata(ctx, db, tblName)
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// SourceMetadata implements driver.Pool.
|
2023-11-21 00:42:38 +03:00
|
|
|
func (p *pool) SourceMetadata(ctx context.Context, noSchema bool) (*metadata.Source, error) {
|
2020-08-18 22:13:25 +03:00
|
|
|
// https://stackoverflow.com/questions/9646353/how-to-find-sqlite-database-file-version
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
md := &metadata.Source{Handle: p.src.Handle, Driver: Type, DBDriver: dbDrvr}
|
2020-08-18 22:13:25 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
dsn, err := PathFromLocation(p.src)
|
2020-08-18 22:13:25 +03:00
|
|
|
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-11-19 05:21:14 +03:00
|
|
|
err = p.db.QueryRowContext(ctx, q).Scan(&md.DBVersion, &md.Schema)
|
2020-08-18 22:13:25 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
md.DBProduct = "SQLite3 v" + md.DBVersion
|
2020-08-18 22:13:25 +03:00
|
|
|
|
|
|
|
fi, err := os.Stat(dsn)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
md.Size = fi.Size()
|
|
|
|
md.Name = fi.Name()
|
2023-11-19 17:21:38 +03:00
|
|
|
md.FQName = fi.Name() + "." + md.Schema
|
|
|
|
// SQLite doesn't support catalog, but we conventionally set it to "default"
|
|
|
|
md.Catalog = "default"
|
2023-11-19 05:21:14 +03:00
|
|
|
md.Location = p.src.Location
|
2020-08-18 22:13:25 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
md.DBProperties, err = getDBProperties(ctx, p.db)
|
2020-08-18 22:13:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-19 17:24:18 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
if noSchema {
|
|
|
|
return md, nil
|
|
|
|
}
|
2023-05-26 06:58:43 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
md.Tables, err = getAllTableMetadata(ctx, p.db, md.Schema)
|
2023-05-19 17:24:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
for _, tbl := range md.Tables {
|
|
|
|
if tbl.TableType == sqlz.TableTypeTable {
|
|
|
|
md.TableCount++
|
|
|
|
} else if tbl.TableType == sqlz.TableTypeView {
|
|
|
|
md.ViewCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 06:58:43 +03:00
|
|
|
return md, nil
|
2020-08-18 22:13:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
// Close implements driver.Pool.
|
|
|
|
func (p *pool) Close() error {
|
|
|
|
p.closeMu.Lock()
|
|
|
|
defer p.closeMu.Unlock()
|
2020-12-30 21:57:58 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
if p.closed {
|
|
|
|
p.log.Warn("SQLite DB already closed", lga.Src, p.src)
|
2020-12-30 21:57:58 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-18 22:13:25 +03:00
|
|
|
|
2023-11-19 05:21:14 +03:00
|
|
|
p.log.Debug(lgm.CloseDB, lga.Handle, p.src.Handle)
|
|
|
|
err := errw(p.db.Close())
|
|
|
|
p.closed = true
|
2020-12-30 21:57:58 +03:00
|
|
|
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
|
2023-06-18 09:44:01 +03:00
|
|
|
// src points at this file. The returned clnup func will delete
|
|
|
|
// the file.
|
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, '_')
|
2023-06-18 09:44:01 +03:00
|
|
|
dir, file, err := source.TempDirFile(name + ".sqlite")
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
2023-06-18 09:44:01 +03:00
|
|
|
return nil, nil, err
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-18 09:44:01 +03:00
|
|
|
log.Debug("Created sqlite3 scratchdb data file", lga.Path, file)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
src = &source.Source{
|
|
|
|
Type: Type,
|
|
|
|
Handle: source.ScratchHandle,
|
2023-06-18 09:44:01 +03:00
|
|
|
Location: Prefix + file,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-10 04:29:13 +03:00
|
|
|
fn := func() error {
|
2023-06-18 09:44:01 +03:00
|
|
|
log.Debug("Deleting sqlite3 scratchdb file", lga.Src, src, lga.Path, file)
|
|
|
|
rmErr := errz.Err(os.RemoveAll(dir))
|
|
|
|
if rmErr != nil {
|
|
|
|
log.Warn("Delete sqlite3 scratchdb file", lga.Err, rmErr)
|
2023-04-10 04:29:13 +03:00
|
|
|
}
|
|
|
|
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
|
2023-11-19 03:05:48 +03:00
|
|
|
// 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
|
2023-01-01 06:17:44 +03:00
|
|
|
//
|
|
|
|
// The final form is particularly nice for shell completion etc.
|
2023-11-19 03:05:48 +03:00
|
|
|
//
|
|
|
|
// Note that this function is OS-dependent, due to the use of pkg filepath.
|
|
|
|
// Thus, on Windows, this is seen:
|
|
|
|
//
|
|
|
|
// C:/Users/sq/sakila.db --> sqlite3://C:/Users/sq/sakila.db
|
|
|
|
//
|
|
|
|
// But that input location gets mangled on non-Windows OSes. This probably
|
|
|
|
// isn't a problem in practice, but longer-term it may make sense to rewrite
|
|
|
|
// MungeLocation to be OS-independent.
|
2023-01-01 06:17:44 +03:00
|
|
|
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:")
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
fp, err := filepath.Abs(loc2)
|
2023-01-01 06:17:44 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return "", errz.Wrapf(errw(err), "invalid location: %s", loc)
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 08:35:33 +03:00
|
|
|
fp = filepath.ToSlash(fp)
|
|
|
|
return "sqlite3://" + fp, nil
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|