Skip to content

Commit 82c2d78

Browse files
committed
refactor: added handler interface to service interfaces
1 parent 80419cd commit 82c2d78

File tree

4 files changed

+96
-55
lines changed

4 files changed

+96
-55
lines changed

sql/interfaces.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type (
1616

1717
// Service is the interface for the service
1818
Service interface {
19-
DB() *sql.DB
19+
Handler
2020
CreateTransaction(
2121
ctx context.Context,
2222
fn TransactionFn,
@@ -28,12 +28,12 @@ type (
2828
query *string,
2929
params ...any,
3030
) (sql.Result, error)
31-
QueryRow(query *string, params ...any) *sql.Row
31+
QueryRow(query *string, params ...any) (*sql.Row, error)
3232
QueryRowWithCtx(
3333
ctx any,
3434
query *string,
3535
params ...any,
36-
) *sql.Row
36+
) (*sql.Row, error)
3737
ScanRow(row *sql.Row, destinations ...any) error
3838
}
3939
)

sql/pgxpool/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type (
2020

2121
// Service is the interface for the service
2222
Service interface {
23-
Pool() *pgxpool.Pool
23+
Handler
2424
CreateTransaction(fn TransactionFn) error
2525
Exec(query *string, params ...any) (*pgconn.CommandTag, error)
2626
ExecWithCtx(

sql/pgxpool/service.go

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pgxpool
22

33
import (
44
"context"
5+
"log/slog"
56
"time"
67

78
"github.com/jackc/pgx/v5"
@@ -14,47 +15,43 @@ import (
1415
type (
1516
// DefaultService is the default service struct
1617
DefaultService struct {
17-
pool *pgxpool.Pool
18+
Handler
1819
statTicker *time.Ticker
20+
logger *slog.Logger
1921
}
2022
)
2123

2224
// NewDefaultService creates a new default service
2325
//
2426
// Parameters:
2527
//
26-
// - pool: the pgxpool.Pool instance
28+
// - config *Config: configuration for the connection
29+
// - logger *slog.Logger: the logger instance
2730
//
2831
// Returns:
2932
//
3033
// - *DefaultService: the DefaultService instance
3134
// - error: if any error occurs
32-
func NewDefaultService(pool *pgxpool.Pool) (
35+
func NewDefaultService(config *Config, logger *slog.Logger) (
3336
instance *DefaultService,
3437
err error,
3538
) {
36-
// Check if the pool is nil
37-
if pool == nil {
38-
return nil, godatabases.ErrNilPool
39+
// Create the handler
40+
handler, err := NewDefaultHandler(config)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
if logger != nil {
46+
logger = logger.With(slog.String("database", "pgxpool"), slog.String("service", "DefaultService"))
3947
}
4048

4149
return &DefaultService{
42-
pool: pool,
50+
Handler: handler,
51+
logger: logger,
4352
}, nil
4453
}
4554

46-
// Pool returns the pool
47-
//
48-
// Returns:
49-
//
50-
// - *pgxpool.Pool: the pgxpool.Pool instance
51-
func (d *DefaultService) Pool() *pgxpool.Pool {
52-
if d == nil {
53-
return nil
54-
}
55-
return d.pool
56-
}
57-
5855
// CreateTransaction creates a transaction for the database with a context
5956
//
6057
// Parameters:
@@ -72,7 +69,15 @@ func (d *DefaultService) CreateTransaction(
7269
if d == nil {
7370
return godatabases.ErrNilService
7471
}
75-
return CreateTransaction(ctx, d.pool, fn)
72+
73+
// Get the pool
74+
pool, err := d.Pool()
75+
if err != nil {
76+
return err
77+
}
78+
79+
// Create the transaction
80+
return CreateTransaction(ctx, pool, fn)
7681
}
7782

7883
// ExecWithCtx executes a query with parameters and returns the result with a context
@@ -104,8 +109,14 @@ func (d *DefaultService) ExecWithCtx(
104109
return nil, godatabases.ErrNilQuery
105110
}
106111

112+
// Get the pool
113+
pool, err := d.Pool()
114+
if err != nil {
115+
return nil, err
116+
}
117+
107118
// Run the exec
108-
commandTag, err := d.pool.Exec(ctx, *query, params...)
119+
commandTag, err := pool.Exec(ctx, *query, params...)
109120
if err != nil {
110121
return nil, err
111122
}
@@ -158,8 +169,14 @@ func (d *DefaultService) QueryWithCtx(
158169
return nil, godatabases.ErrNilQuery
159170
}
160171

172+
// Get the pool
173+
pool, err := d.Pool()
174+
if err != nil {
175+
return nil, err
176+
}
177+
161178
// Run the query
162-
return d.pool.Query(ctx, *query, params...)
179+
return pool.Query(ctx, *query, params...)
163180
}
164181

165182
// Query runs a query with parameters and returns the result
@@ -209,8 +226,14 @@ func (d *DefaultService) QueryRowWithCtx(
209226
return nil, godatabases.ErrNilQuery
210227
}
211228

229+
// Get the pool
230+
pool, err := d.Pool()
231+
if err != nil {
232+
return nil, err
233+
}
234+
212235
// Run the query row
213-
return d.pool.QueryRow(ctx, *query, params...), nil
236+
return pool.QueryRow(ctx, *query, params...), nil
214237
}
215238

216239
// QueryRow runs a query row with parameters and returns the result row
@@ -289,7 +312,14 @@ func (d *DefaultService) SetStatTicker(
289312
case <-ctx.Done():
290313
return // Exit the goroutine when the context is done
291314
case <-d.statTicker.C:
292-
fn(d.pool.Stat())
315+
// Get the pool
316+
pool, err := d.Pool()
317+
if err != nil {
318+
d.logger.Error("Failed to get pool for stat ticker", slog.String("error", err.Error()))
319+
return
320+
}
321+
322+
fn(pool.Stat())
293323
}
294324
}
295325
}()

sql/service.go

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,35 @@ import (
1010
type (
1111
// DefaultService is the default service struct
1212
DefaultService struct {
13-
db *sql.DB
13+
Handler
1414
}
1515
)
1616

1717
// NewDefaultService creates a new default service
1818
//
1919
// Parameters:
2020
//
21-
// *db *sql.DB: the database connection
21+
// - config: the configuration for the connection
2222
//
2323
// Returns:
2424
//
2525
// *DefaultService: the default service
2626
// error: if there was an error creating the service
27-
func NewDefaultService(db *sql.DB) (
27+
func NewDefaultService(config *Config) (
2828
instance *DefaultService,
2929
err error,
3030
) {
31-
// Check if the connection is nil
32-
if db == nil {
33-
return nil, godatabases.ErrNilConnection
31+
// Create the handler
32+
handler, err := NewDefaultHandler(config)
33+
if err != nil {
34+
return nil, err
3435
}
3536

3637
return &DefaultService{
37-
db,
38+
Handler: handler,
3839
}, nil
3940
}
4041

41-
// DB returns the database
42-
//
43-
// Returns:
44-
//
45-
// *sql.DB: the database
46-
func (d *DefaultService) DB() *sql.DB {
47-
if d == nil {
48-
return nil
49-
}
50-
return d.db
51-
}
52-
5342
// CreateTransaction creates a transaction for the database
5443
//
5544
// Parameters:
@@ -69,7 +58,15 @@ func (d *DefaultService) CreateTransaction(
6958
if d == nil {
7059
return godatabases.ErrNilService
7160
}
72-
return CreateTransaction(ctx, d.db, fn, opts)
61+
62+
// Get the database connection
63+
db, err := d.DB()
64+
if err != nil {
65+
return err
66+
}
67+
68+
// Create the transaction
69+
return CreateTransaction(ctx, db, fn, opts)
7370
}
7471

7572
// Exec executes a query with parameters and returns the result
@@ -122,8 +119,14 @@ func (d *DefaultService) ExecWithCtx(
122119
return nil, godatabases.ErrNilQuery
123120
}
124121

122+
// Get the database connection
123+
db, err := d.DB()
124+
if err != nil {
125+
return nil, err
126+
}
127+
125128
// Run the exec
126-
return d.db.ExecContext(ctx, *query, params...)
129+
return db.ExecContext(ctx, *query, params...)
127130
}
128131

129132
// QueryRow runs a query row with parameters and returns the result row
@@ -136,12 +139,13 @@ func (d *DefaultService) ExecWithCtx(
136139
// Returns:
137140
//
138141
// - *sql.Row: the result row
142+
// - error: if any error occurs
139143
func (d *DefaultService) QueryRow(
140144
query *string,
141145
params ...any,
142-
) *sql.Row {
146+
) (*sql.Row, error) {
143147
if d == nil {
144-
return nil
148+
return nil, godatabases.ErrNilService
145149
}
146150
return d.QueryRowWithCtx(context.Background(), query, params...)
147151
}
@@ -157,22 +161,29 @@ func (d *DefaultService) QueryRow(
157161
// Returns:
158162
//
159163
// - *sql.Row: the result row
164+
// - error: if any error occurs
160165
func (d *DefaultService) QueryRowWithCtx(
161166
ctx context.Context,
162167
query *string,
163168
params ...any,
164-
) *sql.Row {
169+
) (*sql.Row, error) {
165170
if d == nil {
166-
return nil
171+
return nil, godatabases.ErrNilService
167172
}
168173

169174
// Check if the query is nil
170175
if query == nil {
171-
return nil
176+
return nil, godatabases.ErrNilQuery
177+
}
178+
179+
// Get the database connection
180+
db, err := d.DB()
181+
if err != nil {
182+
return nil, err
172183
}
173184

174185
// Run the query row
175-
return d.db.QueryRowContext(ctx, *query, params...)
186+
return db.QueryRowContext(ctx, *query, params...), nil
176187
}
177188

178189
// ScanRow scans a row

0 commit comments

Comments
 (0)