cmp: example update
This commit is contained in:
58
README.md
58
README.md
@@ -13,13 +13,14 @@ hood it uses `sqlx/reflectx` package so `sqlx` models will also work with `gocql
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* Flexible `SELECT`, `INSERT`, `UPDATE` `DELETE` and `BATCH` query building using a DSL
|
* Builders for `SELECT`, `INSERT`, `UPDATE` `DELETE` and `BATCH`
|
||||||
* Support for named parameters (:identifier) in queries
|
* Queries with named parameters (:identifier) support
|
||||||
* Binding parameters form struct or map
|
* Binding parameters form struct or map
|
||||||
* Scanning results into structs
|
* Scanning results into structs and slices
|
||||||
|
* Automatic query releasing
|
||||||
* Fast!
|
* Fast!
|
||||||
|
|
||||||
Example, see [full example here](https://github.com/scylladb/gocqlx/blob/master/example_test.go)
|
## Example
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Person struct {
|
type Person struct {
|
||||||
@@ -28,36 +29,45 @@ type Person struct {
|
|||||||
Email []string
|
Email []string
|
||||||
}
|
}
|
||||||
|
|
||||||
p := &Person{
|
// Insert with query parameters bound from struct.
|
||||||
|
{
|
||||||
|
p := &Person{
|
||||||
"Patricia",
|
"Patricia",
|
||||||
"Citizen",
|
"Citizen",
|
||||||
[]string{"patricia.citzen@gocqlx_test.com"},
|
[]string{"patricia.citzen@gocqlx_test.com"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert
|
|
||||||
{
|
|
||||||
stmt, names := qb.Insert("gocqlx_test.person").
|
stmt, names := qb.Insert("gocqlx_test.person").
|
||||||
Columns("first_name", "last_name", "email").
|
Columns("first_name", "last_name", "email").
|
||||||
ToCql()
|
ToCql()
|
||||||
|
|
||||||
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||||
if err != nil {
|
|
||||||
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get
|
// Get first result into a struct.
|
||||||
{
|
{
|
||||||
q := session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")
|
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",
|
||||||
|
})
|
||||||
|
|
||||||
var p Person
|
var p Person
|
||||||
if err := gocqlx.Get(&p, q); err != nil {
|
if err := gocqlx.Get(&p, q.Query); err != nil {
|
||||||
t.Fatal("get:", err)
|
t.Fatal("get:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Log(p)
|
t.Log(p)
|
||||||
|
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select
|
// Select, load all results into a slice.
|
||||||
{
|
{
|
||||||
stmt, names := qb.Select("gocqlx_test.person").
|
stmt, names := qb.Select("gocqlx_test.person").
|
||||||
Where(qb.In("first_name")).
|
Where(qb.In("first_name")).
|
||||||
@@ -73,10 +83,10 @@ p := &Person{
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Log(people)
|
t.Log(people)
|
||||||
// [{Ian Citizen [igy.citzen@gocqlx_test.com]} {Igy Citizen [ian.citzen@gocqlx_test.com]} {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}]
|
// [{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]}]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch
|
// Batch insert two rows in a single query, advanced struct binding.
|
||||||
{
|
{
|
||||||
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
||||||
|
|
||||||
@@ -84,31 +94,33 @@ p := &Person{
|
|||||||
Add("a.", i).
|
Add("a.", i).
|
||||||
Add("b.", i).
|
Add("b.", i).
|
||||||
ToCql()
|
ToCql()
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
|
||||||
|
|
||||||
b := struct {
|
batch := struct {
|
||||||
A Person
|
A Person
|
||||||
B Person
|
B Person
|
||||||
}{
|
}{
|
||||||
A: Person{
|
A: Person{
|
||||||
"Igy",
|
"Igy",
|
||||||
"Citizen",
|
"Citizen",
|
||||||
[]string{"ian.citzen@gocqlx_test.com"},
|
[]string{"igy.citzen@gocqlx_test.com"},
|
||||||
},
|
},
|
||||||
B: Person{
|
B: Person{
|
||||||
"Ian",
|
"Ian",
|
||||||
"Citizen",
|
"Citizen",
|
||||||
[]string{"igy.citzen@gocqlx_test.com"},
|
[]string{"ian.citzen@gocqlx_test.com"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := q.BindStruct(&b).ExecRelease()
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(&batch)
|
||||||
if err != nil {
|
|
||||||
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
See more examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
|
||||||
|
|
||||||
## Performance
|
## Performance
|
||||||
|
|
||||||
Gocqlx is fast, this is a benchmark result comparing `gocqlx` to raw `gocql`
|
Gocqlx is fast, this is a benchmark result comparing `gocqlx` to raw `gocql`
|
||||||
@@ -124,7 +136,7 @@ BenchmarkE2EGocqlSelect-4 30000 2588562 ns/op 34605
|
|||||||
BenchmarkE2EGocqlxSelect-4 30000 2637187 ns/op 27718 B/op 951 allocs/op
|
BenchmarkE2EGocqlxSelect-4 30000 2637187 ns/op 27718 B/op 951 allocs/op
|
||||||
```
|
```
|
||||||
|
|
||||||
See the [benchmark here](https://github.com/scylladb/gocqlx/blob/master/benchmark_test.go).
|
See the benchmark in [benchmark_test.go](https://github.com/scylladb/gocqlx/blob/master/benchmark_test.go).
|
||||||
|
|
||||||
## Notice
|
## Notice
|
||||||
|
|
||||||
|
|||||||
@@ -40,35 +40,36 @@ func TestExample(t *testing.T) {
|
|||||||
[]string{"patricia.citzen@gocqlx_test.com"},
|
[]string{"patricia.citzen@gocqlx_test.com"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert
|
// Insert with query parameters bound from struct.
|
||||||
{
|
{
|
||||||
stmt, names := qb.Insert("gocqlx_test.person").
|
stmt, names := qb.Insert("gocqlx_test.person").
|
||||||
Columns("first_name", "last_name", "email").
|
Columns("first_name", "last_name", "email").
|
||||||
ToCql()
|
ToCql()
|
||||||
|
|
||||||
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||||
if err != nil {
|
|
||||||
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert with TTL
|
// Insert with query parameters bound from struct extended with a map.
|
||||||
{
|
{
|
||||||
stmt, names := qb.Insert("gocqlx_test.person").
|
stmt, names := qb.Insert("gocqlx_test.person").
|
||||||
Columns("first_name", "last_name", "email").
|
Columns("first_name", "last_name", "email").
|
||||||
TTL().
|
TTL().
|
||||||
ToCql()
|
ToCql()
|
||||||
|
|
||||||
err := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
|
q := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
|
||||||
"_ttl": qb.TTL(86400 * time.Second),
|
"_ttl": qb.TTL(86400 * time.Second),
|
||||||
}).ExecRelease()
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update
|
// Easy update with all parameters bound from struct.
|
||||||
{
|
{
|
||||||
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
||||||
|
|
||||||
@@ -77,13 +78,14 @@ func TestExample(t *testing.T) {
|
|||||||
Where(qb.Eq("first_name"), qb.Eq("last_name")).
|
Where(qb.Eq("first_name"), qb.Eq("last_name")).
|
||||||
ToCql()
|
ToCql()
|
||||||
|
|
||||||
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||||
if err != nil {
|
|
||||||
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch
|
// Batch insert two rows in a single query, advanced struct binding.
|
||||||
{
|
{
|
||||||
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
||||||
|
|
||||||
@@ -91,36 +93,42 @@ func TestExample(t *testing.T) {
|
|||||||
Add("a.", i).
|
Add("a.", i).
|
||||||
Add("b.", i).
|
Add("b.", i).
|
||||||
ToCql()
|
ToCql()
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
|
||||||
|
|
||||||
b := struct {
|
batch := struct {
|
||||||
A Person
|
A Person
|
||||||
B Person
|
B Person
|
||||||
}{
|
}{
|
||||||
A: Person{
|
A: Person{
|
||||||
"Igy",
|
"Igy",
|
||||||
"Citizen",
|
"Citizen",
|
||||||
[]string{"ian.citzen@gocqlx_test.com"},
|
[]string{"igy.citzen@gocqlx_test.com"},
|
||||||
},
|
},
|
||||||
B: Person{
|
B: Person{
|
||||||
"Ian",
|
"Ian",
|
||||||
"Citizen",
|
"Citizen",
|
||||||
[]string{"igy.citzen@gocqlx_test.com"},
|
[]string{"ian.citzen@gocqlx_test.com"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := q.BindStruct(&b).ExecRelease()
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(&batch)
|
||||||
if err != nil {
|
|
||||||
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get
|
// Get first result into a struct.
|
||||||
{
|
{
|
||||||
q := session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")
|
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",
|
||||||
|
})
|
||||||
|
|
||||||
var p Person
|
var p Person
|
||||||
if err := gocqlx.Get(&p, q); err != nil {
|
if err := gocqlx.Get(&p, q.Query); err != nil {
|
||||||
t.Fatal("get:", err)
|
t.Fatal("get:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +136,7 @@ func TestExample(t *testing.T) {
|
|||||||
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
|
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select
|
// Select, load all results into a slice.
|
||||||
{
|
{
|
||||||
stmt, names := qb.Select("gocqlx_test.person").
|
stmt, names := qb.Select("gocqlx_test.person").
|
||||||
Where(qb.In("first_name")).
|
Where(qb.In("first_name")).
|
||||||
@@ -144,19 +152,34 @@ func TestExample(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Log(people)
|
t.Log(people)
|
||||||
// [{Ian Citizen [igy.citzen@gocqlx_test.com]} {Igy Citizen [ian.citzen@gocqlx_test.com]} {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}]
|
// [{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]}]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Named queries, using `:name` as the bindvar.
|
// Easy token based pagination.
|
||||||
{
|
{
|
||||||
// compile query to valid gocqlx query and list of named parameters
|
p := &Person{
|
||||||
stmt, names, err := gocqlx.CompileNamedQuery([]byte("INSERT INTO gocqlx_test.person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)"))
|
"Ian",
|
||||||
if err != nil {
|
"Citizen",
|
||||||
t.Fatal("compile:", err)
|
[]string{"ian.citzen@gocqlx_test.com"},
|
||||||
}
|
}
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
|
||||||
|
|
||||||
// bind named parameters from a struct
|
stmt, names := qb.Select("gocqlx_test.person").
|
||||||
|
Where(qb.Token("first_name").Gt()).
|
||||||
|
Limit(10).
|
||||||
|
ToCql()
|
||||||
|
|
||||||
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||||
|
|
||||||
|
var people []Person
|
||||||
|
if err := gocqlx.Select(&people, q.Query); err != nil {
|
||||||
|
t.Fatal("select:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log(people)
|
||||||
|
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]}]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Named query compilation.
|
||||||
{
|
{
|
||||||
p := &Person{
|
p := &Person{
|
||||||
"Jane",
|
"Jane",
|
||||||
@@ -164,22 +187,15 @@ func TestExample(t *testing.T) {
|
|||||||
[]string{"jane.citzen@gocqlx_test.com"},
|
[]string{"jane.citzen@gocqlx_test.com"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := q.BindStruct(p).Exec(); err != nil {
|
stmt, names, err := gocqlx.CompileNamedQuery([]byte("INSERT INTO gocqlx_test.person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)"))
|
||||||
t.Fatal(err)
|
if err != nil {
|
||||||
}
|
t.Fatal("compile:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// bind named parameters from a map
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||||
{
|
|
||||||
m := map[string]interface{}{
|
|
||||||
"first_name": "Bin",
|
|
||||||
"last_name": "Smuth",
|
|
||||||
"email": []string{"bensmith@allblacks.nz"},
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := q.BindMap(m).Exec(); err != nil {
|
if err := q.ExecRelease(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user