Add support for extra ORDER BY fields in RowNumber methods
- Updated RowNumber, RowNumberDesc, RowNumberPartionBy, and RowNumberDescPartionBy to accept variadic extraOrderBy parameters - Fixed bug in RowNumberDesc that was incorrectly using ASC instead of DESC - Enhanced rowNumber internal function to build ORDER BY clause with primary field and additional fields - Backwards compatible - existing code continues to work without extra fields - Added CLAUDE.md documentation for future Claude Code instances
This commit is contained in:
40
pgm_field.go
40
pgm_field.go
@@ -152,24 +152,24 @@ func (f Field) Desc() Field {
|
||||
return Field(f.String() + " DESC")
|
||||
}
|
||||
|
||||
func (f Field) RowNumber(as string) Field {
|
||||
return rowNumber(&f, nil, true, as)
|
||||
func (f Field) RowNumber(as string, extraOrderBy ...Field) Field {
|
||||
return rowNumber(&f, nil, true, as, extraOrderBy...)
|
||||
}
|
||||
|
||||
func (f Field) RowNumberDesc(as string) Field {
|
||||
return rowNumber(&f, nil, true, as)
|
||||
func (f Field) RowNumberDesc(as string, extraOrderBy ...Field) Field {
|
||||
return rowNumber(&f, nil, false, as, extraOrderBy...)
|
||||
}
|
||||
|
||||
// RowNumberPartionBy in ascending order
|
||||
func (f Field) RowNumberPartionBy(partition Field, as string) Field {
|
||||
return rowNumber(&f, &partition, true, as)
|
||||
func (f Field) RowNumberPartionBy(partition Field, as string, extraOrderBy ...Field) Field {
|
||||
return rowNumber(&f, &partition, true, as, extraOrderBy...)
|
||||
}
|
||||
|
||||
func (f Field) RowNumberDescPartionBy(partition Field, as string) Field {
|
||||
return rowNumber(&f, &partition, false, as)
|
||||
func (f Field) RowNumberDescPartionBy(partition Field, as string, extraOrderBy ...Field) Field {
|
||||
return rowNumber(&f, &partition, false, as, extraOrderBy...)
|
||||
}
|
||||
|
||||
func rowNumber(f, partition *Field, isAsc bool, as string) Field {
|
||||
func rowNumber(f, partition *Field, isAsc bool, as string, extraOrderBy ...Field) Field {
|
||||
// Validate as parameter is a valid SQL identifier
|
||||
if as != "" {
|
||||
if err := validateSQLIdentifier(as); err != nil {
|
||||
@@ -188,11 +188,27 @@ func rowNumber(f, partition *Field, isAsc bool, as string) Field {
|
||||
}
|
||||
|
||||
col := f.String()
|
||||
if partition != nil {
|
||||
return Field("ROW_NUMBER() OVER (PARTITION BY " + partition.String() + " ORDER BY " + col + orderBy + ") AS " + as)
|
||||
|
||||
// Build ORDER BY clause with primary field and extra fields
|
||||
sb := getSB()
|
||||
defer putSB(sb)
|
||||
|
||||
sb.WriteString(col)
|
||||
sb.WriteString(orderBy)
|
||||
|
||||
// Add extra ORDER BY fields
|
||||
for _, extra := range extraOrderBy {
|
||||
sb.WriteString(", ")
|
||||
sb.WriteString(extra.String())
|
||||
}
|
||||
|
||||
return Field("ROW_NUMBER() OVER (ORDER BY " + col + orderBy + ") AS " + as)
|
||||
orderByClause := sb.String()
|
||||
|
||||
if partition != nil {
|
||||
return Field("ROW_NUMBER() OVER (PARTITION BY " + partition.String() + " ORDER BY " + orderByClause + ") AS " + as)
|
||||
}
|
||||
|
||||
return Field("ROW_NUMBER() OVER (ORDER BY " + orderByClause + ") AS " + as)
|
||||
}
|
||||
|
||||
func (f Field) IsNull() Conditioner {
|
||||
|
||||
Reference in New Issue
Block a user