Documentation
¶
Overview ¶
Package rqlite is a database/sql driver for rqlite, which runs SQLite on each node of a cluster, and takes SQL over HTTP. It registers the one name "rqlite", and takes a DSN of the form rqlite://user:pass@host:4001?key=value (D141).
db, err := sql.Open("rqlite", "rqlite://admin:pass@localhost:4001")
Each call sends one statement in one request (D142). A query goes to /db/request, so that a write with RETURNING returns its rows, and Exec goes to /db/execute. The server binds each argument (D143). A value has the Go type of the affinity of its declared type, and a value that SQLite stored with another storage class keeps the Go type of its JSON form (D140). rqlite shares one write connection among every client, so the driver has no transactions (D144). When the context has a deadline, the server stops the statement at that deadline (D145). docs/RQLITE.md holds what the driver knows about the server.
Index ¶
Constants ¶
const ( // LevelNone reads the SQLite of the node, with no check of the leader. LevelNone = "none" // LevelWeak reads on the leader, after the node checks that it leads. LevelWeak = "weak" // LevelLinearizable reads on the leader, after a round of Raft that // shows that it still leads. LevelLinearizable = "linearizable" // LevelStrong sends the read through the log of Raft. LevelStrong = "strong" // LevelAuto is LevelNone on a read-only node, and LevelWeak on any // other. LevelAuto = "auto" )
The values of the key level, the read consistency of a query (D141). An empty level sends none, and the server reads at LevelWeak.
const Name = "rqlite"
Name is the name that the driver registers with database/sql, and the scheme of its DSN.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Host is the host of the node.
Host string
// Port is the port of the HTTP API of the node, 4001 by default.
Port int
// TLS is true to speak HTTPS.
TLS bool
// User and Password are the credentials, which go by HTTP Basic
// authentication. With no user, the driver sends no credentials.
User string
Password string
// Level is the read consistency of a query, or "" for the default of
// the server (D141).
Level string
// Freshness is how old the data of a read with LevelNone can be, or zero
// for no bound.
Freshness time.Duration
}
Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector opens connections to one node. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.
func NewConnector ¶
NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver for rqlite.
type Error ¶
type Error struct {
// HTTPStatus is the status code of the response.
HTTPStatus int
// Message is the message of the server.
Message string
// contains filtered or unexported fields
}
Error is an error that rqlite reported. An error of SQL arrives with HTTP 200, as the key error of the result of the statement, such as `no such table: t` (measured). A request that the server refused arrives with another status: HTTP 400 for a body that is not JSON, HTTP 401 for a wrong password, and HTTP 500 for a result that JSON cannot hold, such as an infinity (measured).
func (*Error) AuthenticationRefused ¶ added in v0.24.0
AuthenticationRefused reports whether the server refused the credential. It returns what Is reports for dbimp.ErrAuthentication, and a caller that does not import this module reads it through an interface of its own (D227).
func (*Error) ErrVerbose ¶ added in v0.25.0
ErrVerbose returns the error as psql writes it for \errverbose: the code and the message on the first line, and then each field that the server sent, one on each line with its name in upper case. A caller that does not import this module reads it through an interface of its own (D231).
func (*Error) Is ¶ added in v0.18.0
Is reports whether err matches target. It matches dbimp.ErrAuthentication for HTTP 401, which is the answer for a wrong password (recorded: "a wrong password"). rqlite also sends HTTP 401, with the same empty body, for an endpoint that a user has no permission for (recorded: "the status"), but the driver sends only /db/execute, /db/query and /db/request, so a 401 that the driver sees is a refused credential (D197).
type Option ¶
Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.
func WithDatabase ¶
WithDatabase would choose a database. rqlite has one database (D141), so a statement with it fails with dbimp.ErrNotSupported.
func WithFreshness ¶
WithFreshness sets how old the data of a read with LevelNone can be, as the key freshness of the DSN does (D141).
func WithLevel ¶
WithLevel sets the read consistency of a query, as the key level of the DSN does (D141).
func WithParameter ¶
WithParameter sets any key of the URL of the request by its name, such as "timings" or "qualify_columns", with the text of value. The server reads a flag by its presence, so nil and "" send the key alone. A key named here replaces one that the driver sets itself, such as "db_timeout" or "level".
func WithReadonly ¶
WithReadonly sends the statement to /db/query, which runs it on a read-only connection, so the server refuses a write with the error `attempt to change database via query operation` (measured and D142). A user with only the permission query can run a statement with it.
func WithTimeout ¶
WithTimeout sets the time that the server gives the statement, as the key db_timeout. The server stops a read that runs longer with the error `query timeout`, and a write with `execute timeout`, and keeps nothing of the write (measured). When the context has a deadline too, the shorter of the two goes (D145). The driver also ends the request at that time, because 9.4.5 ignores db_timeout for a read on /db/request, and a read stops when its client leaves (D146). Zero sets no timeout.