|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/bitechdev/ResolveSpec/pkg/logger" |
| 8 | +) |
| 9 | + |
| 10 | +// ValidateAndFixPreloadWhere validates that the WHERE clause for a preload contains |
| 11 | +// the relation prefix (alias). If not present, it attempts to add it to column references. |
| 12 | +// Returns the fixed WHERE clause and an error if it cannot be safely fixed. |
| 13 | +func ValidateAndFixPreloadWhere(where string, relationName string) (string, error) { |
| 14 | + if where == "" { |
| 15 | + return where, nil |
| 16 | + } |
| 17 | + |
| 18 | + // Check if the relation name is already present in the WHERE clause |
| 19 | + lowerWhere := strings.ToLower(where) |
| 20 | + lowerRelation := strings.ToLower(relationName) |
| 21 | + |
| 22 | + // Check for patterns like "relation.", "relation ", or just "relation" followed by a dot |
| 23 | + if strings.Contains(lowerWhere, lowerRelation+".") || |
| 24 | + strings.Contains(lowerWhere, "`"+lowerRelation+"`.") || |
| 25 | + strings.Contains(lowerWhere, "\""+lowerRelation+"\".") { |
| 26 | + // Relation prefix is already present |
| 27 | + return where, nil |
| 28 | + } |
| 29 | + |
| 30 | + // If the WHERE clause is complex (contains OR, parentheses, subqueries, etc.), |
| 31 | + // we can't safely auto-fix it - require explicit prefix |
| 32 | + if strings.Contains(lowerWhere, " or ") || |
| 33 | + strings.Contains(where, "(") || |
| 34 | + strings.Contains(where, ")") { |
| 35 | + return "", fmt.Errorf("preload WHERE condition must reference the relation '%s' (e.g., '%s.column_name'). Complex WHERE clauses with OR/parentheses must explicitly use the relation prefix", relationName, relationName) |
| 36 | + } |
| 37 | + |
| 38 | + // Try to add the relation prefix to simple column references |
| 39 | + // This handles basic cases like "column = value" or "column = value AND other_column = value" |
| 40 | + // Split by AND to handle multiple conditions (case-insensitive) |
| 41 | + originalConditions := strings.Split(where, " AND ") |
| 42 | + |
| 43 | + // If uppercase split didn't work, try lowercase |
| 44 | + if len(originalConditions) == 1 { |
| 45 | + originalConditions = strings.Split(where, " and ") |
| 46 | + } |
| 47 | + |
| 48 | + fixedConditions := make([]string, 0, len(originalConditions)) |
| 49 | + |
| 50 | + for _, cond := range originalConditions { |
| 51 | + cond = strings.TrimSpace(cond) |
| 52 | + if cond == "" { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // Check if this condition already has a table prefix (contains a dot) |
| 57 | + if strings.Contains(cond, ".") { |
| 58 | + fixedConditions = append(fixedConditions, cond) |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + // Check if this is a SQL expression/literal that shouldn't be prefixed |
| 63 | + lowerCond := strings.ToLower(strings.TrimSpace(cond)) |
| 64 | + if IsSQLExpression(lowerCond) { |
| 65 | + // Don't prefix SQL expressions like "true", "false", "1=1", etc. |
| 66 | + fixedConditions = append(fixedConditions, cond) |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + // Extract the column name (first identifier before operator) |
| 71 | + columnName := ExtractColumnName(cond) |
| 72 | + if columnName == "" { |
| 73 | + // Can't identify column name, require explicit prefix |
| 74 | + return "", fmt.Errorf("preload WHERE condition must reference the relation '%s' (e.g., '%s.column_name'). Cannot auto-fix condition: %s", relationName, relationName, cond) |
| 75 | + } |
| 76 | + |
| 77 | + // Add relation prefix to the column name only |
| 78 | + fixedCond := strings.Replace(cond, columnName, relationName+"."+columnName, 1) |
| 79 | + fixedConditions = append(fixedConditions, fixedCond) |
| 80 | + } |
| 81 | + |
| 82 | + fixedWhere := strings.Join(fixedConditions, " AND ") |
| 83 | + logger.Debug("Auto-fixed preload WHERE clause: '%s' -> '%s'", where, fixedWhere) |
| 84 | + return fixedWhere, nil |
| 85 | +} |
| 86 | + |
| 87 | +// IsSQLExpression checks if a condition is a SQL expression that shouldn't be prefixed |
| 88 | +func IsSQLExpression(cond string) bool { |
| 89 | + // Common SQL literals and expressions |
| 90 | + sqlLiterals := []string{"true", "false", "null", "1=1", "1 = 1", "0=0", "0 = 0"} |
| 91 | + for _, literal := range sqlLiterals { |
| 92 | + if cond == literal { |
| 93 | + return true |
| 94 | + } |
| 95 | + } |
| 96 | + return false |
| 97 | +} |
| 98 | + |
| 99 | +// ExtractColumnName extracts the column name from a WHERE condition |
| 100 | +// For example: "status = 'active'" returns "status" |
| 101 | +func ExtractColumnName(cond string) string { |
| 102 | + // Common SQL operators |
| 103 | + operators := []string{" = ", " != ", " <> ", " > ", " >= ", " < ", " <= ", " LIKE ", " like ", " IN ", " in ", " IS ", " is "} |
| 104 | + |
| 105 | + for _, op := range operators { |
| 106 | + if idx := strings.Index(cond, op); idx > 0 { |
| 107 | + columnName := strings.TrimSpace(cond[:idx]) |
| 108 | + // Remove quotes if present |
| 109 | + columnName = strings.Trim(columnName, "`\"'") |
| 110 | + return columnName |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // If no operator found, check if it's a simple identifier (for boolean columns) |
| 115 | + parts := strings.Fields(cond) |
| 116 | + if len(parts) > 0 { |
| 117 | + columnName := strings.Trim(parts[0], "`\"'") |
| 118 | + // Check if it's a valid identifier (not a SQL keyword) |
| 119 | + if !IsSQLKeyword(strings.ToLower(columnName)) { |
| 120 | + return columnName |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return "" |
| 125 | +} |
| 126 | + |
| 127 | +// IsSQLKeyword checks if a string is a SQL keyword that shouldn't be treated as a column name |
| 128 | +func IsSQLKeyword(word string) bool { |
| 129 | + keywords := []string{"select", "from", "where", "and", "or", "not", "in", "is", "null", "true", "false", "like", "between", "exists"} |
| 130 | + for _, kw := range keywords { |
| 131 | + if word == kw { |
| 132 | + return true |
| 133 | + } |
| 134 | + } |
| 135 | + return false |
| 136 | +} |
0 commit comments