-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
171 lines (158 loc) · 6.79 KB
/
database.go
File metadata and controls
171 lines (158 loc) · 6.79 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
package dbx
import (
"context"
"database/sql"
)
// defaultDatabase implements the Database interface by wrapping a sql.DB instance.
// It provides context-aware database operations and transaction management
// while maintaining full compatibility with the standard database/sql package.
type defaultDatabase struct {
db *sql.DB
}
// New creates a new Database instance that wraps the provided sql.DB.
// The returned Database provides context-driven database operations and
// automatic transaction management while preserving all the functionality
// of the underlying sql.DB.
//
// The returned database also implements ContextCreator, allowing direct
// context creation via the Context method.
//
// Parameters:
// - db: A properly initialized sql.DB instance. The caller retains ownership
// and responsibility for the sql.DB's configuration and driver setup.
//
// Returns:
// - DatabaseWithContext: A dbx Database that can create contexts and manage transactions.
//
// Example:
//
// sqlDB, err := sql.Open("postgres", connectionString)
// if err != nil {
// return err
// }
// defer sqlDB.Close()
//
// dbxDB := dbx.New(sqlDB)
// defer dbxDB.Close()
//
// ctx := dbxDB.Context(context.Background())
// rows, err := ctx.Executor().Query("SELECT * FROM users")
func New(db *sql.DB) DatabaseWithContext {
return &defaultDatabase{db}
}
// Close closes the underlying database connection.
// It's important to call this method when the Database is no longer needed
// to properly release database resources.
func (d *defaultDatabase) Close() error {
return d.db.Close()
}
// Context creates a new dbx Context from the provided Go context.
// The returned Context embeds this database instance as the executor,
// allowing database operations to be performed within the context's lifecycle.
//
// Parameters:
// - ctx: The parent Go context for deadline, cancellation, and value propagation.
//
// Returns:
// - Context: A dbx Context that can be used for database operations.
func (d *defaultDatabase) Context(ctx context.Context) Context {
return NewContext(ctx, d)
}
// Begin starts a transaction with default options.
// This method delegates to the underlying sql.DB's Begin method.
func (d *defaultDatabase) Begin() (*sql.Tx, error) {
return d.db.Begin()
}
// BeginTx starts a transaction with the provided context and options.
// The context is used for the transaction's lifecycle - if the context
// is canceled, the transaction will be rolled back.
//
// Parameters:
// - ctx: Context for the transaction's lifecycle
// - opts: Transaction options including isolation level and read-only flag
func (d *defaultDatabase) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
return d.db.BeginTx(ctx, opts)
}
// Exec executes a query without returning any rows.
// This method delegates to the underlying sql.DB's Exec method.
//
// Parameters:
// - query: SQL query string, potentially with placeholder parameters
// - args: Arguments for placeholder parameters in the query
//
// Returns:
// - sql.Result: Contains information about the query execution (rows affected, last insert ID)
// - error: Any error that occurred during query execution
func (d *defaultDatabase) Exec(query string, args ...interface{}) (sql.Result, error) {
return d.db.Exec(query, args...)
}
// Query executes a query that returns rows, typically a SELECT statement.
// This method delegates to the underlying sql.DB's Query method.
//
// Parameters:
// - query: SQL query string, potentially with placeholder parameters
// - args: Arguments for placeholder parameters in the query
//
// Returns:
// - *sql.Rows: Rows returned by the query. Must be closed after use.
// - error: Any error that occurred during query execution
func (d *defaultDatabase) Query(query string, args ...interface{}) (*sql.Rows, error) {
return d.db.Query(query, args...)
}
// QueryRow executes a query that is expected to return at most one row.
// This method delegates to the underlying sql.DB's QueryRow method.
// QueryRow always returns a non-nil value; errors are deferred until Row's Scan method is called.
//
// Parameters:
// - query: SQL query string, potentially with placeholder parameters
// - args: Arguments for placeholder parameters in the query
//
// Returns:
// - *sql.Row: Single row result. Use Scan() to extract values and check for errors.
func (d *defaultDatabase) QueryRow(query string, args ...interface{}) *sql.Row {
return d.db.QueryRow(query, args...)
}
// ExecContext executes a query without returning any rows, using the provided context.
// This method delegates to the underlying sql.DB's ExecContext method.
// The context can be used to cancel the query execution if it takes too long.
//
// Parameters:
// - dbContext: Context for controlling query execution lifecycle
// - query: SQL query string, potentially with placeholder parameters
// - args: Arguments for placeholder parameters in the query
//
// Returns:
// - sql.Result: Contains information about the query execution (rows affected, last insert ID)
// - error: Any error that occurred during query execution or context cancellation
func (d *defaultDatabase) ExecContext(dbContext context.Context, query string, args ...interface{}) (sql.Result, error) {
return d.db.ExecContext(dbContext, query, args...)
}
// QueryContext executes a query that returns rows, using the provided context.
// This method delegates to the underlying sql.DB's QueryContext method.
// The context can be used to cancel the query execution if it takes too long.
//
// Parameters:
// - dbContext: Context for controlling query execution lifecycle
// - query: SQL query string, potentially with placeholder parameters
// - args: Arguments for placeholder parameters in the query
//
// Returns:
// - *sql.Rows: Rows returned by the query. Must be closed after use.
// - error: Any error that occurred during query execution or context cancellation
func (d *defaultDatabase) QueryContext(dbContext context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return d.db.QueryContext(dbContext, query, args...)
}
// QueryRowContext executes a query that is expected to return at most one row, using the provided context.
// This method delegates to the underlying sql.DB's QueryRowContext method.
// QueryRowContext always returns a non-nil value; errors are deferred until Row's Scan method is called.
//
// Parameters:
// - dbContext: Context for controlling query execution lifecycle
// - query: SQL query string, potentially with placeholder parameters
// - args: Arguments for placeholder parameters in the query
//
// Returns:
// - *sql.Row: Single row result. Use Scan() to extract values and check for errors.
func (d *defaultDatabase) QueryRowContext(dbContext context.Context, query string, args ...interface{}) *sql.Row {
return d.db.QueryRowContext(dbContext, query, args...)
}