fireboltgosdk

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: Apache-2.0 Imports: 17 Imported by: 5

README

Firebolt GO SDK

Nightly code check Code quality checks Integration tests Coverage

Firebolt GO driver is an implementation of database/sql/driver.

Installation
go get github.com/firebolt-db/firebolt-go-sdk
DSN (Data source name)

All information for the connection should be specified using the DSN string. The firebolt dsn string has the following format:

firebolt://[/database]?account_name=account_name&client_id=client_id&client_secret=client_secret[&engine=engine]
  • client_id - client id of the service account.
  • client_secret - client secret of the service account.
  • account_name - the name of Firebolt account to log in to.
  • database - (optional) the name of the database to connect to.
  • engine - (optional) the name of the engine to run SQL on.
Querying example

Here is an example of establishing a connection and executing a simple select query. For it to run successfully, you have to specify your credentials, and have a default engine up and running.

package main

import (
	"database/sql"
	"fmt"
	// we need to import firebolt-go-sdk in order to register the driver
	_ "github.com/firebolt-db/firebolt-go-sdk"
)

func main() {

	// set your Firebolt credentials to construct a dsn string
	clientId := ""
	clientSecret := ""
	accountName := ""
	databaseName := ""
	dsn := fmt.Sprintf("firebolt:///%s?account_name=%s&client_id=%s&client_secret=%s", databaseName, accountName, clientId, clientSecret)

	// open a Firebolt connection
	db, err := sql.Open("firebolt", dsn)
	if err != nil {
		fmt.Printf("error during opening a driver: %v", err)
	}

	// create a table
	_, err = db.Query("CREATE TABLE test_table(id INT, value TEXT)")
	if err != nil {
		fmt.Printf("error during select query: %v", err)
	}

	// execute a parametrized insert (only ? placeholders are supported)
	_, err = db.Query("INSERT INTO test_table VALUES (?, ?)", 1, "my value")
	if err != nil {
		fmt.Printf("error during select query: %v", err)
	}

	// execute a simple select query
	rows, err := db.Query("SELECT id FROM test_table")
	if err != nil {
		fmt.Printf("error during select query: %v", err)
	}

	// iterate over the result
	defer func() {
		if err := rows.Close(); err != nil {
			fmt.Printf("error during rows.Close(): %v\n", err)
		}
	}()
	
	for rows.Next() {
		var id int
		if err := rows.Scan(&id); err != nil {
			fmt.Printf("error during scan: %v", err)
		}
		fmt.Println(id)
	}

	if err := rows.Err(); err != nil {
		fmt.Printf("error during rows iteration: %v\n", err)
	}
}
Streaming example

In order to stream the query result (and not store it in memory fully), you need to pass a special context with streaming enabled.

Warning: If you enable streaming the result, the query execution might finish successfully, but the actual error might be returned during the iteration over the rows.

Here is an example of how to do it:

package main

import (
    "context"
    "database/sql"
    "fmt"
	
    // we need to import firebolt-go-sdk in order to register the driver
    _ "github.com/firebolt-db/firebolt-go-sdk"
	fireboltContext "github.com/firebolt-db/firebolt-go-sdk/context"
)

func main() {
	// set your Firebolt credentials to construct a dsn string
	clientId := ""
	clientSecret := ""
	accountName := ""
	databaseName := ""
	dsn := fmt.Sprintf("firebolt:///%s?account_name=%s&client_id=%s&client_secret=%s", databaseName, accountName, clientId, clientSecret)

	// open a Firebolt connection
	db, err := sql.Open("firebolt", dsn)
	if err != nil {
		fmt.Printf("error during opening a driver: %v", err)
	}
	
	// create a streaming context
	streamingCtx := fireboltContext.WithStreaming(context.Background())

	// execute a large select query
	rows, err := db.QueryContext(streamingCtx, "SELECT \"abc\" FROM generate_series(1, 100000000)")
	if err != nil {
		fmt.Printf("error during select query: %v", err)
	}

	// iterating over the result is exactly the same as in the previous example
	defer func() {
		if err := rows.Close(); err != nil {
			fmt.Printf("error during rows.Close(): %v\n", err)
		}
	}()

	for rows.Next() {
		var id int
		if err := rows.Scan(&id); err != nil {
			fmt.Printf("error during scan: %v", err)
		}
		fmt.Println(id)
	}

	if err := rows.Err(); err != nil {
		fmt.Printf("error during rows iteration: %v\n", err)
	}
}
Errors in streaming

If you enable streaming the result, the query execution might finish successfully, but the actual error might be returned during the iteration over the rows.

Limitations

Although, all interfaces are available, not all of them are implemented or could be implemented:

  • driver.Result is a dummy implementation and doesn't return the real result values.
  • Named query parameters are not supported

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDSNString

func ParseDSNString(dsn string) (*types.FireboltSettings, error)

ParseDSNString parses a dsn in a format: firebolt://username:password@db_name[/engine_name][?account_name=organization] returns a settings object where all parsed values are populated returns an error if required fields couldn't be parsed or if after parsing some characters were left unparsed

func SplitStatements added in v0.0.6

func SplitStatements(sql string) ([]string, error)

SplitStatements split multiple statements into a list of statements

func WithAccountID added in v1.6.1

func WithAccountID(accountID string) driverOption

WithAccountID defines account ID for the driver

func WithClientParams added in v1.1.0

func WithClientParams(accountID string, token string, userAgent string) driverOption

WithClientParams defines client parameters for the driver

func WithDatabaseName added in v1.1.0

func WithDatabaseName(databaseName string) driverOption

WithDatabaseName defines database name for the driver

func WithEngineUrl added in v1.1.0

func WithEngineUrl(engineUrl string) driverOption

WithEngineUrl defines engine url for the driver

func WithToken added in v1.6.1

func WithToken(token string) driverOption

WithToken defines token for the driver

func WithUserAgent added in v1.6.1

func WithUserAgent(userAgent string) driverOption

WithUserAgent defines user agent for the driver

Types

type FireboltConnector added in v1.1.0

type FireboltConnector struct {
	// contains filtered or unexported fields
}

FireboltConnector is an intermediate type between a Connection and a Driver which stores session data

func FireboltConnectorWithOptions added in v1.1.0

func FireboltConnectorWithOptions(opts ...driverOption) *FireboltConnector

FireboltConnectorWithOptions builds a custom connector

func (*FireboltConnector) Connect added in v1.1.0

func (c *FireboltConnector) Connect(ctx context.Context) (driver.Conn, error)

Connect returns a connection to the database

func (*FireboltConnector) Driver added in v1.1.0

func (c *FireboltConnector) Driver() driver.Driver

Driver returns the underlying driver of the Connector

type FireboltDriver

type FireboltDriver struct {
	// contains filtered or unexported fields
}

func (*FireboltDriver) Open

func (d *FireboltDriver) Open(dsn string) (driver.Conn, error)

Open parses the dsn string, and if correct tries to establish a connection

func (*FireboltDriver) OpenConnector added in v1.1.0

func (d *FireboltDriver) OpenConnector(dsn string) (driver.Connector, error)

type FireboltResult

type FireboltResult struct {
}

func (FireboltResult) LastInsertId

func (r FireboltResult) LastInsertId() (int64, error)

LastInsertId returns last inserted ID, not supported by firebolt

func (FireboltResult) RowsAffected

func (r FireboltResult) RowsAffected() (int64, error)

RowsAffected returns a number of affected rows, not supported by firebolt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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