sqlite3

package
v1.0.29 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

README

sqlite3 package

This package provides a high-level interface for sqlite3 including connection pooling, transaction and execution management.

This package is part of a wider project, github.com/djthorpe/go-sqlite. Please see the module documentation for more information.

Building

This module does not include a full copy of sqlite as part of the build process, but expect a pkgconfig file called sqlite.pc to be present (and an existing set of header files and libraries to be available to link against, of course).

In order to locate the pkgconfig file in a non-standard location, use the PKG_CONFIG_PATH environment variable. For example, I have installed sqlite using brew install sqlite and this is how I run the tests:

[bash] git clone git@github.com:djthorpe/go-sqlite.git
[bash] cd go-sqlite
[bash] go mod tidy
[bash] PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig" go test -v ./pkg/sqlite3

There are some examples in the cmd folder of the main repository on how to use the bindings, and various pseudo examples in this document.

Contributing & Distribution

Please do file feature requests and bugs here. The license is Apache 2 so feel free to redistribute. Redistributions in either source code or binary form must reproduce the copyright notice, and please link back to this repository for more information:

Copyright (c) 2021, David Thorpe, All rights reserved.

Overview

The package includes:

  • Connection Pool for managing connections to sqlite3 databases;
  • Connection for executing queries;
  • Auth interface for managing authentication and authorization;
  • and a Cache for managing prepared statements and profiling for slow queries.

It's possible to create custom functions (both in a scalar and aggregate context) and use perform streaming read and write operations on large binary (BLOB) objects.

In order to create a connection pool, you can create a default pool using the NewPool method:

package main

import (
    sqlite "github.com/djthorpe/go-sqlite/pkg/sqlite3"
)

func main() {
	pool, err := sqlite.NewPool(path, nil)
	if err != nil {
        panic(err)
	}
	defer pool.Close()

    // Enumerate the tables in the database
    tables := pool.Get(context.Background()).Tables()
    // ...
}

In this example, a database is opened and the Get method obtains a connection to the database. The Tables method enumerates the tables in the database.

Connection Pool

TODO

Transactions and Queries

TODO

Custom Types

TODO

Custom Functions

TODO

Authentication and Authorization

TODO

Pool Status

TODO

Reading and Writing Large Objects

Backup

Documentation

Overview

Package sqlite3 provides a high level interface for sqlite3, including pooled connections object serialization and transactions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsComplete

func IsComplete(v string) bool

func Version

func Version() string

Types

type Conn

type Conn struct {
	sync.Mutex
	*sqlite3.ConnEx
	// contains filtered or unexported fields
}

func OpenPath

func OpenPath(path string, flags sqlite3.OpenFlags) (*Conn, error)

func (*Conn) Close

func (conn *Conn) Close() error

func (*Conn) ColumnsForIndex

func (c *Conn) ColumnsForIndex(schema, index string) []string

ColumnsForIndex returns the indexes associated with a table

func (*Conn) ColumnsForTable

func (c *Conn) ColumnsForTable(schema, table string) []SQColumn

ColumnsForTable returns the columns in a table

func (*Conn) Do

func (conn *Conn) Do(ctx context.Context, flag SQTxnFlag, fn func(SQTransaction) error) error

Perform a transaction, rollback if error is returned

func (*Conn) Exec

func (conn *Conn) Exec(st SQStatement, fn ExecFunc) error

Execute SQL statement without preparing, and invoke a callback for each row of results which may return true to abort

func (*Conn) Filename

func (c *Conn) Filename(schema string) string

Filename returns the filename for a schema

func (*Conn) ForeignKeyConstraints

func (this *Conn) ForeignKeyConstraints() (bool, error)

func (*Conn) IndexesForTable

func (c *Conn) IndexesForTable(schema, table string) []SQIndexView

IndexesForTable returns the indexes associated with a table

func (*Conn) Modules

func (c *Conn) Modules(prefix ...string) []string

Modules returns a list of modules in a schema. If an argument is provided, then only modules with those name prefixes are returned.

func (*Conn) Schemas

func (c *Conn) Schemas() []string

Schemas returns a list of schemas

func (*Conn) SetForeignKeyConstraints

func (this *Conn) SetForeignKeyConstraints(enable bool) error

func (*Conn) Tables

func (c *Conn) Tables(schema string) []string

Tables returns a list of table names in a schema

func (*Conn) Views

func (c *Conn) Views(schema string) []string

Views returns a list of view names in a schema

type ExecFunc

type ExecFunc sqlite3.ExecFunc

type KeywordToken

type KeywordToken string

type NameToken

type NameToken string

type Pool

type Pool struct {
	sync.WaitGroup
	sync.Pool
	PoolConfig
	PoolCache
	// contains filtered or unexported fields
}

Pool is a connection pool object

func NewPool

func NewPool(path string, errs chan<- error) (*Pool, error)

NewPool returns a new default pool with a shared cache and maxiumum pool size of 5 connections. If filename is not empty, this database is opened or else memory is used. Pass a channel to receive errors, or nil to ignore

func OpenPool

func OpenPool(config PoolConfig, errs chan<- error) (*Pool, error)

OpenPool returns a new pool with the specified configuration

func (*Pool) Close

func (p *Pool) Close() error

Close waits for all connections to be released and then releases resources

func (*Pool) Cur

func (p *Pool) Cur() int64

Cur returns the current number of used connections

func (*Pool) Get

func (p *Pool) Get(ctx context.Context) SQConnection

Get a connection from the pool, and return it to the pool when the context is cancelled or it is put back using the Put method. If there are no connections available, nil is returned.

func (*Pool) Max

func (p *Pool) Max() int64

Max returns the maximum number of connections allowed

func (*Pool) Put

func (p *Pool) Put(conn SQConnection)

Return connection to the pool

func (*Pool) SetMax

func (p *Pool) SetMax(n int64)

SetMax allowed connections released from pool. Note this does not change the maximum instantly, it will settle to this value over time. Set as value zero to disable opening new connections

func (*Pool) String

func (p *Pool) String() string

type PoolCache

type PoolCache struct {
}

PoolCache caches prepared statements and profiling information for statements so it's possible to see slow queries, etc.

type PoolConfig

type PoolConfig struct {
	Max     int64             `yaml:"max"`       // The maximum number of connections in the pool
	Schemas map[string]string `yaml:"databases"` // Schema names mapped onto path for database file
	Trace   bool              `yaml:"trace"`     // Profiling for statements
	Create  bool              `yaml:"create"`    // When false, do not allow creation of new file-based databases
	Auth    SQAuth            // Authentication and Authorization interface
	Flags   sqlite3.OpenFlags // Flags for opening connections
}

PoolConfig is the starting configuration for a pool

type PuncuationToken

type PuncuationToken string

type Tokenizer

type Tokenizer struct {
	*bufio.Scanner
}

func NewTokenizer

func NewTokenizer(v string) *Tokenizer

func (*Tokenizer) Next

func (t *Tokenizer) Next() (interface{}, error)

type Txn

type Txn struct {
	*Conn
}

func (*Txn) Query

func (txn *Txn) Query(st SQStatement, v ...interface{}) (SQResult, error)

Execute SQL statement and invoke a callback for each row of results which may return true to abort

type TxnFunc

type TxnFunc func(SQTransaction) error

type TypeToken

type TypeToken string

type ValueToken

type ValueToken string

type WhitespaceToken

type WhitespaceToken string

Jump to

Keyboard shortcuts

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