Skip to content

Commit 4ea6f42

Browse files
zimnxmmatczuk
authored andcommitted
Added Context variants of Query functions
1 parent 8af6506 commit 4ea6f42

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

Diff for: qb/batch.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package qb
66

77
import (
88
"bytes"
9+
"context"
910
"fmt"
1011
"time"
1112

@@ -71,6 +72,11 @@ func (b *BatchBuilder) Query(session gocqlx.Session) *gocqlx.Queryx {
7172
return session.Query(b.ToCql())
7273
}
7374

75+
// QueryContext returns query wrapped with context built on top of current BatchBuilder state.
76+
func (b *BatchBuilder) QueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
77+
return b.Query(session).WithContext(ctx)
78+
}
79+
7480
// Add builds the builder and adds the statement to the batch.
7581
func (b *BatchBuilder) Add(builder Builder) *BatchBuilder {
7682
return b.AddStmt(builder.ToCql())

Diff for: qb/delete.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package qb
99

1010
import (
1111
"bytes"
12+
"context"
1213
"time"
1314

1415
"github.com/scylladb/gocqlx/v2"
@@ -61,6 +62,11 @@ func (b *DeleteBuilder) Query(session gocqlx.Session) *gocqlx.Queryx {
6162
return session.Query(b.ToCql())
6263
}
6364

65+
// QueryContext returns query wrapped with context built on top of current DeleteBuilder state.
66+
func (b *DeleteBuilder) QueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
67+
return b.Query(session).WithContext(ctx)
68+
}
69+
6470
// From sets the table to be deleted from.
6571
func (b *DeleteBuilder) From(table string) *DeleteBuilder {
6672
b.table = table

Diff for: qb/insert.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package qb
99

1010
import (
1111
"bytes"
12+
"context"
1213
"time"
1314

1415
"github.com/scylladb/gocqlx/v2"
@@ -85,6 +86,11 @@ func (b *InsertBuilder) Query(session gocqlx.Session) *gocqlx.Queryx {
8586
return session.Query(b.ToCql())
8687
}
8788

89+
// QueryContext returns query wrapped with context built on top of current InsertBuilder state.
90+
func (b *InsertBuilder) QueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
91+
return b.Query(session).WithContext(ctx)
92+
}
93+
8894
// Into sets the INTO clause of the query.
8995
func (b *InsertBuilder) Into(table string) *InsertBuilder {
9096
b.table = table

Diff for: qb/select.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package qb
99

1010
import (
1111
"bytes"
12+
"context"
1213
"fmt"
1314

1415
"github.com/scylladb/gocqlx/v2"
@@ -125,6 +126,11 @@ func (b *SelectBuilder) Query(session gocqlx.Session) *gocqlx.Queryx {
125126
return session.Query(b.ToCql())
126127
}
127128

129+
// QueryContext returns query wrapped with context built on top of current SelectBuilder state.
130+
func (b *SelectBuilder) QueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
131+
return b.Query(session).WithContext(ctx)
132+
}
133+
128134
// From sets the table to be selected from.
129135
func (b *SelectBuilder) From(table string) *SelectBuilder {
130136
b.table = table

Diff for: qb/update.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package qb
99

1010
import (
1111
"bytes"
12+
"context"
1213
"time"
1314

1415
"github.com/scylladb/gocqlx/v2"
@@ -80,6 +81,11 @@ func (b *UpdateBuilder) Query(session gocqlx.Session) *gocqlx.Queryx {
8081
return session.Query(b.ToCql())
8182
}
8283

84+
// QueryContext returns query wrapped with context built on top of current UpdateBuilder state.
85+
func (b *UpdateBuilder) QueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
86+
return b.Query(session).WithContext(ctx)
87+
}
88+
8389
// Table sets the table to be updated.
8490
func (b *UpdateBuilder) Table(table string) *UpdateBuilder {
8591
b.table = table

Diff for: table/table.go

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package table
66

77
import (
8+
"context"
9+
810
"github.com/scylladb/gocqlx/v2"
911
"github.com/scylladb/gocqlx/v2/qb"
1012
)
@@ -95,6 +97,11 @@ func (t *Table) GetQuery(session gocqlx.Session, columns ...string) *gocqlx.Quer
9597
return session.Query(t.Get(columns...))
9698
}
9799

100+
// GetQueryContext returns query wrapped with context which gets by partition key.
101+
func (t *Table) GetQueryContext(ctx context.Context, session gocqlx.Session, columns ...string) *gocqlx.Queryx {
102+
return t.GetQuery(session, columns...).WithContext(ctx)
103+
}
104+
98105
// Select returns select by partition key statement.
99106
func (t *Table) Select(columns ...string) (stmt string, names []string) {
100107
if len(columns) == 0 {
@@ -112,6 +119,11 @@ func (t *Table) SelectQuery(session gocqlx.Session, columns ...string) *gocqlx.Q
112119
return session.Query(t.Select(columns...))
113120
}
114121

122+
// SelectQueryContext returns query wrapped with context which selects by partition key statement.
123+
func (t *Table) SelectQueryContext(ctx context.Context, session gocqlx.Session, columns ...string) *gocqlx.Queryx {
124+
return t.SelectQuery(session, columns...).WithContext(ctx)
125+
}
126+
115127
// SelectBuilder returns a builder initialised to select by partition key
116128
// statement.
117129
func (t *Table) SelectBuilder(columns ...string) *qb.SelectBuilder {
@@ -128,6 +140,11 @@ func (t *Table) InsertQuery(session gocqlx.Session) *gocqlx.Queryx {
128140
return session.Query(t.Insert())
129141
}
130142

143+
// InsertQueryContext returns query wrapped with context which inserts all columns.
144+
func (t *Table) InsertQueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
145+
return t.InsertQuery(session).WithContext(ctx)
146+
}
147+
131148
// Update returns update by primary key statement.
132149
func (t *Table) Update(columns ...string) (stmt string, names []string) {
133150
return t.UpdateBuilder(columns...).ToCql()
@@ -138,6 +155,11 @@ func (t *Table) UpdateQuery(session gocqlx.Session, columns ...string) *gocqlx.Q
138155
return session.Query(t.Update(columns...))
139156
}
140157

158+
// UpdateQueryContext returns query wrapped with context which updates by primary key.
159+
func (t *Table) UpdateQueryContext(ctx context.Context, session gocqlx.Session, columns ...string) *gocqlx.Queryx {
160+
return t.UpdateQuery(session, columns...).WithContext(ctx)
161+
}
162+
141163
// UpdateBuilder returns a builder initialised to update by primary key statement.
142164
func (t *Table) UpdateBuilder(columns ...string) *qb.UpdateBuilder {
143165
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...)
@@ -153,6 +175,11 @@ func (t *Table) DeleteQuery(session gocqlx.Session, columns ...string) *gocqlx.Q
153175
return session.Query(t.Delete(columns...))
154176
}
155177

178+
// DeleteQueryContext returns query wrapped with context which delete by primary key.
179+
func (t *Table) DeleteQueryContext(ctx context.Context, session gocqlx.Session, columns ...string) *gocqlx.Queryx {
180+
return t.DeleteQuery(session, columns...).WithContext(ctx)
181+
}
182+
156183
// DeleteBuilder returns a builder initialised to delete by primary key statement.
157184
func (t *Table) DeleteBuilder(columns ...string) *qb.DeleteBuilder {
158185
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...)

0 commit comments

Comments
 (0)