use group by instead of distinct when possible

This commit is contained in:
Gani Georgiev
2026-01-13 18:47:32 +02:00
parent 5f8cce3558
commit 0b9a646d41
5 changed files with 138 additions and 44 deletions

18
tools/dbutils/select.go Normal file
View File

@@ -0,0 +1,18 @@
package dbutils
import "regexp"
// Regexp for columns and tables (the same as the one in dbx).
var selectRegex = regexp.MustCompile(`(?i:\s+as\s+|\s+)([\w\-_\.]+)$`)
// AliasOrIdentifier returns the alias from a column or table identifier,
// Returns the identifier unmodified if no alias was found.
func AliasOrIdentifier(columnOrTableIdentifier string) string {
matches := selectRegex.FindStringSubmatch(columnOrTableIdentifier)
if len(matches) > 0 && matches[1] != "" {
return matches[1]
}
return columnOrTableIdentifier
}