add db-wrapper package types
This commit is contained in:
14
internal/app/db-wrapper/main.go
Normal file
14
internal/app/db-wrapper/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
38
internal/app/db-wrapper/mssql.go
Normal file
38
internal/app/db-wrapper/mssql.go
Normal 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
|
||||||
|
}
|
||||||
34
internal/app/db-wrapper/postgres.go
Normal file
34
internal/app/db-wrapper/postgres.go
Normal 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
|
||||||
|
}
|
||||||
25
internal/app/db-wrapper/types.go
Normal file
25
internal/app/db-wrapper/types.go
Normal 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user