2017-10-02 09:35:19 +02:00
# GoCQLX [](http://godoc.org/github.com/scylladb/gocqlx) [](https://goreportcard.com/report/github.com/scylladb/gocqlx) [](https://travis-ci.org/scylladb/gocqlx)
2017-07-24 16:22:30 +02:00
2018-05-25 08:02:16 +02:00
Package `gocqlx` is an idiomatic extension to `gocql` that provides usability features. With gocqlx you can bind the query parameters from maps and structs, use named query parameters (:identifier) and scan the query results into structs and slices. It comes with a fluent and flexible CQL query builder and a database migrations module.
2017-07-24 16:22:30 +02:00
## Installation
2017-08-03 18:39:16 +02:00
go get -u github.com/scylladb/gocqlx
2017-07-24 16:22:30 +02:00
2017-07-25 14:30:13 +02:00
## Features
2018-04-20 14:41:11 +02:00
* Binding query parameters form struct or map
* Scanning results directly into struct or slice
2018-10-26 17:01:00 +02:00
* CQL query builder ([package qb ](https://github.com/scylladb/gocqlx/blob/master/qb ))
* Super simple CRUD operations based on table model ([package table ](https://github.com/scylladb/gocqlx/blob/master/table ))
* Database migrations ([package migrate ](https://github.com/scylladb/gocqlx/blob/master/migrate ))
2017-08-03 18:39:16 +02:00
* Fast!
2017-08-30 11:02:55 +02:00
## Example
2017-07-25 14:30:13 +02:00
```go
2018-10-26 17:01:00 +02:00
// Person represents a row in person table.
// Field names are converted to camel case by default, no need to add special tags.
// If you want to disable a field add `db:"-"` tag, it will not be persisted.
2017-07-28 10:18:38 +02:00
type Person struct {
2018-05-25 08:02:16 +02:00
FirstName string
2017-08-30 11:02:55 +02:00
LastName string
Email []string
2017-07-25 14:30:13 +02:00
}
2018-10-26 17:01:00 +02:00
// Insert, bind data from struct.
2017-07-28 10:18:38 +02:00
{
2018-10-26 17:01:00 +02:00
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql()
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
2017-08-02 11:12:31 +02:00
2018-10-26 17:01:00 +02:00
if err := q.ExecRelease(); err != nil {
2017-08-24 11:54:41 +02:00
t.Fatal(err)
2017-08-02 11:12:31 +02:00
}
}
2018-10-26 17:01:00 +02:00
// Get first result into a struct.
2017-08-02 11:12:31 +02:00
{
2018-05-23 10:40:32 +02:00
var p Person
2018-10-26 17:01:00 +02:00
stmt, names := qb.Select("gocqlx_test.person").Where(qb.Eq("first_name")).ToCql()
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": "Patricia",
})
2018-05-23 10:40:32 +02:00
if err := q.GetRelease(&p); err != nil {
t.Fatal(err)
2017-08-24 11:54:41 +02:00
}
2017-07-25 14:30:13 +02:00
}
2018-05-23 10:40:32 +02:00
// Load all the results into a slice.
2017-07-28 10:18:38 +02:00
{
2018-05-23 10:40:32 +02:00
var people []Person
2018-10-26 17:01:00 +02:00
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
2017-08-24 11:54:41 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": []string{"Patricia", "Igy", "Ian"},
})
2018-05-23 10:40:32 +02:00
if err := q.SelectRelease(&people); err != nil {
t.Fatal(err)
2017-08-24 11:54:41 +02:00
}
2017-07-25 14:30:13 +02:00
}
2018-05-25 08:02:16 +02:00
2018-10-26 17:01:00 +02:00
// metadata specifies table name and columns it must be in sync with schema.
var personMetadata = table.Metadata{
Name: "person",
Columns: []string{"first_name", "last_name", "email"},
PartKey: []string{"first_name"},
SortKey: []string{"last_name"},
}
// personTable allows for simple CRUD operations based on personMetadata.
var personTable = table.New(personMetadata)
// Get by primary key.
2018-05-25 08:02:16 +02:00
{
2018-10-26 17:01:00 +02:00
p := Person{
"Patricia",
2018-05-25 08:02:16 +02:00
"Citizen",
2018-10-26 17:01:00 +02:00
nil, // no email
2018-05-25 08:02:16 +02:00
}
2018-10-26 17:01:00 +02:00
stmt, names := personTable.Get() // you can filter columns too
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
if err := q.GetRelease(&p); err != nil {
2018-05-25 08:02:16 +02:00
t.Fatal(err)
}
}
2017-07-25 14:30:13 +02:00
```
2018-10-26 17:01:00 +02:00
See more examples in [example_test.go ](https://github.com/scylladb/gocqlx/blob/master/example_test.go ) and [table/example_test.go ](https://github.com/scylladb/gocqlx/blob/master/table/example_test.go ).
2017-08-30 11:02:55 +02:00
2017-07-28 14:38:19 +02:00
## Performance
2017-08-03 18:39:16 +02:00
Gocqlx is fast, this is a benchmark result comparing `gocqlx` to raw `gocql`
2018-05-25 08:02:16 +02:00
on a local machine, Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz.
2017-07-28 14:38:19 +02:00
```
2018-08-23 12:45:47 +02:00
BenchmarkE2EGocqlInsert 20000 86713 ns/op 2030 B/op 33 allocs/op
BenchmarkE2EGocqlxInsert 20000 87882 ns/op 2030 B/op 33 allocs/op
BenchmarkE2EGocqlGet 20000 94308 ns/op 1504 B/op 29 allocs/op
BenchmarkE2EGocqlxGet 20000 95722 ns/op 2128 B/op 33 allocs/op
BenchmarkE2EGocqlSelect 1000 1792469 ns/op 43595 B/op 921 allocs/op
BenchmarkE2EGocqlxSelect 1000 1839574 ns/op 36986 B/op 927 allocs/op
2017-07-28 14:38:19 +02:00
```
2017-08-30 11:02:55 +02:00
See the benchmark in [benchmark_test.go ](https://github.com/scylladb/gocqlx/blob/master/benchmark_test.go ).
2017-08-23 10:20:39 +02:00
2017-09-28 15:32:58 +02:00
## License
2017-08-23 10:20:39 +02:00
Copyright (C) 2017 ScyllaDB
2017-09-28 15:32:58 +02:00
This project is distributed under the Apache 2.0 license. See the [LICENSE ](https://github.com/scylladb/gocqlx/blob/master/LICENSE ) file for details.
It contains software from:
2017-08-23 10:20:39 +02:00
* [gocql project ](https://github.com/gocql/gocql ), licensed under the BSD license
* [sqlx project ](https://github.com/jmoiron/sqlx ), licensed under the MIT license
2018-05-25 08:02:16 +02:00
Apache®, Apache Cassandra® are either registered trademarks or trademarks of
2017-08-23 10:20:39 +02:00
the Apache Software Foundation in the United States and/or other countries.
2017-09-28 15:32:58 +02:00
No endorsement by The Apache Software Foundation is implied by the use of these marks.
2017-10-02 09:35:19 +02:00
GitHub star is always appreciated!