3 Commits

Author SHA1 Message Date
81cf698072 added in few field wraping functions 2025-07-29 21:45:32 +05:30
2ec328059f func return type change 2025-07-26 21:45:09 +05:30
f700f3e891 refactor, generator comment change 2025-07-26 20:22:16 +05:30
3 changed files with 57 additions and 24 deletions

View File

@@ -36,7 +36,7 @@ func generate(scheamPath, outDir string) error {
// schema.go will hold all tables info
var sb strings.Builder
sb.WriteString("// Generated by code.patial.tech/go/pgm/cmd. DO NOT EDIT.\n\n")
sb.WriteString("// Code generated by code.patial.tech/go/pgm/cmd DO NOT EDIT.\n\n")
sb.WriteString(fmt.Sprintf("package %s \n", filepath.Base(outDir)))
sb.WriteString(`
import "code.patial.tech/go/pgm"
@@ -91,7 +91,7 @@ func generate(scheamPath, outDir string) error {
func writeColFile(tblName string, cols []*Column, outDir string, caser cases.Caser) error {
var sb strings.Builder
sb.WriteString("// Generated by code.patial.tech/go/pgm/cmd. DO NOT EDIT.\n\n")
sb.WriteString("// Code generated by code.patial.tech/go/pgm/cmd DO NOT EDIT.\n\n")
sb.WriteString(fmt.Sprintf("package %s\n\n", filepath.Base(outDir)))
sb.WriteString(fmt.Sprintf("import %q\n\n", "code.patial.tech/go/pgm"))
sb.WriteString("const (")

54
pgm.go
View File

@@ -4,11 +4,9 @@
package pgm
import (
"errors"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
@@ -51,6 +49,31 @@ func (f Field) Avg() Field {
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 {
col := f.String()
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}
}
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 {
col := f.String()
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 {
col := f.String()
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}
}
// IsNotFound error check
func IsNotFound(err error) bool {
return errors.Is(err, pgx.ErrNoRows)
func ConcatWs(sep string, fields ...Field) Field {
return Field("concat_ws('" + sep + "'," + joinFileds(fields) + ")")
}
func ConcatWs(sep string, fields ...Field) string {
return "concat_ws('" + sep + "'," + joinFileds(fields) + ")"
func StringAgg(exp, sep string) Field {
return Field("string_agg(" + exp + ",'" + sep + "')")
}
func StringAgg(exp, sep string) string {
return "string_agg(" + exp + ",'" + sep + "')"
}
func StringAggCast(exp, sep string) string {
return "string_agg(cast(" + exp + " as varchar),'" + sep + "')"
func StringAggCast(exp, sep string) Field {
return Field("string_agg(cast(" + exp + " as varchar),'" + sep + "')")
}

23
pool.go
View File

@@ -77,6 +77,17 @@ func InitPool(conf Config) {
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
func GetPool() *pgxpool.Pool {
return poolPGX.Load()
@@ -93,13 +104,7 @@ func BeginTx(ctx context.Context) (pgx.Tx, error) {
return tx, err
}
// 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)
// IsNotFound error check
func IsNotFound(err error) bool {
return errors.Is(err, pgx.ErrNoRows)
}