Documentation
¶
Index ¶
- Variables
- func BeginTx(ctx context.Context) (pgx.Tx, error)
- func ClosePool()
- func GetPool() *pgxpool.Pool
- func InitPool(conf Config)
- func IsNotFound(err error) bool
- func PgTime(t time.Time) pgtype.Timestamptz
- func PgTimeNow() pgtype.Timestamptz
- func TsAndQuery(q string) string
- func TsOrQuery(q string) string
- func TsPrefixAndQuery(q string) string
- func TsPrefixOrQuery(q string) string
- type AfterGroupBy
- type AfterHaving
- type AfterLimit
- type AfterOffset
- type AfterOrderBy
- type AfterWhere
- type All
- type Bulder
- type Clause
- type Cond
- type CondAction
- type CondGroup
- type Conditioner
- type Config
- type DeleteCluase
- type Do
- type Execute
- type Field
- func (f Field) Any(val ...any) Conditioner
- func (f Field) Asc() Field
- func (f Field) Avg() Field
- func (f Field) BooleanEscape() Field
- func (f Field) Count() Field
- func (f Field) DateTrunc(level, as string) Field
- func (f Field) Desc() Field
- func (f Field) Eq(val any) Conditioner
- func (f Field) EqFold(val string) Conditioner
- func (f Field) Gt(val any) Conditioner
- func (f Field) Gte(val any) Conditioner
- func (f Field) ILike(val string) Conditioner
- func (f Field) IsNotNull() Conditioner
- func (f Field) IsNull() Conditioner
- func (f Field) Like(val string) Conditioner
- func (f Field) LikeFold(val string) Conditioner
- func (f Field) Lower() Field
- func (f Field) Lt(val any) Conditioner
- func (f Field) Lte(val any) Conditioner
- func (f Field) Max() Field
- func (f Field) Min() Field
- func (f Field) Name() string
- func (f Field) NotAny(val ...any) Conditioner
- func (f Field) NotEq(val any) Conditioner
- func (f Field) NotInSubQuery(qry WhereClause) Conditioner
- func (f Field) NumberEscape() Field
- func (f Field) RowNumber(as string, extraOrderBy ...Field) Field
- func (f Field) RowNumberDesc(as string, extraOrderBy ...Field) Field
- func (f Field) RowNumberDescPartionBy(partition Field, as string, extraOrderBy ...Field) Field
- func (f Field) RowNumberPartionBy(partition Field, as string, extraOrderBy ...Field) Field
- func (f Field) String() string
- func (f Field) StringEscape() Field
- func (f Field) Sum() Field
- func (f Field) Trim() Field
- func (f Field) TsQuery(as string) Conditioner
- func (f Field) TsRank(fieldName, as string) Field
- func (f Field) Upper() Field
- type First
- type GroupByClause
- type HavinClause
- type Insert
- type InsertClause
- type LimitClause
- type OffsetClause
- type OrderByClause
- type Query
- type RowScanner
- type RowsCb
- type SelectClause
- type Stringer
- type Table
- func (t *Table) Debug() Clause
- func (t *Table) Delete() DeleteCluase
- func (t *Table) Field(f string) Field
- func (t *Table) Insert() InsertClause
- func (t *Table) Select(field ...Field) SelectClause
- func (t *Table) Update() UpdateClause
- func (t *Table) WithTextSearch(name, alias, textToSearch string) *Table
- type Update
- type UpdateClause
- type WhereClause
- type WhereOrExec
Constants ¶
This section is empty.
Variables ¶
var ( ErrInitTX = errors.New("failed to init db.tx") ErrCommitTX = errors.New("failed to commit db.tx") ErrNoRows = errors.New("no data found") )
Common errors returned by pgm operations
var (
ErrConnStringMissing = errors.New("connection string is empty")
)
Functions ¶
func BeginTx ¶
BeginTx begins a new database transaction from the connection pool. Returns an error if the transaction cannot be started. Remember to commit or rollback the transaction when done.
Example:
tx, err := pgm.BeginTx(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx) // rollback on error
// ... do work ...
return tx.Commit(ctx)
func ClosePool ¶ added in v0.4.0
func ClosePool()
ClosePool closes the connection pool gracefully. Should be called during application shutdown.
func GetPool ¶
GetPool returns the initialized connection pool instance. It panics with a descriptive message if InitPool() has not been called. This is a fail-fast approach to catch programming errors early.
func InitPool ¶ added in v0.0.2
func InitPool(conf Config)
InitPool initializes the connection pool with the provided configuration. It validates the configuration and panics if invalid. This function should be called once at application startup.
Example:
pgm.InitPool(pgm.Config{
ConnString: "postgres://user:pass@localhost/dbname",
MaxConns: 100,
MinConns: 5,
})
func IsNotFound ¶
IsNotFound checks if an error is a "no rows" error from pgx. Returns true if the error indicates no rows were found in a query result.
func PgTime ¶
func PgTime(t time.Time) pgtype.Timestamptz
PgTime converts a Go time.Time to PostgreSQL timestamptz type. The time is stored as-is (preserves timezone information).
func PgTimeNow ¶
func PgTimeNow() pgtype.Timestamptz
PgTimeNow returns the current time as PostgreSQL timestamptz type.
func TsAndQuery ¶ added in v0.3.2
TsAndQuery converts a text search query to use AND operator between terms. Example: "hello world" becomes "hello & world"
func TsOrQuery ¶ added in v0.3.2
TsOrQuery converts a text search query to use OR operator between terms. Example: "hello world" becomes "hello | world"
func TsPrefixAndQuery ¶ added in v0.3.2
TsPrefixAndQuery converts a text search query to use AND operator with prefix matching. Example: "hello world" becomes "hello:* & world:*"
func TsPrefixOrQuery ¶ added in v0.3.2
TsPrefixOrQuery converts a text search query to use OR operator with prefix matching. Example: "hello world" becomes "hello:* | world:*"
Types ¶
type AfterGroupBy ¶
type AfterGroupBy interface {
HavinClause
OrderByClause
LimitClause
OffsetClause
Query
}
type AfterHaving ¶
type AfterHaving interface {
OrderByClause
LimitClause
OffsetClause
Query
}
type AfterLimit ¶
type AfterLimit interface {
OffsetClause
Query
}
type AfterOffset ¶
type AfterOffset interface {
LimitClause
Query
}
type AfterOrderBy ¶
type AfterOrderBy interface {
LimitClause
OffsetClause
Query
}
type AfterWhere ¶
type AfterWhere interface {
WhereClause
GroupByClause
OrderByClause
LimitClause
OffsetClause
Query
}
type Clause ¶
type Clause interface {
Insert() InsertClause
Select(fields ...Field) SelectClause
Update() UpdateClause
Delete() DeleteCluase
}
type CondAction ¶
type CondAction uint8
const ( CondActionNothing CondAction = iota CondActionNeedToClose CondActionSubQuery )
Contdition actions
type Conditioner ¶
func And ¶
func And(cond ...Conditioner) Conditioner
func Or ¶
func Or(cond ...Conditioner) Conditioner
type Config ¶
type Config struct {
ConnString string
MaxConns int32
MinConns int32
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
}
Config holds the configuration for initializing the connection pool. All fields except ConnString are optional and will use pgx defaults if not set.
type DeleteCluase ¶ added in v0.2.0
type DeleteCluase interface {
WhereOrExec
}
type Field ¶
type Field string
Field related to a table
func ConcatWs ¶
ConcatWs creates a CONCAT_WS SQL function. SECURITY: The sep parameter should only be a constant string, not user input. Single quotes in sep will be escaped automatically.
func StringAgg ¶
StringAgg creates a STRING_AGG SQL function. SECURITY: The field parameter provides type safety by accepting only Field type. The sep parameter should only be a constant string. Single quotes will be escaped.
func StringAggCast ¶
StringAggCast creates a STRING_AGG SQL function with cast to varchar. SECURITY: The field parameter provides type safety by accepting only Field type. The sep parameter should only be a constant string. Single quotes will be escaped.
func (Field) Any ¶ added in v0.2.0
func (f Field) Any(val ...any) Conditioner
func (Field) BooleanEscape ¶ added in v0.0.7
BooleanEscape will wrap field with:
COALESCE(field, FALSE)
func (Field) DateTrunc ¶ added in v0.2.1
DateTrunc will truncate date or timestamp to specified level of precision
Level values:
- microseconds, milliseconds, second, minute, hour
- day, week (Monday start), month, quarter, year
- decade, century, millennium
func (Field) EqFold ¶
func (f Field) EqFold(val string) Conditioner
EqualFold will use LOWER(column_name) = LOWER(val) for comparision
func (Field) Gt ¶
func (f Field) Gt(val any) Conditioner
func (Field) Gte ¶
func (f Field) Gte(val any) Conditioner
func (Field) IsNotNull ¶ added in v0.0.6
func (f Field) IsNotNull() Conditioner
func (Field) IsNull ¶ added in v0.0.6
func (f Field) IsNull() Conditioner
func (Field) Like ¶
func (f Field) Like(val string) Conditioner
func (Field) LikeFold ¶
func (f Field) LikeFold(val string) Conditioner
func (Field) Lt ¶ added in v0.0.5
func (f Field) Lt(val any) Conditioner
func (Field) Lte ¶ added in v0.0.5
func (f Field) Lte(val any) Conditioner
func (Field) NotAny ¶ added in v0.2.0
func (f Field) NotAny(val ...any) Conditioner
func (Field) NotEq ¶ added in v0.1.3
func (f Field) NotEq(val any) Conditioner
func (Field) NotInSubQuery ¶
func (f Field) NotInSubQuery(qry WhereClause) Conditioner
NotInSubQuery using ANY
func (Field) RowNumberDesc ¶ added in v0.2.0
func (Field) RowNumberDescPartionBy ¶ added in v0.2.0
func (Field) RowNumberPartionBy ¶ added in v0.2.0
RowNumberPartionBy in ascending order
func (Field) TsQuery ¶ added in v0.3.0
func (f Field) TsQuery(as string) Conditioner
type GroupByClause ¶
type GroupByClause interface {
GroupBy(fields ...Field) AfterGroupBy
}
type HavinClause ¶
type HavinClause interface {
Having(cond ...Conditioner) AfterHaving
}
type Insert ¶
type Insert interface {
Set(field Field, val any) InsertClause
SetMap(fields map[Field]any) InsertClause
}
type InsertClause ¶
type LimitClause ¶
type LimitClause interface {
Limit(v int) AfterLimit
}
type OffsetClause ¶
type OffsetClause interface {
Offset(v int) AfterOffset
}
type OrderByClause ¶
type OrderByClause interface {
OrderBy(fields ...Field) AfterOrderBy
}
type RowScanner ¶
type RowsCb ¶
type RowsCb func(row RowScanner) error
type SelectClause ¶
type SelectClause interface {
// Join and Inner Join are same
Join(m Table, t1Field, t2Field Field, cond ...Conditioner) SelectClause
LeftJoin(m Table, t1Field, t2Field Field, cond ...Conditioner) SelectClause
RightJoin(m Table, t1Field, t2Field Field, cond ...Conditioner) SelectClause
FullJoin(m Table, t1Field, t2Field Field, cond ...Conditioner) SelectClause
CrossJoin(m Table) SelectClause
WhereClause
OrderByClause
GroupByClause
LimitClause
OffsetClause
Query
// contains filtered or unexported methods
}
type Table ¶
type Table struct {
Name string
DerivedTable Query
PK []string
FieldCount uint16
// contains filtered or unexported fields
}
Table in database
func (*Table) WithTextSearch ¶ added in v0.3.1
type Update ¶
type Update interface {
Set(field Field, val any) UpdateClause
SetMap(fields map[Field]any) UpdateClause
}
type UpdateClause ¶
type UpdateClause interface {
Update
Where(cond ...Conditioner) WhereOrExec
}
type WhereClause ¶
type WhereClause interface {
Where(cond ...Conditioner) AfterWhere
}
type WhereOrExec ¶
type WhereOrExec interface {
Where(cond ...Conditioner) WhereOrExec
Execute
}