Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
81cf698072 | |||
2ec328059f |
54
pgm.go
54
pgm.go
@@ -4,11 +4,9 @@
|
|||||||
package pgm
|
package pgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -51,6 +49,31 @@ func (f Field) Avg() Field {
|
|||||||
return Field("AVG(" + f.String() + ")")
|
return Field("AVG(" + f.String() + ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Field) Sum() Field {
|
||||||
|
return Field("SUM(" + f.String() + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Max() Field {
|
||||||
|
return Field("MAX(" + f.String() + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Min() Field {
|
||||||
|
return Field("Min(" + f.String() + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Lower() Field {
|
||||||
|
return Field("LOWER(" + f.String() + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Upper() Field {
|
||||||
|
return Field("UPPER(" + f.String() + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Trim() Field {
|
||||||
|
return Field("TRIM(" + f.String() + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Eq is equal
|
||||||
func (f Field) Eq(val any) Conditioner {
|
func (f Field) Eq(val any) Conditioner {
|
||||||
col := f.String()
|
col := f.String()
|
||||||
return &Cond{Field: col, Val: val, op: " = $", len: len(col) + 5}
|
return &Cond{Field: col, Val: val, op: " = $", len: len(col) + 5}
|
||||||
@@ -72,11 +95,21 @@ func (f Field) Gt(val any) Conditioner {
|
|||||||
return &Cond{Field: col, Val: val, op: " > $", len: len(col) + 5}
|
return &Cond{Field: col, Val: val, op: " > $", len: len(col) + 5}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Field) Lt(val any) Conditioner {
|
||||||
|
col := f.String()
|
||||||
|
return &Cond{Field: col, Val: val, op: " < $", len: len(col) + 5}
|
||||||
|
}
|
||||||
|
|
||||||
func (f Field) Gte(val any) Conditioner {
|
func (f Field) Gte(val any) Conditioner {
|
||||||
col := f.String()
|
col := f.String()
|
||||||
return &Cond{Field: col, Val: val, op: " >= $", len: len(col) + 5}
|
return &Cond{Field: col, Val: val, op: " >= $", len: len(col) + 5}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Field) Lte(val any) Conditioner {
|
||||||
|
col := f.String()
|
||||||
|
return &Cond{Field: col, Val: val, op: " <= $", len: len(col) + 5}
|
||||||
|
}
|
||||||
|
|
||||||
func (f Field) Like(val string) Conditioner {
|
func (f Field) Like(val string) Conditioner {
|
||||||
col := f.String()
|
col := f.String()
|
||||||
return &Cond{Field: col, Val: val, op: " LIKE $", len: len(f.String()) + 5}
|
return &Cond{Field: col, Val: val, op: " LIKE $", len: len(f.String()) + 5}
|
||||||
@@ -116,19 +149,14 @@ func PgTimeNow() pgtype.Timestamptz {
|
|||||||
return pgtype.Timestamptz{Time: time.Now(), Valid: true}
|
return pgtype.Timestamptz{Time: time.Now(), Valid: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsNotFound error check
|
func ConcatWs(sep string, fields ...Field) Field {
|
||||||
func IsNotFound(err error) bool {
|
return Field("concat_ws('" + sep + "'," + joinFileds(fields) + ")")
|
||||||
return errors.Is(err, pgx.ErrNoRows)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConcatWs(sep string, fields ...Field) string {
|
func StringAgg(exp, sep string) Field {
|
||||||
return "concat_ws('" + sep + "'," + joinFileds(fields) + ")"
|
return Field("string_agg(" + exp + ",'" + sep + "')")
|
||||||
}
|
}
|
||||||
|
|
||||||
func StringAgg(exp, sep string) string {
|
func StringAggCast(exp, sep string) Field {
|
||||||
return "string_agg(" + exp + ",'" + sep + "')"
|
return Field("string_agg(cast(" + exp + " as varchar),'" + sep + "')")
|
||||||
}
|
|
||||||
|
|
||||||
func StringAggCast(exp, sep string) string {
|
|
||||||
return "string_agg(cast(" + exp + " as varchar),'" + sep + "')"
|
|
||||||
}
|
}
|
||||||
|
23
pool.go
23
pool.go
@@ -77,6 +77,17 @@ func InitPool(conf Config) {
|
|||||||
poolPGX.Store(p)
|
poolPGX.Store(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get string builder from pool
|
||||||
|
func getSB() *strings.Builder {
|
||||||
|
return poolStringBuilder.Get().(*strings.Builder)
|
||||||
|
}
|
||||||
|
|
||||||
|
// put string builder back to pool
|
||||||
|
func putSB(sb *strings.Builder) {
|
||||||
|
sb.Reset()
|
||||||
|
poolStringBuilder.Put(sb)
|
||||||
|
}
|
||||||
|
|
||||||
// GetPool instance
|
// GetPool instance
|
||||||
func GetPool() *pgxpool.Pool {
|
func GetPool() *pgxpool.Pool {
|
||||||
return poolPGX.Load()
|
return poolPGX.Load()
|
||||||
@@ -93,13 +104,7 @@ func BeginTx(ctx context.Context) (pgx.Tx, error) {
|
|||||||
return tx, err
|
return tx, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get string builder from pool
|
// IsNotFound error check
|
||||||
func getSB() *strings.Builder {
|
func IsNotFound(err error) bool {
|
||||||
return poolStringBuilder.Get().(*strings.Builder)
|
return errors.Is(err, pgx.ErrNoRows)
|
||||||
}
|
|
||||||
|
|
||||||
// put string builder back to pool
|
|
||||||
func putSB(sb *strings.Builder) {
|
|
||||||
sb.Reset()
|
|
||||||
poolStringBuilder.Put(sb)
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user