statementComplexity scores the complexity of a single statement.
(stmt ast.Statement)
| 210 | |
| 211 | // statementComplexity scores the complexity of a single statement. |
| 212 | func statementComplexity(stmt ast.Statement) int { |
| 213 | score := 0 |
| 214 | |
| 215 | switch s := stmt.(type) { |
| 216 | case *ast.SelectStatement: |
| 217 | // Base complexity |
| 218 | score++ |
| 219 | |
| 220 | // JOINs add complexity |
| 221 | score += len(s.Joins) |
| 222 | |
| 223 | // Subqueries in FROM |
| 224 | for _, from := range s.From { |
| 225 | if from.Subquery != nil { |
| 226 | score += 2 |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // GROUP BY |
| 231 | if len(s.GroupBy) > 0 { |
| 232 | score++ |
| 233 | } |
| 234 | |
| 235 | // HAVING |
| 236 | if s.Having != nil { |
| 237 | score++ |
| 238 | } |
| 239 | |
| 240 | // Window functions |
| 241 | if len(s.Windows) > 0 { |
| 242 | score++ |
| 243 | } |
| 244 | |
| 245 | // CTE (WITH clause) |
| 246 | if s.With != nil { |
| 247 | score += len(s.With.CTEs) |
| 248 | } |
| 249 | |
| 250 | // Subqueries in WHERE |
| 251 | if s.Where != nil { |
| 252 | score += countSubqueries(s.Where) |
| 253 | } |
| 254 | |
| 255 | case *ast.SetOperation: |
| 256 | score += statementComplexity(s.Left) + statementComplexity(s.Right) + 1 |
| 257 | |
| 258 | default: |
| 259 | score++ |
| 260 | } |
| 261 | |
| 262 | return score |
| 263 | } |
| 264 | |
| 265 | // countSubqueries recursively counts subquery expressions in an expression tree. |
| 266 | func countSubqueries(expr ast.Expression) int { |
no test coverage detected