BindStruct, BindStructMap, BindStructMap returns *Queryx
This commit is contained in:
46
README.md
46
README.md
@@ -30,41 +30,53 @@ p := &Person{
|
|||||||
|
|
||||||
// Insert
|
// Insert
|
||||||
{
|
{
|
||||||
q := Query(qb.Insert("person").Columns("first_name", "last_name", "email").ToCql())
|
stmt, names := qb.Insert("person").Columns("first_name", "last_name", "email").ToCql()
|
||||||
if err := q.BindStruct(p); err != nil {
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
t.Fatal("bind:", err)
|
|
||||||
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert with TTL
|
||||||
|
{
|
||||||
|
stmt, names := qb.Insert("person").Columns("first_name", "last_name", "email").TTL().ToCql()
|
||||||
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
|
|
||||||
|
if err := q.BindStructMap(p, qb.M{"_ttl": qb.TTL(86400 * time.Second)}).Exec(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
{
|
{
|
||||||
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
||||||
|
|
||||||
q := Query(qb.Update("person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql())
|
stmt, names := qb.Update("person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql()
|
||||||
if err := q.BindStruct(p); err != nil {
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
t.Fatal("bind:", err)
|
|
||||||
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
{
|
{
|
||||||
q := Query(qb.Select("person").Where(qb.In("first_name")).ToCql())
|
stmt, names := qb.Select("person").Where(qb.In("first_name")).ToCql()
|
||||||
m := map[string]interface{}{
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
"first_name": []string{"Patricia", "John"},
|
|
||||||
}
|
q.BindMap(qb.M{"first_name": []string{"Patricia", "John"}})
|
||||||
if err := q.BindMap(m); err != nil {
|
if err := q.Err(); err != nil {
|
||||||
t.Fatal("bind:", err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var people []Person
|
var people []Person
|
||||||
if err := gocqlx.Select(&people, q.Query); err != nil {
|
if err := gocqlx.Select(&people, q.Query); err != nil {
|
||||||
t.Fatal(err)
|
log.Fatal("select:", err)
|
||||||
}
|
}
|
||||||
t.Log(people)
|
log.Println(people)
|
||||||
|
|
||||||
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {John Doe [johndoeDNE@gmail.net]}]
|
// [{Patricia Citizen [patricia.citzen@com patricia1.citzen@com]} {John Doe [johndoeDNE@gmail.net]}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -102,13 +102,8 @@ func BenchmarkE2EGocqlxInsert(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
// prepare
|
|
||||||
p := people[i%len(people)]
|
p := people[i%len(people)]
|
||||||
if err := q.BindStruct(p); err != nil {
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
b.Fatal("bind:", err)
|
|
||||||
}
|
|
||||||
// insert
|
|
||||||
if err := q.Exec(); err != nil {
|
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -229,10 +224,7 @@ func initTable(b *testing.B, session *gocql.Session, people []*benchPerson) {
|
|||||||
q := gocqlx.Query(session.Query(stmt), names)
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
|
|
||||||
for _, p := range people {
|
for _, p := range people {
|
||||||
if err := q.BindStruct(p); err != nil {
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := q.Exec(); err != nil {
|
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,9 +117,6 @@ func TestExample(t *testing.T) {
|
|||||||
|
|
||||||
// Query builder, using DSL to build queries, using `:name` as the bindvar.
|
// Query builder, using DSL to build queries, using `:name` as the bindvar.
|
||||||
{
|
{
|
||||||
// helper function for creating session queries
|
|
||||||
Query := gocqlx.SessionQuery(session)
|
|
||||||
|
|
||||||
p := &Person{
|
p := &Person{
|
||||||
"Patricia",
|
"Patricia",
|
||||||
"Citizen",
|
"Citizen",
|
||||||
@@ -128,43 +125,44 @@ func TestExample(t *testing.T) {
|
|||||||
|
|
||||||
// Insert
|
// Insert
|
||||||
{
|
{
|
||||||
q := Query(qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql())
|
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql()
|
||||||
if err := q.BindStruct(p); err != nil {
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
t.Fatal("bind:", err)
|
|
||||||
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert with TTL
|
// Insert with TTL
|
||||||
{
|
{
|
||||||
q := Query(qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").TTL().ToCql())
|
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").TTL().ToCql()
|
||||||
if err := q.BindStructMap(p, map[string]interface{}{
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
"_ttl": qb.TTL(86400 * time.Second),
|
|
||||||
}); err != nil {
|
if err := q.BindStructMap(p, qb.M{"_ttl": qb.TTL(86400 * time.Second)}).Exec(); err != nil {
|
||||||
t.Fatal("bind:", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
{
|
{
|
||||||
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
||||||
|
|
||||||
q := Query(qb.Update("gocqlx_test.person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql())
|
stmt, names := qb.Update("gocqlx_test.person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql()
|
||||||
if err := q.BindStruct(p); err != nil {
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
t.Fatal("bind:", err)
|
|
||||||
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
{
|
{
|
||||||
q := Query(qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql())
|
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
|
||||||
m := map[string]interface{}{
|
q := gocqlx.Query(session.Query(stmt), names)
|
||||||
"first_name": []string{"Patricia", "John"},
|
|
||||||
}
|
q.BindMap(qb.M{"first_name": []string{"Patricia", "John"}})
|
||||||
if err := q.BindMap(m); err != nil {
|
if err := q.Err(); err != nil {
|
||||||
t.Fatal("bind:", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var people []Person
|
var people []Person
|
||||||
@@ -194,10 +192,9 @@ func TestExample(t *testing.T) {
|
|||||||
[]string{"jane.citzen@gocqlx_test.com"},
|
[]string{"jane.citzen@gocqlx_test.com"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := q.BindStruct(p); err != nil {
|
if err := q.BindStruct(p).Exec(); err != nil {
|
||||||
t.Fatal("bind:", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// bind named parameters from a map
|
// bind named parameters from a map
|
||||||
@@ -208,10 +205,9 @@ func TestExample(t *testing.T) {
|
|||||||
"email": []string{"bensmith@allblacks.nz"},
|
"email": []string{"bensmith@allblacks.nz"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := q.BindMap(m); err != nil {
|
if err := q.BindMap(m).Exec(); err != nil {
|
||||||
t.Fatal("bind:", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mustExec(q.Query)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
qb/qb.go
3
qb/qb.go
@@ -5,3 +5,6 @@ type Builder interface {
|
|||||||
// ToCql builds the query into a CQL string and named args.
|
// ToCql builds the query into a CQL string and named args.
|
||||||
ToCql() (stmt string, names []string)
|
ToCql() (stmt string, names []string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// M is a map.
|
||||||
|
type M map[string]interface{}
|
||||||
|
|||||||
47
queryx.go
47
queryx.go
@@ -80,6 +80,7 @@ type Queryx struct {
|
|||||||
*gocql.Query
|
*gocql.Query
|
||||||
Names []string
|
Names []string
|
||||||
Mapper *reflectx.Mapper
|
Mapper *reflectx.Mapper
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query creates a new Queryx from gocql.Query using a default mapper.
|
// Query creates a new Queryx from gocql.Query using a default mapper.
|
||||||
@@ -93,29 +94,31 @@ func Query(q *gocql.Query, names []string) Queryx {
|
|||||||
|
|
||||||
// BindStruct binds query named parameters to values from arg using mapper. If
|
// BindStruct binds query named parameters to values from arg using mapper. If
|
||||||
// value cannot be found error is reported.
|
// value cannot be found error is reported.
|
||||||
func (q Queryx) BindStruct(arg interface{}) error {
|
func (q *Queryx) BindStruct(arg interface{}) *Queryx {
|
||||||
arglist, err := bindStructArgs(q.Names, arg, nil, q.Mapper)
|
arglist, err := bindStructArgs(q.Names, arg, nil, q.Mapper)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
q.err = fmt.Errorf("bind error: %s", err)
|
||||||
|
} else {
|
||||||
|
q.err = nil
|
||||||
|
q.Bind(arglist...)
|
||||||
}
|
}
|
||||||
|
|
||||||
q.Bind(arglist...)
|
return q
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindStructMap binds query named parameters to values from arg0 and arg1
|
// BindStructMap binds query named parameters to values from arg0 and arg1
|
||||||
// using a mapper. If value cannot be found in arg0 it's looked up in arg1
|
// using a mapper. If value cannot be found in arg0 it's looked up in arg1
|
||||||
// before reporting an error.
|
// before reporting an error.
|
||||||
func (q Queryx) BindStructMap(arg0 interface{}, arg1 map[string]interface{}) error {
|
func (q *Queryx) BindStructMap(arg0 interface{}, arg1 map[string]interface{}) *Queryx {
|
||||||
arglist, err := bindStructArgs(q.Names, arg0, arg1, q.Mapper)
|
arglist, err := bindStructArgs(q.Names, arg0, arg1, q.Mapper)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
q.err = fmt.Errorf("bind error: %s", err)
|
||||||
|
} else {
|
||||||
|
q.err = nil
|
||||||
|
q.Bind(arglist...)
|
||||||
}
|
}
|
||||||
|
|
||||||
q.Bind(arglist...)
|
return q
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func bindStructArgs(names []string, arg0 interface{}, arg1 map[string]interface{}, m *reflectx.Mapper) ([]interface{}, error) {
|
func bindStructArgs(names []string, arg0 interface{}, arg1 map[string]interface{}, m *reflectx.Mapper) ([]interface{}, error) {
|
||||||
@@ -145,15 +148,16 @@ func bindStructArgs(names []string, arg0 interface{}, arg1 map[string]interface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BindMap binds query named parameters using map.
|
// BindMap binds query named parameters using map.
|
||||||
func (q Queryx) BindMap(arg map[string]interface{}) error {
|
func (q *Queryx) BindMap(arg map[string]interface{}) *Queryx {
|
||||||
arglist, err := bindMapArgs(q.Names, arg)
|
arglist, err := bindMapArgs(q.Names, arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
q.err = fmt.Errorf("bind error: %s", err)
|
||||||
|
} else {
|
||||||
|
q.err = nil
|
||||||
|
q.Bind(arglist...)
|
||||||
}
|
}
|
||||||
|
|
||||||
q.Bind(arglist...)
|
return q
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, error) {
|
func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, error) {
|
||||||
@@ -169,6 +173,19 @@ func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, err
|
|||||||
return arglist, nil
|
return arglist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Err returns any binding errors.
|
||||||
|
func (q *Queryx) Err() error {
|
||||||
|
return q.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exec executes the query without returning any rows.
|
||||||
|
func (q *Queryx) Exec() error {
|
||||||
|
if q.err != nil {
|
||||||
|
return q.err
|
||||||
|
}
|
||||||
|
return q.Query.Exec()
|
||||||
|
}
|
||||||
|
|
||||||
// QueryFunc creates Queryx from qb.Builder.ToCql() output.
|
// QueryFunc creates Queryx from qb.Builder.ToCql() output.
|
||||||
type QueryFunc func(stmt string, names []string) Queryx
|
type QueryFunc func(stmt string, names []string) Queryx
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user