Files
gocqlx/README.md

131 lines
4.5 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
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-05-25 08:02:16 +02:00
* CQL query builder ([see more](https://github.com/scylladb/gocqlx/blob/master/qb))
* Database migrations ([see more](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-05-25 08:02:16 +02:00
// Field names are converted to camel case by default, no need to add
// `db:"first_name"`, if you want to disable a filed add `db:"-"` tag.
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
}
// Bind query parameters from a struct.
2017-07-28 10:18:38 +02:00
{
p := Person{
2017-08-30 11:02:55 +02:00
"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()
err := gocqlx.Query(session.Query(stmt), names).BindStruct(&p).ExecRelease()
if err != nil {
2017-08-24 11:54:41 +02:00
t.Fatal(err)
}
}
// Load 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()
var p Person
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": "Patricia",
})
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
}
// 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()
var people []Person
2017-08-24 11:54:41 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": []string{"Patricia", "Igy", "Ian"},
})
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
// Use named query parameters.
{
p := &Person{
"Jane",
"Citizen",
[]string{"jane.citzen@gocqlx_test.com"},
}
stmt, names, err := gocqlx.CompileNamedQuery([]byte("INSERT INTO gocqlx_test.person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)"))
if err != nil {
t.Fatal(err)
}
err = gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
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`
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
```
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
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!