driver

package
v0.48.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const Comma = ", "

Comma is the comma string to use in SQL queries.

Variables

View Source
var (
	// OptConnMaxOpen controls sql.DB.SetMaxOpenConn.
	OptConnMaxOpen = options.NewInt(
		"conn.max-open",
		nil,
		0,
		"Max open connections to DB",
		`Maximum number of open connections to the database.

A value of zero indicates no limit.`,
		options.TagSource,
		options.TagSQL,
	)

	// OptConnMaxIdle controls sql.DB.SetMaxIdleConns.
	OptConnMaxIdle = options.NewInt(
		"conn.max-idle",
		nil,
		2,
		"Max connections in idle connection pool",
		`Set the maximum number of connections in the idle connection pool. If
conn.max-open is greater than 0 but less than the new conn.max-idle, then the
new conn.max-idle will be reduced to match the conn.max-open limit.

If n <= 0, no idle connections are retained.`,
		options.TagSource,
		options.TagSQL,
	)

	// OptConnMaxIdleTime controls sql.DB.SetConnMaxIdleTime.
	OptConnMaxIdleTime = options.NewDuration(
		"conn.max-idle-time",
		nil,
		time.Second*2,
		"Max connection idle time",
		`Sets the maximum amount of time a connection may be idle. Expired connections
may be closed lazily before reuse.

If n <= 0, connections are not closed due to a connection's idle time.`,
		options.TagSource,
		options.TagSQL,
	)

	// OptConnMaxLifetime controls sql.DB.SetConnMaxLifetime.
	OptConnMaxLifetime = options.NewDuration(
		"conn.max-lifetime",
		nil,
		time.Minute*10,
		"Max connection lifetime",
		`
Set the maximum amount of time a connection may be reused. Expired connections
may be closed lazily before reuse.

If n <= 0, connections are not closed due to a connection's age.`,
		options.TagSource,
		options.TagSQL,
	)

	// OptConnOpenTimeout controls connection open timeout.
	OptConnOpenTimeout = options.NewDuration(
		"conn.open-timeout",
		nil,
		time.Second*10,
		"Connection open timeout",
		"Max time to wait before a connection open timeout occurs.",
		options.TagSource,
		options.TagSQL,
	)
)
View Source
var OptIngestCache = options.NewBool(
	"ingest.cache",
	&options.Flag{
		Name:   "no-cache",
		Invert: true,
		Usage:  "Don't cache ingest data",
	},
	true,
	"Cache ingest data",
	`Specifies whether ingested data is cached or not, on a default or per-source
basis. When data is ingested from a document source, it is stored in a cache DB.
Subsequent uses of that same source will use that cached DB instead of ingesting
the data again, unless this option is set to false, in which case, the data is
ingested each time.

  # Set default ingest caching behavior
  $ sq config set ingest.cache false

  # Set ingest caching behavior for a specific source
  $ sq config set --src @sakila ingest.cache false`,
	options.TagSource,
)

OptIngestCache specifies whether ingested data is cached or not.

View Source
var OptIngestColRename = options.NewString(
	"ingest.column.rename",
	nil,
	"{{.Name}}{{with .Recurrence}}_{{.}}{{end}}",
	func(s string) error {
		return stringz.ValidTemplate("ingest.column.rename", s)
	},
	"Template to rename ingest columns",
	`This Go text template is executed on ingested column names.
Its primary purpose is to rename duplicate header column names in the
ingested data. For example, given a CSV file with header row:

  actor_id, first_name, actor_id

The default template renames the columns to:

  actor_id, first_name, actor_id_1

The fields available in the template are:

  .Name         column header name
  .Index        zero-based index of the column in the header row
  .Alpha        alphabetical index of the column, e.g. [A, B ... Z, AA, AB]
  .Recurrence   nth recurrence of the colum name in the header row

For a unique column name, e.g. "first_name" above, ".Recurrence" will be 0.
For duplicate column names, ".Recurrence" will be 0 for the first instance,
then 1 for the next instance, and so on.`,
	options.TagSource,
	options.TagIngestMutate,
)

OptIngestColRename transforms a column name in ingested data.

View Source
var OptIngestHeader = options.NewBool(
	"ingest.header",
	nil,
	false,
	"Ingest data has a header row",
	`Specifies whether ingested data has a header row or not.
If not set, the ingester *may* try to detect if the input has a header.
Generally it is best to leave this option unset and allow the ingester
to detect the header.`,
	options.TagSource,
	options.TagIngestMutate,
)

OptIngestHeader specifies whether ingested data has a header row or not. If not set, the ingester *may* try to detect if the input has a header.

View Source
var OptIngestSampleSize = options.NewInt(
	"ingest.sample-size",
	nil,
	256,
	"Ingest data sample size for type detection",
	`Specify the number of samples that a detector should take to determine type.`,
	options.TagSource,
	options.TagIngestMutate,
)

OptIngestSampleSize specifies the number of samples that a detector should take to determine ingest data type.

View Source
var OptResultColRename = options.NewString(
	"result.column.rename",
	nil,
	"{{.Name}}{{with .Recurrence}}_{{.}}{{end}}",
	func(s string) error {
		return stringz.ValidTemplate("result.column.rename", s)
	},
	"Template to rename result columns",
	`This Go text template is executed on the column names returned from the DB. Its
primary purpose is to rename duplicate column names. For example, given a query
that results in this SQL:

  SELECT * FROM actor JOIN film_actor ON actor.actor_id = film_actor.actor_id

The returned result set will have these column names:

  actor_id, first_name, last_name, last_update, actor_id, film_id, last_update
  |-              from "actor"               -| |-    from "film_actor"     -|

Note the duplicate "actor_id" and "last_update" column names. When output in a
format (such as JSON) that doesn't permit duplicate keys, only one of each
duplicate column could appear.

The fields available in the template are:

  .Name         column name
  .Index        zero-based index of the column in the result set
  .Alpha        alphabetical index of the column, i.e. [A, B ... Z, AA, AB]
  .Recurrence   nth recurrence of the colum name in the result set

For a unique column name, e.g. "first_name" above, ".Recurrence" will be 0. For
duplicate column names, ".Recurrence" will be 0 for the first instance, then 1
for the next instance, and so on.

Note that this option only applies when the result set contains duplicates. To
rename result columns generally, use a column alias. Note also that this option
applies globally; it cannot be set on a per-source basis. This is because it's
ambiguous what would happen on a join where each source had a different renaming
template.

The default template renames the columns to:

  actor_id, first_name, last_name, last_update, actor_id_1, film_id, last_update_1`,
	options.TagOutput,
)

OptResultColRename transforms a column name returned from the DB.

Functions

func ConfigureDB added in v0.34.0

func ConfigureDB(_ context.Context, db *sql.DB, o options.Options)

ConfigureDB configures DB using o. It is no-op if o is nil.

func MaxBatchRows

func MaxBatchRows(drvr SQLDriver, numCols int) int

MaxBatchRows returns the maximum number of rows allowed for a batch insert for drvr. Note that the returned value may differ for each database driver.

func MungeIngestColNames added in v0.41.0

func MungeIngestColNames(ctx context.Context, ogColNames []string) (colNames []string, err error)

MungeIngestColNames transforms ingest data column names, per the template defined in the option driver.OptIngestColRename found on the context. It is the ingest counterpart of MungeResultColNames.

For example, given a CSV file with header [actor_id, first_name, actor_id], the columns might be renamed to [actor_id, first_name, actor_id_1].

MungeIngestColNames should be invoked by each ingester impl that may encounter duplicate col names in the ingest data.

func MungeResultColNames added in v0.41.0

func MungeResultColNames(ctx context.Context, ogColNames []string) (colNames []string, err error)

MungeResultColNames transforms column names, per the template defined in the option driver.OptResultColRename found on the context. This mechanism is used to deduplicate column names, as can happen in in "SELECT * FROM ... JOIN" situations. For example, if the result set has columns [actor_id, first_name, actor_id], the columns might be transformed to [actor_id, first_name, actor_id_1].

driver.MungeResultColNames should be invoked by each impl of SQLDriver.RecordMeta before returning the record.Meta.

See also: MungeIngestColNames.

func NewEmptyDataError added in v0.47.0

func NewEmptyDataError(format string, args ...any) error

NewEmptyDataError returns a EmptyDataError.

func NewNotExistError added in v0.47.0

func NewNotExistError(err error) error

NewNotExistError returns a NotExistError, or nil.

func NewRecordFromScanRow

func NewRecordFromScanRow(meta record.Meta, row []any, skip []int) (rec record.Record, skipped []int)

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.

Any row elements specified in skip will not be processed; the value will be copied directly from row[i] into rec[i]. If any element of row otherwise cannot be processed, its value is copied directly into rec, and its index is returned in skipped. The caller must take appropriate action to deal with all elements of rec listed in skipped.

REVISIT: Do we need the skip mechanism at all? REVISIT: This function implementation is an unholy mess. Much of it dates from sq's earliest days, and it's been hacked on ever since. Several of the code paths can probably never be reached. It should be completely rewritten.

func OpeningPing added in v0.36.0

func OpeningPing(ctx context.Context, src *source.Source, db *sql.DB) error

OpeningPing is a standardized mechanism to ping db using driver.OptConnOpenTimeout. This should be invoked by each SQL driver impl in its Open method. If the ping fails, db is closed. In practice, this function probably isn't needed. Maybe ditch it.

func PrepareInsertStmt

func PrepareInsertStmt(ctx context.Context, drvr SQLDriver, db sqlz.Preparer, destTbl string, destCols []string,
	numRows int,
) (stmt *sql.Stmt, err error)

PrepareInsertStmt prepares an insert statement using driver-specific syntax from drvr. numRows specifies how many rows of values are inserted by each execution of the insert statement (1 row being the prototypical usage).

Types

type BatchInsert

type BatchInsert struct {
	// RecordCh is the channel that the caller sends records on. The
	// caller must close RecordCh when done.
	RecordCh chan<- []any

	// ErrCh returns any errors that occur during insert. ErrCh is
	// closed by BatchInsert when processing is complete.
	ErrCh <-chan error
	// contains filtered or unexported fields
}

BatchInsert encapsulates inserting records to a db. The caller sends (munged) records on recCh; the record values should be munged via the Munge method prior to sending. Records are written to db in batches of batchSize as passed to NewBatchInsert (the final batch may be less than batchSize). The caller must close recCh to indicate that all records have been sent, or cancel the ctx passed to NewBatchInsert to stop the insertion goroutine. Any error is returned on errCh. Processing is complete when errCh is closed: the caller must select on errCh.

func NewBatchInsert

func NewBatchInsert(ctx context.Context, msg string, drvr SQLDriver, db sqlz.DB,
	destTbl string, destColNames []string, batchSize int,
) (*BatchInsert, error)

NewBatchInsert returns a new BatchInsert instance. The internal goroutine is started.

Note that the db arg must guarantee a single connection: that is, it must be a sql.Conn or sql.Tx.

func (*BatchInsert) Munge

func (bi *BatchInsert) Munge(rec []any) error

Munge should be invoked on every record before sending on RecordCh.

func (*BatchInsert) Written

func (bi *BatchInsert) Written() int64

Written returns the number of records inserted (at the time of invocation). For the final value, Written should be invoked after ErrCh is closed.

type Driver

type Driver interface {
	// Open returns a Grip instance for src.
	Open(ctx context.Context, src *source.Source) (Grip, error)

	// Ping verifies that the source is reachable, or returns an error if not.
	// The exact behavior of Ping is driver-dependent. Even if Ping does not
	// return an error, the source may still be bad for other reasons.
	Ping(ctx context.Context, src *source.Source) error

	// DriverMetadata returns driver metadata.
	DriverMetadata() Metadata

	// ValidateSource verifies that the source is valid for this driver. It
	// may transform the source into a canonical form, which is returned in
	// the return value (the original source is not changed). An error
	// is returned if the source is invalid.
	ValidateSource(src *source.Source) (*source.Source, error)
}

Driver is the core interface that must be implemented for each type of data source.

type EmptyDataError added in v0.47.0

type EmptyDataError string

EmptyDataError indicates that there's no data, e.g. an empty document. This is subtly different to NotExistError, which would indicate that the document doesn't exist.

func (EmptyDataError) Error added in v0.47.0

func (e EmptyDataError) Error() string

Error satisfies the stdlib error interface.

type Grip added in v0.47.0

type Grip interface {
	// DB returns the sql.DB object for this Grip. This operation some time
	// to complete if opening the DB requires an ingest of data.
	DB(ctx context.Context) (*sql.DB, error)

	// SQLDriver returns the underlying database driver. The type of the SQLDriver
	// may be different from the driver type reported by the Source.
	SQLDriver() SQLDriver

	// Source returns the source for which this Grip was opened.
	Source() *source.Source

	// SourceMetadata returns metadata about the Grip.
	// If noSchema is true, schema details are not populated
	// on the returned metadata.Source.
	SourceMetadata(ctx context.Context, noSchema bool) (*metadata.Source, error)

	// TableMetadata returns metadata for the specified table in the Grip.
	TableMetadata(ctx context.Context, tblName string) (*metadata.Table, error)

	// Close is invoked to close and release any underlying resources.
	Close() error
}

Grip is the link between a source and its database connection. Why is it named Grip? TLDR: all the other names were taken, including Handle, Conn, DB, Source, etc.

Grip is conceptually equivalent to stdlib sql.DB, and in fact encapsulates a sql.DB instance. The realized sql.DB instance can be accessed via the DB method.

type GripOpenIngester added in v0.47.0

type GripOpenIngester interface {
	// OpenIngest opens a Grip for src by executing ingestFn, which is
	// responsible for ingesting data into dest. If allowCache is false,
	// ingest always occurs; if true, the cache is consulted first (and
	// ingestFn may not be invoked).
	OpenIngest(ctx context.Context, src *source.Source, allowCache bool,
		ingestFn func(ctx context.Context, dest Grip) error) (Grip, error)
}

GripOpenIngester opens a Grip via an ingest function.

type Grips added in v0.47.0

type Grips struct {
	// contains filtered or unexported fields
}

Grips provides a mechanism for getting Grip instances. Note that at this time instances returned by Open are cached and then closed by Close. This may be a bad approach.

func NewGrips added in v0.47.0

func NewGrips(drvrs Provider, fs *files.Files, scratchSrcFn ScratchSrcFunc) *Grips

NewGrips returns a Grips instances.

func (*Grips) Close added in v0.47.0

func (gs *Grips) Close() error

Close closes gs, invoking any cleanup funcs.

func (*Grips) DriverFor added in v0.47.0

func (gs *Grips) DriverFor(typ drivertype.Type) (Driver, error)

DriverFor returns the driver for typ.

func (*Grips) IsSQLSource added in v0.47.0

func (gs *Grips) IsSQLSource(src *source.Source) bool

IsSQLSource returns true if src's driver is a SQLDriver.

func (*Grips) Open added in v0.47.0

func (gs *Grips) Open(ctx context.Context, src *source.Source) (Grip, error)

Open returns an opened Grip for src. The returned Grip may be cached and returned on future invocations for the identical source. Thus, the caller should typically not close the Grip: it will be closed via d.Close.

NOTE: This entire logic re caching/not-closing is a bit sketchy, and needs to be revisited.

func (*Grips) OpenEphemeral added in v0.47.0

func (gs *Grips) OpenEphemeral(ctx context.Context) (Grip, error)

OpenEphemeral returns an ephemeral scratch Grip instance. It is not necessary for the caller to close the returned Grip as its Close method will be invoked by Grips.Close.

func (*Grips) OpenIngest added in v0.47.0

func (gs *Grips) OpenIngest(ctx context.Context, src *source.Source, allowCache bool,
	ingestFn func(ctx context.Context, dest Grip) error,
) (Grip, error)

OpenIngest implements driver.GripOpenIngester. It opens a Grip, ingesting the source's data into the Grip. If allowCache is true, the ingest cache DB is used if possible. If allowCache is false, any existing ingest cache DB is not utilized, and is overwritten by the ingestion process.

func (*Grips) OpenJoin added in v0.47.0

func (gs *Grips) OpenJoin(ctx context.Context, srcs ...*source.Source) (Grip, error)

OpenJoin opens an appropriate Grip for use as a work DB for joining across sources.

REVISIT: There is much work to be done on this method. Ultimately OpenJoin should be able to inspect the join srcs and use heuristics to determine the best location for the join to occur (to minimize copying of data for the join etc.).

type InsertMungeFunc

type InsertMungeFunc func(vals record.Record) error

InsertMungeFunc is invoked on vals before insertion (or update, despite the name). Note that InsertMungeFunc operates on the vals slice, while NewRecordFunc returns a new slice.

func DefaultInsertMungeFunc

func DefaultInsertMungeFunc(destTbl string, destMeta record.Meta) InsertMungeFunc

DefaultInsertMungeFunc returns an InsertMungeFunc that checks the values of rec against destMeta and performs necessary munging. For example, if any element is a ptr to an empty string and the dest type is not of kind Text, the empty string was probably intended to mean nil. This happens when the original source doesn't handle nil, e.g. with CSV, where nil is effectively represented by "".

The returned InsertMungeFunc accounts for common cases, but it's possible that certain databases will require a custom InsertMungeFunc.

type Metadata

type Metadata struct {
	// Type is the driver type, e.g. "mysql" or "csv", etc.
	Type drivertype.Type `json:"type" yaml:"type"`

	// Description is typically the long name of the driver, e.g.
	// "MySQL" or "Microsoft Excel XLSX".
	Description string `json:"description" yaml:"description"`

	// Doc is optional documentation, typically a URL.
	Doc string `json:"doc,omitempty" yaml:"doc,omitempty"`

	// UserDefined is true if this driver is the product of a
	// user driver definition, and false if built-in.
	UserDefined bool `json:"user_defined" yaml:"user_defined"`

	// IsSQL is true if this driver is a SQL driver.
	IsSQL bool `json:"is_sql" yaml:"is_sql"`

	// Monotable is true if this is a non-SQL document type that
	// effectively has a single table, such as CSV.
	Monotable bool `json:"monotable" yaml:"monotable"`

	// DefaultPort is the default port that a driver connects on. A
	// value <= 0 indicates not applicable.
	DefaultPort int `json:"default_port" yaml:"default_port"`
}

Metadata holds driver metadata.

TODO: Can driver.Metadata and dialect.Dialect be merged?

type NewRecordFunc

type NewRecordFunc func(scanRow []any) (rec record.Record, err error)

NewRecordFunc is invoked on a query result row (scanRow) to normalize and standardize the data, returning a new record. The provided scanRow arg is available for reuse after this func returns.

Ultimately rec should only contain:

nil, *int64, *bool, *float64, *string, *[]byte, *time.Time

Thus a func instance might unbox sql.NullString et al, or deal with any driver specific quirks.

type NotExistError added in v0.47.0

type NotExistError struct {
	// contains filtered or unexported fields
}

NotExistError indicates that a DB object, such as a table, does not exist.

func (*NotExistError) Unwrap added in v0.47.0

func (e *NotExistError) Unwrap() error

Unwrap satisfies the stdlib errors.Unwrap function.

type Provider

type Provider interface {
	// DriverFor returns a driver instance for the given type.
	DriverFor(typ drivertype.Type) (Driver, error)
}

Provider is a factory that returns Driver instances.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry provides access to driver implementations.

func NewRegistry

func NewRegistry(log *slog.Logger) *Registry

NewRegistry returns a new Registry instance that provides access to driver implementations. Note that Registry implements Provider.

func (*Registry) AddProvider

func (r *Registry) AddProvider(typ drivertype.Type, p Provider)

AddProvider registers the provider for the specified driver type. This method has no effect if there's already a provider for typ.

func (*Registry) DriverFor

func (r *Registry) DriverFor(typ drivertype.Type) (Driver, error)

DriverFor implements Provider.

func (*Registry) Drivers

func (r *Registry) Drivers() []Driver

Drivers returns the registered drivers.

func (*Registry) DriversMetadata

func (r *Registry) DriversMetadata() []Metadata

DriversMetadata returns metadata for each registered driver type.

func (*Registry) ProviderFor

func (r *Registry) ProviderFor(typ drivertype.Type) Provider

ProviderFor returns the provider for typ, or nil if no registered provider.

func (*Registry) SQLDriverFor added in v0.37.0

func (r *Registry) SQLDriverFor(typ drivertype.Type) (SQLDriver, error)

SQLDriverFor for is a convenience method for getting a SQLDriver.

type SQLDriver

type SQLDriver interface {
	Driver

	// Dialect returns the SQL dialect.
	Dialect() dialect.Dialect

	// ConnParams returns the db parameters available for use in a connection
	// string. The key is the parameter name (e.g. "sslmode"), and the value
	// can be either the set of allowed values, sample values, or nil.
	// These values are used for shell completion and the like. The returned
	// map does not have to be exhaustive, and can be nil.
	ConnParams() map[string][]string

	// ErrWrapFunc returns a func that wraps the driver's errors.
	ErrWrapFunc() func(error) error

	// Renderer returns the SQL renderer for this driver.
	Renderer() *render.Renderer

	// CurrentSchema returns the current schema name.
	CurrentSchema(ctx context.Context, db sqlz.DB) (string, error)

	// ListSchemas lists the names of the schemas on db.
	ListSchemas(ctx context.Context, db sqlz.DB) ([]string, error)

	// ListSchemaMetadata returns the metadata for the schemas on db.
	ListSchemaMetadata(ctx context.Context, db sqlz.DB) ([]*metadata.Schema, error)

	// CurrentCatalog returns the current catalog name. An error is
	// returned if the driver doesn't support catalogs.
	CurrentCatalog(ctx context.Context, db sqlz.DB) (string, error)

	// ListCatalogs lists the available catalog names on db. The first
	// returned element is the current catalog, and the remaining
	// catalogs are sorted alphabetically. An error is returned
	// if the driver doesn't support catalogs.
	ListCatalogs(ctx context.Context, db sqlz.DB) ([]string, error)

	// TableColumnTypes returns the column type info from
	// the SQL driver. If len(colNames) is 0, info is returned
	// for all columns in the table.
	TableColumnTypes(ctx context.Context, db sqlz.DB, tblName string, colNames []string) ([]*sql.ColumnType, error)

	// RecordMeta returns the result metadata (the metadata for
	// each col) from colTypes. RecordMeta is preferred over
	// sql.Rows.ColumnTypes because of the inconsistent behavior
	// of various SQL driver implementations wrt reporting
	// "nullable" information and other quirks. The returned
	// metadata may differ from the original metadata returned
	// by rows.ColumnTypes.
	//
	// The caller should typically should invoke rows.Next before
	// this method is invoked, as some implementations do not return
	// complete column type info until after the first call to rows.Next.
	//
	// RecordMeta also returns a NewRecordFunc which can be
	// applied to the scan row from sql.Rows.
	RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta, NewRecordFunc, error)

	// PrepareInsertStmt prepares a statement for inserting
	// values to destColNames in destTbl. numRows specifies
	// how many rows of values are inserted by each execution of
	// the insert statement (1 row being the prototypical usage).
	// It is the caller's responsibility to close the execer.
	//
	// Note that db must guarantee a single connection: that is, db
	// must be a sql.Conn or sql.Tx.
	PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string,
		numRows int) (*StmtExecer, error)

	// PrepareUpdateStmt prepares a statement for updating destColNames in
	// destTbl, using the supplied where clause (which may be empty).
	// The where arg should use question mark "?" as the placeholder: it will
	// be translated to the appropriate driver-specific placeholder. For example,
	// the where arg could be:
	//
	//   "actor_id = ? AND first_name = ?".
	//
	// Use the returned StmtExecer per its documentation. It is the caller's
	// responsibility to close the execer.
	//
	// Note that db must guarantee a single connection: that is, db
	// must be a sql.Conn or sql.Tx.
	PrepareUpdateStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string,
		where string) (*StmtExecer, error)

	// CreateTable creates the table defined by tblDef. Some implementations
	// may not honor every field of tblDef, e.g. an impl might not
	// build the foreign key constraints. At a minimum the implementation
	// must honor the table name and column names and kinds from tblDef.
	CreateTable(ctx context.Context, db sqlz.DB, tblDef *schema.Table) error

	// CreateSchema creates a new schema in db. Note that db's current
	// connection schema is not changed.
	CreateSchema(ctx context.Context, db sqlz.DB, schemaName string) error

	// DropSchema drops the named schema in db.
	DropSchema(ctx context.Context, db sqlz.DB, schemaName string) error

	// CatalogExists returns true if db can reference the named catalog. If
	// catalog is empty string, false is returned.
	CatalogExists(ctx context.Context, db sqlz.DB, catalog string) (bool, error)

	// SchemaExists returns true if db can reference the named schema. If
	// schma is empty string, false is returned.
	SchemaExists(ctx context.Context, db sqlz.DB, schma string) (bool, error)

	// Truncate truncates tbl in src. If arg reset is true, the
	// identity counter for tbl should be reset, if supported
	// by the driver. Some DB impls may reset the identity
	// counter regardless of the val of reset.
	Truncate(ctx context.Context, src *source.Source, tbl string, reset bool) (affected int64, err error)

	// TableExists returns true if there's an existing table tbl in db.
	TableExists(ctx context.Context, db sqlz.DB, tbl string) (bool, error)

	// ListTableNames lists the tables of schma in db. The "tables" and "views"
	// args filter TABLE and VIEW types, respectively. If both are false, an empty
	// slice is returned. If schma is empty, the current schema is used.
	ListTableNames(ctx context.Context, db sqlz.DB, schma string, tables, views bool) ([]string, error)

	// CopyTable copies fromTable into a new table toTable.
	// If copyData is true, fromTable's data is also copied.
	// Constraints (keys, defaults etc.) may not be copied. The
	// number of copied rows is returned in copied.
	CopyTable(ctx context.Context, db sqlz.DB, fromTable, toTable tablefq.T, copyData bool) (copied int64, err error)

	// DropTable drops tbl from db. If ifExists is true, an "IF EXISTS"
	// or equivalent clause is added, if supported.
	DropTable(ctx context.Context, db sqlz.DB, tbl tablefq.T, ifExists bool) error

	// AlterTableRename renames a table.
	AlterTableRename(ctx context.Context, db sqlz.DB, tbl, newName string) error

	// AlterTableAddColumn adds column col to tbl. The column is appended
	// to the list of columns (that is, the column position cannot be
	// specified).
	AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl, col string, knd kind.Kind) error

	// AlterTableRenameColumn renames a column.
	AlterTableRenameColumn(ctx context.Context, db sqlz.DB, tbl, col, newName string) error

	// AlterTableColumnKinds changes the kinds of colNames in tbl. The length of
	// args colNames and kinds must be equal. The method may create a transaction
	// internally if db is not already a transaction.
	AlterTableColumnKinds(ctx context.Context, db sqlz.DB, tbl string, colNames []string, kinds []kind.Kind) error

	// DBProperties returns a map of key-value database properties. The value
	// is often a scalar such as an int, string, or bool, but can be a nested
	// map or array.
	DBProperties(ctx context.Context, db sqlz.DB) (map[string]any, error)
}

SQLDriver is implemented by Driver instances for SQL databases.

type ScratchSrcFunc

type ScratchSrcFunc func(ctx context.Context, name string) (src *source.Source, cleanFn func() error, err error)

ScratchSrcFunc is a function that returns a scratch source. The caller is responsible for invoking cleanFn.

type StmtExecFunc

type StmtExecFunc func(ctx context.Context, args ...any) (affected int64, err error)

StmtExecFunc is provided by driver implementations to wrap execution of a prepared statement. Typically the func will perform some driver-specific action, such as managing retryable errors.

type StmtExecer

type StmtExecer struct {
	// contains filtered or unexported fields
}

StmtExecer encapsulates the elements required to execute a SQL statement. Typically the statement is an INSERT. The Munge method should be applied to each row of values prior to invoking Exec. The caller is responsible for invoking Close.

func NewStmtExecer

func NewStmtExecer(stmt *sql.Stmt, mungeFn InsertMungeFunc, execFn StmtExecFunc, destMeta record.Meta,
) *StmtExecer

NewStmtExecer returns a new StmtExecer instance. The caller is responsible for invoking Close on the returned StmtExecer.

func (*StmtExecer) Close

func (x *StmtExecer) Close() error

Close closes x's statement.

func (*StmtExecer) DestMeta

func (x *StmtExecer) DestMeta() record.Meta

DestMeta returns the record.Meta for the destination table columns.

func (*StmtExecer) Exec

func (x *StmtExecer) Exec(ctx context.Context, args ...any) (affected int64, err error)

Exec executes the statement. The caller should invoke Munge on each row of values prior to passing those values to Exec.

func (*StmtExecer) Munge

func (x *StmtExecer) Munge(rec []any) error

Munge should be applied to each row of values prior to inserting invoking Exec.

Directories

Path Synopsis
Package dialect contains functionality for SQL dialects.
Package dialect contains functionality for SQL dialects.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL