Documentation
¶
Overview ¶
Package influxdb is a database/sql driver for InfluxDB 1, InfluxDB 2, and InfluxDB 3 and later (D78). It registers the one name "influxdb", and takes a DSN of the form influxdb://user:pass@host:port/database?key=value (D82).
db, err := sql.Open("influxdb", "influxdb://_admin:apiv3_token@localhost:8181/mydb")
The driver speaks two dialects. SQL, the dialect "influxdb", goes to /api/v3/query_sql, which only InfluxDB 3 and later have. InfluxQL, the dialect "influxql", goes to /query on every release. The key sqlmode says which one a connection speaks, as sslmode does for PostgreSQL, and its default, prefer, asks the server for its release (D78). Dialect and Version tell a caller what a connection speaks and what it talks to.
The server binds each argument, except in INSERT, which the driver binds (D85): sql.Named("k", v) fills $k, and a positional argument n fills $n, such as $1, which is measured for SQL. SQL reads its columns and their types from DESCRIBE (D80). InfluxQL gives each series of a statement a result set of its own. Its first columns are measurement, which holds the name of the series, and its tags (D81, D83 and D96). InfluxDB has no transactions. docs/INFLUXDB.md holds what the driver knows about the server.
Index ¶
Constants ¶
const ( // SQLModeDisable speaks InfluxQL, and sends no request to learn the // release. SQLModeDisable = "disable" // SQLModeAllow speaks SQL, and sends no request to learn the release. SQLModeAllow = "allow" // SQLModePrefer learns the release from GET /ping, and speaks SQL to // InfluxDB 3 and later and InfluxQL to InfluxDB 1 and 2. It is the // default. SQLModePrefer = "prefer" // SQLModeRequire learns the release from GET /ping, and speaks SQL. The // connection fails on InfluxDB 1 and 2. SQLModeRequire = "require" )
The values of the key sqlmode, which say which dialect a connection speaks (D78).
const ( // DescribeAlways sends DESCRIBE before each statement, and reads the // columns and their types from it. It is the default. DescribeAlways = "always" // DescribeDisable reads the columns from the keys of the first row, and // learns no types. DescribeDisable = "disable" )
The values of the key describe, which say where SQL reads its columns (D80).
const ( // ChunkedPrefer asks InfluxDB 1 for chunks, and InfluxDB 2 and 3 for one // document. It is the default. ChunkedPrefer = "prefer" // ChunkedDisable asks every release for one document. ChunkedDisable = "disable" )
The values of the key chunked, which say whether InfluxQL asks for its answer in chunks (D83).
const ( // SQL is the dialect of SQL on InfluxDB 3 and later. Its name is the name // of the driver, as dburl D29 names it. SQL = "influxdb" // InfluxQL is the dialect of InfluxQL, on every release. InfluxQL = "influxql" )
The dialects of a connection (D78).
const Name = "influxdb"
Name is the name that the driver registers with database/sql, and the scheme of its DSN.
Variables ¶
This section is empty.
Functions ¶
func Dialect ¶
Dialect returns the dialect that a connection speaks: SQL or InfluxQL (D78). A caller calls it inside sql.Conn.Raw, as for Version.
func Version ¶
Version returns the release of the server of a connection, from the header X-Influxdb-Version of GET /ping, such as 1.13.1, 2.9.1 or 3.11.5 (D78). It removes the "v" that InfluxDB 2 writes before the number. A caller calls it inside sql.Conn.Raw, with the connection of the driver:
err = conn.Raw(func(dc any) error {
v, err = influxdb.Version(ctx, dc)
return err
})
Types ¶
type Config ¶
type Config struct {
// Host is the host of the server.
Host string
// Port is the port of the server: 8086 for Version 1 or 2, and 8181
// otherwise, by default.
Port int
// TLS is true to speak HTTPS.
TLS bool
// User and Password are the credentials, sent with basic authentication
// on every request. A token is the password. With neither, the driver
// sends no credentials, for a server that runs with authentication off.
User string
Password string
// Database is the database, which the driver sends as db. It is empty
// for a DSN with no path, and then a statement names its database.
Database string
// RetentionPolicy is the retention policy that InfluxQL reads, sent as
// rp, or empty.
RetentionPolicy string
// SQLMode is SQLModeDisable, SQLModeAllow, SQLModePrefer or
// SQLModeRequire (D78).
SQLMode string
// Version is the major release, 1, 2 or 3, that the driver talks to when
// SQLMode is SQLModeDisable or SQLModeAllow (D78). It also chooses the
// default port, under every SQLMode (D82).
Version int
// Describe is DescribeAlways or DescribeDisable (D80).
Describe string
// Chunked is ChunkedPrefer or ChunkedDisable (D83).
Chunked string
}
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 server. 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.
func (*Connector) Close ¶
Close closes the idle connections of the transport. database/sql calls it when the database closes.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver for InfluxDB.
type Error ¶
type Error struct {
// HTTPStatus is the status code of the response.
HTTPStatus int
// Statement is the statement_id of the InfluxQL statement that failed.
// It is -1 for an error of the whole request.
Statement int
// Message is the message of the server.
Message string
// contains filtered or unexported fields
}
Error is an error that InfluxDB reported. It is the answer to a request whose status is not 2xx, the error of one InfluxQL statement or of the whole InfluxQL answer with HTTP 200, or an answer with HTTP 200 that is not JSON (measured).