perf enhancement
This commit is contained in:
19
pgm_field.go
19
pgm_field.go
@@ -45,7 +45,12 @@ func escapeSQLString(s string) string {
|
||||
}
|
||||
|
||||
func (f Field) Name() string {
|
||||
return strings.Split(string(f), ".")[1]
|
||||
s := string(f)
|
||||
idx := strings.LastIndexByte(s, '.')
|
||||
if idx == -1 {
|
||||
return s // Return as-is if no dot
|
||||
}
|
||||
return s[idx+1:] // Return part after last dot
|
||||
}
|
||||
|
||||
func (f Field) String() string {
|
||||
@@ -66,19 +71,19 @@ func ConcatWs(sep string, fields ...Field) Field {
|
||||
}
|
||||
|
||||
// StringAgg creates a STRING_AGG SQL function.
|
||||
// SECURITY: The exp parameter must be a valid field/column name, not arbitrary SQL.
|
||||
// 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 StringAgg(exp, sep string) Field {
|
||||
func StringAgg(field Field, sep string) Field {
|
||||
escapedSep := escapeSQLString(sep)
|
||||
return Field("string_agg(" + exp + ",'" + escapedSep + "')")
|
||||
return Field("string_agg(" + field.String() + ",'" + escapedSep + "')")
|
||||
}
|
||||
|
||||
// StringAggCast creates a STRING_AGG SQL function with cast to varchar.
|
||||
// SECURITY: The exp parameter must be a valid field/column name, not arbitrary SQL.
|
||||
// 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(exp, sep string) Field {
|
||||
func StringAggCast(field Field, sep string) Field {
|
||||
escapedSep := escapeSQLString(sep)
|
||||
return Field("string_agg(cast(" + exp + " as varchar),'" + escapedSep + "')")
|
||||
return Field("string_agg(cast(" + field.String() + " as varchar),'" + escapedSep + "')")
|
||||
}
|
||||
|
||||
// StringEscape will wrap field with:
|
||||
|
||||
Reference in New Issue
Block a user