Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit f394212

Browse files
committed
query: allow general binding using Serializable interface
1 parent 5d5325d commit f394212

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

query.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,31 @@ func (q *Query) checkBounds(pos int) error {
185185
return nil
186186
}
187187

188+
type Serializable interface {
189+
Serialize(*frame.Option) (n int32, bytes []byte, err error)
190+
}
191+
192+
// BindAny allows binding any value to the bind marker at given pos in query,
193+
// it shouldn't be used on non-prepared queries, as it will always result in query execution error later.
194+
func (q *Query) Bind(pos int, v Serializable) *Query {
195+
if q.stmt.Metadata == nil {
196+
q.err = append(q.err, fmt.Errorf("binding any to unprepared queries is not supported"))
197+
return q
198+
}
199+
if err := q.checkBounds(pos); err != nil {
200+
q.err = append(q.err, err)
201+
return q
202+
}
203+
p := &q.stmt.Values[pos]
204+
205+
var err error
206+
p.N, p.Bytes, err = v.Serialize(&q.stmt.Metadata.Columns[pos].Type)
207+
if err != nil {
208+
q.err = append(q.err, err)
209+
}
210+
return q
211+
}
212+
188213
func (q *Query) BindInt64(pos int, v int64) *Query {
189214
if err := q.checkBounds(pos); err != nil {
190215
q.err = append(q.err, err)

0 commit comments

Comments
 (0)