pgdb

package
v0.2.1-alpha.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEmptyBatch

func NewEmptyBatch() database.Batch

func SetupPostgresDB

func SetupPostgresDB(ctx context.Context, dbConf database.ConnConfig, preparesStatements ...database.PreparedStatement) database.InstanceManager

SetupPostgresDB - setup Postgres DB connection.

Types

type PgResultSet added in v0.2.0

type PgResultSet struct {
	database.DefaultResultSet
	// contains filtered or unexported fields
}

PgResultSet - Postgres specific resultset. It implements database.ResultSet

func (*PgResultSet) Close added in v0.2.0

func (rs *PgResultSet) Close()

type PgRowScan

type PgRowScan struct {
	Values []any
}

PgRowScan - Wrapper around a row scanner. It implements database.RowScan

func (*PgRowScan) Scan

func (p *PgRowScan) Scan(dest ...any) error

Scan implements the RowScan interface to scan Values into the provided dest.

type PgxBatch

type PgxBatch struct {
	// contains filtered or unexported fields
}

PgxBatch - Postgres Transaction Batch manager.

func (*PgxBatch) GetBatch

func (ptb *PgxBatch) GetBatch() any

func (*PgxBatch) Len

func (ptb *PgxBatch) Len() int

func (*PgxBatch) Queue

func (ptb *PgxBatch) Queue(query string, arguments ...any)

type PostgresDB

type PostgresDB struct {
	// contains filtered or unexported fields
}

PostgresDB - db manager. It Implements database.InstanceManager

func (*PostgresDB) AcquireDistributedLock

func (db *PostgresDB) AcquireDistributedLock(ctx context.Context, lockId int64) (int64, error)

AcquireDistributedLock acquire a distribuited lock with the given Id. It doesn't release the connection associated with the lockId, so it's important to release that when finishing the transaction. This is done by the method ReleaseDistributedLock

func (*PostgresDB) CloseDbConnPool

func (db *PostgresDB) CloseDbConnPool()

CloseDbConnPool - close db connection pool.

func (*PostgresDB) Exec

func (db *PostgresDB) Exec(ctx context.Context, lockId int64, execQuery string, args ...any) (int64, error)

Exec - execute a command query and return 1 or 0 if successful or failure. , If lockId = 0 it will get a connection from pool, otherwise will try to retrieve the connection associated with the given lockId In case of we get the lockId != 0 remember that the connection won't be released, because it belongs to the of the Distributed Lock Scope That needs to release it

func (*PostgresDB) ExecTaskWithDistributedLock

func (db *PostgresDB) ExecTaskWithDistributedLock(ctx context.Context, lockId int64, task func(ctx context.Context) error) (err error)

ExecTaskWithDistributedLock - exec a task wrapped around a distributed lock. LockId is managed by the business logic and not autogenerated, so it needs to be passed as parameter

func (*PostgresDB) ExecTransactionalTask

func (db *PostgresDB) ExecTransactionalTask(ctx context.Context, lockId int64, task func(ctx context.Context, pgTx database.Transaction) error) error

ExecTransactionalTask - execute a transactional task, and optionally using the distributed lock context (if previously created). If lockId = 0 it will not use distributed lock context

func (*PostgresDB) GetConnFromPool

func (db *PostgresDB) GetConnFromPool(ctx context.Context) (any, error)

GetConnFromPool - get a connection from the pool.

func (*PostgresDB) GetConnectionConfig

func (db *PostgresDB) GetConnectionConfig() database.ConnConfig

GetConnectionConfig - get Db Connection config.

func (*PostgresDB) GetDbConnPool

func (db *PostgresDB) GetDbConnPool() (any, error)

GetDbConnPool - get the connection pool.

func (*PostgresDB) Query

func (db *PostgresDB) Query(ctx context.Context, lockId int64, query string, args ...any) (database.ResultSet, error)

Query executes a query and returns a ResultSet and an error, requiring manual resource management.

This method is designed for performance and flexibility. It retrieves the connection from the pool, executes the query, and returns a ResultSet with the query results. The caller is responsible for calling the Close() method on the ResultSet. This will release the connection

Arguments:

  • ctx: The context for the query execution.
  • lockId: The ID used to retrieve a specific connection associated with a distributed lock. If 0, it gets a connection from the pool.
  • query: The SQL query to be executed.
  • args: The arguments for the SQL query.

Returns:

  • ResultSet: The query results encapsulated in a ResultSet.
  • error: Any error encountered during query execution.

Note:

  • This method is more performant because it does not copy the row data. However, it requires the caller to manage the lifecycle of the connection and rows, including calling Close() on the ResultSet.
  • In case of we get the lockId != 0 remember that the connection won't be released, because it belongs to the of the Distributed Lock Scope That needs to release it

func (*PostgresDB) QueryAndClose added in v0.2.0

func (db *PostgresDB) QueryAndClose(ctx context.Context, lockId int64, query string, args ...any) (database.ResultSet, error)

QueryAndClose executes a query, copies the results, and returns a ResultSet and error, with automatic resource management.

This method is user-friendly because it handles the connection and row management internally. It retrieves the connection from the pool, executes the query, and returns a ResultSet with the query results. It also closes the pgx.Rows and the pgxpool.Conn internally after copying the row data, so the caller does not need to worry about closing the connection or the rows.

Arguments:

  • ctx: The context for the query execution.
  • lockId: The ID used to retrieve a specific connection associated with a distributed lock. If 0, it gets a connection from the pool.
  • query: The SQL query to be executed.
  • args: The arguments for the SQL query.

Returns:

  • ResultSet: The query results encapsulated in a ResultSet.
  • error: Any error encountered during query execution.

Note:

  • This method has a performance overhead due to the deep copy of row data. It is convenient but less performant.
  • In case of we get the lockId != 0 remember that the connection won't be released, because it belongs to the of the Distributed Lock Scope That needs to release it

func (*PostgresDB) QueryAndProcess

func (db *PostgresDB) QueryAndProcess(ctx context.Context, lockId int64, processCallback func(row database.Row, rowScan database.RowScan) error, query string, args ...any) error

QueryAndProcess executes a query and applies the processCallback function to each row instead of returning the entire result set in memory. This method is designed for processing large result sets efficiently by handling each row sequentially, which helps in reducing memory usage.

If lockId is 0, the method will acquire a connection from the pool. Otherwise, it will try to retrieve the connection associated with the given lockId. In case of a non-zero lockId, the connection won't be released by this method because it belongs to the scope of the distributed lock, and needs to be released separately.

Arguments:

  • ctx: The context for the query execution.
  • lockId: The ID used to retrieve a specific connection associated with a distributed lock. If 0, it gets a connection from the pool.
  • processCallback: A function that processes each row. This function takes a row (as database.Row) and a row scanner (as database.RowScan) as arguments and returns an error if processing fails.
  • query: The SQL query to be executed.
  • args: The arguments for the SQL query.

Returns:

  • error: Any error encountered during query execution or row processing.

Note:

  • The processCallback function is responsible for handling each row individually.
  • The method ensures efficient memory usage by not storing the entire result set in memory.
  • The pgx.Rows are closed automatically at the end of the method execution, ensuring proper resource management.

func (*PostgresDB) ReleaseDistributedLock

func (db *PostgresDB) ReleaseDistributedLock(ctx context.Context, lockId int64) error

func (*PostgresDB) TxBegin

func (db *PostgresDB) TxBegin(ctx context.Context, lockId int64) (pgxTx database.Transaction, err error)

TxBegin - Begin a Transaction and return Transaction. If lockId = 0 it will get a connection from pool, otherwise will try to retrieve the connection associated with the given lockId In case of we get the lockId != 0 remember that the connection won't be released, because it belongs to the of the Distributed Lock Scope That needs to release it

type PostgresTx

type PostgresTx struct {
	// contains filtered or unexported fields
}

PostgresTx - Postgres Transaction manager. It Implements database.Transaction

func (*PostgresTx) TxCommit

func (t *PostgresTx) TxCommit(ctx context.Context, lockId int64) error

TxCommit - Commit a transaction and release the connection to the pool.

func (*PostgresTx) TxExec

func (t *PostgresTx) TxExec(ctx context.Context, execQuery string, args ...any) (int64, error)

TxExec - Execute a command query under a Transaction and return 1 or 0 if successful or failure.

func (*PostgresTx) TxExecBatch

func (t *PostgresTx) TxExecBatch(ctx context.Context, batch database.Batch) (int64, error)

TxExecBatch - Execute batch query in single transaction.

func (*PostgresTx) TxQuery

func (t *PostgresTx) TxQuery(ctx context.Context, query string, args ...any) (database.ResultSet, error)

TxQuery executes a query within a transaction and returns a ResultSet and an error, requiring manual resource management.

This method is designed for performance and flexibility within a transaction context. It retrieves the query results and returns a ResultSet. The caller is responsible for closing the pgx.Rows to release the resources by calling the Close() method on the ResultSet.

Arguments:

  • ctx: The context for the query execution.
  • query: The SQL query to be executed.
  • args: The arguments for the SQL query.

Returns:

  • ResultSet: The query results encapsulated in a ResultSet.
  • error: Any error encountered during query execution.

Note: This method is more performant because it does not copy the row data. However, it requires the caller to manage the lifecycle of the connection and rows, including calling Close() on the ResultSet.

func (*PostgresTx) TxQueryAndClose added in v0.2.0

func (t *PostgresTx) TxQueryAndClose(ctx context.Context, query string, args ...any) (database.ResultSet, error)

TxQueryAndClose executes a query within a transaction, copies the results, and returns a ResultSet and an error, with automatic resource management.

This method is user-friendly within a transaction context because it handles the connection and row management internally. It retrieves the query results and returns a ResultSet with the query results. It also closes the pgx.Rows internally after copying the row data, so the caller does not need to worry about closing the connection or the rows.

Arguments:

  • ctx: The context for the query execution.
  • query: The SQL query to be executed.
  • args: The arguments for the SQL query.

Returns:

  • ResultSet: The query results encapsulated in a ResultSet.
  • error: Any error encountered during query execution.

Note: This method has a performance overhead due to the deep copy of row data. It is convenient but less performant.

func (*PostgresTx) TxRollback

func (t *PostgresTx) TxRollback(ctx context.Context, lockId int64)

TxRollback - Rollback a transaction and release the connection to the pool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL