driver

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package driver

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectRedis

func ConnectRedis(addr string, password string, db int) (*redis.Client, error)

ConnectRedis connects to the Redis server and returns a *redis.Client and an errors

Types

type DB

type DB struct {
	Pool PostgresPool
}

DB holds the driver connection pool

func ConnectSQL

func ConnectSQL(dsn string) (*DB, error)

ConnectSQL connects to the Postgres server and returns a DB instance and an error. It requires a server.Server struct as a parameter. The function constructs the connection string using the server.Server fields and the pgxpool.ParseConfig function. It then sets the max connection count and connection lifetime on the config. Next, it creates a connection pool using pgxpool.NewWithConfig function. The function assigns the created pool to the SQL field of the dbConn variable. It also calls the testDB function to check if the connection to the driver is successful. If any errors occur during the process, it returns nil and the errors. Otherwise, it returns the dbConn variable and nil.

type PostgresPool

type PostgresPool interface {
	// Acquire returns a connection from the pool.
	Acquire(ctx context.Context) (*pgxpool.Conn, error)

	// BeginTx starts a new transaction and returns a Tx.
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

	// Exec executes an SQL command and returns the command tag.
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)

	// Query executes an SQL query and returns the resulting rows.
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

	// QueryRow executes an SQL query and returns a single row.
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

	// SendBatch sends a batch of queries to the server. The batch is executed as a single transaction.
	SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults

	// Close closes the pool and all its connections.
	Close()
}

PostgresPool is an interface that represents a connection pool to a driver.

type PostgresTx

type PostgresTx interface {
	// Begin starts a pseudo nested transaction.
	Begin(ctx context.Context) (pgx.Tx, error)

	// Commit commits the transaction if this is a real transaction or releases the savepoint if this is a pseudo nested
	// transaction. Commit will return an errors where errors.Is(ErrTxClosed) is true if the Tx is already closed, but is
	// otherwise safe to call multiple times. If the commit fails with a rollback status (e.g., the transaction was already
	// in a broken state), then an errors where errors.Is(ErrTxCommitRollback) is true will be returned.
	Commit(ctx context.Context) error

	// Rollback rolls back the transaction if this is a real transaction or rolls back to the savepoint if this is a
	// pseudo nested transaction. Rollback will return an errors where errors.Is(ErrTxClosed) is true if the Tx is already
	// closed, but is otherwise safe to call multiple times. Hence, a defer tx.Rollback() is safe even if tx.Commit() will
	// be called first in a non-errors condition. Any other failure of a real transaction will result in the connection
	// being closed.
	Rollback(ctx context.Context) error

	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	LargeObjects() pgx.LargeObjects

	Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)

	Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

	// Conn returns the underlying *Conn that on which this transaction is executing.
	Conn() *pgx.Conn
}

Jump to

Keyboard shortcuts

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