-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrove.go
More file actions
275 lines (233 loc) · 7.36 KB
/
Copy pathgrove.go
File metadata and controls
275 lines (233 loc) · 7.36 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
package grove
import (
"context"
"fmt"
"sync"
"github.com/xraph/grove/hook"
)
// GroveDriver is the minimal interface that a database driver must satisfy
// to work with the top-level DB handle. This avoids importing the driver
// package directly, which would create a circular dependency
// (schema -> grove -> driver -> schema).
//
// The full driver.Driver interface (in the driver package) extends this
// with query execution methods.
type GroveDriver interface { //nolint:revive // GroveDriver is the established public API name
// Name returns the driver identifier (e.g., "pg", "mysql", "mongo").
Name() string
// Close terminates all connections.
Close() error
// Ping checks connectivity.
Ping(ctx context.Context) error
}
// ModelRegistry is the interface for model metadata caching.
// The concrete implementation is schema.Registry. This interface exists
// to avoid an import cycle between grove and schema packages.
type ModelRegistry interface {
// Register registers a model and returns its cached metadata.
Register(model any) (any, error)
// Get returns cached metadata for a registered model, or nil.
Get(model any) any
}
// DB is the top-level database handle. It manages the connection pool,
// model registry, hook engine, and provides the entry point for all
// database operations.
//
// Create a DB using Open:
//
// pgdb := pgdriver.New()
// pgdb.Open(ctx, "postgres://localhost:5432/mydb")
// db, err := grove.Open(pgdb)
type DB struct {
drv GroveDriver
opts *options
hooks *hook.Engine
mu sync.RWMutex
closed bool
// models tracks registered model types (simple map fallback).
models map[string]any
}
// Open creates a new DB with the given driver. The driver must already be
// connected (call driver.Open before grove.Open).
//
// pgdb := pgdriver.New()
// pgdb.Open(ctx, "postgres://localhost:5432/mydb", driver.WithPoolSize(20))
// db, err := grove.Open(pgdb)
func Open(drv GroveDriver, opts ...Option) (*DB, error) {
o := defaultOptions()
o.apply(opts)
if drv == nil {
return nil, fmt.Errorf("grove: driver must not be nil")
}
db := &DB{
drv: drv,
opts: o,
hooks: hook.NewEngine(),
models: make(map[string]any),
}
// Drivers that support lifecycle hooks receive the DB's engine, so
// db.Hooks().AddHook(...) registrations fire on driver-built queries
// without the caller having to wire the engine manually.
if hookable, ok := drv.(interface{ SetHooks(engine *hook.Engine) }); ok {
hookable.SetHooks(db.hooks)
}
return db, nil
}
// Driver returns the underlying driver.
func (db *DB) Driver() GroveDriver {
return db.drv
}
// Hooks returns the hook engine for registering pre/post query and mutation hooks.
//
// db.Hooks().AddHook(&TenantIsolation{}, hook.Scope{Tables: []string{"users"}})
func (db *DB) Hooks() *hook.Engine {
return db.hooks
}
// Close closes the database connection pool and releases all resources.
func (db *DB) Close() error {
db.mu.Lock()
defer db.mu.Unlock()
if db.closed {
return ErrDriverClosed
}
db.closed = true
return db.drv.Close()
}
// Ping verifies the database connection is alive.
func (db *DB) Ping(ctx context.Context) error {
db.mu.RLock()
defer db.mu.RUnlock()
if db.closed {
return ErrDriverClosed
}
return db.drv.Ping(ctx)
}
// RegisterModel registers model types for later use.
// Reflection is performed once per model type and cached by the driver.
//
// db.RegisterModel((*User)(nil), (*Post)(nil))
func (db *DB) RegisterModel(models ...any) {
db.mu.Lock()
defer db.mu.Unlock()
for _, model := range models {
key := fmt.Sprintf("%T", model)
db.models[key] = model
}
}
// TxOptions holds transaction configuration for BeginTx.
type TxOptions struct {
IsolationLevel int
ReadOnly bool
}
// Tx wraps a driver transaction, exposing Commit and Rollback.
type Tx struct {
underlying any
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
type committer interface{ Commit() error }
if c, ok := tx.underlying.(committer); ok {
return c.Commit()
}
return fmt.Errorf("grove: transaction does not support Commit")
}
// Rollback rolls back the transaction.
func (tx *Tx) Rollback() error {
type rollbacker interface{ Rollback() error }
if r, ok := tx.underlying.(rollbacker); ok {
return r.Rollback()
}
return fmt.Errorf("grove: transaction does not support Rollback")
}
// Raw returns the underlying driver transaction for advanced usage.
// The returned value should be type-asserted to the driver-specific Tx type
// (e.g., driver.Tx for pgdriver).
func (tx *Tx) Raw() any {
return tx.underlying
}
// txBeginner is the adapter interface for drivers that support transactions.
// Drivers implement this with matching signature to avoid import cycles.
// See PgDB.GroveTx for the PostgreSQL implementation.
type txBeginner interface {
GroveTx(ctx context.Context, isolationLevel int, readOnly bool) (any, error)
}
// BeginTx starts a new transaction. The returned Tx wraps the driver
// transaction. Use pgdriver.Unwrap(db).BeginTx() for full driver.Tx access.
//
// tx, err := db.BeginTx(ctx, nil)
// defer tx.Rollback()
// // ... use pgdriver.Unwrap with tx ...
// tx.Commit()
func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
db.mu.RLock()
defer db.mu.RUnlock()
if db.closed {
return nil, ErrDriverClosed
}
starter, ok := db.drv.(txBeginner)
if !ok {
return nil, ErrNotSupported
}
isoLevel := 0
readOnly := false
if opts != nil {
isoLevel = opts.IsolationLevel
readOnly = opts.ReadOnly
}
raw, err := starter.GroveTx(ctx, isoLevel, readOnly)
if err != nil {
return nil, err
}
return &Tx{underlying: raw}, nil
}
// queryBuilder is the adapter interface for drivers that provide typed query
// builders. Drivers implement these methods returning any to bridge the gap
// between concrete return types and this generic interface.
// See PgDB.GroveSelect, GroveInsert, GroveUpdate, GroveDelete.
type queryBuilder interface {
GroveSelect(model ...any) any
GroveInsert(model any) any
GroveUpdate(model any) any
GroveDelete(model any) any
}
// NewSelect creates a new SELECT query builder via the driver.
// The returned value should be type-asserted to the driver-specific builder
// (e.g., *pgdriver.SelectQuery).
//
// For typed access, prefer: pgdriver.Unwrap(db).NewSelect(model)
func (db *DB) NewSelect(model ...any) any {
if qb, ok := db.drv.(queryBuilder); ok {
return qb.GroveSelect(model...)
}
return nil
}
// NewInsert creates a new INSERT query builder via the driver.
// The returned value should be type-asserted to the driver-specific builder.
//
// For typed access, prefer: pgdriver.Unwrap(db).NewInsert(model)
func (db *DB) NewInsert(model any) any {
if qb, ok := db.drv.(queryBuilder); ok {
return qb.GroveInsert(model)
}
return nil
}
// NewUpdate creates a new UPDATE query builder via the driver.
// The returned value should be type-asserted to the driver-specific builder.
//
// For typed access, prefer: pgdriver.Unwrap(db).NewUpdate(model)
func (db *DB) NewUpdate(model any) any {
if qb, ok := db.drv.(queryBuilder); ok {
return qb.GroveUpdate(model)
}
return nil
}
// NewDelete creates a new DELETE query builder via the driver.
// The returned value should be type-asserted to the driver-specific builder.
//
// For typed access, prefer: pgdriver.Unwrap(db).NewDelete(model)
func (db *DB) NewDelete(model any) any {
if qb, ok := db.drv.(queryBuilder); ok {
return qb.GroveDelete(model)
}
return nil
}