Skip to content

Instantly share code, notes, and snippets.

@zeroallox
Created March 24, 2022 18:23
Show Gist options
  • Save zeroallox/b655b5849aa4ea4bd24767a6e80e6b19 to your computer and use it in GitHub Desktop.
Save zeroallox/b655b5849aa4ea4bd24767a6e80e6b19 to your computer and use it in GitHub Desktop.
package dbclient
import (
"context"
"database/sql"
"github.com/jackc/pgx/v4/pgxpool"
)
// ResultWrapper is a wrapper around sql.Result
type ResultWrapper struct {
ra int64
}
func (r *ResultWrapper) LastInsertId() (int64, error) {
return 0, nil
}
func (r *ResultWrapper) RowsAffected() (int64, error) {
return r.ra, nil
}
// PoolWrapper is a wrapper around pgxpool.Pool
type PoolWrapper struct {
pxp *pgxpool.Pool
}
func (s *PoolWrapper) Exec(query string, args ...interface{}) (sql.Result, error) {
exec, err := s.pxp.Exec(context.Background(), query, args)
if err != nil {
return nil, err
}
return &ResultWrapper{
ra: exec.RowsAffected(),
}, nil
}
func (s *PoolWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
exec, err := s.pxp.Exec(ctx, query, args)
if err != nil {
return nil, err
}
return &ResultWrapper{
ra: exec.RowsAffected(),
}, nil
}
func (s *PoolWrapper) Query(query string, args ...interface{}) (*sql.Rows, error) {
// TODO How to create a *sql.Rows?
panic("implement me")
}
func (s *PoolWrapper) QueryRow(query string, args ...interface{}) *sql.Row {
// TODO How to create a *sql.Row?
panic("implement me")
}
func (s *PoolWrapper) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
// TODO How to create a *sql.Rows?
panic("implement me")
}
func (s *PoolWrapper) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
// TODO How to create a *sql.Row?
panic("implement me")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment