|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/parser" |
| 10 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer" |
| 11 | +) |
| 12 | + |
| 13 | +// ValidationResult holds the result of validating a single SQL file |
| 14 | +type ValidationResult struct { |
| 15 | + FilePath string |
| 16 | + Valid bool |
| 17 | + Error error |
| 18 | +} |
| 19 | + |
| 20 | +// ValidateFile validates a single SQL file |
| 21 | +func ValidateFile(filePath string) ValidationResult { |
| 22 | + // Read the file |
| 23 | + content, err := os.ReadFile(filePath) |
| 24 | + if err != nil { |
| 25 | + return ValidationResult{ |
| 26 | + FilePath: filePath, |
| 27 | + Valid: false, |
| 28 | + Error: fmt.Errorf("failed to read file: %w", err), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Get tokenizer from pool |
| 33 | + tkz := tokenizer.GetTokenizer() |
| 34 | + defer tokenizer.PutTokenizer(tkz) |
| 35 | + |
| 36 | + // Tokenize the SQL |
| 37 | + tokens, err := tkz.Tokenize(content) |
| 38 | + if err != nil { |
| 39 | + return ValidationResult{ |
| 40 | + FilePath: filePath, |
| 41 | + Valid: false, |
| 42 | + Error: fmt.Errorf("tokenization error: %w", err), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Convert tokens for parser |
| 47 | + parserTokens, err := parser.ConvertTokensForParser(tokens) |
| 48 | + if err != nil { |
| 49 | + return ValidationResult{ |
| 50 | + FilePath: filePath, |
| 51 | + Valid: false, |
| 52 | + Error: fmt.Errorf("token conversion error: %w", err), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Create parser |
| 57 | + p := parser.NewParser() |
| 58 | + defer p.Release() |
| 59 | + |
| 60 | + // Parse the tokens |
| 61 | + _, err = p.Parse(parserTokens) |
| 62 | + if err != nil { |
| 63 | + return ValidationResult{ |
| 64 | + FilePath: filePath, |
| 65 | + Valid: false, |
| 66 | + Error: fmt.Errorf("parse error: %w", err), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return ValidationResult{ |
| 71 | + FilePath: filePath, |
| 72 | + Valid: true, |
| 73 | + Error: nil, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// ValidateDirectory recursively validates all .sql files in a directory |
| 78 | +func ValidateDirectory(dirPath string) ([]ValidationResult, error) { |
| 79 | + var results []ValidationResult |
| 80 | + |
| 81 | + err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + // Skip directories |
| 87 | + if info.IsDir() { |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + // Only process .sql files |
| 92 | + if !strings.HasSuffix(strings.ToLower(path), ".sql") { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + // Validate the file |
| 97 | + result := ValidateFile(path) |
| 98 | + results = append(results, result) |
| 99 | + |
| 100 | + return nil |
| 101 | + }) |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + return nil, fmt.Errorf("failed to walk directory: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + return results, nil |
| 108 | +} |
| 109 | + |
| 110 | +// PrintResults prints validation results in a user-friendly format |
| 111 | +func PrintResults(results []ValidationResult) { |
| 112 | + validCount := 0 |
| 113 | + invalidCount := 0 |
| 114 | + |
| 115 | + fmt.Println("\n=== SQL Validation Results ===\n") |
| 116 | + |
| 117 | + for _, result := range results { |
| 118 | + if result.Valid { |
| 119 | + fmt.Printf("✓ %s\n", result.FilePath) |
| 120 | + validCount++ |
| 121 | + } else { |
| 122 | + fmt.Printf("✗ %s\n", result.FilePath) |
| 123 | + fmt.Printf(" Error: %v\n\n", result.Error) |
| 124 | + invalidCount++ |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fmt.Printf("\n=== Summary ===\n") |
| 129 | + fmt.Printf("Total files: %d\n", len(results)) |
| 130 | + fmt.Printf("Valid: %d\n", validCount) |
| 131 | + fmt.Printf("Invalid: %d\n", invalidCount) |
| 132 | +} |
0 commit comments