gockle

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: Apache-2.0 Imports: 4 Imported by: 16

README

gockle

Go Reference

Note: Test coverage is low because there is no Cassandra database for the tests to use. Providing one yields 97.37% test coverage. Some code is uncovered because gocql cannot be mocked. This is one difficulty your code avoids by using gockle.

Documentation

Overview

Package gockle simplifies and mocks github.com/gocql/gocql. It provides simple interfaces to insert, query, and mutate Cassandra data, as well as get basic keyspace and table metadata.

The entry points are NewSession and NewSimpleSession. Call them to get a Session. Session interacts with the database. It executes queries and batched queries and iterates result rows. Closing the Session closes the underlying gocql.Session, including the one passed to NewSimpleSession.

Mocks are provided for testing use of Batch, Iterator, and Session.

Tx is short for transaction.

The name gockle comes from a pronunciation of gocql.

Index

Constants

View Source
const ColumnApplied = "[applied]"

ColumnApplied is the name of a special column that has a bool that indicates whether a conditional statement was applied.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch interface {
	// Add adds the query for statement and arguments.
	Add(statement string, arguments ...interface{})

	// Exec executes the queries in the order they were added.
	Exec() error

	// ExecTx executes the queries in the order they were added. It returns a slice
	// of maps from columns to values, the maps corresponding to all the conditional
	// queries, and ordered in the same relative order. The special column
	// ColumnApplied has a bool that indicates whether the conditional statement was
	// applied. If a conditional statement was not applied, the current values for
	// the columns are put into the map.
	ExecTx() ([]map[string]interface{}, error)
}

Batch is an ordered collection of CQL queries.

type BatchKind

type BatchKind byte

BatchKind is the kind of Batch. The choice of kind mostly affects performance.

const (
	// BatchLogged queries are atomic. Queries are only isolated within a single
	// partition.
	BatchLogged BatchKind = 0

	// BatchUnlogged queries are not atomic. Atomic queries spanning multiple partitions cost performance.
	BatchUnlogged BatchKind = 1

	// BatchCounter queries update counters and are not idempotent.
	BatchCounter BatchKind = 2
)

Kinds of batches.

type BatchMock

type BatchMock struct {
	mock.Mock
}

BatchMock is a mock Batch. See github.com/maraino/go-mock.

func (BatchMock) Add

func (m BatchMock) Add(statement string, arguments ...interface{})

Add implements Batch.

func (BatchMock) Exec

func (m BatchMock) Exec() error

Exec implements Batch.

func (BatchMock) ExecTx

func (m BatchMock) ExecTx() ([]map[string]interface{}, error)

ExecTx implements Batch.

type Iterator

type Iterator interface {
	// Close closes the Iterator.
	Close() error

	// Scan puts the current result row in results and returns whether there are
	// more result rows.
	Scan(results ...interface{}) bool

	// ScanMap puts the current result row in results and returns whether there are
	// more result rows.
	ScanMap(results map[string]interface{}) bool

	// WillSwitchPage detects if iterator reached end of current page and the
	// next page is available.
	WillSwitchPage() bool

	// PageState return the current paging state for a query which can be used
	// for subsequent quries to resume paging this point.
	PageState() []byte
}

Iterator iterates CQL query result rows.

type IteratorMock

type IteratorMock struct {
	mock.Mock
}

IteratorMock is a mock Iterator. See github.com/maraino/go-mock.

func (IteratorMock) Close

func (m IteratorMock) Close() error

Close implements Iterator.

func (IteratorMock) PageState added in v0.2.0

func (m IteratorMock) PageState() []byte

PageState implements Iterator.

func (IteratorMock) Scan

func (m IteratorMock) Scan(results ...interface{}) bool

Scan implements Iterator.

func (IteratorMock) ScanMap

func (m IteratorMock) ScanMap(results map[string]interface{}) bool

ScanMap implements Iterator.

func (IteratorMock) WillSwitchPage added in v0.2.0

func (m IteratorMock) WillSwitchPage() bool

WillSwitchPage implements Iterator.

type Query added in v0.2.0

type Query interface {
	// PageSize will tell the iterator to fetch the result in pages of size n.
	PageSize(n int) Query

	// WithContext will set the context to use during a query, it will be used
	// to timeout when waiting for responses from Cassandra.
	WithContext(ctx context.Context) Query

	// PageState sets the paging state for the query to resume paging from a
	// specific point in time. Setting this will disable to query paging for
	// this query, and must be used for all subsequent pages.
	PageState(state []byte) Query

	// Exec executes the query without returning any rows.
	Exec() error

	// Iter executes the query and returns an iterator capable of iterating
	// over all results.
	Iter() Iterator

	// MapScan executes the query, copies the columns of the first selected row
	// into the map pointed at by m and discards the rest. If no rows
	// were selected, ErrNotFound is returned.
	MapScan(m map[string]interface{}) error

	// MapScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
	// statement containing an IF clause). If the transaction fails because
	// the existing values did not match, the previous values will be stored
	// in dest map.
	//
	// As for INSERT .. IF NOT EXISTS, previous values will be returned as if
	// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
	// column mismatching. MapScanCAS is added to capture them safely.
	MapScanCAS(dest map[string]interface{}) (applied bool, err error)

	// Scan executes the query, copies the columns of the first selected row
	// into the values pointed at by dest and discards the rest. If no rows
	// were selected, ErrNotFound is returned.
	Scan(dest ...interface{}) error

	// Release releases a query back into a pool of queries. Released queries
	// cannot be reused.
	Release()

	// GetConsistency returns the currently configured consistency level for
	// the query.
	GetConsistency() gocql.Consistency

	// SetConsistency sets the consistency level for this query.
	SetConsistency(c gocql.Consistency)

	// Consistency sets the consistency level for this query. If no consistency
	// level has been set, the default consistency level of the cluster
	// is used.
	Consistency(c gocql.Consistency) Query

	// SerialConsistency sets the consistency level for the
	// serial phase of conditional updates. That consistency can only be
	// either SERIAL or LOCAL_SERIAL and if not present, it defaults to
	// SERIAL. This option will be ignored for anything else that a
	// conditional update/insert.
	SerialConsistency(cons gocql.SerialConsistency) Query
}

Query represents a CQL query.

type QueryMock added in v0.2.0

type QueryMock struct {
	mock.Mock
}

QueryMock is a mock Query.

func (QueryMock) Consistency added in v0.4.0

func (m QueryMock) Consistency(c gocql.Consistency) Query

func (QueryMock) Exec added in v0.2.0

func (m QueryMock) Exec() error

Exec implements Query.

func (QueryMock) GetConsistency added in v0.3.0

func (m QueryMock) GetConsistency() gocql.Consistency

func (QueryMock) Iter added in v0.2.0

func (m QueryMock) Iter() Iterator

Iter implements Query.

func (QueryMock) MapScan added in v0.2.0

func (m QueryMock) MapScan(mm map[string]interface{}) error

MapScan implements Query.

func (QueryMock) MapScanCAS added in v0.4.0

func (m QueryMock) MapScanCAS(mm map[string]interface{}) (bool, error)

MapScan implements Query.

func (QueryMock) PageSize added in v0.2.0

func (m QueryMock) PageSize(n int) Query

PageSize implements Query.

func (QueryMock) PageState added in v0.2.0

func (m QueryMock) PageState(state []byte) Query

PageState implements Query.

func (QueryMock) Release added in v0.2.0

func (m QueryMock) Release()

Release implements Query.

func (QueryMock) Scan added in v0.2.0

func (m QueryMock) Scan(dest ...interface{}) error

Scan implements Query.

func (QueryMock) SerialConsistency added in v0.5.0

func (m QueryMock) SerialConsistency(c gocql.SerialConsistency) Query

func (QueryMock) SetConsistency added in v0.3.0

func (m QueryMock) SetConsistency(c gocql.Consistency)

func (QueryMock) WithContext added in v0.2.0

func (m QueryMock) WithContext(ctx context.Context) Query

WithContext implements Query.

type Session

type Session interface {
	// Batch returns a new Batch for the Session.
	Batch(kind BatchKind) Batch

	// Close closes the Session.
	Close()

	// Columns returns a map from column names to types for keyspace and table.
	// Schema changes during a session are not reflected; you must open a new
	// Session to observe them.
	Columns(keyspace, table string) (map[string]gocql.TypeInfo, error)

	// Exec executes the query for statement and arguments.
	Exec(statement string, arguments ...interface{}) error

	// Scan executes the query for statement and arguments and puts the first
	// result row in results.
	Scan(statement string, results []interface{}, arguments ...interface{}) error

	// ScanIterator executes the query for statement and arguments and returns an
	// Iterator for the results.
	ScanIterator(statement string, arguments ...interface{}) Iterator

	// ScanMap executes the query for statement and arguments and puts the first
	// result row in results.
	ScanMap(statement string, results map[string]interface{}, arguments ...interface{}) error

	// ScanMapSlice executes the query for statement and arguments and returns all
	// the result rows.
	ScanMapSlice(statement string, arguments ...interface{}) ([]map[string]interface{}, error)

	// ScanMapTx executes the query for statement and arguments as a lightweight
	// transaction. If the query is not applied, it puts the current values for the
	// conditional columns in results. It returns whether the query is applied.
	ScanMapTx(statement string, results map[string]interface{}, arguments ...interface{}) (bool, error)

	// Tables returns the table names for keyspace. Schema changes during a session
	// are not reflected; you must open a new Session to observe them.
	Tables(keyspace string) ([]string, error)

	// Query returns a Query to interact with the database.
	// Further details of the query may be tweaked using the resulting Query
	// before the query is executed. Query is automatically prepared if
	// it has not previously been executed.
	Query(statement string, arguments ...interface{}) Query
}

Session is a Cassandra connection. The Query methods run CQL queries. The Columns and Tables methods provide simple metadata.

func NewSession

func NewSession(s *gocql.Session) Session

NewSession returns a new Session for s.

func NewSimpleSession

func NewSimpleSession(hosts ...string) (Session, error)

NewSimpleSession returns a new Session for hosts. It uses native protocol version 4.

type SessionMock

type SessionMock struct {
	mock.Mock
}

SessionMock is a mock Session. See github.com/maraino/go-mock.

func (SessionMock) Batch

func (m SessionMock) Batch(kind BatchKind) Batch

Batch implements Session.

func (SessionMock) Close

func (m SessionMock) Close()

Close implements Session.

func (SessionMock) Columns

func (m SessionMock) Columns(keyspace, table string) (map[string]gocql.TypeInfo, error)

Columns implements Session.

func (SessionMock) Exec

func (m SessionMock) Exec(statement string, arguments ...interface{}) error

Exec implements Session.

func (SessionMock) Query added in v0.2.0

func (m SessionMock) Query(statement string, arguments ...interface{}) Query

Query implements Session.

func (SessionMock) Scan

func (m SessionMock) Scan(statement string, results []interface{}, arguments ...interface{}) error

Scan implements Session.

func (SessionMock) ScanIterator

func (m SessionMock) ScanIterator(statement string, arguments ...interface{}) Iterator

ScanIterator implements Session.

func (SessionMock) ScanMap

func (m SessionMock) ScanMap(statement string, results map[string]interface{}, arguments ...interface{}) error

ScanMap implements Session.

func (SessionMock) ScanMapSlice

func (m SessionMock) ScanMapSlice(statement string, arguments ...interface{}) ([]map[string]interface{}, error)

ScanMapSlice implements Session.

func (SessionMock) ScanMapTx

func (m SessionMock) ScanMapTx(statement string, results map[string]interface{}, arguments ...interface{}) (bool, error)

ScanMapTx implements Session.

func (SessionMock) Tables

func (m SessionMock) Tables(keyspace string) ([]string, error)

Tables implements Session.

Jump to

Keyboard shortcuts

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