add db-wrapper package types

This commit is contained in:
DiegoAlessandroMotta
2026-04-15 20:22:23 -05:00
parent 803f8988b8
commit ed889b740a
4 changed files with 111 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package dbwrapper
import "fmt"
func NewWrapper(driverType string) (DbWrapper, error) {
switch driverType {
case "postgres":
return &postgresDbWrapper{}, nil
case "sqlserver":
return &mssqlDbWrapper{}, nil
default:
return nil, fmt.Errorf("driver not yet supported: %s", driverType)
}
}

View File

@@ -0,0 +1,38 @@
package dbwrapper
import (
"context"
"database/sql"
)
type mssqlDbWrapper struct {
db *sql.DB
}
func (wrapper *mssqlDbWrapper) Connect(ctx context.Context, dbUrl string) error { return nil }
func (wrapper *mssqlDbWrapper) Close() error { return nil }
func (wrapper *mssqlDbWrapper) Exec(ctx context.Context, query string, args ...any) (ExecResult, error) {
result, execErr := wrapper.db.ExecContext(ctx, query, args...)
if execErr != nil {
return ExecResult{}, execErr
}
affectedRows, err := result.RowsAffected()
if err != nil {
return ExecResult{}, err
}
return ExecResult{
AffectedRows: affectedRows,
}, nil
}
func (wrapper *mssqlDbWrapper) Query(ctx context.Context, query string, args ...any) (RowsResult, error) {
return nil, nil
}
func (wrapper *mssqlDbWrapper) SaveMassive(ctx context.Context, schema string, table string, columnNames []string, rows [][]any) (int64, error) {
return 0, nil
}

View File

@@ -0,0 +1,34 @@
package dbwrapper
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
type postgresDbWrapper struct {
db *pgxpool.Pool
}
func (wrapper *postgresDbWrapper) Connect(ctx context.Context, dbUrl string) error { return nil }
func (wrapper *postgresDbWrapper) Close() error { return nil }
func (wrapper *postgresDbWrapper) Exec(ctx context.Context, query string, args ...any) (ExecResult, error) {
result, err := wrapper.db.Exec(ctx, query, args...)
if err != nil {
return ExecResult{}, err
}
return ExecResult{
AffectedRows: result.RowsAffected(),
}, nil
}
func (wrapper *postgresDbWrapper) Query(ctx context.Context, query string, args ...any) (RowsResult, error) {
return nil, nil
}
func (wrapper *postgresDbWrapper) SaveMassive(ctx context.Context, schema string, table string, columnNames []string, rows [][]any) (int64, error) {
return 0, nil
}

View File

@@ -0,0 +1,25 @@
package dbwrapper
import (
"context"
)
type ExecResult struct {
AffectedRows int64
}
type RowsResult interface {
Close()
Err() error
Next() bool
Values() ([]any, error)
Columns() ([]string, error)
}
type DbWrapper interface {
Connect(ctx context.Context, dbUrl string) error
Close() error
Exec(ctx context.Context, query string, args ...any) (ExecResult, error)
Query(ctx context.Context, query string, args ...any) (RowsResult, error)
SaveMassive(ctx context.Context, schema string, table string, columnNames []string, rows [][]any) (int64, error)
}