sql_query_analyzer 0.5.2

Static analysis tool for SQL queries with 20 built-in rules for performance, security, and style
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Helper functions for CLI operations.
//!
//! This module provides utility functions used throughout the CLI application
//! for tasks such as reading input, calculating exit codes, building LLM
//! providers, and managing configuration defaults.

use std::{
    fs::read_to_string,
    io::{self, Read}
};

use super::convert::convert_format;
use crate::{
    cache::{cache_queries, get_cached},
    cli::{Format, Provider},
    error::{AppResult, config_error, file_read_error},
    llm::LlmProvider,
    output::OutputOptions,
    query::{Query, SqlDialect, parse_queries},
    rules::{AnalysisReport, Severity}
};

/// Calculates the process exit code based on violation severities.
///
/// Examines all violations in the analysis report and returns an exit
/// code reflecting the highest severity found:
/// - `0` - No violations or only informational messages
/// - `1` - At least one warning present
/// - `2` - At least one error present
///
/// # Arguments
///
/// * `report` - The analysis report containing violations
///
/// # Returns
///
/// An integer exit code (0, 1, or 2).
///
/// # Example
///
/// ```
/// use sql_query_analyzer::{app::calculate_exit_code, rules::AnalysisReport};
///
/// let report = AnalysisReport::new(1, 0);
/// assert_eq!(calculate_exit_code(&report), 0);
/// ```
pub fn calculate_exit_code(report: &AnalysisReport) -> i32 {
    if report
        .violations
        .iter()
        .any(|v| v.severity == Severity::Error)
    {
        2
    } else if report
        .violations
        .iter()
        .any(|v| v.severity == Severity::Warning)
    {
        1
    } else {
        0
    }
}

/// Reads SQL queries from a file or stdin.
///
/// Supports reading from a file path or from standard input when the
/// path is "-".
///
/// # Arguments
///
/// * `path` - File path or "-" for stdin
///
/// # Returns
///
/// The file contents as a string, or an error if reading fails.
///
/// # Errors
///
/// Returns an error if the file cannot be read or stdin fails.
pub fn read_queries_input(path: &str) -> AppResult<String> {
    if path == "-" {
        let mut buffer = String::new();
        io::stdin()
            .read_to_string(&mut buffer)
            .map_err(|e| file_read_error("stdin", e))?;
        Ok(buffer)
    } else {
        read_to_string(path).map_err(|e| file_read_error(path, e))
    }
}

/// Parses SQL queries with caching support.
///
/// Attempts to retrieve parsed queries from the cache first. If not
/// found, parses the SQL and stores the result in the cache for
/// future use.
///
/// # Arguments
///
/// * `sql` - Raw SQL string containing one or more queries
/// * `dialect` - SQL dialect for parsing
///
/// # Returns
///
/// A vector of parsed Query objects.
///
/// # Errors
///
/// Returns an error if SQL parsing fails.
pub fn parse_queries_cached(sql: &str, dialect: SqlDialect) -> AppResult<Vec<Query>> {
    if let Some(cached) = get_cached(sql) {
        Ok(cached)
    } else {
        let queries = parse_queries(sql, dialect)?;
        cache_queries(sql, queries.clone());
        Ok(queries)
    }
}

/// Creates output options from CLI parameters.
///
/// Constructs an `OutputOptions` struct from the CLI format, color,
/// and verbosity settings.
///
/// # Arguments
///
/// * `format` - Output format (text, json, yaml, sarif)
/// * `no_color` - Whether to disable colored output
/// * `verbose` - Whether to enable verbose output
///
/// # Returns
///
/// An `OutputOptions` struct configured with the given settings.
pub fn create_output_options(format: Format, no_color: bool, verbose: bool) -> OutputOptions {
    OutputOptions {
        format: convert_format(format),
        colored: !no_color,
        verbose
    }
}

/// Builds an LLM provider configuration from CLI parameters.
///
/// Constructs the appropriate `LlmProvider` variant based on the
/// selected provider type. For cloud providers (OpenAI, Anthropic),
/// an API key is required.
///
/// # Arguments
///
/// * `provider` - The LLM provider type
/// * `api_key` - Optional API key for cloud providers
/// * `model` - Model name to use
/// * `ollama_url` - Base URL for Ollama server
///
/// # Returns
///
/// An `LlmProvider` configured for the selected provider.
///
/// # Errors
///
/// Returns an error if a cloud provider is selected without an API key.
pub fn build_llm_provider(
    provider: Provider,
    api_key: Option<String>,
    model: String,
    ollama_url: String
) -> AppResult<LlmProvider> {
    match provider {
        Provider::OpenAI => {
            let key = api_key.ok_or_else(|| {
                config_error("API key required for OpenAI (use --api-key or LLM_API_KEY)")
            })?;
            Ok(LlmProvider::OpenAI {
                api_key: key,
                model
            })
        }
        Provider::Anthropic => {
            let key = api_key.ok_or_else(|| {
                config_error("API key required for Anthropic (use --api-key or LLM_API_KEY)")
            })?;
            Ok(LlmProvider::Anthropic {
                api_key: key,
                model
            })
        }
        Provider::Ollama => Ok(LlmProvider::Ollama {
            base_url: ollama_url,
            model
        })
    }
}

/// Checks if LLM access is available.
///
/// Determines whether LLM analysis can be performed based on the
/// provider type and API key availability. Ollama doesn't require
/// an API key, while cloud providers do.
///
/// # Arguments
///
/// * `api_key` - Optional API key
/// * `provider` - The LLM provider type
///
/// # Returns
///
/// `true` if LLM access is available, `false` otherwise.
pub fn has_llm_access(api_key: &Option<String>, provider: &Provider) -> bool {
    api_key.is_some() || matches!(provider, Provider::Ollama)
}

/// Gets the effective model name from available sources.
///
/// Resolves the model name in order of precedence:
/// 1. Explicitly provided model name
/// 2. Model from configuration file
/// 3. Default model for the provider
///
/// # Arguments
///
/// * `model` - Explicitly provided model name
/// * `config_model` - Model from configuration
/// * `provider` - The LLM provider (for default)
///
/// # Returns
///
/// The resolved model name.
pub fn get_effective_model(
    model: Option<String>,
    config_model: Option<String>,
    provider: &Provider
) -> String {
    model
        .or(config_model)
        .unwrap_or_else(|| provider.default_model().to_string())
}

/// Gets the effective Ollama URL from available sources.
///
/// Uses the config URL if the provided URL is the default localhost,
/// otherwise uses the explicitly provided URL.
///
/// # Arguments
///
/// * `url` - Explicitly provided URL
/// * `config_url` - URL from configuration
///
/// # Returns
///
/// The resolved Ollama URL.
pub fn get_effective_ollama_url(url: String, config_url: Option<String>) -> String {
    if url == "http://localhost:11434" {
        config_url.unwrap_or(url)
    } else {
        url
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rules::{AnalysisReport, RuleCategory, Violation};

    #[test]
    fn test_calculate_exit_code_no_violations() {
        let report = AnalysisReport::new(1, 1);
        assert_eq!(calculate_exit_code(&report), 0);
    }

    #[test]
    fn test_calculate_exit_code_info_only() {
        let mut report = AnalysisReport::new(1, 1);
        report.add_violation(Violation {
            rule_id:     "TEST",
            rule_name:   "Test",
            message:     "Test".to_string(),
            severity:    Severity::Info,
            category:    RuleCategory::Style,
            suggestion:  None,
            query_index: 0
        });
        assert_eq!(calculate_exit_code(&report), 0);
    }

    #[test]
    fn test_calculate_exit_code_warning() {
        let mut report = AnalysisReport::new(1, 1);
        report.add_violation(Violation {
            rule_id:     "TEST",
            rule_name:   "Test",
            message:     "Test".to_string(),
            severity:    Severity::Warning,
            category:    RuleCategory::Performance,
            suggestion:  None,
            query_index: 0
        });
        assert_eq!(calculate_exit_code(&report), 1);
    }

    #[test]
    fn test_calculate_exit_code_error() {
        let mut report = AnalysisReport::new(1, 1);
        report.add_violation(Violation {
            rule_id:     "TEST",
            rule_name:   "Test",
            message:     "Test".to_string(),
            severity:    Severity::Error,
            category:    RuleCategory::Security,
            suggestion:  None,
            query_index: 0
        });
        assert_eq!(calculate_exit_code(&report), 2);
    }

    #[test]
    fn test_calculate_exit_code_error_takes_precedence() {
        let mut report = AnalysisReport::new(1, 1);
        report.add_violation(Violation {
            rule_id:     "W1",
            rule_name:   "Warning",
            message:     "Warning".to_string(),
            severity:    Severity::Warning,
            category:    RuleCategory::Performance,
            suggestion:  None,
            query_index: 0
        });
        report.add_violation(Violation {
            rule_id:     "E1",
            rule_name:   "Error",
            message:     "Error".to_string(),
            severity:    Severity::Error,
            category:    RuleCategory::Security,
            suggestion:  None,
            query_index: 0
        });
        assert_eq!(calculate_exit_code(&report), 2);
    }

    #[test]
    fn test_has_llm_access_with_api_key() {
        assert!(has_llm_access(&Some("key".to_string()), &Provider::OpenAI));
    }

    #[test]
    fn test_has_llm_access_ollama_no_key() {
        assert!(has_llm_access(&None, &Provider::Ollama));
    }

    #[test]
    fn test_has_llm_access_openai_no_key() {
        assert!(!has_llm_access(&None, &Provider::OpenAI));
    }

    #[test]
    fn test_has_llm_access_anthropic_no_key() {
        assert!(!has_llm_access(&None, &Provider::Anthropic));
    }

    #[test]
    fn test_get_effective_model_explicit() {
        let model = get_effective_model(Some("gpt-4o".to_string()), None, &Provider::OpenAI);
        assert_eq!(model, "gpt-4o");
    }

    #[test]
    fn test_get_effective_model_from_config() {
        let model = get_effective_model(None, Some("claude-3".to_string()), &Provider::Anthropic);
        assert_eq!(model, "claude-3");
    }

    #[test]
    fn test_get_effective_model_default() {
        let model = get_effective_model(None, None, &Provider::OpenAI);
        assert_eq!(model, "gpt-4");
    }

    #[test]
    fn test_get_effective_ollama_url_explicit() {
        let url = get_effective_ollama_url(
            "http://custom:11434".to_string(),
            Some("http://other:11434".to_string())
        );
        assert_eq!(url, "http://custom:11434");
    }

    #[test]
    fn test_get_effective_ollama_url_from_config() {
        let url = get_effective_ollama_url(
            "http://localhost:11434".to_string(),
            Some("http://config:11434".to_string())
        );
        assert_eq!(url, "http://config:11434");
    }

    #[test]
    fn test_get_effective_ollama_url_default() {
        let url = get_effective_ollama_url("http://localhost:11434".to_string(), None);
        assert_eq!(url, "http://localhost:11434");
    }

    #[test]
    fn test_create_output_options_text_colored() {
        let opts = create_output_options(Format::Text, false, true);
        assert!(matches!(opts.format, crate::output::OutputFormat::Text));
        assert!(opts.colored);
        assert!(opts.verbose);
    }

    #[test]
    fn test_create_output_options_json_no_color() {
        let opts = create_output_options(Format::Json, true, false);
        assert!(matches!(opts.format, crate::output::OutputFormat::Json));
        assert!(!opts.colored);
        assert!(!opts.verbose);
    }

    #[test]
    fn test_build_llm_provider_ollama() {
        let provider = build_llm_provider(
            Provider::Ollama,
            None,
            "llama3".to_string(),
            "http://localhost:11434".to_string()
        )
        .unwrap();
        assert!(matches!(provider, LlmProvider::Ollama { .. }));
    }

    #[test]
    fn test_build_llm_provider_openai_no_key() {
        let result = build_llm_provider(
            Provider::OpenAI,
            None,
            "gpt-4".to_string(),
            "http://localhost:11434".to_string()
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_build_llm_provider_openai_with_key() {
        let provider = build_llm_provider(
            Provider::OpenAI,
            Some("sk-test".to_string()),
            "gpt-4".to_string(),
            "http://localhost:11434".to_string()
        )
        .unwrap();
        assert!(matches!(provider, LlmProvider::OpenAI { .. }));
    }

    #[test]
    fn test_build_llm_provider_anthropic_no_key() {
        let result = build_llm_provider(
            Provider::Anthropic,
            None,
            "claude-3".to_string(),
            "http://localhost:11434".to_string()
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_build_llm_provider_anthropic_with_key() {
        let provider = build_llm_provider(
            Provider::Anthropic,
            Some("sk-test".to_string()),
            "claude-3".to_string(),
            "http://localhost:11434".to_string()
        )
        .unwrap();
        assert!(matches!(provider, LlmProvider::Anthropic { .. }));
    }

    #[test]
    fn test_parse_queries_cached() {
        let sql = "SELECT id FROM test_cached_table_helpers";
        let queries1 = parse_queries_cached(sql, SqlDialect::Generic).unwrap();
        let queries2 = parse_queries_cached(sql, SqlDialect::Generic).unwrap();
        assert_eq!(queries1.len(), queries2.len());
    }
}