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
|
||||
|
||||
* Flexible `SELECT`, `INSERT`, `UPDATE` `DELETE` and `BATCH` query building using a DSL
|
||||
* Support for named parameters (:identifier) in queries
|
||||
* Builders for `SELECT`, `INSERT`, `UPDATE` `DELETE` and `BATCH`
|
||||
* Queries with named parameters (:identifier) support
|
||||
* Binding parameters form struct or map
|
||||
* Scanning results into structs
|
||||
* Scanning results into structs and slices
|
||||
* Automatic query releasing
|
||||
* Fast!
|
||||
|
||||
Example, see [full example here](https://github.com/scylladb/gocqlx/blob/master/example_test.go)
|
||||
## Example
|
||||
|
||||
```go
|
||||
type Person struct {
|
||||
@@ -28,36 +29,45 @@ type Person struct {
|
||||
Email []string
|
||||
}
|
||||
|
||||
p := &Person{
|
||||
// Insert with query parameters bound from struct.
|
||||
{
|
||||
p := &Person{
|
||||
"Patricia",
|
||||
"Citizen",
|
||||
[]string{"patricia.citzen@gocqlx_test.com"},
|
||||
}
|
||||
}
|
||||
|
||||
// Insert
|
||||
{
|
||||
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 {
|
||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
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
|
||||
if err := gocqlx.Get(&p, q); err != nil {
|
||||
if err := gocqlx.Get(&p, q.Query); err != nil {
|
||||
t.Fatal("get:", err)
|
||||
}
|
||||
|
||||
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").
|
||||
Where(qb.In("first_name")).
|
||||
@@ -73,10 +83,10 @@ p := &Person{
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
@@ -84,31 +94,33 @@ p := &Person{
|
||||
Add("a.", i).
|
||||
Add("b.", i).
|
||||
ToCql()
|
||||
q := gocqlx.Query(session.Query(stmt), names)
|
||||
|
||||
b := struct {
|
||||
batch := struct {
|
||||
A Person
|
||||
B Person
|
||||
}{
|
||||
A: Person{
|
||||
"Igy",
|
||||
"Citizen",
|
||||
[]string{"ian.citzen@gocqlx_test.com"},
|
||||
[]string{"igy.citzen@gocqlx_test.com"},
|
||||
},
|
||||
B: Person{
|
||||
"Ian",
|
||||
"Citizen",
|
||||
[]string{"igy.citzen@gocqlx_test.com"},
|
||||
[]string{"ian.citzen@gocqlx_test.com"},
|
||||
},
|
||||
}
|
||||
|
||||
err := q.BindStruct(&b).ExecRelease()
|
||||
if err != nil {
|
||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(&batch)
|
||||
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
See more examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
|
||||
|
||||
## Performance
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -40,35 +40,36 @@ func TestExample(t *testing.T) {
|
||||
[]string{"patricia.citzen@gocqlx_test.com"},
|
||||
}
|
||||
|
||||
// Insert
|
||||
// Insert with query parameters bound from struct.
|
||||
{
|
||||
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 {
|
||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Insert with TTL
|
||||
// Insert with query parameters bound from struct extended with a map.
|
||||
{
|
||||
stmt, names := qb.Insert("gocqlx_test.person").
|
||||
Columns("first_name", "last_name", "email").
|
||||
TTL().
|
||||
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),
|
||||
}).ExecRelease()
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Update
|
||||
// Easy update with all parameters bound from struct.
|
||||
{
|
||||
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")).
|
||||
ToCql()
|
||||
|
||||
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
|
||||
if err != nil {
|
||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
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")
|
||||
|
||||
@@ -91,36 +93,42 @@ func TestExample(t *testing.T) {
|
||||
Add("a.", i).
|
||||
Add("b.", i).
|
||||
ToCql()
|
||||
q := gocqlx.Query(session.Query(stmt), names)
|
||||
|
||||
b := struct {
|
||||
batch := struct {
|
||||
A Person
|
||||
B Person
|
||||
}{
|
||||
A: Person{
|
||||
"Igy",
|
||||
"Citizen",
|
||||
[]string{"ian.citzen@gocqlx_test.com"},
|
||||
[]string{"igy.citzen@gocqlx_test.com"},
|
||||
},
|
||||
B: Person{
|
||||
"Ian",
|
||||
"Citizen",
|
||||
[]string{"igy.citzen@gocqlx_test.com"},
|
||||
[]string{"ian.citzen@gocqlx_test.com"},
|
||||
},
|
||||
}
|
||||
|
||||
err := q.BindStruct(&b).ExecRelease()
|
||||
if err != nil {
|
||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(&batch)
|
||||
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
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
|
||||
if err := gocqlx.Get(&p, q); err != nil {
|
||||
if err := gocqlx.Get(&p, q.Query); err != nil {
|
||||
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]}
|
||||
}
|
||||
|
||||
// Select
|
||||
// Select, load all results into a slice.
|
||||
{
|
||||
stmt, names := qb.Select("gocqlx_test.person").
|
||||
Where(qb.In("first_name")).
|
||||
@@ -144,19 +152,34 @@ func TestExample(t *testing.T) {
|
||||
}
|
||||
|
||||
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
|
||||
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("compile:", err)
|
||||
p := &Person{
|
||||
"Ian",
|
||||
"Citizen",
|
||||
[]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{
|
||||
"Jane",
|
||||
@@ -164,22 +187,15 @@ func TestExample(t *testing.T) {
|
||||
[]string{"jane.citzen@gocqlx_test.com"},
|
||||
}
|
||||
|
||||
if err := q.BindStruct(p).Exec(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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("compile:", err)
|
||||
}
|
||||
|
||||
// bind named parameters from a map
|
||||
{
|
||||
m := map[string]interface{}{
|
||||
"first_name": "Bin",
|
||||
"last_name": "Smuth",
|
||||
"email": []string{"bensmith@allblacks.nz"},
|
||||
}
|
||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
||||
|
||||
if err := q.BindMap(m).Exec(); err != nil {
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user