-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Chuiko-GIT/add-db-client-wrapper
Added wrapper for database client to improve modularity and reusability
- Loading branch information
Showing
9 changed files
with
295 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgconn" | ||
"github.com/jackc/pgx/v4" | ||
) | ||
|
||
type ( | ||
Client interface { | ||
DB() DB | ||
Close() error | ||
} | ||
|
||
SQLExec interface { | ||
NamedExec | ||
QueryExec | ||
} | ||
|
||
NamedExec interface { | ||
ScanOneContext(ctx context.Context, dest interface{}, q Query, args ...interface{}) error | ||
ScanAllContext(ctx context.Context, dest interface{}, q Query, args ...interface{}) error | ||
} | ||
|
||
QueryExec interface { | ||
ExecContext(ctx context.Context, q Query, args ...interface{}) (pgconn.CommandTag, error) | ||
QueryContext(ctx context.Context, q Query, args ...interface{}) (pgx.Rows, error) | ||
QueryRowContext(ctx context.Context, q Query, args ...interface{}) pgx.Row | ||
} | ||
|
||
Ping interface { | ||
Ping(ctx context.Context) error | ||
} | ||
|
||
DB interface { | ||
SQLExec | ||
Ping | ||
Close() | ||
} | ||
|
||
Query struct { | ||
Name string | ||
QueryRaw string | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pg | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgx/v4/pgxpool" | ||
"github.com/pkg/errors" | ||
|
||
db "github.com/Chuiko-GIT/auth/internal/client" | ||
) | ||
|
||
type pgClient struct { | ||
masterDBC db.DB | ||
} | ||
|
||
func New(ctx context.Context, dsn string) (db.Client, error) { | ||
dbc, err := pgxpool.Connect(ctx, dsn) | ||
if err != nil { | ||
return nil, errors.Errorf("failed to connect to db: %v", err) | ||
} | ||
|
||
return &pgClient{ | ||
masterDBC: &pg{dbc: dbc}, | ||
}, nil | ||
} | ||
|
||
func (c pgClient) DB() db.DB { | ||
return c.masterDBC | ||
} | ||
|
||
func (c pgClient) Close() error { | ||
if c.masterDBC != nil { | ||
c.masterDBC.Close() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package pg | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/georgysavva/scany/pgxscan" | ||
"github.com/jackc/pgconn" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/pgxpool" | ||
|
||
db "github.com/Chuiko-GIT/auth/internal/client" | ||
"github.com/Chuiko-GIT/auth/internal/client/db/prettier" | ||
) | ||
|
||
type pg struct { | ||
dbc *pgxpool.Pool | ||
} | ||
|
||
func NewDB(dbc *pgxpool.Pool) db.DB { | ||
return &pg{ | ||
dbc: dbc, | ||
} | ||
} | ||
|
||
func (p pg) ScanOneContext(ctx context.Context, dest interface{}, q db.Query, args ...interface{}) error { | ||
logQuery(ctx, q, args...) | ||
|
||
rows, err := p.QueryContext(ctx, q, args...) | ||
if err != nil { | ||
return err | ||
} | ||
return pgxscan.ScanOne(dest, rows) | ||
} | ||
|
||
func (p pg) ScanAllContext(ctx context.Context, dest interface{}, q db.Query, args ...interface{}) error { | ||
logQuery(ctx, q, args...) | ||
|
||
rows, err := p.QueryContext(ctx, q, args...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return pgxscan.ScanAll(dest, rows) | ||
} | ||
|
||
func (p pg) ExecContext(ctx context.Context, q db.Query, args ...interface{}) (pgconn.CommandTag, error) { | ||
logQuery(ctx, q, args...) | ||
|
||
return p.dbc.Exec(ctx, q.QueryRaw, args...) | ||
} | ||
|
||
func (p pg) QueryContext(ctx context.Context, q db.Query, args ...interface{}) (pgx.Rows, error) { | ||
logQuery(ctx, q, args...) | ||
|
||
return p.dbc.Query(ctx, q.QueryRaw, args...) | ||
} | ||
|
||
func (p pg) QueryRowContext(ctx context.Context, q db.Query, args ...interface{}) pgx.Row { | ||
logQuery(ctx, q, args...) | ||
|
||
return p.dbc.QueryRow(ctx, q.QueryRaw, args...) | ||
} | ||
|
||
func (p pg) Ping(ctx context.Context) error { | ||
return p.dbc.Ping(ctx) | ||
} | ||
|
||
func (p pg) Close() { | ||
p.dbc.Close() | ||
} | ||
|
||
func logQuery(ctx context.Context, q db.Query, args ...interface{}) { | ||
prettyQuery := prettier.Pretty(q.QueryRaw, prettier.PlaceholderDollar, args...) | ||
log.Println( | ||
ctx, | ||
fmt.Sprintf("sql: %s", q.Name), | ||
fmt.Sprintf("query: %s", prettyQuery), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package prettier | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
PlaceholderDollar = "$" | ||
PlaceholderQuestion = "?" | ||
) | ||
|
||
func Pretty(query, placeholder string, args ...any) string { | ||
for i, param := range args { | ||
var value string | ||
switch v := param.(type) { | ||
case string: | ||
value = fmt.Sprintf("%q", v) | ||
case []byte: | ||
value = fmt.Sprintf("%q", string(v)) | ||
default: | ||
value = fmt.Sprintf("%v", v) | ||
} | ||
|
||
query = strings.Replace(query, fmt.Sprintf("%s%s", placeholder, strconv.Itoa(i+1)), value, -1) | ||
} | ||
|
||
query = strings.ReplaceAll(query, "\t", "") | ||
query = strings.ReplaceAll(query, "\n", " ") | ||
|
||
return strings.TrimSpace(query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.