Simplify: O2M batch, domain dedup, db.go split, constants
Performance: - O2M formatO2MFields: N+1 → single batched query per O2M field (collect parent IDs, WHERE inverse IN (...), group by parent) Code dedup: - domain.go compileSimpleCondition: 80 lines → 12 lines by delegating to compileQualifiedCondition (eliminates duplicate operator handling) - db.go SeedWithSetup: 170-line monolith → 5 focused sub-functions (seedCurrencyAndCountry, seedCompanyAndAdmin, seedJournalsAndSequences, seedChartOfAccounts, resetSequences) Quality: - Magic numbers (80, 200, 8) replaced with named constants - Net -34 lines Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -242,87 +242,18 @@ func (dc *DomainCompiler) compileCondition(c Condition) (string, error) {
|
||||
}
|
||||
|
||||
func (dc *DomainCompiler) compileSimpleCondition(column, operator string, value Value) (string, error) {
|
||||
paramIdx := len(dc.params) + 1
|
||||
|
||||
// Operators that need special handling (not in compileQualifiedCondition)
|
||||
switch operator {
|
||||
case "=", "!=", "<", ">", "<=", ">=":
|
||||
if value == nil || value == false {
|
||||
if operator == "=" {
|
||||
return fmt.Sprintf("%q IS NULL", column), nil
|
||||
}
|
||||
return fmt.Sprintf("%q IS NOT NULL", column), nil
|
||||
}
|
||||
dc.params = append(dc.params, value)
|
||||
return fmt.Sprintf("%q %s $%d", column, operator, paramIdx), nil
|
||||
|
||||
case "in":
|
||||
vals := normalizeSlice(value)
|
||||
if vals == nil {
|
||||
return "", fmt.Errorf("'in' operator requires a slice value")
|
||||
}
|
||||
if len(vals) == 0 {
|
||||
return "FALSE", nil
|
||||
}
|
||||
placeholders := make([]string, len(vals))
|
||||
for i, v := range vals {
|
||||
dc.params = append(dc.params, v)
|
||||
placeholders[i] = fmt.Sprintf("$%d", paramIdx+i)
|
||||
}
|
||||
return fmt.Sprintf("%q IN (%s)", column, strings.Join(placeholders, ", ")), nil
|
||||
|
||||
case "not in":
|
||||
vals := normalizeSlice(value)
|
||||
if vals == nil {
|
||||
return "", fmt.Errorf("'not in' operator requires a slice value")
|
||||
}
|
||||
if len(vals) == 0 {
|
||||
return "TRUE", nil
|
||||
}
|
||||
placeholders := make([]string, len(vals))
|
||||
for i, v := range vals {
|
||||
dc.params = append(dc.params, v)
|
||||
placeholders[i] = fmt.Sprintf("$%d", paramIdx+i)
|
||||
}
|
||||
return fmt.Sprintf("%q NOT IN (%s)", column, strings.Join(placeholders, ", ")), nil
|
||||
|
||||
case "like":
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q LIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "not like":
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q NOT LIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "ilike":
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q ILIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "not ilike":
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q NOT ILIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "=like":
|
||||
dc.params = append(dc.params, value)
|
||||
return fmt.Sprintf("%q LIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "=ilike":
|
||||
dc.params = append(dc.params, value)
|
||||
return fmt.Sprintf("%q ILIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "child_of":
|
||||
return dc.compileHierarchyOp(column, value, true)
|
||||
|
||||
case "parent_of":
|
||||
return dc.compileHierarchyOp(column, value, false)
|
||||
|
||||
case "any":
|
||||
return dc.compileAnyOp(column, value, false)
|
||||
|
||||
case "not any":
|
||||
return dc.compileAnyOp(column, value, true)
|
||||
|
||||
default:
|
||||
return "", fmt.Errorf("unhandled operator: %q", operator)
|
||||
return dc.compileQualifiedCondition(fmt.Sprintf("%q", column), operator, value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user