Files
gocqlx/README.md

129 lines
4.6 KiB
Markdown
Raw Normal View History

2017-10-02 09:35:19 +02:00
# GoCQLX [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/scylladb/gocqlx) [![Go Report Card](https://goreportcard.com/badge/github.com/scylladb/gocqlx)](https://goreportcard.com/report/github.com/scylladb/gocqlx) [![Build Status](https://travis-ci.org/scylladb/gocqlx.svg?branch=master)](https://travis-ci.org/scylladb/gocqlx)
2017-07-24 16:22:30 +02:00
2017-08-23 10:20:39 +02:00
Package `gocqlx` is a productivity toolkit for ScyllaDB and Apache Cassandra®.
It's an extension of `gocql`, similar to what `sqlx` is to `database/sql`.
2017-07-24 16:22:30 +02:00
2017-07-25 14:30:13 +02:00
It contains wrappers over `gocql` types that provide convenience methods which
2017-08-23 16:07:55 +02:00
are useful in the development of database driven applications. Under the
2017-07-25 14:30:13 +02:00
hood it uses `sqlx/reflectx` package so `sqlx` models will also work with `gocqlx`.
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
2017-08-03 18:39:16 +02:00
* Fast!
2018-04-20 14:41:11 +02:00
In addition to that:
Package `qb` provides query builders for `SELECT`, `INSERT`, `UPDATE` `DELETE`
and `BATCH` statements supporting full spec including literals, functions,
collections and counters.
Package `migrate` provides a simple database migration system.
2017-08-30 11:02:55 +02:00
## Example
2017-07-25 14:30:13 +02:00
```go
2017-07-28 10:18:38 +02:00
type Person struct {
2017-08-30 11:02:55 +02:00
FirstName string // no need to add `db:"first_name"` etc.
LastName string
Email []string
2017-07-25 14:30:13 +02:00
}
2017-08-30 11:02:55 +02:00
// Insert with query parameters bound from struct.
2017-07-28 10:18:38 +02:00
{
2017-08-30 11:02:55 +02:00
p := &Person{
"Patricia",
"Citizen",
[]string{"patricia.citzen@gocqlx_test.com"},
}
2017-08-24 11:54:41 +02:00
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
ToCql()
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
if err := q.ExecRelease(); err != nil {
2017-08-24 11:54:41 +02:00
t.Fatal(err)
}
}
2017-08-31 16:41:31 +02:00
// Get the first result into a struct.
{
2017-08-30 11:02:55 +02:00
stmt, names := qb.Select("gocqlx_test.person").
Where(qb.Eq("first_name")).
ToCql()
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": "Patricia",
})
2017-08-24 11:54:41 +02:00
var p Person
2017-08-30 11:02:55 +02:00
if err := gocqlx.Get(&p, q.Query); err != nil {
2017-08-24 11:54:41 +02:00
t.Fatal("get:", err)
}
2017-08-30 11:02:55 +02:00
2017-08-24 11:54:41 +02:00
t.Log(p)
2017-08-30 11:02:55 +02:00
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
2017-07-25 14:30:13 +02:00
}
2017-08-31 16:41:31 +02:00
// Select, load all the results into a slice.
2017-07-28 10:18:38 +02:00
{
2017-08-24 11:54:41 +02:00
stmt, names := qb.Select("gocqlx_test.person").
Where(qb.In("first_name")).
ToCql()
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": []string{"Patricia", "Igy", "Ian"},
})
var people []Person
if err := gocqlx.Select(&people, q.Query); err != nil {
t.Fatal("select:", err)
}
t.Log(people)
2017-08-30 11:02:55 +02:00
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Ian Citizen [ian.citzen@gocqlx_test.com]}]
2017-07-25 14:30:13 +02:00
}
```
2017-08-30 11:02:55 +02:00
See more examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
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`
on a local machine. For query binding (insert) `gocqlx` is faster then `gocql`
thanks to smart caching, otherwise the performance is comparable.
2017-07-28 14:38:19 +02:00
```
BenchmarkE2EGocqlInsert-4 500000 258434 ns/op 2627 B/op 59 allocs/op
BenchmarkE2EGocqlxInsert-4 1000000 120257 ns/op 1555 B/op 34 allocs/op
BenchmarkE2EGocqlGet-4 1000000 131424 ns/op 1970 B/op 55 allocs/op
BenchmarkE2EGocqlxGet-4 1000000 131981 ns/op 2322 B/op 58 allocs/op
BenchmarkE2EGocqlSelect-4 30000 2588562 ns/op 34605 B/op 946 allocs/op
BenchmarkE2EGocqlxSelect-4 30000 2637187 ns/op 27718 B/op 951 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
Apache®, Apache Cassandra®, are either registered trademarks or trademarks of
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!