sqlite3

package
v0.0.0-...-0a47f3e Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sqlite3 provides SQLite helpers for golembic.

Index

Constants

View Source
const (
	// DefaultDataSourceName is the DSN / connection string for completely
	// in-memory values. Richer support for SQLite connection strings can be
	// added if desired; for reference: https://www.sqlite.org/uri.html
	DefaultDataSourceName = "file::memory:?cache=shared"

	// DefaultDriverName is the default SQL driver to be used when creating
	// a new database connection pool via `sql.Open()`. This default driver
	// is expected to be registered by importing one of
	// - github.com/gwenn/gosqlite
	// - github.com/mattn/go-sqlite3
	// - github.com/mxk/go-sqlite
	// - github.com/rsc/sqlite
	// - modernc.org/sqlite
	DefaultDriverName = "sqlite3"
)

Variables

View Source
var (
	// ErrTimestampNotInteger is the error returned when a TimeFromInteger column
	// is expected but the value in the database is not an integer.
	ErrTimestampNotInteger = errors.New("Timestamp was not stored as an integer")
	// ErrTimestampRounding is the error returned when a TimeFromInteger column
	// can't be converted an integer (e.g. microseconds since the epoch) without
	// rounding.
	ErrTimestampRounding = errors.New("Timestamp cannot be converted to integer without rounding")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// DataSourceName is the DSN or connection string for a SQLite connection.
	DataSourceName string

	// DriverName specifies the name of SQL driver to be used when creating
	// a new database connection pool via `sql.Open()`. The default driver
	// is expected to be registered by importing `github.com/mattn/go-sqlite3`,
	// though `https://github.com/golang/go/wiki/SQLDrivers` lists (as of
	// January 9, 2021) four other implementations that all register the
	// same driver name
	// - github.com/gwenn/gosqlite
	// - github.com/mxk/go-sqlite
	// - github.com/rsc/sqlite
	// - modernc.org/sqlite
	DriverName string
}

Config is a set of connection config options.

type Option

type Option = func(*Config) error

Option describes options used to create a new config for a SQL provider.

func OptDataSourceName

func OptDataSourceName(name string) Option

OptDataSourceName sets the `DataSourceName` on a `Config`.

func OptDriverName

func OptDriverName(name string) Option

OptDriverName sets the `DriverName` on a `Config`.

type SQLProvider

type SQLProvider struct {
	Config *Config
}

SQLProvider is a SQLite-specific database engine provider.

func New

func New(opts ...Option) (*SQLProvider, error)

New creates a SQLite-specific database engine provider from some options.

func (*SQLProvider) NewCreateTableParameters

func (*SQLProvider) NewCreateTableParameters() golembic.CreateTableParameters

NewCreateTableParameters produces the SQL expressions used in the the `CREATE TABLE` statement used to create the migrations table.

func (*SQLProvider) Open

func (sp *SQLProvider) Open() (*sql.DB, error)

Open creates a database connection pool to a SQLite instance.

func (*SQLProvider) QueryParameter

func (*SQLProvider) QueryParameter(index int) string

QueryParameter produces the placeholder `?NNN` for a numbered parameter in a SQLite query.

See: https://sqlite.org/lang_expr.html#parameters

func (*SQLProvider) QuoteIdentifier

func (sp *SQLProvider) QuoteIdentifier(name string) string

QuoteIdentifier quotes an identifier, such as a table name, for usage in a query. See: https://www.sqlite.org/lang_keywords.html

func (*SQLProvider) QuoteLiteral

func (sp *SQLProvider) QuoteLiteral(literal string) string

QuoteLiteral quotes a literal, such as `2023-01-05 15:00:00Z`, for usage in a query. See: https://www.sqlite.org/lang_keywords.html

func (*SQLProvider) TableExistsSQL

func (sp *SQLProvider) TableExistsSQL() string

TableExistsSQL returns a SQL query that can be used to determine if a table exists.

See: https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema

func (*SQLProvider) TimestampColumn

func (*SQLProvider) TimestampColumn() golembic.TimestampColumn

TimestampColumn produces a value that can be used for reading / writing an `INTEGER` column to a `time.Time` in SQLite.

type TimeFromInteger

type TimeFromInteger struct {
	Stored time.Time
}

TimeFromInteger represents a `time.Time` stored in a database as an `INTEGER` number of microseconds (in UTC) since the epoch.

This is **necessary** because SQLite doesn't have rich support for timestamps and the drivers can't paper over this issue, e.g. https://github.com/mattn/go-sqlite3/issues/142

func (*TimeFromInteger) Pointer

func (tfi *TimeFromInteger) Pointer() interface{}

Pointer returns the current pointer receiver.

func (*TimeFromInteger) Scan

func (tfi *TimeFromInteger) Scan(src interface{}) error

Scan implements the Scanner interface.

func (TimeFromInteger) Timestamp

func (tfi TimeFromInteger) Timestamp() time.Time

Timestamp returns the stored timestamp value.

func (TimeFromInteger) Value

func (tfi TimeFromInteger) Value() (driver.Value, error)

Value implements the driver Valuer interface.

Jump to

Keyboard shortcuts

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