summaryrefslogtreecommitdiff
path: root/internal/db/sqlite/driver_libsqlite3.go
blob: fe9df7aac83b2be51f6052142dd64cf7efcbe58c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

//go:build !moderncsqlite3 && !nowasm && libsqlite3

package sqlite

import (
	"context"
	"database/sql/driver"

	sqlite3 "github.com/mattn/go-sqlite3"
	"github.com/superseriousbusiness/gotosocial/internal/db"
)

// Driver is our own wrapper around the
// driver.SQLite{} type in order to wrap
// further SQL types with our own
// functionality, e.g. err processing.
type Driver struct{ sqlite3.SQLiteDriver }

func (d *Driver) Open(name string) (driver.Conn, error) {
	conn, err := d.SQLiteDriver.Open(name)
	if err != nil {
		err = processSQLiteError(err)
		return nil, err
	}

	return &sqliteConn{conn.(connIface)}, nil
}

type sqliteConn struct{ connIface }

func (c *sqliteConn) Begin() (driver.Tx, error) {
	return c.BeginTx(context.Background(), driver.TxOptions{})
}

func (c *sqliteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
	tx, err = c.connIface.BeginTx(ctx, opts)
	err = processSQLiteError(err)
	if err != nil {
		return nil, err
	}
	return &sqliteTx{tx}, nil
}

func (c *sqliteConn) Prepare(query string) (driver.Stmt, error) {
	return c.PrepareContext(context.Background(), query)
}

func (c *sqliteConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
	stmt, err = c.connIface.PrepareContext(ctx, query)
	err = processSQLiteError(err)
	if err != nil {
		return nil, err
	}

	return &sqliteStmt{stmtIface: stmt.(stmtIface)}, nil
}

func (c *sqliteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
	return c.ExecContext(context.Background(), query, db.ToNamedValues(args))
}

func (c *sqliteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (res driver.Result, err error) {
	res, err = c.connIface.ExecContext(ctx, query, args)
	err = processSQLiteError(err)
	return
}

func (c *sqliteConn) Close() (err error) {
	// see: https://www.sqlite.org/pragma.html#pragma_optimize
	const onClose = "PRAGMA optimize;"
	_, _ = c.connIface.ExecContext(context.Background(), onClose, nil)

	// Finally, close the conn.
	err = c.connIface.Close()
	return
}

type sqliteTx struct{ driver.Tx }

func (tx *sqliteTx) Commit() (err error) {
	err = tx.Tx.Commit()
	err = processSQLiteError(err)
	return
}

func (tx *sqliteTx) Rollback() (err error) {
	err = tx.Tx.Rollback()
	err = processSQLiteError(err)
	return
}

type sqliteStmt struct{ stmtIface }

func (stmt *sqliteStmt) Exec(args []driver.Value) (driver.Result, error) {
	return stmt.ExecContext(context.Background(), db.ToNamedValues(args))
}

func (stmt *sqliteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
	res, err = stmt.stmtIface.ExecContext(ctx, args)
	err = processSQLiteError(err)
	return
}

func (stmt *sqliteStmt) Query(args []driver.Value) (driver.Rows, error) {
	return stmt.QueryContext(context.Background(), db.ToNamedValues(args))
}

func (stmt *sqliteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
	rows, err = stmt.stmtIface.QueryContext(ctx, args)
	err = processSQLiteError(err)
	if err != nil {
		return nil, err
	}
	return &sqliteRows{rows.(rowsIface)}, nil
}

func (stmt *sqliteStmt) Close() (err error) {
	err = stmt.stmtIface.Close()
	err = processSQLiteError(err)
	return
}

type sqliteRows struct{ rowsIface }

func (r *sqliteRows) Next(dest []driver.Value) (err error) {
	err = r.rowsIface.Next(dest)
	err = processSQLiteError(err)
	return
}

func (r *sqliteRows) Close() (err error) {
	err = r.rowsIface.Close()
	err = processSQLiteError(err)
	return
}

// connIface is the driver.Conn interface
// types (and the like) that go-sqlite3/driver.conn
// conforms to. Useful so you don't need
// to repeatedly perform checks yourself.
type connIface interface {
	driver.Conn
	driver.ConnBeginTx
	driver.ConnPrepareContext
	driver.ExecerContext
}

// StmtIface is the driver.Stmt interface
// types (and the like) that go-sqlite3/driver.stmt
// conforms to. Useful so you don't need
// to repeatedly perform checks yourself.
type stmtIface interface {
	driver.Stmt
	driver.StmtExecContext
	driver.StmtQueryContext
}

// RowsIface is the driver.Rows interface
// types (and the like) that go-sqlite3/driver.rows
// conforms to. Useful so you don't need
// to repeatedly perform checks yourself.
type rowsIface interface {
	driver.Rows
	driver.RowsColumnTypeDatabaseTypeName
	driver.RowsColumnTypeNullable
}