Merge pull request #12 from scylladb/mmt/query_prt
query: Query return ptr
This commit is contained in:
71
README.md
71
README.md
@@ -36,17 +36,49 @@ p := &Person{
|
|||||||
|
|
||||||
// Insert
|
// Insert
|
||||||
{
|
{
|
||||||
stmt, names := qb.Insert("person").Columns("first_name", "last_name", "email").ToCql()
|
stmt, names := qb.Insert("gocqlx_test.person").
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
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()
|
||||||
log.Fatal(err)
|
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
|
// 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().
|
stmt, names := qb.Batch().
|
||||||
Add("a.", i).
|
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)
|
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
|
## Performance
|
||||||
|
|||||||
@@ -42,20 +42,28 @@ func TestExample(t *testing.T) {
|
|||||||
|
|
||||||
// Insert
|
// Insert
|
||||||
{
|
{
|
||||||
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql()
|
stmt, names := qb.Insert("gocqlx_test.person").
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert with TTL
|
// Insert with TTL
|
||||||
{
|
{
|
||||||
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").TTL().ToCql()
|
stmt, names := qb.Insert("gocqlx_test.person").
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,10 +72,13 @@ func TestExample(t *testing.T) {
|
|||||||
{
|
{
|
||||||
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
|
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()
|
stmt, names := qb.Update("gocqlx_test.person").
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
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)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get
|
// Get
|
||||||
{
|
{
|
||||||
|
q := session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")
|
||||||
|
|
||||||
var p Person
|
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.Fatal("get:", err)
|
||||||
}
|
}
|
||||||
t.Log(p)
|
|
||||||
|
|
||||||
|
t.Log(p)
|
||||||
// {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
|
||||||
{
|
{
|
||||||
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
|
stmt, names := qb.Select("gocqlx_test.person").
|
||||||
q := gocqlx.Query(session.Query(stmt), names)
|
Where(qb.In("first_name")).
|
||||||
|
ToCql()
|
||||||
|
|
||||||
q.BindMap(qb.M{"first_name": []string{"Patricia", "Igy", "Ian"}})
|
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
|
||||||
if err := q.Err(); err != nil {
|
"first_name": []string{"Patricia", "Igy", "Ian"},
|
||||||
t.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("select:", err)
|
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]}]
|
// [{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]}]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
queryx.go
14
queryx.go
@@ -84,8 +84,8 @@ type Queryx struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Query creates a new Queryx from gocql.Query using a default mapper.
|
// Query creates a new Queryx from gocql.Query using a default mapper.
|
||||||
func Query(q *gocql.Query, names []string) Queryx {
|
func Query(q *gocql.Query, names []string) *Queryx {
|
||||||
return Queryx{
|
return &Queryx{
|
||||||
Query: q,
|
Query: q,
|
||||||
Names: names,
|
Names: names,
|
||||||
Mapper: DefaultMapper,
|
Mapper: DefaultMapper,
|
||||||
@@ -192,13 +192,3 @@ func (q *Queryx) ExecRelease() error {
|
|||||||
defer q.Release()
|
defer q.Release()
|
||||||
return q.Exec()
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user