-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathreadonly.go
More file actions
287 lines (246 loc) · 8.81 KB
/
readonly.go
File metadata and controls
287 lines (246 loc) · 8.81 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package filesql
import (
"context"
"database/sql"
"errors"
"strings"
)
// ErrReadOnly is returned when a write operation is attempted on a read-only database.
var ErrReadOnly = errors.New("database is read-only: write operations are not allowed")
// ReadOnlyDB wraps a *sql.DB to prevent write operations.
// All SELECT queries work normally, but INSERT, UPDATE, DELETE, DROP, ALTER, and CREATE
// statements are rejected with ErrReadOnly.
//
// This is useful for audit scenarios where you want to view data without risk of modification.
//
// Example:
//
// db, err := filesql.Open("payment.ach")
// if err != nil {
// return err
// }
// defer db.Close()
//
// rodb := filesql.NewReadOnlyDB(db)
//
// // SELECT works fine
// rows, err := rodb.Query("SELECT * FROM payment_entries")
//
// // UPDATE/DELETE/INSERT are rejected
// _, err = rodb.Exec("DELETE FROM payment_entries") // returns ErrReadOnly
type ReadOnlyDB struct {
db *sql.DB
}
// NewReadOnlyDB creates a read-only wrapper around an existing database connection.
// The underlying database is not modified; write operations are simply rejected at the API level.
func NewReadOnlyDB(db *sql.DB) *ReadOnlyDB {
return &ReadOnlyDB{db: db}
}
// isWriteStatement checks if the SQL statement is a write operation.
func isWriteStatement(query string) bool {
// Normalize: trim whitespace and convert to uppercase for comparison
normalized := strings.ToUpper(strings.TrimSpace(query))
// Check for write operation keywords at the start of the statement
writeKeywords := []string{
"INSERT",
"UPDATE",
"DELETE",
"DROP",
"ALTER",
"CREATE",
"TRUNCATE",
"REPLACE",
"UPSERT",
}
for _, keyword := range writeKeywords {
if strings.HasPrefix(normalized, keyword) {
return true
}
}
return false
}
// QueryContext executes a query that returns rows with context.
func (r *ReadOnlyDB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return r.db.QueryContext(ctx, query, args...)
}
// Query executes a query that returns rows (SELECT statements).
// Deprecated: Use QueryContext instead.
func (r *ReadOnlyDB) Query(query string, args ...any) (*sql.Rows, error) {
return r.db.QueryContext(context.Background(), query, args...)
}
// QueryRowContext executes a query that returns at most one row with context.
func (r *ReadOnlyDB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
return r.db.QueryRowContext(ctx, query, args...)
}
// QueryRow executes a query that returns at most one row.
// Deprecated: Use QueryRowContext instead.
func (r *ReadOnlyDB) QueryRow(query string, args ...any) *sql.Row {
return r.db.QueryRowContext(context.Background(), query, args...)
}
// ExecContext rejects write operations and returns ErrReadOnly.
// For read-only databases, use Query methods instead.
func (r *ReadOnlyDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
if isWriteStatement(query) {
return nil, ErrReadOnly
}
return r.db.ExecContext(ctx, query, args...)
}
// Exec rejects write operations and returns ErrReadOnly.
// For read-only databases, use Query methods instead.
// Deprecated: Use ExecContext instead.
func (r *ReadOnlyDB) Exec(query string, args ...any) (sql.Result, error) {
if isWriteStatement(query) {
return nil, ErrReadOnly
}
return r.db.ExecContext(context.Background(), query, args...)
}
// PrepareContext creates a prepared statement with context.
func (r *ReadOnlyDB) PrepareContext(ctx context.Context, query string) (*ReadOnlyStmt, error) {
if isWriteStatement(query) {
return nil, ErrReadOnly
}
stmt, err := r.db.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &ReadOnlyStmt{stmt: stmt, isWrite: isWriteStatement(query)}, nil
}
// Prepare creates a prepared statement.
// Deprecated: Use PrepareContext instead.
func (r *ReadOnlyDB) Prepare(query string) (*ReadOnlyStmt, error) {
return r.PrepareContext(context.Background(), query)
}
// BeginTx starts a read-only transaction with context and options.
func (r *ReadOnlyDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*ReadOnlyTx, error) {
tx, err := r.db.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &ReadOnlyTx{tx: tx}, nil
}
// Begin starts a read-only transaction.
// Deprecated: Use BeginTx instead.
func (r *ReadOnlyDB) Begin() (*ReadOnlyTx, error) {
return r.BeginTx(context.Background(), nil)
}
// Close closes the underlying database connection.
func (r *ReadOnlyDB) Close() error {
return r.db.Close()
}
// PingContext verifies the connection to the database with context.
func (r *ReadOnlyDB) PingContext(ctx context.Context) error {
return r.db.PingContext(ctx)
}
// Ping verifies the connection to the database.
// Deprecated: Use PingContext instead.
func (r *ReadOnlyDB) Ping() error {
return r.db.PingContext(context.Background())
}
// DB returns the underlying *sql.DB.
// Use with caution as this bypasses read-only protection.
func (r *ReadOnlyDB) DB() *sql.DB {
return r.db
}
// ReadOnlyStmt wraps a *sql.Stmt to enforce read-only operations.
type ReadOnlyStmt struct {
stmt *sql.Stmt
isWrite bool
}
// Query executes a prepared query statement.
// Deprecated: Use QueryContext instead.
func (s *ReadOnlyStmt) Query(args ...any) (*sql.Rows, error) {
return s.stmt.QueryContext(context.Background(), args...)
}
// QueryContext executes a prepared query statement with context.
func (s *ReadOnlyStmt) QueryContext(ctx context.Context, args ...any) (*sql.Rows, error) {
return s.stmt.QueryContext(ctx, args...)
}
// QueryRow executes a prepared query statement that returns at most one row.
// Deprecated: Use QueryRowContext instead.
func (s *ReadOnlyStmt) QueryRow(args ...any) *sql.Row {
return s.stmt.QueryRowContext(context.Background(), args...)
}
// QueryRowContext executes a prepared query statement that returns at most one row with context.
func (s *ReadOnlyStmt) QueryRowContext(ctx context.Context, args ...any) *sql.Row {
return s.stmt.QueryRowContext(ctx, args...)
}
// Exec is not allowed for read-only statements.
// Deprecated: Use ExecContext instead.
func (s *ReadOnlyStmt) Exec(args ...any) (sql.Result, error) {
if s.isWrite {
return nil, ErrReadOnly
}
return s.stmt.ExecContext(context.Background(), args...)
}
// ExecContext is not allowed for read-only statements.
func (s *ReadOnlyStmt) ExecContext(ctx context.Context, args ...any) (sql.Result, error) {
if s.isWrite {
return nil, ErrReadOnly
}
return s.stmt.ExecContext(ctx, args...)
}
// Close closes the statement.
func (s *ReadOnlyStmt) Close() error {
return s.stmt.Close()
}
// ReadOnlyTx wraps a *sql.Tx to enforce read-only operations.
type ReadOnlyTx struct {
tx *sql.Tx
}
// Query executes a query that returns rows.
// Deprecated: Use QueryContext instead.
func (t *ReadOnlyTx) Query(query string, args ...any) (*sql.Rows, error) {
return t.tx.QueryContext(context.Background(), query, args...)
}
// QueryContext executes a query that returns rows with context.
func (t *ReadOnlyTx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return t.tx.QueryContext(ctx, query, args...)
}
// QueryRow executes a query that returns at most one row.
// Deprecated: Use QueryRowContext instead.
func (t *ReadOnlyTx) QueryRow(query string, args ...any) *sql.Row {
return t.tx.QueryRowContext(context.Background(), query, args...)
}
// QueryRowContext executes a query that returns at most one row with context.
func (t *ReadOnlyTx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
return t.tx.QueryRowContext(ctx, query, args...)
}
// Exec rejects write operations.
// Deprecated: Use ExecContext instead.
func (t *ReadOnlyTx) Exec(query string, args ...any) (sql.Result, error) {
if isWriteStatement(query) {
return nil, ErrReadOnly
}
return t.tx.ExecContext(context.Background(), query, args...)
}
// ExecContext rejects write operations.
func (t *ReadOnlyTx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
if isWriteStatement(query) {
return nil, ErrReadOnly
}
return t.tx.ExecContext(ctx, query, args...)
}
// Commit commits the transaction.
func (t *ReadOnlyTx) Commit() error {
return t.tx.Commit()
}
// Rollback aborts the transaction.
func (t *ReadOnlyTx) Rollback() error {
return t.tx.Rollback()
}
// Prepare creates a prepared statement within the transaction.
// Deprecated: Use PrepareContext instead.
func (t *ReadOnlyTx) Prepare(query string) (*ReadOnlyStmt, error) {
return t.PrepareContext(context.Background(), query)
}
// PrepareContext creates a prepared statement within the transaction with context.
func (t *ReadOnlyTx) PrepareContext(ctx context.Context, query string) (*ReadOnlyStmt, error) {
if isWriteStatement(query) {
return nil, ErrReadOnly
}
stmt, err := t.tx.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &ReadOnlyStmt{stmt: stmt, isWrite: isWriteStatement(query)}, nil
}