redshiftdatasqldriver

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 15 Imported by: 3

README

redshift-data-sql-driver

Documentation Latest GitHub tag Github Actions test Go Report Card License

Redshift-Data API SQL Driver for Go's database/sql package

Usage

for example:

package main

import (
    "context"
    "database/sql"
    "log"

    _ "github.com/mashiike/redshift-data-sql-driver"
)

func main() {
    db, err := sql.Open("redshift-data", "workgroup(default)/dev?timeout=1m")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()
    rows, err := db.QueryContext(
        context.Background(),
        `SELECT table_schema,table_name,table_type FROM svv_tables WHERE table_schema not like :ignore_schema`,
        sql.Named("ignore_schema", "pg_%"),
    )
    if err != nil {
        log.Fatalln(err)
    }
    for rows.Next() {
        var schema, name, tableType string
        err := rows.Scan(&schema, &name, &tableType)
        if err != nil {
            log.Println(err)
            return
        }
        log.Printf("%s.%s\t%s", schema, name, tableType)
    }
}

The pattern for specifying DSNs is as follows

  • with redshift serverless: workgroup([name])/[database]
  • with provisoned cluster: [dbuser]@cluster([name])/[database]
  • with AWS Secrets Manager: arn:aws:secretsmanager:us-east-1:0123456789012:secret:redshift

The DSN parameters include

  • timeout: Timeout for query execution. default = 15m0s
  • polling: Interval to check for the end of a running query. default = 10ms
  • region: Redshift Data API's region. Default is environment setting

Parameter settings are in the format of URL query parameter

workgroup(default)/dev?timeout=1m&polling=1ms

Transaction Notes

The Redshift Data API does not have an interface for pasting transactions and querying sequentially. Therefore, we have implemented an unusual implementation.

package main

import (
    "context"
    "database/sql"
    "log"

    _ "github.com/mashiike/redshift-data-sql-driver"
)

func main() {
    db, err := sql.Open("redshift-data", "workgroup(default)/dev?timeout=1m")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()
    tx, err := db.BeginTx(context.Background(), nil)
    if err != nil {
        log.Fatalln(err)
    }
    tx.ExecContext(context.Background(), "INSERT INTO foo VALUES (1)")
    tx.ExecContext(context.Background(), "INSERT INTO foo VALUES (2)")
    tx.ExecContext(context.Background(), "INSERT INTO foo VALUES (3)")
    tx.Commit() // BatchExecuteStatement API is called here, and the queries called during the transaction are executed together
}

Also, because the interface does not match, Query and QueryContext in the transaction are not supported. Exec and ExecContext in the transaction are not supported.

Unsupported Features

The following functions are not available

Prepare

Prepared Statements were not supported because the Redshift Data API does not have the concept of connecting to a DB.

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupported = errors.New("not supported")
	ErrDSNEmpty     = errors.New("dsn is empty")
	ErrConnClosed   = errors.New("connection closed")
	ErrBeforeCommit = errors.New("transaction is not committed")
	ErrNotInTx      = errors.New("not in transaction")
	ErrInTx         = errors.New("already in transaction")
)
View Source
var RedshiftDataClientConstructor func(ctx context.Context, cfg *RedshiftDataConfig) (RedshiftDataClient, error)

Functions

func SetDebugLogger

func SetDebugLogger(l Logger) error

func SetLogger

func SetLogger(l Logger) error

Types

type Logger

type Logger interface {
	Printf(format string, v ...any)
	SetOutput(w io.Writer)
	Writer() io.Writer
}

type RedshiftDataConfig

type RedshiftDataConfig struct {
	ClusterIdentifier *string
	Database          *string
	DbUser            *string
	WorkgroupName     *string
	SecretsARN        *string

	Timeout time.Duration
	Polling time.Duration

	Params             url.Values
	RedshiftDataOptFns []func(*redshiftdata.Options)
}

func ParseDSN

func ParseDSN(dsn string) (*RedshiftDataConfig, error)

func (*RedshiftDataConfig) String

func (cfg *RedshiftDataConfig) String() string

func (*RedshiftDataConfig) WithRegion

func (cfg *RedshiftDataConfig) WithRegion(region string) *RedshiftDataConfig

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL