Analyze inspects all statements in the given AST and returns an OptimizationResult. Each configured rule is applied to every statement in the AST. The result includes all suggestions, a complexity classification, and an optimization score.
(tree *ast.AST)
| 146 | // Each configured rule is applied to every statement in the AST. The result |
| 147 | // includes all suggestions, a complexity classification, and an optimization score. |
| 148 | func (o *Optimizer) Analyze(tree *ast.AST) *OptimizationResult { |
| 149 | if tree == nil { |
| 150 | return &OptimizationResult{ |
| 151 | Suggestions: []Suggestion{}, |
| 152 | QueryComplexity: ComplexitySimple, |
| 153 | Score: 100, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | var suggestions []Suggestion |
| 158 | |
| 159 | for _, stmt := range tree.Statements { |
| 160 | for _, rule := range o.rules { |
| 161 | ruleSuggestions := rule.Analyze(stmt) |
| 162 | suggestions = append(suggestions, ruleSuggestions...) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | complexity := classifyComplexity(tree) |
| 167 | score := calculateScore(suggestions) |
| 168 | |
| 169 | return &OptimizationResult{ |
| 170 | Suggestions: suggestions, |
| 171 | QueryComplexity: complexity, |
| 172 | Score: score, |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // AnalyzeSQL is a convenience method that parses the SQL string and analyzes it. |
| 177 | // |
no test coverage detected