-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.go
More file actions
74 lines (57 loc) · 2.1 KB
/
database.go
File metadata and controls
74 lines (57 loc) · 2.1 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
package scaf
import (
"context"
"fmt"
)
// Database represents an execution target (neo4j, postgres, mysql).
// It handles connection establishment and query execution.
type Database interface {
// Name returns the database identifier (e.g., "neo4j", "postgres").
Name() string
// Dialect returns the query language this database uses.
Dialect() Dialect
// Execute runs a query with parameters and returns results.
Execute(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)
// Close releases database resources.
Close() error
}
// DatabaseTransaction represents an active database transaction.
// Queries executed through a transaction are isolated until Commit or Rollback.
type DatabaseTransaction interface {
// Execute runs a query within this transaction.
Execute(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)
// Commit commits the transaction.
Commit(ctx context.Context) error
// Rollback aborts the transaction.
Rollback(ctx context.Context) error
}
// TransactionalDatabase is implemented by databases that support transactions.
// The runner uses this for test isolation (rollback after each test).
type TransactionalDatabase interface {
Database
// Begin starts a new transaction.
Begin(ctx context.Context) (DatabaseTransaction, error)
}
// DatabaseFactory creates a Database from configuration.
type DatabaseFactory func(cfg any) (Database, error)
var databases = make(map[string]DatabaseFactory)
// RegisterDatabase registers a database factory by name.
func RegisterDatabase(name string, factory DatabaseFactory) {
databases[name] = factory
}
// NewDatabase creates a database instance by name.
func NewDatabase(name string, cfg any) (Database, error) { //nolint:ireturn
factory, ok := databases[name]
if !ok {
return nil, fmt.Errorf("%w: %s", ErrUnknownDatabase, name)
}
return factory(cfg)
}
// RegisteredDatabases returns the names of all registered databases.
func RegisteredDatabases() []string {
names := make([]string, 0, len(databases))
for name := range databases {
names = append(names, name)
}
return names
}