aboutsummaryrefslogtreecommitdiff
path: root/internal/sqlutil
diff options
context:
space:
mode:
authorKegan Dougal <kegan@matrix.org>2021-07-28 18:30:04 +0100
committerKegan Dougal <kegan@matrix.org>2021-07-28 18:30:04 +0100
commited4097825bc65f2332bcdc975ed201841221ff7c (patch)
tree4ac6270fa282b1d13ac73c97e4078f1961dbdf4e /internal/sqlutil
parent9e4618000e0347741eac1279bf6c94c3b9980785 (diff)
Factor out StatementList to `sqlutil` and use it in `userapi`
It helps with the boilerplate.
Diffstat (limited to 'internal/sqlutil')
-rw-r--r--internal/sqlutil/sql.go16
1 files changed, 16 insertions, 0 deletions
diff --git a/internal/sqlutil/sql.go b/internal/sqlutil/sql.go
index 6cf17bea..98cc396a 100644
--- a/internal/sqlutil/sql.go
+++ b/internal/sqlutil/sql.go
@@ -152,3 +152,19 @@ func RunLimitedVariablesQuery(ctx context.Context, query string, qp QueryProvide
}
return nil
}
+
+// StatementList is a list of SQL statements to prepare and a pointer to where to store the resulting prepared statement.
+type StatementList []struct {
+ Statement **sql.Stmt
+ SQL string
+}
+
+// Prepare the SQL for each statement in the list and assign the result to the prepared statement.
+func (s StatementList) Prepare(db *sql.DB) (err error) {
+ for _, statement := range s {
+ if *statement.Statement, err = db.Prepare(statement.SQL); err != nil {
+ return
+ }
+ }
+ return
+}