Documentation
¶
Overview ¶
Package snowflake is a database/sql driver for Snowflake, which runs SQL over its SQL REST API, /api/v2/statements, with HTTPS and JSON. It registers the one name "snowflake", and takes a DSN of the form snowflake://user:key@<org>-<account>.snowflakecomputing.com/database/schema (D183).
db, err := sql.Open("snowflake", "snowflake://alice:"+key+"@org-acct.snowflakecomputing.com/db/public")
The password of the DSN is the private key of the user, as the base64url text of its PKCS8 DER bytes. The driver signs a token with it, with RS256 and the claims that Snowflake names, and signs a new one before the old one ends. The driver sends the token and never the key, and only to the host of the DSN.
The driver starts each statement with async=true and polls its handle until it ends, so that it can cancel the statement when the context ends or the caller closes the rows before the end. The server runs the whole statement before it sends the first partition, so an error comes before any row. The driver reads the first partition from the answer, and each later one with GET, in order and one at a time, one token at a time. The server binds each argument as a typed binding. The server has no session across two requests, so the driver has no transactions. docs/SNOWFLAKE.md holds what the driver knows about the server.
Index ¶
- Constants
- func WithOptions(ctx context.Context, opts ...Option) context.Context
- type Config
- type Connector
- type Driver
- type Error
- type Option
- func WithDatabase(name string) Option
- func WithParameter(name string, value any) Option
- func WithReadonly(readonly bool) Option
- func WithRole(name string) Option
- func WithSchema(name string) Option
- func WithTimeZone(name string) Option
- func WithTimeout(d time.Duration) Option
- func WithWarehouse(name string) Option
Constants ¶
const ( // ErrCut is the error of a partition of a result that ends before the // count of rows that the server named for it. The server runs the whole // statement before it sends the first partition, so only a partition can // be cut (D21 and D183). After a row, it comes wrapped with // dbimp.ErrIncomplete (D107). ErrCut dbimp.Error = "the answer was cut short" // ErrCanceled is the error of a statement that the server ended with the // code 000604, when the context of the statement did not end. Someone // else canceled it, such as an administrator (measured). ErrCanceled dbimp.Error = "the statement was canceled" // ErrTimeout is the error of a statement that reached its timeout on the // server, which answers HTTP 408 and the code 000630 (measured). ErrTimeout dbimp.Error = "the statement reached its timeout" )
The sentinel errors of the driver. Each one wraps into a failure with errors.Is (D6).
const Name = "snowflake"
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 account, such as
// <org>-<account>.snowflakecomputing.com.
Host string
// Port is the port of the server, 443 by default.
Port int
// User is the login user. The claims of the token name it in upper case.
User string
// Password is the private key of the user, as the base64url text of its
// PKCS8 DER bytes (D183). The driver signs a token with it, and sends the
// token and never the key.
Password string
// Database and Schema are the database and the schema of each statement,
// which the driver sends in the body. Each one is optional.
Database string
Schema string
// Role is the role of each statement, and empty for the default role of
// the user.
Role string
// Warehouse is the warehouse of each statement, and empty for the
// default warehouse of the user.
Warehouse string
// TimeZone is the time zone of the session of each statement, which the
// driver sends as the parameter TIMEZONE, such as UTC or Asia/Jakarta.
// Empty leaves the zone of the account. A timestamp_ltz value takes this
// zone too, and time.Local when it is empty. The name Local is time.Local.
TimeZone string
// Timeout is the time that the server gives each statement. The driver
// rounds it up to whole seconds for the server. Zero sends none.
Timeout time.Duration
}
Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).
func ParseDSN ¶
ParseDSN parses a DSN of the form snowflake://user:key@<org>-<account>.snowflakecomputing.com/database/schema?role=r (D27, D35 and D183). The password is the private key. The path holds the database and the schema, and each is optional. The keys are role, warehouse, timeout and timezone. The timeout is a duration with a unit, such as 60s or 1m, and a bare number is refused, and the driver refuses any other. The host must end in .snowflakecomputing.com, unless the DSN names a port or the host is an IP address, which a test of the driver uses for a fake server.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector opens connections to one account. It owns its transport, and every connection shares it, with the token that the driver signs. 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. A private key that is not valid fails the first Connect.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver for Snowflake.
type Error ¶
type Error struct {
// HTTPStatus is the status code of the response.
HTTPStatus int
// Code is code, such as 001003 for a syntax error, and empty when the
// answer has none.
Code string
// SQLState is sqlState, such as 42000, and empty when the answer has
// none. A wrong token has none.
SQLState string
// Message is message, or the text of the answer when it has none.
Message string
// Handle is statementHandle, and empty when the answer has none.
Handle string
// contains filtered or unexported fields
}
Error is an error that Snowflake reported. Every answer that is not a result has a JSON object with the code, the SQLSTATE and the message, with HTTP 422 for a statement that failed, HTTP 408 for one that reached its timeout, HTTP 400 for a request that the server refused, and HTTP 401 for a token that is not valid (measured). An error comes before any row, because the server runs the whole statement before it sends the first partition.
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 sets the database of one statement, as the path of the DSN does.
func WithParameter ¶
WithParameter sets a parameter of the session of one statement by its name, such as "QUERY_TAG", in parameters of the body. The value is written as text with fmt.Sprint. The server reads a parameter in the same way in each statement, and the parameter ends with the statement, because each request is its own session (measured).
A parameter that changes the text of a value is refused with dbimp.ErrNotSupported, because the driver reads each value in the form that docs/SNOWFLAKE.md names: DATE_OUTPUT_FORMAT, TIME_OUTPUT_FORMAT, TIMESTAMP_OUTPUT_FORMAT, TIMESTAMP_LTZ_OUTPUT_FORMAT, TIMESTAMP_NTZ_OUTPUT_FORMAT, TIMESTAMP_TZ_OUTPUT_FORMAT, BINARY_OUTPUT_FORMAT, GEOGRAPHY_OUTPUT_FORMAT and GEOMETRY_OUTPUT_FORMAT. MULTI_STATEMENT_COUNT is refused too, because the driver does not read the child statements that a request of several statements makes (D183). WithParameter("TIMEZONE", v) replaces the time zone of the DSN. A value of nil fails the statement with dbimp.ErrInvalidValue.
func WithReadonly ¶
WithReadonly asks that the statement write nothing. Snowflake has no such setting for one statement, so WithReadonly(true) fails the statement with dbimp.ErrNotSupported (D109).
func WithSchema ¶
WithSchema sets the schema of one statement, as the path of the DSN does.
func WithTimeZone ¶
WithTimeZone sets the time zone of the session of one statement, as the parameter TIMEZONE, as the key timezone of the DSN does. A timestamp_ltz value of the statement takes this zone.
func WithTimeout ¶
WithTimeout sets the time that the server gives the statement, as timeout in the body, as the key timeout of the DSN does. The server counts whole seconds, so the driver rounds d up to the next second. The server stops a statement that runs longer with HTTP 408 and the code 000630 (measured). Zero sends no timeout, and the server uses its own.
func WithWarehouse ¶
WithWarehouse sets the warehouse of one statement, as the key warehouse of the DSN does.