-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
90 lines (72 loc) · 2.44 KB
/
Copy pathtx.go
File metadata and controls
90 lines (72 loc) · 2.44 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
package postgres
import (
"context"
"database/sql"
"github.com/tinywasm/ddl"
"github.com/tinywasm/model"
"github.com/tinywasm/storage"
)
// PostgresTx wraps a SQL transaction.
type PostgresTx struct {
tx *sql.Tx
adapter *PostgresAdapter
}
// Ensure PostgresTx implements ddl.Compiler.
var _ ddl.Compiler = (*PostgresTx)(nil)
// Ensure PostgresTx implements storage.Conn.
var _ storage.Conn = (*PostgresTx)(nil)
// Compile delegates to the PostgresAdapter.
func (p *PostgresTx) Compile(q storage.Query, m model.Model) (storage.Plan, error) {
return p.adapter.Compile(q, m)
}
// CompileDDL delegates to translateDDL.
func (p *PostgresTx) CompileDDL(s ddl.Stmt, m model.Model) (string, []any, error) {
return translateDDL(s, m)
}
// Exec executes a query within the transaction.
func (p *PostgresTx) Exec(query string, args ...any) error {
_, err := p.tx.Exec(query, args...)
return err
}
// QueryRow executes a query that is expected to return at most one row within the transaction.
func (p *PostgresTx) QueryRow(query string, args ...any) storage.Scanner {
return &errScanner{s: p.tx.QueryRow(query, args...)}
}
// Query executes a query that returns rows within the transaction.
func (p *PostgresTx) Query(query string, args ...any) (storage.Rows, error) {
return p.tx.Query(query, args...)
}
// Close is a no-op for storage.Conn in this context.
func (p *PostgresTx) Close() error {
return nil
}
// Commit commits the transaction.
func (p *PostgresTx) Commit() error {
return p.tx.Commit()
}
// Rollback aborts the transaction.
func (p *PostgresTx) Rollback() error {
return p.tx.Rollback()
}
// TableColumns returns the column names for the given table within the transaction.
func (p *PostgresTx) TableColumns(table string) ([]string, error) {
return tableColumns(p, table)
}
// Tables returns all user-defined table names in the current schema.
func (p *PostgresTx) Tables() ([]string, error) {
return tables(p)
}
// Columns returns full column metadata for the given table.
func (p *PostgresTx) Columns(table string) ([]ddl.ColumnInfo, error) {
return columns(p, table)
}
// Ensure PostgresTx implements ddl.SchemaInspector.
var _ ddl.SchemaInspector = (*PostgresTx)(nil)
// BeginTx starts a transaction and returns a new storage.TxBoundExecutor.
func (p *PostgresAdapter) BeginTx() (storage.TxBoundExecutor, error) {
tx, err := p.db.BeginTx(context.Background(), nil)
if err != nil {
return nil, err
}
return &PostgresTx{tx: tx, adapter: p}, nil
}