cqlr

package module
v0.0.0-...-0ac3589 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2014 License: Apache-2.0 Imports: 4 Imported by: 6

README

cqlr

Build Status

cqlr extends the gocql runtime API and adds the ability to auto-bind a CQL iterator to a struct:

type Tweet struct {
	Timeline string     `cql:"timeline"`
	Id       gocql.UUID `cql:"id"`
	Text     string     `cql:"text"`
}

var s *gocql.Session

q := s.Query(`SELECT text, id, timeline FROM tweet WHERE timeline = ?`, "me")
b := cqlr.BindQuery(q)

var t Tweet
for b.Scan(&t) {
	// Application specific code goes here
}

You can also bind structs to INSERT statements:

tw := Tweet{
	Timeline: "me",
	Id:       gocql.TimeUUID(),
	Text:     "some random message",
}

var s *gocql.Session

b := Bind(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`, tw)
if err := b.Exec(s); err != nil {
	// .....
}

Supported CQL Operations

  • SELECT with BindQuery(), Bind() and Scan()
  • INSERT, UPDATE and DELETE with Bind()
  • Maps and Lists (without deltafication)

Not Yet Supported CQL Operations

  • CAS
  • Counters
  • Sets

Feature Roadmap

(In no particular order of priority)

  • Support for all CQL operations
  • Batching
  • Re-binding new struct instances to existing binding instances
  • Protoype guess-based CQL CRUD
  • Investigate implementing the skip metadata flag in EXECUTE with the gocql driver
  • Consider exposing a low level binary interface in gocql that handles query argument marshaling

Supported Binding Mechanisms

Right now, cqlr supports the following mechanisms to bind iterators:

  • Application supplied binding function
  • Map of column name to struct field name
  • By struct tags
  • By field names

Cassandra Support

Right now cqlr is known to work against Cassandra 2.0.9.

Motivation

gocql users are looking for ways to automatically bind query results to application defined structs, but this functionality is not available in the core library. In addition, it is possible that the core library does not want to support this feature, because it significantly increases the functional scope of that codebase. So the goal of cqlr is to see if this functionality can be layered on top of the core gocql API in a re-useable way.

Design

cqlr should sit on top of the core gocql runtime and concern itself only with struct binding. There are two modes of operation:

  • Binding a struct's fields to the query parameters of a particular statement using Bind()
  • Wrapping a gocql *Query instance to perform runtime introspection of the target struct in conjunction with meta data provided by the query result

The binding is specifically stateful so that down the line, the first loop execution can perform expensive introspection and subsequent loop invocations can benefit from this cached runtime metadata. So in a sense, it is a bit like cqlc, except that the metadata processing is done on the first loop, rather than at compile time.

Status

Right now this is an experiment to try to come up with a design that people think is useful and can be implemented sanely.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingStrategy = errors.New("insufficient column mapping")
)

Functions

This section is empty.

Types

type Binding

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

func Bind

func Bind(s string, v interface{}) *Binding

func BindQuery

func BindQuery(q *gocql.Query) *Binding

func (*Binding) Bind

func (b *Binding) Bind(v interface{}) *Binding

func (*Binding) Close

func (b *Binding) Close() error

func (*Binding) Exec

func (b *Binding) Exec(s *gocql.Session) error

func (*Binding) Map

func (b *Binding) Map(m map[string]string) *Binding

func (*Binding) Query

func (b *Binding) Query(s *gocql.Session) *gocql.Query

func (*Binding) Scan

func (b *Binding) Scan(dest interface{}) bool

func (*Binding) Strict

func (b *Binding) Strict() *Binding

func (*Binding) Use

func (b *Binding) Use(f func(gocql.ColumnInfo) (reflect.StructField, bool)) *Binding

Jump to

Keyboard shortcuts

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