Help us shape the future of Go! Take the 2026 Go Developer Survey

cql

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 14 Imported by: 10

README

Go CQL Driver

GoDoc Reference Build Status Coverage Go Report Card

Golang CQL Cassandra Driver conforming to the built-in database/sql interface

This is a database/sql interface wrapper for https://github.com/gocql/gocql

Get

go get github.com/MichaelS11/go-cql-driver

Example

A simple SQL select example can be found on the godoc

https://godoc.org/github.com/MichaelS11/go-cql-driver#example-package--SqlSelect

Important note:

When done with rows from QueryContext or Query, make sure to check errors from Close and Err

	err = rows.Close()
	if err != nil {
		fmt.Println(err)
		return
	}
	cancel()

	err = rows.Err()
	if err != nil {
		fmt.Println(err)
		return
	}

Documentation

Overview

Example (SqlConnector)
package main

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"github.com/MichaelS11/go-cql-driver"
	"github.com/gocql/gocql"
)

func main() {
	// Example shows how to use OpenDB Connector

	// Normal NewConnector to localhost would look like:
	// connector := cql.NewConnector("127.0.0.1")
	// For testing, need to use additional variables
	connector := cql.NewConnector(cql.TestHostValid)

	db := sql.OpenDB(connector)

	// If you would like change some of the ClusterConfig options
	// https://godoc.org/github.com/gocql/gocql#ClusterConfig
	// Can do a type cast to get to them
	cqlConnector := connector.(*cql.CqlConnector)
	cqlConnector.ClusterConfig.Timeout = cql.TimeoutValid
	cqlConnector.ClusterConfig.ConnectTimeout = cql.ConnectTimeoutValid
	if cql.EnableAuthentication {
		passwordAuthenticator := gocql.PasswordAuthenticator{
			Username: cql.Username,
			Password: cql.Password,
		}
		cqlConnector.ClusterConfig.Authenticator = passwordAuthenticator
	}

	ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
	defer cancel()
	rows, err := db.QueryContext(ctx, "select cql_version from system.local")
	if err != nil {
		fmt.Println("QueryContext error is not nil:", err)
		return
	}
	if !rows.Next() {
		fmt.Println("no Next rows")
		return
	}

	dest := make([]interface{}, 1)
	destPointer := make([]interface{}, 1)
	destPointer[0] = &dest[0]
	err = rows.Scan(destPointer...)
	if err != nil {
		fmt.Println("Scan error is not nil:", err)
		return
	}

	if len(dest) != 1 {
		fmt.Println("len dest != 1")
		return
	}
	data, ok := dest[0].(string)
	if !ok {
		fmt.Println("dest type not string")
		return
	}
	if len(data) < 3 {
		fmt.Println("data string len too small")
		return
	}

	if rows.Next() {
		fmt.Println("has Next rows")
		return
	}

	err = rows.Close()
	if err != nil {
		fmt.Println("Close error is not nil:", err)
		return
	}

	err = rows.Err()
	if err != nil {
		fmt.Println("Err error is not nil:", err)
		return
	}

	err = db.Close()
	if err != nil {
		fmt.Println("Close error is not nil:", err)
		return
	}

	fmt.Println("received cql_version from system.local")

}
Output:
received cql_version from system.local
Example (SqlSelect)
package main

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"github.com/MichaelS11/go-cql-driver"
)

func main() {
	// Example shows how to do a basic select

	openString := cql.TestHostValid + "?timeout=" + cql.TimeoutValidString + "&connectTimeout=" + cql.ConnectTimeoutValidString
	if cql.EnableAuthentication {
		openString += "&username=" + cql.Username + "&password=" + cql.Password
	}

	// A normal simple Open to localhost would look like:
	// db, err := sql.Open("cql", "127.0.0.1")
	// For testing, need to use additional variables
	db, err := sql.Open("cql", openString)
	if err != nil {
		fmt.Printf("Open error is not nil: %v", err)
		return
	}
	if db == nil {
		fmt.Println("db is nil")
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
	defer cancel()
	rows, err := db.QueryContext(ctx, "select cql_version from system.local")
	if err != nil {
		fmt.Println("QueryContext error is not nil:", err)
		return
	}
	if !rows.Next() {
		fmt.Println("no Next rows")
		return
	}

	dest := make([]interface{}, 1)
	destPointer := make([]interface{}, 1)
	destPointer[0] = &dest[0]
	err = rows.Scan(destPointer...)
	if err != nil {
		fmt.Println("Scan error is not nil:", err)
		return
	}

	if len(dest) != 1 {
		fmt.Println("len dest != 1")
		return
	}
	data, ok := dest[0].(string)
	if !ok {
		fmt.Println("dest type not string")
		return
	}
	if len(data) < 3 {
		fmt.Println("data string len too small")
		return
	}

	if rows.Next() {
		fmt.Println("has Next rows")
		return
	}

	err = rows.Close()
	if err != nil {
		fmt.Println("Close error is not nil:", err)
		return
	}

	err = rows.Err()
	if err != nil {
		fmt.Println("Err error is not nil:", err)
		return
	}

	err = db.Close()
	if err != nil {
		fmt.Println("Close error is not nil:", err)
		return
	}

	fmt.Println("received cql_version from system.local")

}
Output:
received cql_version from system.local
Example (SqlStatement)
package main

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"github.com/MichaelS11/go-cql-driver"
)

func main() {
	// Example shows how to use database statement

	openString := cql.TestHostValid + "?timeout=" + cql.TimeoutValidString + "&connectTimeout=" + cql.ConnectTimeoutValidString
	if cql.EnableAuthentication {
		openString += "&username=" + cql.Username + "&password=" + cql.Password
	}

	// A normal simple Open to localhost would look like:
	// db, err := sql.Open("cql", "127.0.0.1")
	// For testing, need to use additional variables
	db, err := sql.Open("cql", openString)
	if err != nil {
		fmt.Printf("Open error is not nil: %v", err)
		return
	}
	if db == nil {
		fmt.Println("db is nil")
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
	stmt, err := db.PrepareContext(ctx, "select cql_version from system.local")
	cancel()
	if err != nil {
		fmt.Println("PrepareContext error is not nil:", err)
		return
	}
	if stmt == nil {
		fmt.Println("stmt is nil")
		return
	}

	ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
	defer cancel()
	rows, err := stmt.QueryContext(ctx)
	if err != nil {
		fmt.Println("QueryContext error is not nil:", err)
		return
	}
	if !rows.Next() {
		fmt.Println("no Next rows")
		return
	}

	dest := make([]interface{}, 1)
	destPointer := make([]interface{}, 1)
	destPointer[0] = &dest[0]
	err = rows.Scan(destPointer...)
	if err != nil {
		fmt.Println("Scan error is not nil:", err)
		return
	}

	if len(dest) != 1 {
		fmt.Println("len dest != 1")
		return
	}
	data, ok := dest[0].(string)
	if !ok {
		fmt.Println("dest type not string")
		return
	}
	if len(data) < 3 {
		fmt.Println("data string len too small")
		return
	}

	if rows.Next() {
		fmt.Println("has Next rows")
		return
	}

	err = rows.Close()
	if err != nil {
		fmt.Println("Close error is not nil:", err)
		return
	}

	err = rows.Err()
	if err != nil {
		fmt.Println("Err error is not nil:", err)
		return
	}

	err = db.Close()
	if err != nil {
		fmt.Println("Close error is not nil:", err)
		return
	}

	fmt.Println("received cql_version from system.local")

}
Output:
received cql_version from system.local

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSupported is returned for any method that is not supported
	ErrNotSupported = fmt.Errorf("not supported")
	// ErrNotImplementedYet is returned for any method that is not implemented yet
	ErrNotImplementedYet = fmt.Errorf("not implemented yet")
	// ErrQueryIsNil is returned when a query is nil
	ErrQueryIsNil = fmt.Errorf("query is nil")
	// ErrNamedValuesNotSupported is returned when values are named. Named values are not supported.
	ErrNamedValuesNotSupported = fmt.Errorf("named values not supported")
	// ErrOrdinalOutOfRange is returned when values ordinal is out of range
	ErrOrdinalOutOfRange = fmt.Errorf("ordinal out of range")

	// CqlDriver is the sql driver
	CqlDriver = &CqlDriverStruct{
		Logger: log.New(os.Stderr, "cql ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile),
	}
)
View Source
var DbConsistency = map[gocql.Consistency]string{
	gocql.Any:         "any",
	gocql.One:         "one",
	gocql.Two:         "two",
	gocql.Three:       "three",
	gocql.Quorum:      "quorum",
	gocql.All:         "all",
	gocql.LocalQuorum: "localQuorum",
	gocql.EachQuorum:  "eachQuorum",
	gocql.LocalOne:    "localOne",
}

DbConsistency maps gocql consistency levels to string

View Source
var DbConsistencyLevels = map[string]gocql.Consistency{
	"any":         gocql.Any,
	"one":         gocql.One,
	"two":         gocql.Two,
	"three":       gocql.Three,
	"quorum":      gocql.Quorum,
	"all":         gocql.All,
	"localQuorum": gocql.LocalQuorum,
	"eachQuorum":  gocql.EachQuorum,
	"localOne":    gocql.LocalOne,
}

DbConsistencyLevels maps string to gocql consistency levels

Functions

func ClusterConfigToConfigString

func ClusterConfigToConfigString(clusterConfig *gocql.ClusterConfig) string

ClusterConfigToConfigString converts a gocql ClusterConfig to a config string https://godoc.org/github.com/gocql/gocql#ClusterConfig

func ConfigStringToClusterConfig

func ConfigStringToClusterConfig(configString string) (*gocql.ClusterConfig, error)

ConfigStringToClusterConfig converts a config string to a gocql ClusterConfig

func DurationToDuration

func DurationToDuration(cqlDuration gocql.Duration) time.Duration

DurationToDuration converts gocql.Duration type to time.Duration. Does not check for overflow

func InterfaceToDuration

func InterfaceToDuration(aInterface interface{}) time.Duration

InterfaceToDuration converts an interface of gocql.Duration type to time.Duration. Does not check for overflow. Returns 0 if interface is not gocql.Duration

func NewClusterConfig

func NewClusterConfig(hosts ...string) *gocql.ClusterConfig

NewClusterConfig returns a new gocql ClusterConfig

func NewConnector

func NewConnector(hosts ...string) driver.Connector

NewConnector returns a new database connector

Types

type CqlConnector

type CqlConnector struct {
	// Logger is used to log connection ping errors
	Logger *log.Logger
	// ClusterConfig is used for changing config options
	// https://godoc.org/github.com/gocql/gocql#ClusterConfig
	ClusterConfig *gocql.ClusterConfig
}

CqlConnector is the sql driver connector

func (*CqlConnector) Connect

func (cqlConnector *CqlConnector) Connect(ctx context.Context) (driver.Conn, error)

Connect returns a new database connection

func (*CqlConnector) Driver

func (cqlConnector *CqlConnector) Driver() driver.Driver

Driver returns the cql driver

type CqlDriverStruct

type CqlDriverStruct struct {
	// Logger is used to log connection ping errors
	Logger *log.Logger
}

CqlDriverStruct is the sql driver

func (*CqlDriverStruct) Open

func (cqlDriver *CqlDriverStruct) Open(configString string) (driver.Conn, error)

Open returns a new database connection

func (*CqlDriverStruct) OpenConnector

func (cqlDriver *CqlDriverStruct) OpenConnector(configString string) (driver.Connector, error)

OpenConnector returns a new database connector

type CqlStmt

type CqlStmt struct {
	// CqlQuery is used for changing query options
	// https://godoc.org/github.com/gocql/gocql#Query
	// This will only work if Go sql every gives access to the driver
	CqlQuery *gocql.Query
}

CqlStmt is the sql driver statement

func (*CqlStmt) Close

func (cqlStmt *CqlStmt) Close() error

Close a statement

func (*CqlStmt) ColumnConverter

func (cqlStmt *CqlStmt) ColumnConverter(index int) driver.ValueConverter

ColumnConverter provides driver ValueConverter for statement

func (*CqlStmt) Exec

func (cqlStmt *CqlStmt) Exec(args []driver.Value) (driver.Result, error)

Exec executes a statement with background context

func (*CqlStmt) ExecContext

func (cqlStmt *CqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)

ExecContext executes a statement with context

func (*CqlStmt) NumInput

func (cqlStmt *CqlStmt) NumInput() int

NumInput not supported

func (*CqlStmt) Query

func (cqlStmt *CqlStmt) Query(args []driver.Value) (driver.Rows, error)

Query queries a statement with background context

func (*CqlStmt) QueryContext

func (cqlStmt *CqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

QueryContext queries a statement with context

Jump to

Keyboard shortcuts

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