Merge pull request #12 from scylladb/mmt/query_prt

query: Query return ptr
This commit is contained in:
Michał Matczuk
2017-08-24 12:47:08 +02:00
committed by GitHub
3 changed files with 93 additions and 82 deletions

View File

@@ -36,17 +36,49 @@ p := &Person{
// Insert
{
stmt, names := qb.Insert("person").Columns("first_name", "last_name", "email").ToCql()
q := gocqlx.Query(session.Query(stmt), names)
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
ToCql()
if err := q.BindStruct(p).Exec(); err != nil {
log.Fatal(err)
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
// Get
{
q := session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")
var p Person
if err := gocqlx.Get(&p, q); err != nil {
t.Fatal("get:", err)
}
t.Log(p)
}
// Select
{
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)
// [{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]}]
}
// Batch
{
i := qb.Insert("person").Columns("first_name", "last_name", "email")
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
stmt, names := qb.Batch().
Add("a.", i).
@@ -70,36 +102,11 @@ p := &Person{
},
}
if err := q.BindStruct(&b).Exec(); err != nil {
err := q.BindStruct(&b).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
// Get
{
var p Person
if err := gocqlx.Get(&p, session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")); err != nil {
t.Fatal("get:", err)
}
t.Log(p) // {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
}
// Select
{
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
q := gocqlx.Query(session.Query(stmt), names)
q.BindMap(qb.M{"first_name": []string{"Patricia", "Igy", "Ian"}})
if err := q.Err(); err != nil {
t.Fatal(err)
}
var people []Person
if err := gocqlx.Select(&people, q.Query); err != nil {
t.Fatal("select:", err)
}
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]}]
}
```
## Performance

View File

@@ -42,20 +42,28 @@ func TestExample(t *testing.T) {
// Insert
{
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql()
q := gocqlx.Query(session.Query(stmt), names)
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
ToCql()
if err := q.BindStruct(p).Exec(); err != nil {
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
// Insert with TTL
{
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").TTL().ToCql()
q := gocqlx.Query(session.Query(stmt), names)
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
TTL().
ToCql()
if err := q.BindStructMap(p, qb.M{"_ttl": qb.TTL(86400 * time.Second)}).Exec(); err != nil {
err := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
"_ttl": qb.TTL(86400 * time.Second),
}).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
@@ -64,10 +72,13 @@ func TestExample(t *testing.T) {
{
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
stmt, names := qb.Update("gocqlx_test.person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql()
q := gocqlx.Query(session.Query(stmt), names)
stmt, names := qb.Update("gocqlx_test.person").
Set("email").
Where(qb.Eq("first_name"), qb.Eq("last_name")).
ToCql()
if err := q.BindStruct(p).Exec(); err != nil {
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
@@ -98,38 +109,41 @@ func TestExample(t *testing.T) {
},
}
if err := q.BindStruct(&b).Exec(); err != nil {
err := q.BindStruct(&b).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
// Get
{
q := session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")
var p Person
if err := gocqlx.Get(&p, session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")); err != nil {
if err := gocqlx.Get(&p, q); err != nil {
t.Fatal("get:", err)
}
t.Log(p)
t.Log(p)
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
}
// Select
{
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
q := gocqlx.Query(session.Query(stmt), names)
stmt, names := qb.Select("gocqlx_test.person").
Where(qb.In("first_name")).
ToCql()
q.BindMap(qb.M{"first_name": []string{"Patricia", "Igy", "Ian"}})
if err := q.Err(); err != nil {
t.Fatal(err)
}
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)
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]}]
}

View File

@@ -84,8 +84,8 @@ type Queryx struct {
}
// Query creates a new Queryx from gocql.Query using a default mapper.
func Query(q *gocql.Query, names []string) Queryx {
return Queryx{
func Query(q *gocql.Query, names []string) *Queryx {
return &Queryx{
Query: q,
Names: names,
Mapper: DefaultMapper,
@@ -192,13 +192,3 @@ func (q *Queryx) ExecRelease() error {
defer q.Release()
return q.Exec()
}
// QueryFunc creates Queryx from qb.Builder.ToCql() output.
type QueryFunc func(stmt string, names []string) Queryx
// SessionQuery creates QueryFunc that's session aware.
func SessionQuery(session *gocql.Session) QueryFunc {
return func(stmt string, names []string) Queryx {
return Query(session.Query(stmt), names)
}
}