Documentation
¶
Overview ¶
Package tablestore is a database/sql driver for Alibaba Cloud Tablestore, which runs SQL over its HTTPS API. It registers the one name "tablestore", and takes a DSN of the form tablestore://key:secret@instance.region.ots.aliyuncs.com/instance (D222).
db, err := sql.Open("tablestore", "tablestore://key:secret@myinstance.ap-southeast-5.ots.aliyuncs.com/myinstance")
A request and an answer are protobuf, and the rows of an answer are FlatBuffers. The driver writes both encodings with the standard library (D13 and D222). It signs each request with the first version of the signature, and it reads no credential from the environment or from a file (D7).
A statement is one call, SQLQuery. The server binds no parameter, so the driver writes each argument into the text of the statement (D34 and D224). The SQL works over a mapping table, which names a table of Tablestore. The server runs SELECT, INSERT, UPDATE and DELETE, and refuses a second statement in the same call. The columns of a result come with their types, and the driver reads each value by the type of its column (D223).
Tablestore has no transaction in SQL, so BeginTx fails with dbimp.ErrNotSupported (D20). docs/TABLESTORE.md holds what the driver knows about the server.
Example ¶
Example maps a table, then writes and reads it with SQL. The server binds no argument, so the driver writes each argument into the text of the statement.
package main
import (
"context"
"database/sql"
"fmt"
"log"
)
// exampleDSN is the DSN of an instance. SQL maps a table that the API made, so an
// example starts from a table named orders that the API created with the key id.
const exampleDSN = "tablestore://key:secret@myinstance.ap-southeast-5.ots.aliyuncs.com/myinstance"
func main() { //nolint:testableexamples // The example needs an instance, so it has no output to check.
if err := exampleQuery(context.Background()); err != nil {
log.Fatal(err)
}
}
func exampleQuery(ctx context.Context) error {
db, err := sql.Open("tablestore", exampleDSN)
if err != nil {
return fmt.Errorf("opening the database: %w", err)
}
defer db.Close()
if _, err := db.ExecContext(ctx, "CREATE TABLE orders (id BIGINT, name MEDIUMTEXT, placed DATETIME, PRIMARY KEY(id));"); err != nil {
return fmt.Errorf("mapping the table: %w", err)
}
if _, err := db.ExecContext(ctx, "INSERT INTO orders (id, name) VALUES (?, ?)", int64(1), "first"); err != nil {
return fmt.Errorf("inserting: %w", err)
}
var name string
if err := db.QueryRowContext(ctx, "SELECT name FROM orders WHERE id = ?", int64(1)).Scan(&name); err != nil {
return fmt.Errorf("selecting: %w", err)
}
fmt.Println(name)
return nil
}
Output:
Index ¶
Examples ¶
Constants ¶
const ErrNoCredentials dbimp.Error = "the configuration has no access key id or secret"
ErrNoCredentials is the error of a connector whose Config holds no access key id and no secret, because the driver reads no credential from the environment (D7 and D222).
const Name = "tablestore"
Name is the name that the driver registers with database/sql, and the scheme of its DSN.
Variables ¶
This section is empty.
Functions ¶
func WithOptions ¶
WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.
Example ¶
ExampleWithOptions sets the options of every statement that a context starts. The server has no setting for a timeout, so the context is the way to bound a statement.
package main
import (
"context"
"database/sql"
"fmt"
"log"
"time"
"github.com/xo/dbimp/tablestore"
)
// exampleDSN is the DSN of an instance. SQL maps a table that the API made, so an
// example starts from a table named orders that the API created with the key id.
const exampleDSN = "tablestore://key:secret@myinstance.ap-southeast-5.ots.aliyuncs.com/myinstance"
func main() { //nolint:testableexamples // The example needs an instance, so it has no output to check.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
err := examplePing(tablestore.WithOptions(ctx, tablestore.WithDatabase("myinstance")))
cancel()
if err != nil {
log.Fatal(err)
}
}
func examplePing(ctx context.Context) error {
db, err := sql.Open("tablestore", exampleDSN)
if err != nil {
return fmt.Errorf("opening the database: %w", err)
}
defer db.Close()
return db.PingContext(ctx)
}
Output:
Types ¶
type Config ¶
type Config struct {
// Host is the host of the endpoint, such as
// instance.ap-southeast-5.ots.aliyuncs.com.
Host string
// Port is the port of the endpoint. Zero means the port of the scheme, 443
// for HTTPS and 80 for HTTP.
Port int
// TLS is true to speak HTTPS. The DSN turns it on by default, and only a
// test with a fake server turns it off.
TLS bool
// Instance is the instance of Tablestore that a statement runs in, which the
// path of the DSN names. It is the first label of the host.
Instance string
// User is the access key id, and Password is the access key secret, which
// the driver signs each request with (D94 and D222). A Config with no User
// and no Password opens no connection, because the driver reads no
// credential from the environment (D7).
User string
Password string
}
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 tablestore://key:secret@instance.region.ots.aliyuncs.com/instance (D27, D35 and D222). The path is the instance, with no further slash, and the DSN needs it. The only key is tls, which is true by default. The access key id and the secret are the user and the password of the URL. They can be left out, and then the Config has none and the connector refuses to connect (D7). Any other key, and a key that appears twice, is refused.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector opens connections to one endpoint. 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.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver for Tablestore.
type Error ¶
type Error struct {
// HTTPStatus is the status code of the response.
HTTPStatus int
// Code is the code of the error, such as OTSParameterInvalid.
Code string
// Message is the text of the error.
Message string
// RequestID is the id that the server gave the request, in the header
// X-Ots-Requestid. It can be empty.
RequestID string
// contains filtered or unexported fields
}
Error is an error that Tablestore reported. A request that the server refuses answers HTTP 400 or 403 with a protobuf message that holds a code and a text, such as OTSParameterInvalid with the text of a syntax error (recorded: "a syntax error"). A request with no credentials answers HTTP 400 with the same two members as XML (recorded: "no credentials").
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 instance of one statement, as the path of the DSN does. The host of the DSN names the instance too, so a name that differs from the instance of the DSN fails the statement with dbimp.ErrNotSupported.
func WithParameter ¶
WithParameter sets a member of SQLQueryRequest by its name. The driver sets sql_version (an integer, 1 by default) and search_token (a string, empty by default), and a value here replaces them. The member version is not open to the caller, because the driver reads FlatBuffers only (D223). Any other name fails the statement with dbimp.ErrInvalidValue.
Example ¶
ExampleWithParameter sends the member sql_version of the request for one statement.
package main
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/xo/dbimp/tablestore"
)
// exampleDSN is the DSN of an instance. SQL maps a table that the API made, so an
// example starts from a table named orders that the API created with the key id.
const exampleDSN = "tablestore://key:secret@myinstance.ap-southeast-5.ots.aliyuncs.com/myinstance"
func main() { //nolint:testableexamples // The example needs an instance, so it has no output to check.
if err := exampleParameter(context.Background()); err != nil {
log.Fatal(err)
}
}
func exampleParameter(ctx context.Context) error {
db, err := sql.Open("tablestore", exampleDSN)
if err != nil {
return fmt.Errorf("opening the database: %w", err)
}
defer db.Close()
var version string
if err := db.QueryRowContext(ctx, "SELECT @@version", tablestore.WithParameter("sql_version", 1)).Scan(&version); err != nil {
return fmt.Errorf("reading the version: %w", err)
}
fmt.Println(version)
return nil
}
Output:
func WithReadonly ¶
WithReadonly asks that the statement write nothing. Tablestore has no setting that makes it refuse a write for one request, so WithReadonly(true) fails the statement with dbimp.ErrNotSupported. A policy that allows reads only makes a read-only principal.
func WithTimeout ¶
WithTimeout sets the time that the server gives the statement. The server accepts the hint MAX_EXECUTION_TIME in the text, but a test did not show that it stops a statement, so a positive value fails the statement with dbimp.ErrNotSupported. The context of the statement is the way to bound it. Zero asks for nothing.