aboutsummaryrefslogtreecommitdiff
path: root/query.go
diff options
context:
space:
mode:
Diffstat (limited to 'query.go')
-rw-r--r--query.go17
1 files changed, 10 insertions, 7 deletions
diff --git a/query.go b/query.go
index e9d6862..6e375e4 100644
--- a/query.go
+++ b/query.go
@@ -28,8 +28,11 @@ import (
var PrepareThreshold int = 8
// prepare behaves like [Handle.Prepare].
-func prepare(ctx context.Context, db gsql.Handle, query, operation string, inputSize int) (gsql.Statement, error) {
+func prepare(ctx context.Context, db gsql.Handle, readOnly bool, query, operation string, inputSize int) (gsql.Statement, error) {
if query == "" {
+ if readOnly {
+ return nil, fmt.Errorf("cannot execute %s() because query planning used the ReadOnly() option", operation)
+ }
return nil, fmt.Errorf("cannot execute %s() because query could not be autogenerated", operation)
}
@@ -51,7 +54,7 @@ func (s Store[R]) Insert(ctx context.Context, db gsql.Handle, records ...*R) err
// NOTE: This function body should be as short as possible to reduce the binary size after monomorphization.
// Any expression that does not depend on type R should be factored out into a reusable function.
- stmt, err := prepare(ctx, db, s.plan.Insert.Query, "Insert", len(records))
+ stmt, err := prepare(ctx, db, s.plan.ReadOnly, s.plan.Insert.Query, "Insert", len(records))
if err != nil {
return err
}
@@ -159,7 +162,7 @@ func (s Store[R]) Update(ctx context.Context, db gsql.Handle, records ...R) erro
// NOTE: This function body should be as short as possible to reduce the binary size after monomorphization.
// Any expression that does not depend on type R should be factored out into a reusable function.
- stmt, err := prepare(ctx, db, s.plan.Update.Query, "Update", len(records))
+ stmt, err := prepare(ctx, db, s.plan.ReadOnly, s.plan.Update.Query, "Update", len(records))
if err != nil {
return err
}
@@ -208,7 +211,7 @@ func (s Store[R]) Delete(ctx context.Context, db gsql.Handle, records ...R) erro
// NOTE: This function body should be as short as possible to reduce the binary size after monomorphization.
// Any expression that does not depend on type R should be factored out into a reusable function.
- stmt, err := prepare(ctx, db, s.plan.Delete.Query, "Delete", len(records))
+ stmt, err := prepare(ctx, db, s.plan.ReadOnly, s.plan.Delete.Query, "Delete", len(records))
if err != nil {
return err
}
@@ -257,7 +260,7 @@ func (s Store[R]) Upsert(ctx context.Context, db gsql.Handle, records ...*R) err
// Any expression that does not depend on type R should be factored out into a reusable function.
if len(s.plan.AutoColumnNames) == 0 {
- stmt, err := prepare(ctx, db, s.plan.Upsert.Query, "Upsert", len(records))
+ stmt, err := prepare(ctx, db, s.plan.ReadOnly, s.plan.Upsert.Query, "Upsert", len(records))
if err != nil {
return err
}
@@ -265,11 +268,11 @@ func (s Store[R]) Upsert(ctx context.Context, db gsql.Handle, records ...*R) err
}
// TODO: respect PrepareThreshold (or not? may be too much bookkeeping overhead for not a whole lot of benefit)
- insertStmt, err := prepare(ctx, db, s.plan.Insert.Query, "Insert", 0)
+ insertStmt, err := prepare(ctx, db, s.plan.ReadOnly, s.plan.Insert.Query, "Insert", 0)
if err != nil {
return err
}
- updateStmt, err := prepare(ctx, db, s.plan.Update.Query, "Update", 0)
+ updateStmt, err := prepare(ctx, db, s.plan.ReadOnly, s.plan.Update.Query, "Update", 0)
if err != nil {
return errext.WithCleanup(err, "InsertStmt.Close", insertStmt.Close())
}