package build import ( "fmt" "strings" "git.secnex.io/secnex/pgson/schema" "git.secnex.io/secnex/pgson/utils" ) func UpdateSQL(s *schema.Table, data map[string]any, where string) (string, error) { // Create a map for quick field lookup fieldMap := make(map[string]*schema.Field) for i := range s.Schema { fieldMap[s.Schema[i].Name] = &s.Schema[i] } setClause := make([]string, 0, len(data)) for field, value := range data { // Check if field is of type "hash" and needs hashing if schemaField, exists := fieldMap[field]; exists && schemaField.Type == "hash" && schemaField.Algorithm != nil { valueStr := fmt.Sprintf("%v", value) hashed, err := utils.Hash(valueStr, *schemaField.Algorithm) if err != nil { return "", err } value = hashed } setClause = append(setClause, fmt.Sprintf("%s = %s", utils.SQLQuoteIdent(field), utils.SQLQuoteValue(value))) } setClause = append(setClause, "updated_at = CURRENT_TIMESTAMP") return fmt.Sprintf("UPDATE %s SET %s WHERE %s = %s", utils.SQLQuoteIdent(s.Name), strings.Join(setClause, ", "), utils.SQLQuoteIdent(s.PrimaryKey), utils.SQLQuoteValue(where)), nil }