sqliter

package module
v0.4.188 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: ISC Imports: 20 Imported by: 0

Documentation

Overview

Package sqliter provides partitioning, cached prepared statements and data conversion for SQLite3.

conversions for:

additionally:

Index

Constants

View Source
const (
	// SQLite3 special filename for in-memory databases
	//	- default [In-Memory Databases] name is “:memory:”
	//	- if two threads or a query while another query is read,
	//		a second database connection is opened which uses a different database
	//	- from SQLite3 [version] 3.7.13+, multiple connections may
	//		share in-memory database
	//	- use URI filename: “file::memory:?cache=shared”
	//	- Go package modernc.org/sqlite 1.30.1 240606
	//		is pure Go code-compatible with SQLite 3.46.0
	//	- the in-memory filename provided here does not support
	//		partitioning.
	//		It is used for testing
	//	- because Go may open parallel database connections
	//		at any time, use of legacy filename “:memory:”
	//		produces unpredictable results
	//
	// [In-Memory Databases]: https://sqlite.org/inmemorydb.html
	// [version]: https://stackoverflow.com/questions/36447766/multiple-sqlite-connections-to-a-database-in-memory#comment60508526_36447766
	SQLiteMemoryDataSourceName = "file::memory:?cache=shared"
	// name of the SQLite3 database driver
	//	- “modernc.org/sqlite”
	SQLiteDriverName = "sqlite"
)
View Source
const (
	CodeBusy                    = 5    // sqlite3.SQLITE_BUSY
	CodeConstraintFailedNOTNULL = 1299 // SQLITE_CONSTRAINT_NOTNULL
	CodeConstraintFailedUNIQUE  = 2067 // SQLITE_CONSTRAINT_UNIQUE
	CodeDatabaseIsLocked        = 261  // locked WAL file
)

SQLite errors are of type sqlite.Error

  • sqlite.Error has a code int value
  • Some code values are not exported
  • For calling code to not have to import the driver itself, frequent error-code values are here

Variables

View Source
var DSNrFactory = &dSNrFactory{}

DSNrFactory provides an abstract factory method for an SQLite3 data-source namer

View Source
var ErrDsnNotExist = &dsnNotExist{error: errors.New("ErrDsnNotExist")}

ErrDsnNotExist can detect DSN not exist errors

Usage:

if errors.Is(err, sqliter.ErrDsnNotExist) {
View Source
var MemDsnr = &memDsnr{}

MemDsnr is a data source namer always returning an in-memory data source

Functions

func BoolToDB added in v0.4.14

func BoolToDB(b bool) (dbValue int)

BoolToDB converts Go bool to SQLite INTEGER

  • Go reflect type is bool
  • SQLite storage class for Boolean Datatype is INTEGER
  • SQLite values used are 0 or 1

func DATETIMEtoTime added in v0.4.127

func DATETIMEtoTime(sqliteText string) (t time.Time, err error)

DATETIMEtoTime converts SQLite DATETIME to Go time.Time with location time.Local

  • better to use is TimeToDB and ToTime using iso8601 utc time-zone with nanosecond precision
  • database stores as TEXT in UTC, millisecond precision
  • “2019-01-01 21:30:42.000 +00:00”

func MarkDsnNotExist added in v0.4.127

func MarkDsnNotExist(err error) (err2 error)

MarkDsnNotExist marks error as cased by read-only DSN

func NullableToTime added in v0.4.18

func NullableToTime(nullString sql.NullString) (t time.Time, err error)

NullableToTime parses SQLite TEXT to Go time.Time in Local location

  • nullable database column
  • SQLite TEXT ISO8601 nano-second resolution UTC time zone
  • SQLite TEXT: “2022-01-01T08:00:00.000000000Z”
  • NULL corresponds to time.Time{} time.Time.IsZero true

func OpenDataSource added in v0.4.127

func OpenDataSource(dataSourceName parl.DataSourceName) (dataSource parl.DataSource, err error)

OpenDataSource creates a database-file in the file-system and returns its database implementation

  • dataSourceName: a filename specifying a SQLite3 database file
  • — for an in-memory database, SQLiteMemoryDataSourceName or ":memory:" is used
  • dataSource: wraps a sql.DB value

func OpenDataSourceNamer added in v0.4.127

func OpenDataSourceNamer(appName string) (dsnr parl.DataSourceNamer, err error)

OpenDataSourceNamer is a parl.DSNrFactory function that returns a SQLite3 data-source names that returns:

  • SQLite3 database filenames based on a partition key
  • SQLite3 implemented data sources providing generic SQL query execution

func OpenDataSourceNamerRO added in v0.4.127

func OpenDataSourceNamerRO(appName string) (dsn parl.DataSourceNamer, err error)

OpenDataSourceNamerRO returns a SQLIte3 parl.DataSourceNamer that does not create databases or write to the file-system

  • errors can be checked to be cause by read-only:

Usage:

if errors.Is(err, sqliter.ErrDsnNotExist) {

func Pragma added in v0.4.127

func Pragma(dataSource parl.DataSource, ctx context.Context) (pragmas parl.Pragmas, err error)

Pragma returns SQLite3 pragma database-settings based on a data source

  • parl.DataSource is a caching of prepared statements obtained from OpenDataSource
  • When Pragma is used to retieve database pragmas, the issued queries will be cached in dataSource

func PragmaDB added in v0.4.127

func PragmaDB(db parl.DB, partition parl.DBPartition, ctx context.Context) (pragmas parl.Pragmas, err error)

Pragma returns some common SQLite3 database settings

  • parl.DB is a map of multiple DB objects facilitating partitioning
  • in normal use, parl.DB is not available from [psql.NewDBMap]. The DB value is cached internally and retrieved via its data source name
  • When PragmaDB is used to retieve database pragmas, the issued queries will be cached in the parl.DB data source

func PragmaSQL added in v0.4.136

func PragmaSQL(sqlDB *sql.DB, ctx context.Context) (pragmas parl.Pragmas, err error)

PragmaSQL returns common SQLite3 database settings

func TimeToDATETIME added in v0.4.127

func TimeToDATETIME(t time.Time) (dateTime string)

TimeToDATETIME converts Go time.Time to SQLite DATETIME UTC millisecond precision

  • better to use is TimeToDB and ToTime using iso8601 utc time-zone with nanosecond precision
  • “2019-01-01 21:30:42.000 +00:00”

func TimeToDB added in v0.4.14

func TimeToDB(t time.Time) (dbValue string)

TimeToDB converts ns-resolution Go time.Time in any time.Location to SQLite TEXT ISO8601 nano-second resolution UTC time zone

  • SQLite TEXT: “2022-01-01T08:00:00.000000000Z”

func TimeToDBNullable added in v0.4.18

func TimeToDBNullable(t time.Time) (dbValue any)

TimeToDBNullable converts ns-resolution Go time.Time in any time.Location to SQLite TEXT ISO8601 nano-second resolution UTC time zone

  • for nullable database column
  • SQLite TEXT: “2022-01-01T08:00:00.000000000Z”
  • NULL corresponds to time.Time{} time.Time.IsZero true

func ToBool added in v0.4.14

func ToBool(dbValue int) (b bool, err error)

ToBool converts SQLite INTEGER 0 or 1 to Go bool

  • Go reflect type is bool
  • SQLite INTEGER values are 0 or 1

func ToTime added in v0.4.14

func ToTime(timeString string) (t time.Time, err error)

ToTime parses SQLite TEXT to Go time.Time in Local location

  • SQLite TEXT ISO8601 nano-second resolution UTC time zone
  • SQLite TEXT: “2022-01-01T08:00:00.000000000Z”

func ToUUID added in v0.4.14

func ToUUID(IDString string) (ID uuid.UUID, err error)

ToUUID converts an SQLite TEXT value to Go uuid.UUID

  • err: non-nil on failure
  • ID: valid or zero-value on failure
  • Go reflect type: [16]byte uuid.UUID from package github.com/google/uuid
  • — 16×8 is 128 bits
  • SQLite value: 36-character TEXT “01234567-89ab-cdef-0123-456789abcdef”
  • — 32×4 is 128 bits

func UUIDToDB added in v0.4.14

func UUIDToDB(ID uuid.UUID) (dbValue string)

UUIDToDB stores a Go 128-bit UUID as 36 characters SQLite TEXT

  • Go reflect type: array [16]byte from package github.com/google/uuid
  • — 16×8 is 128 bits
  • SQLite type: 36-character string TEXT “01234567-89ab-cdef-0123-456789abcdef”
  • — 32×4 is 128 bits
  • — SQLite Storage Classes: NULL INTEGER REAL TEXT BLOB

Types

type DataSource added in v0.4.12

type DataSource struct {
	// DB represents a generic SQL database that can:
	//	- offer connections
	//	- execute generuic SQL queries
	*sql.DB
	// contains filtered or unexported fields
}

DataSource represents a SQL database that can prepare generic SQL queries

func (*DataSource) WrapStmt added in v0.4.26

func (ds *DataSource) WrapStmt(stmt *sql.Stmt) (stm psql2.Stmt)

PrepareContext returns a sql.Stmt that does retries on 5 SQLITE_BUSY

  • this is used by [parl.psql]

type DataSourceNamer added in v0.4.12

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

DataSourceNamer provides partitioned SQLite3 database files within the user’s home directory

func (*DataSourceNamer) DSN added in v0.4.12

func (n *DataSourceNamer) DSN(year ...parl.DBPartition) (dsnr parl.DataSourceName)

DSN returns a data source name, ie. a filename from application name and a partition key like year

  • effectyively an absolute path of a writable SQLite3 “.db” database file
  • implements parl’s [DatasourceNamer.DSN]

func (*DataSourceNamer) DataSource added in v0.4.12

func (n *DataSourceNamer) DataSource(dataSourceName parl.DataSourceName) (dataSource parl.DataSource, err error)

DataSource returns a data-source that can execute generic SQL queries based on a data-source name

  • implements parl’s [DatasourceNamer.DataSource]

type DataSourceNamerRO added in v0.4.127

type DataSourceNamerRO struct {
	DataSourceNamer
}

DataSourceNamerRO is a SQLite3 data source that does not create files or directories

func (*DataSourceNamerRO) DataSource added in v0.4.127

func (n *DataSourceNamerRO) DataSource(dataSourceName parl.DataSourceName) (dataSource parl.DataSource, err error)

DataSource does not create database files

  • dataSourceName is the path toa file-syste database file
  • make sure it already exists

type ErrorCode added in v0.4.13

type ErrorCode interface {
	error
	Code() (code int)
}

ErrorCode is the SQLite error implementation

  • ErrorCode is an error instance
  • The Code method provides an int error code

func Code added in v0.4.14

func Code(err error) (code int, sqliteError ErrorCode)

GetErrorCode traverses an error chain looking for an SQLite error

  • If an SQLite error is found, it is returned in sqliteError
  • — code is the int error code
  • If no SQLite error exists in the error chain, sqliteError is nil and code is 0

type Stmt added in v0.4.26

type Stmt struct {
	*sql.Stmt
	// contains filtered or unexported fields
}

Stmt implements retries for a SQLite3 table

  • the code here implements retries due to SQLite3 producing various concurrency errors
  • this is legacy code from 2022-06

func (*Stmt) ExecContext added in v0.4.26

func (st *Stmt) ExecContext(ctx context.Context, args ...any) (sqlResult sql.Result, err error)

ExecContext executes a SQL statements that does not return any rows

  • for performance reasons cached prepared statements are used
  • the code here implements retries due to SQLite3 producing various concurrency errors
  • this is legacy code from 2022-06

func (*Stmt) QueryContext added in v0.4.26

func (st *Stmt) QueryContext(ctx context.Context, args ...any) (sqlRows *sql.Rows, err error)

QueryContext executes a SQL statements that may return multiple rows

  • for performance reasons cached prepared statements are used
  • the code here implements retries due to SQLite3 producing various concurrency errors
  • this is legacy code from 2022-06

func (*Stmt) QueryRowContext added in v0.4.26

func (st *Stmt) QueryRowContext(ctx context.Context, args ...any) (sqlRow *sql.Row)

QueryRowContext executes a SQL statements that returns exactly one row

  • for performance reasons cached prepared statements are used
  • the code here implements retries due to SQLite3 producing various concurrency errors
  • this is legacy code from 2022-06

Jump to

Keyboard shortcuts

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