fix(sql_helpers): strip empty RHS conditions from SQL strings

* Add regex patterns to identify and remove empty comparisons
* Implement tests for stripping empty RHS conditions
fix(handler): prevent duplicate JOIN aliases from preload
* Skip custom SQL JOINs if alias already provided by preload
* Split multiple JOIN clauses for individual alias handling
This commit is contained in:
Hein
2026-05-15 13:35:24 +02:00
parent 2ae4d07544
commit ae9e06c98b
5 changed files with 133 additions and 13 deletions

View File

@@ -575,11 +575,25 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
}
}
// Apply custom SQL JOIN clauses
// Apply custom SQL JOIN clauses, skipping any whose alias is already provided by a
// preload LEFT JOIN (to prevent "table name specified more than once" errors).
if len(options.CustomSQLJoin) > 0 {
for _, joinClause := range options.CustomSQLJoin {
preloadAliasSet := make(map[string]bool, len(options.Preload))
for _, p := range options.Preload {
if alias := common.RelationPathToBunAlias(p.Relation); alias != "" {
preloadAliasSet[alias] = true
}
}
for i, joinClause := range options.CustomSQLJoin {
if i < len(options.JoinAliases) && options.JoinAliases[i] != "" {
alias := strings.ToLower(options.JoinAliases[i])
if preloadAliasSet[alias] {
logger.Debug("Skipping custom SQL JOIN (alias '%s' already joined by preload): %s", alias, joinClause)
continue
}
}
logger.Debug("Applying custom SQL JOIN: %s", joinClause)
// Joins are already sanitized during parsing, so we can apply them directly
query = query.Join(joinClause)
}
}