Skip to content

maraino/ecql

Repository files navigation

ecql

GoDoc

Package ecql (EasyCQL) implements an easy to use Cassandra client for the Go programing language.

EasyCQL is based on gocql.

The current interface is still experimental and it will change without notice.

Features

Easy API:

  • Map struct types with Cassandra tables.
  • SELECT statements.
  • INSERT statements.
  • DELETE statements.
  • UPDATE statements.
  • Compound primary keys.

Statement API:

  • Map struct types with Cassandra tables.
  • SELECT statements.
  • SELECT COUNT(1) statements.
  • INSERT statements.
  • DELETE statements.
  • UPDATE statements.
  • BATCH statements.
  • Iterators to go through multiple results.
  • WHERE filtering (=, >, >=, <, or <=).
  • WHERE filtering (AND).
  • WHERE filtering (IN).
  • WHERE filtering (Interface mapping of keys).
  • WHERE filtering (CONTAINS, CONTAINS KEY)
  • LIMIT on SELECT statements.
  • ORDER BY on SELECT statements.
  • ALLOW FILTERING ON SELECT statements.
  • IF NOT EXISTS on INSERT statements.
  • IF and IF EXISTS on DELETE statements.
  • IF and IF EXISTS on UPDATE statements.
  • USING TTL on INSERT statements.
  • USING TIMESTAMP on INSERT statements.
  • USING TIMESTAMP on DELETE statements.
  • USING TIMESTAMP on BATCH statements.
  • USING TTL on UPDATE statements.
  • USING TIMESTAMP on UPDATE statements.
  • Counters.
  • Functions.

Documentation.

Defining a table.

To be able to bind a table in Cassandra to a Go struct we will need tag the struct fields using the tag cql, cqltable and cqlkey. The tag cql defines the column name, the tag cqltable defines the name of the table, and cqlkey is a comma separated list of the primary keys in the right order.

For example, for the CREATE TABLE statement:

CREATE TABLE tweet (
  id uuid,
  timeline text,
  text text,
  time timestamp,
  PRIMARY KEY (id)
);

We can use the following struct in Go:

type Tweet struct {
	ID       gocql.UUID `cql:"id" cqltable:"tweet" cqlkey:"id"`
	Timeline string     `cql:"timeline"`
	Text     string     `cql:"text"`
	Time     time.Time  `cql:"time"`
}

func init() {
	ecql.Register(Tweet{})
}

It is recommended to register the struct on init functions, but ecql will register new types if they are not registered.

Queries.

Easy API

sess.Get(i interface{}, keys ...interface{}) error

Creates and execute a SELECT statement in the table defined by the argument i using the keys as the values of the primary keys. It stores the result in the first argument, so it must be passed as a reference.

var tw Tweet
err := sess.Get(&tw, "a5450908-17d7-11e6-b9ec-542696d5770f")
sess.Set(i interface{}) error

Creates and execute a INSERT statement in the table defined by the argument i and sets the values in the mapped columns.

tw  := Tweet{
	ID:       gocql.TimeUUID(),
	Timeline: "ecql",
	Text:     "Hello World",
	Time:     time.Now(),
}
err := sess.Set(tw)
sess.Del(i interface{}) error

Creates a DELETE statement in the table defined by the argument i using the filtering by the primary keys defined on i.

uuid, _ := gocql.ParseUUID("a5450908-17d7-11e6-b9ec-542696d5770f")
tw := Tweet{
	ID: uuid,
}
err := sess.Del(tw)

About

EasyCQL client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published