feat: refactor db handling to use db-wrapper package; enhance connection management and result handling for MSSQL and Postgres
This commit is contained in:
@@ -15,15 +15,23 @@ func init() {
|
||||
}
|
||||
|
||||
type mssqlRowResult struct {
|
||||
row *sql.Row
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Scan(dest ...any) error {
|
||||
return mr.row.Scan(dest...)
|
||||
}
|
||||
|
||||
type mssqlRowsResult struct {
|
||||
columns []string
|
||||
rows *sql.Rows
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Close() error {
|
||||
func (mr *mssqlRowsResult) Close() error {
|
||||
return mr.rows.Close()
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Columns() ([]string, error) {
|
||||
func (mr *mssqlRowsResult) Columns() ([]string, error) {
|
||||
if mr.columns != nil {
|
||||
return mr.columns, nil
|
||||
}
|
||||
@@ -31,19 +39,19 @@ func (mr *mssqlRowResult) Columns() ([]string, error) {
|
||||
return mr.rows.Columns()
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Err() error {
|
||||
func (mr *mssqlRowsResult) Err() error {
|
||||
return mr.rows.Err()
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Next() bool {
|
||||
func (mr *mssqlRowsResult) Next() bool {
|
||||
return mr.rows.Next()
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Scan(dest ...any) error {
|
||||
func (mr *mssqlRowsResult) Scan(dest ...any) error {
|
||||
return mr.rows.Scan(dest...)
|
||||
}
|
||||
|
||||
func (mr *mssqlRowResult) Values() ([]any, error) {
|
||||
func (mr *mssqlRowsResult) Values() ([]any, error) {
|
||||
columns, err := mr.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -112,7 +120,12 @@ func (mw *mssqlDbWrapper) Query(ctx context.Context, query string, args ...any)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &mssqlRowResult{columns: nil, rows: rows}, nil
|
||||
return &mssqlRowsResult{columns: nil, rows: rows}, nil
|
||||
}
|
||||
|
||||
func (mw *mssqlDbWrapper) QueryRow(ctx context.Context, query string, args ...any) RowResult {
|
||||
row := mw.db.QueryRowContext(ctx, query, args...)
|
||||
return &mssqlRowResult{row: row}
|
||||
}
|
||||
|
||||
func (mw *mssqlDbWrapper) SaveMassive(ctx context.Context, schema string, table string, columnNames []string, rows [][]any) (int64, error) {
|
||||
|
||||
@@ -15,16 +15,24 @@ func init() {
|
||||
}
|
||||
|
||||
type postgresRowResult struct {
|
||||
row pgx.Row
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Scan(dest ...any) error {
|
||||
return pr.row.Scan(dest...)
|
||||
}
|
||||
|
||||
type postgresRowsResult struct {
|
||||
columns []string
|
||||
rows pgx.Rows
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Close() error {
|
||||
func (pr *postgresRowsResult) Close() error {
|
||||
pr.rows.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Columns() ([]string, error) {
|
||||
func (pr *postgresRowsResult) Columns() ([]string, error) {
|
||||
if pr.columns != nil {
|
||||
return pr.columns, nil
|
||||
}
|
||||
@@ -42,19 +50,19 @@ func (pr *postgresRowResult) Columns() ([]string, error) {
|
||||
return columns, nil
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Err() error {
|
||||
func (pr *postgresRowsResult) Err() error {
|
||||
return pr.rows.Err()
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Next() bool {
|
||||
func (pr *postgresRowsResult) Next() bool {
|
||||
return pr.rows.Next()
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Scan(dest ...any) error {
|
||||
func (pr *postgresRowsResult) Scan(dest ...any) error {
|
||||
return pr.rows.Scan(dest...)
|
||||
}
|
||||
|
||||
func (pr *postgresRowResult) Values() ([]any, error) {
|
||||
func (pr *postgresRowsResult) Values() ([]any, error) {
|
||||
return pr.rows.Values()
|
||||
}
|
||||
|
||||
@@ -102,7 +110,12 @@ func (pw *postgresDbWrapper) Query(ctx context.Context, query string, args ...an
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &postgresRowResult{columns: nil, rows: rows}, nil
|
||||
return &postgresRowsResult{columns: nil, rows: rows}, nil
|
||||
}
|
||||
|
||||
func (pw *postgresDbWrapper) QueryRow(ctx context.Context, query string, args ...any) RowResult {
|
||||
row := pw.db.QueryRow(ctx, query, args...)
|
||||
return &postgresRowResult{row: row}
|
||||
}
|
||||
|
||||
func (pw *postgresDbWrapper) SaveMassive(ctx context.Context, schema string, table string, columnNames []string, rows [][]any) (int64, error) {
|
||||
|
||||
@@ -20,11 +20,16 @@ type RowsResult interface {
|
||||
Values() ([]any, error)
|
||||
}
|
||||
|
||||
type RowResult interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
||||
type DbWrapper interface {
|
||||
Close() error
|
||||
Connect(ctx context.Context, dbUrl string) error
|
||||
Exec(ctx context.Context, query string, args ...any) (ExecResult, error)
|
||||
GetDialect() string
|
||||
Query(ctx context.Context, query string, args ...any) (RowsResult, error)
|
||||
QueryRow(ctx context.Context, query string, args ...any) RowResult
|
||||
SaveMassive(ctx context.Context, schema string, table string, columnNames []string, rows [][]any) (int64, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user