Documentation ¶
Index ¶
- func Begin(connection Connection) (pgx.Tx, error)
- func CloseDatabase()
- func ClosePool()
- func ConnectionConfig() *pgx.ConnConfig
- func ConnectionString() string
- func Database(applicationName string) *sql.DB
- func DatabaseName() string
- func EmptyFloatIsNull(float *float64) pgtype.Float8
- func EmptyIntIsNull(int *int32) pgtype.Int4
- func EmptyJsonIsNull[T any](any *T) pgtype.JSON
- func EmptyLongIntIsNull(int *int64) pgtype.Int8
- func EmptySmallIntIsNull(int *int16) pgtype.Int2
- func EmptyStringIsNull[T any](string *T) pgtype.Text
- func Exec(connection Connection, sql string, args ...interface{}) error
- func ExecFile(connection Connection, path string) error
- func FloatIsNull(float *float64, null float64) pgtype.Float8
- func Hostname() string
- func IntIsNull(int *int32, null int32) pgtype.Int4
- func Listen[T any](conn *pgx.Conn, channel string, payloads chan T)
- func ListenWithContext[T any](ctx context.Context, conn *pgx.Conn, channel string, payloads chan T)
- func LongIntIsNull(int *int64, null int64) pgtype.Int8
- func NewConnection() *pgx.Conn
- func NewConnectionWithContext(ctx context.Context) *pgx.Conn
- func NewDatabase(applicationName string) *sql.DB
- func NewPool() *pgxpool.Pool
- func Password() string
- func Pool() *pgxpool.Pool
- func PoolConfig() *pgxpool.Config
- func Port() int
- func Query[T any](connection Connection, sql string, results chan T, args ...interface{}) error
- func QuerySingleRow[T any](connection Connection, sql string, args ...interface{}) (T, error)
- func SmallIntIsNull(int *int16, null int16) pgtype.Int2
- func StringIsNull[T any](s *T, null string) pgtype.Text
- func Username() string
- type Connection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseDatabase ¶ added in v1.0.5
func CloseDatabase()
CloseDatabase closes the default database hold by this package.
func ConnectionConfig ¶
func ConnectionConfig() *pgx.ConnConfig
ConnectionConfig returns the connection config defined by CONNECTION_STRING environment variable.
func ConnectionString ¶
func ConnectionString() string
ConnectionString returns the connection string defined in the environment variable CONNECTION_STRING.
func Database ¶ added in v1.0.5
Database returns the configured database connection from CONNECTION_STRING. If once opened this method returns always the same database.
func DatabaseName ¶ added in v1.0.2
func DatabaseName() string
DatabaseName returns the defined database name configured in CONNECTION_STRING
func EmptyFloatIsNull ¶
func EmptyIntIsNull ¶
func EmptyJsonIsNull ¶
func EmptyLongIntIsNull ¶ added in v1.0.1
func EmptySmallIntIsNull ¶
func EmptyStringIsNull ¶
func Exec ¶
func Exec(connection Connection, sql string, args ...interface{}) error
Exec inserts a row using the given sql with arguments
func ExecFile ¶
func ExecFile(connection Connection, path string) error
func Hostname ¶ added in v1.0.2
func Hostname() string
Hostname returns the defined hostname configured in CONNECTION_STRING
func Listen ¶
Listen waits for notifications on database channel and writes the payload to the go channel. The type of the go channel have to correspond to the payload JSON structure
func ListenWithContext ¶ added in v1.0.4
ListenWithContext waits for notifications on database channel and writes the payload to the go channel. The type of the go channel have to correspond to the payload JSON structure
func NewConnection ¶
func NewConnection() *pgx.Conn
NewConnection returns a new connection defined by CONNECTION_STRING environment variable.
func NewConnectionWithContext ¶ added in v1.0.4
NewConnectionWithContext returns a new connection defined by CONNECTION_STRING environment variable.
func NewDatabase ¶ added in v1.0.5
NewDatabase returns always a new database connection from CONNECTION_STRING.
func Password ¶ added in v1.0.2
func Password() string
Password returns the defined password configured in CONNECTION_STRING
func Pool ¶
Pool returns the default pool hold by this package. The pool is created if this function is called first time. Afterwards this function returns always the same pool. Don't forget to defer the pool with ClosePool function.
func PoolConfig ¶
func Port ¶ added in v1.0.2
func Port() int
Port returns the defined port configured in CONNECTION_STRING
func Query ¶
func Query[T any](connection Connection, sql string, results chan T, args ...interface{}) error
Query gets values read from database into a channel. The value type of channel must match the fields defined in the query. The type can be a single value (e.g. string) if the query returns only a single field. Otherwise, the type have to be a struct with the identical number of elements and corresponding types like the query statement
func QuerySingleRow ¶
func QuerySingleRow[T any](connection Connection, sql string, args ...interface{}) (T, error)
QuerySingleRow returns the value if only a single row is queried
Types ¶
type Connection ¶
type Connection interface { Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) Begin(ctx context.Context) (pgx.Tx, error) }
The Connection interface allows mocking database connection for testing