Documentation
¶
Overview ¶
Package gorqlite provieds a database/sql-like driver for rqlite, the distributed consistent sqlite.
Copyright (c)2016 andrew fabbro (andrew@fabbro.org)
See LICENSE.md for license. tl;dr: MIT. Conveniently, the same license as rqlite.
Project home page: https://github.com/raindo308/gorqlite
Learn more about rqlite at: https://github.com/rqlite/rqlite
Index ¶
- Constants
- Variables
- func ParseConsistencyLevel(s string) (consistencyLevel, error)
- func TraceOff()
- func TraceOn(w io.Writer)
- type Connection
- func (conn *Connection) Close()
- func (conn *Connection) ConsistencyLevel() (string, error)
- func (conn *Connection) Leader() (string, error)
- func (conn *Connection) Peers() ([]string, error)
- func (conn *Connection) Query(sqlStatements []string) (results []QueryResult, err error)
- func (conn *Connection) QueryContext(ctx context.Context, sqlStatements []string) (results []QueryResult, err error)
- func (conn *Connection) QueryOne(sqlStatement string) (qr QueryResult, err error)
- func (conn *Connection) QueryOneContext(ctx context.Context, sqlStatement string) (qr QueryResult, err error)
- func (conn *Connection) QueryOneParameterized(statement ParameterizedStatement) (qr QueryResult, err error)
- func (conn *Connection) QueryOneParameterizedContext(ctx context.Context, statement ParameterizedStatement) (qr QueryResult, err error)
- func (conn *Connection) QueryParameterized(sqlStatements []ParameterizedStatement) (results []QueryResult, err error)
- func (conn *Connection) QueryParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []QueryResult, err error)
- func (conn *Connection) Queue(sqlStatements []string) (seq int64, err error)
- func (conn *Connection) QueueContext(ctx context.Context, sqlStatements []string) (seq int64, err error)
- func (conn *Connection) QueueOne(sqlStatement string) (seq int64, err error)
- func (conn *Connection) QueueOneContext(ctx context.Context, sqlStatement string) (seq int64, err error)
- func (conn *Connection) QueueOneParameterized(statement ParameterizedStatement) (seq int64, err error)
- func (conn *Connection) QueueOneParameterizedContext(ctx context.Context, statement ParameterizedStatement) (seq int64, err error)
- func (conn *Connection) QueueParameterized(sqlStatements []ParameterizedStatement) (seq int64, err error)
- func (conn *Connection) QueueParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (seq int64, err error)
- func (conn *Connection) Request(sqlStatements []string) (results []RequestResult, err error)
- func (conn *Connection) RequestContext(ctx context.Context, sqlStatements []string) (results []RequestResult, err error)
- func (conn *Connection) RequestParameterized(sqlStatements []ParameterizedStatement) (results []RequestResult, err error)
- func (conn *Connection) RequestParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []RequestResult, err error)
- func (conn *Connection) SetConsistencyLevel(levelDesired consistencyLevel) error
- func (conn *Connection) SetExecutionWithTransaction(state bool) error
- func (conn *Connection) Write(sqlStatements []string) (results []WriteResult, err error)
- func (conn *Connection) WriteContext(ctx context.Context, sqlStatements []string) (results []WriteResult, err error)
- func (conn *Connection) WriteOne(sqlStatement string) (wr WriteResult, err error)
- func (conn *Connection) WriteOneContext(ctx context.Context, sqlStatement string) (wr WriteResult, err error)
- func (conn *Connection) WriteOneParameterized(statement ParameterizedStatement) (wr WriteResult, err error)
- func (conn *Connection) WriteOneParameterizedContext(ctx context.Context, statement ParameterizedStatement) (wr WriteResult, err error)
- func (conn *Connection) WriteParameterized(sqlStatements []ParameterizedStatement) (results []WriteResult, err error)
- func (conn *Connection) WriteParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []WriteResult, err error)
- type NullBool
- type NullFloat64
- type NullInt16
- type NullInt32
- type NullInt64
- type NullString
- type NullTime
- type ParameterizedStatement
- type QueryResult
- func (qr *QueryResult) Columns() []string
- func (qr *QueryResult) Map() (map[string]interface{}, error)
- func (qr *QueryResult) Next() bool
- func (qr *QueryResult) NumRows() int64
- func (qr *QueryResult) RowNumber() int64
- func (qr *QueryResult) Scan(dest ...interface{}) error
- func (qr *QueryResult) Slice() ([]interface{}, error)
- func (qr *QueryResult) Types() []string
- type RequestResult
- type StatementErrors
- type WriteResult
Constants ¶
const ( // ConsistencyLevelNone provides no consistency to other nodes. ConsistencyLevelNone consistencyLevel = iota // ConsistencyLevelWeak provides a weak consistency that guarantees the // queries are sent to the leader. ConsistencyLevelWeak // ConsitencyLevelLinearizable provides a linearizable consistency and // guarantees that read result will reflect all previous writes. ConsistencyLevelLinearizable // ConsistencyLevelStrong provides a strong consistency and guarantees // that the read result will reflect all previous writes and that all // previously commmitted writes in the Raft log have been applied.. ConsistencyLevelStrong )
Variables ¶
var DefaultHTTPClient = &http.Client{ Timeout: defaultTimeout * time.Second, }
var ErrClosed = errors.New("gorqlite: connection is closed")
ErrClosed indicates that the connection was closed.
Functions ¶
func ParseConsistencyLevel ¶
ParseConsistencyLevel parses a string into a consistencyLevel.
func TraceOff ¶
func TraceOff()
TraceOff turns off tracing output. Once you call TraceOff(), no further info is sent to the io.Writer, unless it is TraceOn'd again.
Types ¶
type Connection ¶
type Connection struct {
ID string
// contains filtered or unexported fields
}
Connection provides the connection abstraction.
Since rqlite is stateless, there is no real connection — this type holds the configuration plus the discovered cluster topology (current leader, peers, scheme, credentials, etc.).
Connections are assigned a "connection ID" — a pseudo-UUID built from 16 crypto/rand bytes — used only for tagging trace output, so concurrent connections can be distinguished.
Connection methods are safe to call concurrently from multiple goroutines. State changes go through a single mutex; the HTTP client is reused as-is so the standard library handles its own concurrency.
func Open ¶
func Open(connURL string) (*Connection, error)
Open creates and returns a "connection" to rqlite, using the default HTTP client.
Since rqlite is stateless, there is no actual connection. Open() creates and initializes a gorqlite Connection type, which represents various config information.
The URL should be in a form like this:
http://localhost:4001 http:// default, no auth, localhost:4001 https:// default, no auth, localhost:4001, using https http://localhost:1234 http://mary:secret2@localhost:1234 https://mary:secret2@somewhere.example.com:1234 https://mary:secret2@somewhere.example.com // will use 4001
func OpenWithClient ¶
func OpenWithClient(connURL string, client *http.Client) (*Connection, error)
OpenWithClient creates and returns a "connection" to rqlite, and uses the given HTTP client for all connections to rqlite. This allows clients to have complete conntrol over the HTTP communications between this client and the rqlite system.
func (*Connection) Close ¶
func (conn *Connection) Close()
Close marks the connection as closed. Safe to call multiple times.
func (*Connection) ConsistencyLevel ¶
func (conn *Connection) ConsistencyLevel() (string, error)
ConsistencyLevel returns the current consistency level as a string.
func (*Connection) Leader ¶
func (conn *Connection) Leader() (string, error)
Leader returns the current cluster leader's API address. With cluster discovery enabled, calling Leader refreshes the cached topology before returning.
func (*Connection) Peers ¶
func (conn *Connection) Peers() ([]string, error)
Peers returns the current cluster peers, leader first. With cluster discovery enabled, calling Peers refreshes the cached topology before returning.
func (*Connection) Query ¶
func (conn *Connection) Query(sqlStatements []string) (results []QueryResult, err error)
Query is used to perform SELECT operations in the database. It takes an array of SQL statements and executes them in a single transaction, returning an array of QueryResult.
Query uses context.Background() internally; to specify the context, use QueryContext.
func (*Connection) QueryContext ¶
func (conn *Connection) QueryContext(ctx context.Context, sqlStatements []string) (results []QueryResult, err error)
QueryContext is used to perform SELECT operations in the database. It takes an array of SQL statements and executes them in a single transaction, returning an array of QueryResult.
func (*Connection) QueryOne ¶
func (conn *Connection) QueryOne(sqlStatement string) (qr QueryResult, err error)
QueryOne wraps Query into a single-statement method.
QueryOne uses context.Background() internally; to specify the context, use QueryOneContext.
func (*Connection) QueryOneContext ¶
func (conn *Connection) QueryOneContext(ctx context.Context, sqlStatement string) (qr QueryResult, err error)
QueryOneContext wraps Query into a single-statement method.
func (*Connection) QueryOneParameterized ¶
func (conn *Connection) QueryOneParameterized(statement ParameterizedStatement) (qr QueryResult, err error)
QueryOneParameterized wraps QueryParameterized into a single-statement method.
QueryOneParameterized uses context.Background() internally; to specify the context, use QueryOneParameterizedContext.
func (*Connection) QueryOneParameterizedContext ¶
func (conn *Connection) QueryOneParameterizedContext(ctx context.Context, statement ParameterizedStatement) (qr QueryResult, err error)
QueryOneParameterizedContext wraps QueryParameterizedContext into a single-statement method.
func (*Connection) QueryParameterized ¶
func (conn *Connection) QueryParameterized(sqlStatements []ParameterizedStatement) (results []QueryResult, err error)
QueryParameterized is used to perform SELECT operations in the database.
It takes an array of parameterized SQL statements and executes them in a single transaction, returning an array of QueryResult vars.
QueryParameterized uses context.Background() internally; to specify the context, use QueryParameterizedContext.
func (*Connection) QueryParameterizedContext ¶
func (conn *Connection) QueryParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []QueryResult, err error)
QueryParameterizedContext is used to perform SELECT operations in the database.
It takes an array of parameterized SQL statements and executes them in a single transaction, returning an array of QueryResult vars.
func (*Connection) Queue ¶
func (conn *Connection) Queue(sqlStatements []string) (seq int64, err error)
Queue performs asynchronous (queued) writes to the rqlite database, as described in https://rqlite.io/docs/api/queued-writes/. The returned sequence number can be used to wait for the writes to be applied.
Queue uses context.Background() internally; to specify the context, use QueueContext. To use Queue with parameterized queries, use QueueParameterized.
func (*Connection) QueueContext ¶
func (conn *Connection) QueueContext(ctx context.Context, sqlStatements []string) (seq int64, err error)
QueueContext is the context-aware version of Queue.
To use QueueContext with parameterized queries, use QueueParameterizedContext.
func (*Connection) QueueOne ¶
func (conn *Connection) QueueOne(sqlStatement string) (seq int64, err error)
QueueOne wraps Queue into a single-statement method.
QueueOne uses context.Background() internally; to specify the context, use QueueOneContext.
func (*Connection) QueueOneContext ¶
func (conn *Connection) QueueOneContext(ctx context.Context, sqlStatement string) (seq int64, err error)
QueueOneContext wraps QueueContext into a single-statement method.
func (*Connection) QueueOneParameterized ¶
func (conn *Connection) QueueOneParameterized(statement ParameterizedStatement) (seq int64, err error)
QueueOneParameterized wraps QueueParameterized into a single-statement method.
QueueOneParameterized uses context.Background() internally; to specify the context, use QueueOneParameterizedContext.
func (*Connection) QueueOneParameterizedContext ¶
func (conn *Connection) QueueOneParameterizedContext(ctx context.Context, statement ParameterizedStatement) (seq int64, err error)
QueueOneParameterizedContext wraps QueueParameterizedContext into a single-statement method.
func (*Connection) QueueParameterized ¶
func (conn *Connection) QueueParameterized(sqlStatements []ParameterizedStatement) (seq int64, err error)
QueueParameterized performs queued writes with parameter binding.
QueueParameterized uses context.Background() internally; to specify the context, use QueueParameterizedContext.
func (*Connection) QueueParameterizedContext ¶
func (conn *Connection) QueueParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (seq int64, err error)
QueueParameterizedContext is the context-aware version of QueueParameterized.
func (*Connection) Request ¶
func (conn *Connection) Request(sqlStatements []string) (results []RequestResult, err error)
Request is used to access Unified Endpoint to send read and writes requests in one operation.
func (*Connection) RequestContext ¶
func (conn *Connection) RequestContext(ctx context.Context, sqlStatements []string) (results []RequestResult, err error)
RequestContext is used to access Unified Endpoint to send read and writes requests in one operation.
To use RequestContext with parameterized queries, use RequestParameterizedContext.
func (*Connection) RequestParameterized ¶
func (conn *Connection) RequestParameterized(sqlStatements []ParameterizedStatement) (results []RequestResult, err error)
RequestParameterized returns an error if one is encountered during its operation. If it's something like a call to the rqlite API, then it'll return that error. If one statement out of several has an error, you can look at the individual statement's Err for more info.
RequestParameterized uses context.Background() internally; to specify the context, use RequestParameterizedContext.
func (*Connection) RequestParameterizedContext ¶
func (conn *Connection) RequestParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []RequestResult, err error)
RequestParameterizedContext returns an error if one is encountered during its operation. If it's something like a call to the rqlite API, then it'll return that error. If one statement out of several has an error, you can look at the individual statement's Err for more info.
func (*Connection) SetConsistencyLevel ¶
func (conn *Connection) SetConsistencyLevel(levelDesired consistencyLevel) error
SetConsistencyLevel sets the consistency level used for future queries on this connection.
func (*Connection) SetExecutionWithTransaction ¶
func (conn *Connection) SetExecutionWithTransaction(state bool) error
SetExecutionWithTransaction toggles whether multi-statement queries/writes are wrapped in a single rqlite transaction.
func (*Connection) Write ¶
func (conn *Connection) Write(sqlStatements []string) (results []WriteResult, err error)
Write performs DDL/DML synchronously without parameters.
Write uses context.Background() internally; to specify the context, use WriteContext. To use Write with parameterized queries, use WriteParameterized.
func (*Connection) WriteContext ¶
func (conn *Connection) WriteContext(ctx context.Context, sqlStatements []string) (results []WriteResult, err error)
WriteContext performs DDL/DML synchronously without parameters.
To use WriteContext with parameterized queries, use WriteParameterizedContext.
func (*Connection) WriteOne ¶
func (conn *Connection) WriteOne(sqlStatement string) (wr WriteResult, err error)
WriteOne wraps Write into a single-statement method.
WriteOne uses context.Background() internally; to specify the context, use WriteOneContext.
func (*Connection) WriteOneContext ¶
func (conn *Connection) WriteOneContext(ctx context.Context, sqlStatement string) (wr WriteResult, err error)
WriteOneContext wraps WriteContext into a single-statement method.
func (*Connection) WriteOneParameterized ¶
func (conn *Connection) WriteOneParameterized(statement ParameterizedStatement) (wr WriteResult, err error)
WriteOneParameterized wraps WriteParameterized into a single-statement method.
WriteOneParameterized uses context.Background() internally; to specify the context, use WriteOneParameterizedContext.
func (*Connection) WriteOneParameterizedContext ¶
func (conn *Connection) WriteOneParameterizedContext(ctx context.Context, statement ParameterizedStatement) (wr WriteResult, err error)
WriteOneParameterizedContext wraps WriteParameterizedContext into a single-statement method.
func (*Connection) WriteParameterized ¶
func (conn *Connection) WriteParameterized(sqlStatements []ParameterizedStatement) (results []WriteResult, err error)
WriteParameterized performs DDL/DML synchronously.
It takes a slice of statements and returns a same-length slice of WriteResults; statement N's result lives in results[N]. All statements run in a single rqlite transaction.
The returned error is non-nil when the API call itself failed (and then results contains a single error-bearing element), or when one or more statements failed. Inspect each result's Err for per-statement errors.
WriteParameterized uses context.Background() internally; to specify the context, use WriteParameterizedContext.
func (*Connection) WriteParameterizedContext ¶
func (conn *Connection) WriteParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []WriteResult, err error)
WriteParameterizedContext is the context-aware version of WriteParameterized.
type NullFloat64 ¶
NullFloat64 represents a float64 that may be null.
type NullString ¶
NullString represents a string that may be null.
type ParameterizedStatement ¶
type ParameterizedStatement struct {
Query string
Arguments []interface{}
}
type QueryResult ¶
QueryResult holds the results of a call to Query — effectively a rowset.
For "SELECT id, name FROM some_table", a QueryResult holds any errors produced by the query, the column names and types, and the rows.
Query returns a slice of QueryResult; QueryOne returns a single one.
func (*QueryResult) Columns ¶
func (qr *QueryResult) Columns() []string
Columns returns a list of the column names for this QueryResult.
func (*QueryResult) Map ¶
func (qr *QueryResult) Map() (map[string]interface{}, error)
Map returns the current row (advanced by Next) as a map[columnName]value.
Date and datetime columns are converted to time.Time; everything else is whatever encoding/json produced — usually float64 for numbers, string for text, nil for NULL.
func (*QueryResult) Next ¶
func (qr *QueryResult) Next() bool
Next advances the result cursor to the next row. The first call positions on row 0; subsequent calls advance by one. It returns false when no more rows remain.
Map, Slice, and Scan all require Next to have been called first; they return an error otherwise.
A common idiom:
rows, err := conn.QueryOne("SELECT ...")
for rows.Next() {
rows.Scan(&dst)
}
func (*QueryResult) NumRows ¶
func (qr *QueryResult) NumRows() int64
NumRows returns the number of rows returned by the query.
func (*QueryResult) RowNumber ¶
func (qr *QueryResult) RowNumber() int64
RowNumber returns the current row number as Next() iterates through the result's rows.
func (*QueryResult) Scan ¶
func (qr *QueryResult) Scan(dest ...interface{}) error
Scan copies the current row's columns into the destinations supplied.
Each destination must be a pointer to a supported type:
*string, *[]byte *int, *int64, *float64 *bool (parsed from "1"/"0", "true"/"false", etc.) *time.Time *NullString, *NullInt64, *NullInt32, *NullInt16, *NullFloat64, *NullBool, *NullTime
JSON numbers come back as float64 from the rqlite API; gorqlite converts them to the destination's underlying type for you.
A NULL column is left unchanged for primitive destinations and recorded as Valid=false on Null* destinations.
func (*QueryResult) Slice ¶
func (qr *QueryResult) Slice() ([]interface{}, error)
Slice returns the current row (advanced by Next) as a []interface{}. The slice is freshly allocated; the values inside it are the same references the QueryResult holds, so don't mutate them.
Date and datetime columns are converted to time.Time; everything else is whatever encoding/json produced — usually float64 for numbers, string for text, nil for NULL.
func (*QueryResult) Types ¶
func (qr *QueryResult) Types() []string
Types returns the column types declared by the SELECT — note that SQLite's type affinity rules mean these are advisory and may disagree with the actual stored value. See https://www.sqlite.org/datatype3.html.
type RequestResult ¶
type RequestResult struct {
Err error
Query *QueryResult
Write *WriteResult
}
RequestResult holds the result of a single statement sent to Unified Endpoint.
If statement failed, Err contains the error, neither Query nor Write is set. If statement succeeded, either of Query or Write is set — depending on the type of the statement. Query.Err and Write.Err are never set.
type StatementErrors ¶
type StatementErrors []error
func (StatementErrors) As ¶
func (errs StatementErrors) As(target interface{}) bool
As returns true if the current error, or one of the statement errors can be assigned to the target error.
func (StatementErrors) Error ¶
func (errs StatementErrors) Error() string
Error returns a string representation of the statement errors.
func (StatementErrors) Is ¶
func (errs StatementErrors) Is(target error) bool
Is returns true if the current error, or one of the statement errors is equal to the target error.
func (StatementErrors) Unwrap ¶
func (errs StatementErrors) Unwrap() []error
Unwrap returns the slice of statement errors.
type WriteResult ¶
type WriteResult struct {
Err error // don't trust the rest if this isn't nil
Timing float64
RowsAffected int64 // affected by the change
LastInsertID int64 // if relevant, otherwise zero value
// contains filtered or unexported fields
}
WriteResult holds the result of a single statement sent to Write.
Write returns a slice of WriteResult; WriteOne returns a single one.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package stdlib provides a compatability layer from gorqlite to database/sql.
|
Package stdlib provides a compatability layer from gorqlite to database/sql. |