Documentation ¶
Overview ¶
Package sql is a generated GoMock package.
Index ¶
- Constants
- Variables
- func AppendToCachedSlice[T any](db any, key QueryCacheItemKey, v T)
- func GetBlobSizes(db Executor, cmd string, ids [][]byte) (sizes []int, err error)
- func InMemory(opts ...Opt) *sqliteDatabase
- func InMemoryTest(tb testing.TB, opts ...Opt) *sqliteDatabase
- func IsCached(db any) bool
- func IsNull(stmt *Statement, col int) bool
- func LoadBlob(db Executor, cmd string, id []byte, blob *Blob) error
- func LoadDBSchemaScript(db Executor) (string, error)
- func Open(uri string, opts ...Opt) (*sqliteDatabase, error)
- func OpenInMemory(opts ...Opt) (*sqliteDatabase, error)
- func Vacuum(db Executor) error
- func Version(uri string) (int, error)
- func WithCachedSubKey[T any](ctx context.Context, db any, key QueryCacheItemKey, subKey QueryCacheSubKey, ...) (T, error)
- func WithCachedValue[T any](ctx context.Context, db any, key QueryCacheItemKey, ...) (T, error)
- type Blob
- type Database
- type Decoder
- type Encoder
- type Executor
- type Interceptor
- type LocalDatabase
- type Migration
- type MigrationList
- type MockExecutor
- type MockExecutorExecCall
- type MockExecutorMockRecorder
- type MockMigration
- type MockMigrationApplyCall
- type MockMigrationMockRecorder
- type MockMigrationNameCall
- type MockMigrationOrderCall
- type MockMigrationRollbackCall
- type Opt
- func WithAllowSchemaDrift(allow bool) Opt
- func WithConnections(n int) Opt
- func WithDatabaseSchema(schema *Schema) Opt
- func WithExclusive() Opt
- func WithForceMigrations(force bool) Opt
- func WithLatencyMetering(enable bool) Opt
- func WithLogger(logger *zap.Logger) Opt
- func WithMigrationsDisabled() Opt
- func WithNoCheckSchemaDrift() Opt
- func WithQueryCache(enable bool) Opt
- func WithQueryCacheSizes(sizes map[QueryCacheKind]int) Opt
- func WithReadOnly() Opt
- func WithTemp() Opt
- func WithVacuumState(i int) Opt
- type QueryCache
- type QueryCacheItemKey
- type QueryCacheKind
- type QueryCacheSubKey
- type RetrieveFunc
- type Schema
- func (s *Schema) Apply(db Database) error
- func (s *Schema) CheckDBVersion(logger *zap.Logger, db Database) (before, after int, err error)
- func (s *Schema) Diff(actualScript string) string
- func (s *Schema) Migrate(logger *zap.Logger, db Database, before, vacuumState int) error
- func (s *Schema) MigrateTempDB(logger *zap.Logger, db Database, before int) error
- func (s *Schema) SkipMigrations(i ...int)
- func (s *Schema) WriteToFile(basedir string) error
- type SchemaGen
- type SchemaGenOpt
- type SliceAppender
- type StateDatabase
- type Statement
- type Transaction
- type UntypedRetrieveFunc
Constants ¶
const ( SchemaPath = "schema/schema.sql" UpdatedSchemaPath = "schema/schema.sql.updated" )
Variables ¶
var ( // ErrClosed is returned if database is closed. ErrClosed = errors.New("database closed") // ErrNoConnection is returned if pooled connection is not available. ErrNoConnection = errors.New("database: no free connection") // ErrNotFound is returned if requested record is not found. ErrNotFound = errors.New("database: not found") // ErrObjectExists is returned if database constraints didn't allow to insert an object. ErrObjectExists = errors.New("database: object exists") // ErrConflict is returned if database constraints didn't allow to update an object. ErrConflict = errors.New("database: conflict") // ErrTooNew is returned if database version is newer than expected. ErrTooNew = errors.New("database version is too new") // ErrOldSchema is returned when the database version differs from the expected one // and migrations are disabled. ErrOldSchema = errors.New("old database version") )
Functions ¶
func AppendToCachedSlice ¶ added in v1.4.0
func AppendToCachedSlice[T any](db any, key QueryCacheItemKey, v T)
AppendToCachedSlice adds a value to the slice stored in the cache by invoking the specified SliceAppender. If the entry is not cached, the function does nothing.
func GetBlobSizes ¶ added in v1.4.1
GetBlobSizes returns a slice containing the sizes of blobs corresponding to the specified ids. For non-existent ids the corresponding value is -1.
func InMemory ¶
func InMemory(opts ...Opt) *sqliteDatabase
InMemory creates an in-memory database for testing and panics if there's an error.
func InMemoryTest ¶ added in v1.7.7
InMemoryTest returns an in-mem database for testing and ensures database is closed during `tb.Cleanup`.
func LoadDBSchemaScript ¶ added in v1.7.0
LoadDBSchemaScript retrieves the database schema as text.
func Open ¶
Open database with options.
Database is opened in WAL mode and pragma synchronous=normal. https://sqlite.org/wal.html https://www.sqlite.org/pragma.html#pragma_synchronous
func OpenInMemory ¶ added in v1.7.0
OpenInMemory creates an in-memory database.
func WithCachedSubKey ¶ added in v1.4.0
func WithCachedSubKey[T any]( ctx context.Context, db any, key QueryCacheItemKey, subKey QueryCacheSubKey, retrieve func(ctx context.Context) (T, error), ) (T, error)
WithCachedValue retrieves the specified value identified by the key and subKey from the cache. If the entry is absent from the cache, it's populated by calling retrieve func. Note that the retrieve func should never cause UpdateSlice to be called.
func WithCachedValue ¶ added in v1.4.0
func WithCachedValue[T any]( ctx context.Context, db any, key QueryCacheItemKey, retrieve func(ctx context.Context) (T, error), ) (T, error)
WithCachedValue retrieves the specified value from the cache. If the entry is absent from the cache, it's populated by calling retrieve func. Note that the retrieve func should never cause UpdateSlice to be called.
Types ¶
type Blob ¶ added in v1.4.1
type Blob struct {
Bytes []byte
}
Blob represents a binary blob data. It can be reused efficiently across multiple data retrieval operations, minimizing reallocations of the underlying byte slice.
func (*Blob) FromColumn ¶ added in v1.6.0
type Database ¶
type Database interface { Executor QueryCache // Close closes the database. Close() error // QueryCount returns the number of queries executed on the database. QueryCount() int // QueryCache returns the query cache for this database, if it's present, // or nil otherwise. QueryCache() QueryCache // Tx creates deferred sqlite transaction. // // Deferred transactions are not started until the first statement. // Transaction may be started in read mode and automatically upgraded to write mode // after one of the write statements. // // https://www.sqlite.org/lang_transaction.html Tx(ctx context.Context) (Transaction, error) // WithTx starts a new transaction and passes it to the exec function. // It then commits the transaction if the exec function doesn't return an error, // and rolls it back otherwise. // If the context is canceled, the currently running SQL statement is interrupted. WithTx(ctx context.Context, exec func(Transaction) error) error // TxImmediate begins a new immediate transaction on the database, that is, // a transaction that starts a write immediately without waiting for a write // statement. // The transaction returned from this function must always be released by calling // its Release method. Release rolls back the transaction if it hasn't been // committed. // If the context is canceled, the currently running SQL statement is interrupted. TxImmediate(ctx context.Context) (Transaction, error) // WithTxImmediate starts a new immediate transaction and passes it to the exec // function. // An immediate transaction is started immediately, without waiting for a write // statement. // It then commits the transaction if the exec function doesn't return an error, // and rolls it back otherwise. // If the context is canceled, the currently running SQL statement is interrupted. WithTxImmediate(ctx context.Context, exec func(Transaction) error) error // WithConnection executes the provided function with a connection from the // database pool. // If many queries are to be executed in a row, but there's no need for an // explicit transaction which may be long-running and thus block // WAL checkpointing, it may be preferable to use a single connection for // it to avoid database pool overhead. // The connection is released back to the pool after the function returns. // If the context is canceled, the currently running SQL statement is interrupted. WithConnection(ctx context.Context, exec func(Executor) error) error // Intercept adds an interceptor function to the database. The interceptor // functions are invoked upon each query on the database, including queries // executed within transactions. // The query will fail if the interceptor returns an error. // The interceptor can later be removed using RemoveInterceptor with the same key. Intercept(key string, fn Interceptor) // RemoveInterceptor removes the interceptor function with specified key from the database. RemoveInterceptor(key string) }
Database represents a database.
type Encoder ¶
type Encoder func(*Statement)
Encoder for parameters. Both positional parameters: select block from blocks where id = ?1;
and named parameters are supported: select blocks from blocks where id = @id;
For complete information see https://www.sqlite.org/c3ref/bind_blob.html.
type Executor ¶
type Executor interface { // Exec executes a statement. Exec(string, Encoder, Decoder) (int, error) }
Executor is an interface for executing raw statement.
type Interceptor ¶ added in v1.7.0
Interceptor is invoked on every query after it's added to a database using PushIntercept. The query will fail if Interceptor returns an error.
type LocalDatabase ¶ added in v1.7.0
type LocalDatabase interface { Database IsLocalDatabase() }
LocalDatabase is a Database used for local node data.
type Migration ¶ added in v1.3.0
type Migration interface { // Apply applies the migration. Apply(db Executor, logger *zap.Logger) error // Rollback rolls back the migration. Rollback() error // Name returns the name of the migration. Name() string // Order returns the sequential number of the migration. Order() int }
Migration is interface for migrations provider.
type MigrationList ¶ added in v1.7.0
type MigrationList []Migration
MigrationList denotes a list of migrations.
func LoadSQLMigrations ¶ added in v1.7.0
func LoadSQLMigrations(fsys fs.FS) (MigrationList, error)
func (MigrationList) AddMigration ¶ added in v1.7.0
func (l MigrationList) AddMigration(migration Migration) MigrationList
AddMigration adds a Migration to the MigrationList, overriding the migration with the same order number if it already exists. The function returns updated migration list. The state of the original migration list is undefined after calling this function.
func (MigrationList) Version ¶ added in v1.7.0
func (l MigrationList) Version() int
Version returns database version for the specified migration list.
type MockExecutor ¶ added in v1.7.5
type MockExecutor struct {
// contains filtered or unexported fields
}
MockExecutor is a mock of Executor interface.
func NewMockExecutor ¶ added in v1.7.5
func NewMockExecutor(ctrl *gomock.Controller) *MockExecutor
NewMockExecutor creates a new mock instance.
func (*MockExecutor) EXPECT ¶ added in v1.7.5
func (m *MockExecutor) EXPECT() *MockExecutorMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
type MockExecutorExecCall ¶ added in v1.7.5
MockExecutorExecCall wrap *gomock.Call
func (*MockExecutorExecCall) Do ¶ added in v1.7.5
func (c *MockExecutorExecCall) Do(f func(string, Encoder, Decoder) (int, error)) *MockExecutorExecCall
Do rewrite *gomock.Call.Do
func (*MockExecutorExecCall) DoAndReturn ¶ added in v1.7.5
func (c *MockExecutorExecCall) DoAndReturn(f func(string, Encoder, Decoder) (int, error)) *MockExecutorExecCall
DoAndReturn rewrite *gomock.Call.DoAndReturn
func (*MockExecutorExecCall) Return ¶ added in v1.7.5
func (c *MockExecutorExecCall) Return(arg0 int, arg1 error) *MockExecutorExecCall
Return rewrite *gomock.Call.Return
type MockExecutorMockRecorder ¶ added in v1.7.5
type MockExecutorMockRecorder struct {
// contains filtered or unexported fields
}
MockExecutorMockRecorder is the mock recorder for MockExecutor.
func (*MockExecutorMockRecorder) Exec ¶ added in v1.7.5
func (mr *MockExecutorMockRecorder) Exec(arg0, arg1, arg2 any) *MockExecutorExecCall
Exec indicates an expected call of Exec.
type MockMigration ¶ added in v1.3.0
type MockMigration struct {
// contains filtered or unexported fields
}
MockMigration is a mock of Migration interface.
func NewMockMigration ¶ added in v1.3.0
func NewMockMigration(ctrl *gomock.Controller) *MockMigration
NewMockMigration creates a new mock instance.
func (*MockMigration) Apply ¶ added in v1.3.0
func (m *MockMigration) Apply(db Executor, logger *zap.Logger) error
Apply mocks base method.
func (*MockMigration) EXPECT ¶ added in v1.3.0
func (m *MockMigration) EXPECT() *MockMigrationMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockMigration) Name ¶ added in v1.3.0
func (m *MockMigration) Name() string
Name mocks base method.
func (*MockMigration) Order ¶ added in v1.3.0
func (m *MockMigration) Order() int
Order mocks base method.
func (*MockMigration) Rollback ¶ added in v1.3.0
func (m *MockMigration) Rollback() error
Rollback mocks base method.
type MockMigrationApplyCall ¶ added in v1.4.0
MockMigrationApplyCall wrap *gomock.Call
func (*MockMigrationApplyCall) Do ¶ added in v1.4.0
func (c *MockMigrationApplyCall) Do(f func(Executor, *zap.Logger) error) *MockMigrationApplyCall
Do rewrite *gomock.Call.Do
func (*MockMigrationApplyCall) DoAndReturn ¶ added in v1.4.0
func (c *MockMigrationApplyCall) DoAndReturn(f func(Executor, *zap.Logger) error) *MockMigrationApplyCall
DoAndReturn rewrite *gomock.Call.DoAndReturn
func (*MockMigrationApplyCall) Return ¶ added in v1.4.0
func (c *MockMigrationApplyCall) Return(arg0 error) *MockMigrationApplyCall
Return rewrite *gomock.Call.Return
type MockMigrationMockRecorder ¶ added in v1.3.0
type MockMigrationMockRecorder struct {
// contains filtered or unexported fields
}
MockMigrationMockRecorder is the mock recorder for MockMigration.
func (*MockMigrationMockRecorder) Apply ¶ added in v1.3.0
func (mr *MockMigrationMockRecorder) Apply(db, logger any) *MockMigrationApplyCall
Apply indicates an expected call of Apply.
func (*MockMigrationMockRecorder) Name ¶ added in v1.3.0
func (mr *MockMigrationMockRecorder) Name() *MockMigrationNameCall
Name indicates an expected call of Name.
func (*MockMigrationMockRecorder) Order ¶ added in v1.3.0
func (mr *MockMigrationMockRecorder) Order() *MockMigrationOrderCall
Order indicates an expected call of Order.
func (*MockMigrationMockRecorder) Rollback ¶ added in v1.3.0
func (mr *MockMigrationMockRecorder) Rollback() *MockMigrationRollbackCall
Rollback indicates an expected call of Rollback.
type MockMigrationNameCall ¶ added in v1.4.0
MockMigrationNameCall wrap *gomock.Call
func (*MockMigrationNameCall) Do ¶ added in v1.4.0
func (c *MockMigrationNameCall) Do(f func() string) *MockMigrationNameCall
Do rewrite *gomock.Call.Do
func (*MockMigrationNameCall) DoAndReturn ¶ added in v1.4.0
func (c *MockMigrationNameCall) DoAndReturn(f func() string) *MockMigrationNameCall
DoAndReturn rewrite *gomock.Call.DoAndReturn
func (*MockMigrationNameCall) Return ¶ added in v1.4.0
func (c *MockMigrationNameCall) Return(arg0 string) *MockMigrationNameCall
Return rewrite *gomock.Call.Return
type MockMigrationOrderCall ¶ added in v1.4.0
MockMigrationOrderCall wrap *gomock.Call
func (*MockMigrationOrderCall) Do ¶ added in v1.4.0
func (c *MockMigrationOrderCall) Do(f func() int) *MockMigrationOrderCall
Do rewrite *gomock.Call.Do
func (*MockMigrationOrderCall) DoAndReturn ¶ added in v1.4.0
func (c *MockMigrationOrderCall) DoAndReturn(f func() int) *MockMigrationOrderCall
DoAndReturn rewrite *gomock.Call.DoAndReturn
func (*MockMigrationOrderCall) Return ¶ added in v1.4.0
func (c *MockMigrationOrderCall) Return(arg0 int) *MockMigrationOrderCall
Return rewrite *gomock.Call.Return
type MockMigrationRollbackCall ¶ added in v1.4.0
MockMigrationRollbackCall wrap *gomock.Call
func (*MockMigrationRollbackCall) Do ¶ added in v1.4.0
func (c *MockMigrationRollbackCall) Do(f func() error) *MockMigrationRollbackCall
Do rewrite *gomock.Call.Do
func (*MockMigrationRollbackCall) DoAndReturn ¶ added in v1.4.0
func (c *MockMigrationRollbackCall) DoAndReturn(f func() error) *MockMigrationRollbackCall
DoAndReturn rewrite *gomock.Call.DoAndReturn
func (*MockMigrationRollbackCall) Return ¶ added in v1.4.0
func (c *MockMigrationRollbackCall) Return(arg0 error) *MockMigrationRollbackCall
Return rewrite *gomock.Call.Return
type Opt ¶
type Opt func(c *conf)
Opt for configuring database.
func WithAllowSchemaDrift ¶ added in v1.7.0
WithAllowSchemaDrift prevents Open from failing upon schema drift when schema drift checks are enabled. A warning is printed instead.
func WithConnections ¶
WithConnections overwrites number of pooled connections.
func WithDatabaseSchema ¶ added in v1.7.0
WithSchema specifies database schema script.
func WithExclusive ¶ added in v1.7.0
func WithExclusive() Opt
WithExclusive specifies that the database is to be open in exclusive mode. This means that no other processes can open the database at the same time. If the database is already open by any process, this Open will fail. Any subsequent attempts by other processes to open the database will fail until this db handle is closed. In Exclusive mode, the database supports just one concurrent connection.
func WithForceMigrations ¶ added in v1.7.0
WithForceMigrations forces database to run all the migrations instead of using a schema snapshot in case of a fresh database.
func WithLatencyMetering ¶
WithLatencyMetering enables metric that track latency for every database query. Note that it will be a significant amount of data, and should not be enabled on multiple nodes by default.
func WithLogger ¶ added in v1.3.6
WithLogger specifies logger for the database.
func WithMigrationsDisabled ¶ added in v1.7.0
func WithMigrationsDisabled() Opt
WithMigrationsDisabled disables migrations for the database. The migrations are enabled by default.
func WithNoCheckSchemaDrift ¶ added in v1.7.0
func WithNoCheckSchemaDrift() Opt
WithNoCheckSchemaDrift disables schema drift checks.
func WithQueryCache ¶ added in v1.4.0
WithQueryCache enables in-memory caching of results of some queries.
func WithQueryCacheSizes ¶ added in v1.4.0
func WithQueryCacheSizes(sizes map[QueryCacheKind]int) Opt
WithQueryCacheSizes sets query cache sizes for the specified cache kinds.
func WithReadOnly ¶ added in v1.7.5
func WithReadOnly() Opt
WithReadOnly specifies that the database is to be open in read-only mode.
func WithTemp ¶ added in v1.7.0
func WithTemp() Opt
WithTemp specifies temporary database mode. For the temporary database, the migrations are always run in place, and vacuuming is nover done. PRAGMA journal_mode=OFF and PRAGMA synchronous=OFF are used.
func WithVacuumState ¶ added in v1.3.0
WithVacuumState will execute vacuum if database version before the migration was less or equal to the provided value.
type QueryCache ¶ added in v1.4.0
type QueryCache interface { // IsCached returns true if the requests are being cached. IsCached() bool // GetValue retrieves the specified key+subKey value from the cache. If // the entry is absent from cache, it's populated by calling retrieve func. // Note that the retrieve func should never cause UpdateSlice to be // called for this cache. GetValue( ctx context.Context, key QueryCacheItemKey, subKey QueryCacheSubKey, retrieve UntypedRetrieveFunc, ) (any, error) // UpdateSlice updates the slice stored in the cache by invoking the // specified SliceAppender. If the entry is not cached, the method does // nothing. UpdateSlice(key QueryCacheItemKey, update SliceAppender) // ClearCache empties the cache. ClearCache() }
QueryCache stores results of SQL queries and data derived from these results. Presently, the cached entries are never removed, but eventually, it might become an LRU cache.
var NullQueryCache QueryCache = (*queryCache)(nil)
type QueryCacheItemKey ¶ added in v1.7.0
type QueryCacheItemKey struct { Kind QueryCacheKind Key string }
func QueryCacheKey ¶ added in v1.4.0
func QueryCacheKey(kind QueryCacheKind, key string) QueryCacheItemKey
QueryCacheKey creates a key for QueryCache.
type QueryCacheKind ¶ added in v1.4.0
type QueryCacheKind string
type QueryCacheSubKey ¶ added in v1.4.0
type QueryCacheSubKey string
QueryCacheSubKey denotes a cache subkey. The empty subkey refers to the main key. All other subkeys are cleared by UpdateSlice for the key. The subkeys are intended to store data derived from the query results, such as serialized responses.
type RetrieveFunc ¶ added in v1.4.0
RetrieveFunc retrieves a value to be stored in the cache.
type Schema ¶ added in v1.7.0
type Schema struct { Script string Migrations MigrationList // contains filtered or unexported fields }
Schema represents database schema.
func (*Schema) CheckDBVersion ¶ added in v1.7.0
func (*Schema) Diff ¶ added in v1.7.0
Diff diffs the database schema against the actual schema. If there's no differences, it returns an empty string.
func (*Schema) Migrate ¶ added in v1.7.0
Migrate performs database migration. In case if migrations are disabled, the database version is checked but no migrations are run, and if the database is too old and migrations are disabled, an error is returned.
func (*Schema) MigrateTempDB ¶ added in v1.7.0
MigrateTempDB performs database migration on the temporary database. It doesn't use transactions and the temporary database should be considered invalid and discarded if it fails. The database is switched into synchronous mode with WAL journal enabled and synced after the migrations are completed before setting the database version, which triggers file sync.
func (*Schema) SkipMigrations ¶ added in v1.7.0
SkipMigrations skips the specified migrations.
func (*Schema) WriteToFile ¶ added in v1.7.0
WriteToFile writes the schema to the corresponding updated schema file.
type SchemaGen ¶ added in v1.7.0
type SchemaGen struct {
// contains filtered or unexported fields
}
SchemaGen generates database schema files.
func NewSchemaGen ¶ added in v1.7.0
func NewSchemaGen(logger *zap.Logger, schema *Schema, opts ...SchemaGenOpt) *SchemaGen
NewSchemaGen creates a new SchemaGen instance.
type SchemaGenOpt ¶ added in v1.7.0
type SchemaGenOpt func(g *SchemaGen)
SchemaGenOpt represents a schema generator option.
type SliceAppender ¶ added in v1.4.0
SliceAppender modifies slice value stored in the cache, appending the specified item to it and returns the updated slice.
type StateDatabase ¶ added in v1.7.0
type StateDatabase interface { Database IsStateDatabase() }
StateDatabase is a Database used for Spacemesh state.
type Transaction ¶ added in v1.7.0
type Transaction interface { Executor // Commit commits the transaction. Commit() error // Release releases the transaction. If the transaction hasn't been committed, // it's rolled back. Release() error }
Transaction represents a transaction.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package expr proviedes a simple SQL expression parser and builder.
|
Package expr proviedes a simple SQL expression parser and builder. |