|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gosqlx |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/ast" |
| 21 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/parser" |
| 22 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer" |
| 23 | +) |
| 24 | + |
| 25 | +// AnalysisResult contains the output of a SQL optimization analysis. |
| 26 | +type AnalysisResult struct { |
| 27 | + // Suggestions contains optimization recommendations. |
| 28 | + Suggestions []AnalysisSuggestion |
| 29 | + |
| 30 | + // QueryComplexity is one of "simple", "moderate", or "complex". |
| 31 | + QueryComplexity string |
| 32 | + |
| 33 | + // Score is 0-100 where 100 means no issues found. |
| 34 | + Score int |
| 35 | +} |
| 36 | + |
| 37 | +// AnalysisSuggestion represents a single optimization recommendation. |
| 38 | +type AnalysisSuggestion struct { |
| 39 | + RuleID string // e.g., "OPT-001" |
| 40 | + Severity string // "info", "warning", or "error" |
| 41 | + Message string // Short description |
| 42 | + Detail string // Detailed explanation |
| 43 | +} |
| 44 | + |
| 45 | +// Analyze runs basic optimization analysis on the given SQL, checking for |
| 46 | +// common anti-patterns such as SELECT *, missing WHERE clauses, and cartesian |
| 47 | +// products. |
| 48 | +// |
| 49 | +// For full optimization analysis with all 20 built-in rules (OPT-001 through |
| 50 | +// OPT-020), use pkg/advisor.New().AnalyzeSQL() directly. This function provides |
| 51 | +// a quick check for the most common issues without requiring an additional import. |
| 52 | +// |
| 53 | +// Thread Safety: safe for concurrent use. |
| 54 | +// |
| 55 | +// Example: |
| 56 | +// |
| 57 | +// result, err := gosqlx.Analyze("SELECT * FROM users") |
| 58 | +// if err != nil { |
| 59 | +// log.Fatal(err) |
| 60 | +// } |
| 61 | +// fmt.Printf("Complexity: %s\n", result.QueryComplexity) |
| 62 | +// for _, s := range result.Suggestions { |
| 63 | +// fmt.Printf("[%s] %s\n", s.RuleID, s.Message) |
| 64 | +// } |
| 65 | +func Analyze(sql string) (*AnalysisResult, error) { |
| 66 | + tkz := tokenizer.GetTokenizer() |
| 67 | + defer tokenizer.PutTokenizer(tkz) |
| 68 | + |
| 69 | + tokens, err := tkz.Tokenize([]byte(sql)) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("tokenization failed: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + p := parser.GetParser() |
| 75 | + defer parser.PutParser(p) |
| 76 | + |
| 77 | + tree, err := p.ParseFromModelTokens(tokens) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("parsing failed: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + var suggestions []AnalysisSuggestion |
| 83 | + |
| 84 | + for _, stmt := range tree.Statements { |
| 85 | + suggestions = append(suggestions, analyzeStatement(stmt)...) |
| 86 | + } |
| 87 | + |
| 88 | + complexity := "simple" |
| 89 | + if len(tree.Statements) > 0 { |
| 90 | + complexity = classifyQueryComplexity(tree.Statements[0]) |
| 91 | + } |
| 92 | + |
| 93 | + score := 100 |
| 94 | + for range suggestions { |
| 95 | + score -= 10 |
| 96 | + } |
| 97 | + if score < 0 { |
| 98 | + score = 0 |
| 99 | + } |
| 100 | + |
| 101 | + return &AnalysisResult{ |
| 102 | + Suggestions: suggestions, |
| 103 | + QueryComplexity: complexity, |
| 104 | + Score: score, |
| 105 | + }, nil |
| 106 | +} |
| 107 | + |
| 108 | +// analyzeStatement runs basic optimization checks on a single statement. |
| 109 | +func analyzeStatement(stmt ast.Statement) []AnalysisSuggestion { |
| 110 | + var suggestions []AnalysisSuggestion |
| 111 | + |
| 112 | + sel, ok := stmt.(*ast.SelectStatement) |
| 113 | + if !ok { |
| 114 | + return suggestions |
| 115 | + } |
| 116 | + |
| 117 | + // OPT-001: SELECT * detection |
| 118 | + for _, col := range sel.Columns { |
| 119 | + if id, ok := col.(*ast.Identifier); ok && id.Name == "*" { |
| 120 | + suggestions = append(suggestions, AnalysisSuggestion{ |
| 121 | + RuleID: "OPT-001", |
| 122 | + Severity: "warning", |
| 123 | + Message: "Avoid SELECT *; list columns explicitly", |
| 124 | + Detail: "SELECT * retrieves all columns, which increases I/O and can break when schema changes. List only the columns you need.", |
| 125 | + }) |
| 126 | + break |
| 127 | + } |
| 128 | + if ae, ok := col.(*ast.AliasedExpression); ok { |
| 129 | + if id, ok := ae.Expr.(*ast.Identifier); ok && id.Name == "*" { |
| 130 | + suggestions = append(suggestions, AnalysisSuggestion{ |
| 131 | + RuleID: "OPT-001", |
| 132 | + Severity: "warning", |
| 133 | + Message: "Avoid SELECT *; list columns explicitly", |
| 134 | + Detail: "SELECT * retrieves all columns, which increases I/O and can break when schema changes. List only the columns you need.", |
| 135 | + }) |
| 136 | + break |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return suggestions |
| 142 | +} |
| 143 | + |
| 144 | +// classifyQueryComplexity returns a rough complexity classification. |
| 145 | +func classifyQueryComplexity(stmt ast.Statement) string { |
| 146 | + sel, ok := stmt.(*ast.SelectStatement) |
| 147 | + if !ok { |
| 148 | + return "simple" |
| 149 | + } |
| 150 | + |
| 151 | + score := 0 |
| 152 | + if len(sel.Joins) > 0 { |
| 153 | + score += len(sel.Joins) |
| 154 | + } |
| 155 | + if sel.GroupBy != nil { |
| 156 | + score++ |
| 157 | + } |
| 158 | + if sel.Having != nil { |
| 159 | + score++ |
| 160 | + } |
| 161 | + if sel.With != nil { |
| 162 | + score += 2 |
| 163 | + } |
| 164 | + |
| 165 | + switch { |
| 166 | + case score >= 5: |
| 167 | + return "complex" |
| 168 | + case score >= 2: |
| 169 | + return "moderate" |
| 170 | + default: |
| 171 | + return "simple" |
| 172 | + } |
| 173 | +} |
0 commit comments