1// Copyright 2017-2018 New Vector Ltd
2// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16package shared
17
18import (
19	"context"
20	"database/sql"
21)
22
23// StatementList is a list of SQL statements to prepare and a pointer to where to store the resulting prepared statement.
24type StatementList []struct {
25	Statement **sql.Stmt
26	SQL       string
27}
28
29// Prepare the SQL for each statement in the list and assign the result to the prepared statement.
30func (s StatementList) Prepare(db *sql.DB) (err error) {
31	for _, statement := range s {
32		if *statement.Statement, err = db.Prepare(statement.SQL); err != nil {
33			return
34		}
35	}
36	return
37}
38
39type transaction struct {
40	ctx context.Context
41	txn *sql.Tx
42}
43
44// Commit implements types.Transaction
45func (t *transaction) Commit() error {
46	if t.txn == nil {
47		// The Updater structs can operate in useTxns=false mode. The code will still call this though.
48		return nil
49	}
50	return t.txn.Commit()
51}
52
53// Rollback implements types.Transaction
54func (t *transaction) Rollback() error {
55	if t.txn == nil {
56		// The Updater structs can operate in useTxns=false mode. The code will still call this though.
57		return nil
58	}
59	return t.txn.Rollback()
60}
61