cqlite-cli 0.11.0

Command-line interface for CQLite — read Apache Cassandra 5.0 SSTables without a cluster
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Script execution module for handling CQL script files
//!
//! This module provides functionality to execute CQL scripts from files,
//! processing multiple statements sequentially with proper error handling
//! and output formatting.

use anyhow::{Context, Result};
use cqlite_core::Database;
use std::path::Path;

use crate::cli::OutputFormat;
use crate::config::OutputConfig;
use crate::error::{print_error, CliExitCode};

/// Execute a CQL script file containing multiple statements
///
/// This function parses a CQL script file and executes each statement
/// sequentially against the provided database. If any statement fails,
/// execution stops immediately and an error is returned.
///
/// # Arguments
///
/// * `file_path` - Path to the CQL script file
/// * `database` - Database instance to execute statements against
/// * `output_config` - Output configuration (color, pagination, etc.)
/// * `format` - Output format (table, json, csv, parquet)
///
/// # Returns
///
/// * `Ok(())` - Script executed successfully
/// * `Err(_)` - Error occurred during script parsing or execution
///
/// # Exit Code
///
/// On query execution errors, this function prints the error and exits
/// with code 5 (as per M2_CLI_SPEC.md line 340).
///
/// # Examples
///
/// ```no_run
/// use std::path::Path;
/// use cqlite_core::Database;
/// use cqlite_cli::config::OutputConfig;
/// use cqlite_cli::cli::OutputFormat;
/// use cqlite_cli::script_executor::execute_script_file;
///
/// # async fn example() -> anyhow::Result<()> {
/// let db = Database::open(Path::new("test.db"), Default::default()).await?;
/// let config = OutputConfig::default();
///
/// execute_script_file(
///     Path::new("script.cql"),
///     &db,
///     &config,
///     OutputFormat::Table,
/// ).await?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "state_machine")]
pub async fn execute_script_file(
    file_path: &Path,
    database: &Database,
    output_config: &OutputConfig,
    format: OutputFormat,
) -> Result<()> {
    // Parse the script file
    let statements = load_script(file_path)
        .with_context(|| format!("Failed to parse script file: {}", file_path.display()))?;

    if statements.is_empty() {
        eprintln!("Warning: Script file contains no statements");
        return Ok(());
    }

    println!(
        "Executing {} statement(s) from {}",
        statements.len(),
        file_path.display()
    );

    // Execute each statement sequentially
    for (index, statement) in statements.iter().enumerate() {
        let statement_num = index + 1;

        // Execute the statement using the existing execute_query function
        if let Err(e) = crate::commands::execute_query(
            database,
            statement,
            false, // explain
            false, // timing
            format.clone(),
            output_config,
        )
        .await
        {
            // Print error with statement context and helpful hint
            eprintln!(
                "Error executing statement {} in {}",
                statement_num,
                file_path.display()
            );
            eprintln!("Statement: {}", statement);
            print_error(&e, CliExitCode::QueryExecutionError);

            // Exit with code 5 as per M2_CLI_SPEC.md line 340
            std::process::exit(CliExitCode::QueryExecutionError.as_i32());
        }
    }

    println!("\nSuccessfully executed {} statement(s)", statements.len());
    Ok(())
}

/// Stub for execute_script_file when state_machine feature is disabled
#[cfg(not(feature = "state_machine"))]
pub async fn execute_script_file(
    _file_path: &Path,
    _database: &Database,
    _output_config: &OutputConfig,
    _format: OutputFormat,
) -> Result<()> {
    anyhow::bail!(
        "Script execution is not available in M1.\n\
         Build with --features state_machine to enable this feature.\n\
         See CLAUDE.md for M1 API examples."
    )
}

/// Parse a CQL script into individual statements
///
/// Handles:
/// - Semicolon-terminated statements
/// - Line comments (-- comment)
/// - Block comments (/* comment */)
/// - String literals with both single and double quotes
/// - Escaped quotes (doubled quotes like '' or "")
/// - Strings with embedded semicolons
/// - Multi-line statements
/// - Blank lines and whitespace
///
/// Returns a vector of trimmed statement strings (without trailing semicolons)
///
/// # Errors
///
/// Returns an error if:
/// - An unterminated statement is found (missing semicolon)
/// - An unterminated string literal is found
/// - An unterminated block comment is found
pub fn parse_script(script_content: &str) -> Result<Vec<String>> {
    let mut statements = Vec::new();
    let mut current_statement = String::new();
    let mut chars = script_content.chars().peekable();

    let mut in_string = false;
    let mut in_line_comment = false;
    let mut in_block_comment = false;
    let mut string_delimiter = '\0';

    while let Some(ch) = chars.next() {
        // Handle line comments
        if !in_string && !in_block_comment && ch == '-' {
            if chars.peek() == Some(&'-') {
                chars.next(); // consume second '-'
                in_line_comment = true;
                continue;
            }
        }

        // End line comment at newline
        if in_line_comment {
            if ch == '\n' {
                in_line_comment = false;
                current_statement.push(ch);
            }
            continue;
        }

        // Handle block comments
        if !in_string && !in_line_comment && ch == '/' {
            if chars.peek() == Some(&'*') {
                chars.next(); // consume '*'
                in_block_comment = true;
                continue;
            }
        }

        // End block comment
        if in_block_comment {
            if ch == '*' && chars.peek() == Some(&'/') {
                chars.next(); // consume '/'
                in_block_comment = false;
            }
            continue;
        }

        // Handle string literals
        if !in_block_comment && !in_line_comment {
            if ch == '\'' || ch == '"' {
                if !in_string {
                    in_string = true;
                    string_delimiter = ch;
                    current_statement.push(ch);
                } else if ch == string_delimiter {
                    // Check for escaped quote (doubled quote)
                    if chars.peek() == Some(&ch) {
                        current_statement.push(ch);
                        current_statement.push(chars.next().unwrap());
                    } else {
                        in_string = false;
                        current_statement.push(ch);
                    }
                } else {
                    current_statement.push(ch);
                }
                continue;
            }
        }

        // Handle statement terminator (semicolon)
        if !in_string && !in_line_comment && !in_block_comment && ch == ';' {
            let trimmed = current_statement.trim();
            if !trimmed.is_empty() {
                statements.push(trimmed.to_string());
            }
            current_statement.clear();
            continue;
        }

        // Regular character - add to current statement
        if !in_line_comment && !in_block_comment {
            current_statement.push(ch);
        }
    }

    // Check for unterminated constructs
    if in_string {
        anyhow::bail!("Unterminated string literal in script");
    }

    if in_block_comment {
        anyhow::bail!("Unterminated block comment in script");
    }

    // Check for unterminated statement
    let remaining = current_statement.trim();
    if !remaining.is_empty() {
        anyhow::bail!(
            "Unterminated statement found (missing semicolon): {}",
            if remaining.len() > 50 {
                format!("{}...", &remaining[..50])
            } else {
                remaining.to_string()
            }
        );
    }

    Ok(statements)
}

/// Load and parse a CQL script file
pub fn load_script(script_path: &Path) -> Result<Vec<String>> {
    let content = std::fs::read_to_string(script_path)
        .with_context(|| format!("Failed to read script file: {}", script_path.display()))?;

    parse_script(&content)
        .with_context(|| format!("Failed to parse script file: {}", script_path.display()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_empty_file() {
        let result = parse_script("");
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0);
    }

    #[test]
    fn test_parse_single_statement() {
        let content = "SELECT * FROM users;";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "SELECT * FROM users");
    }

    #[test]
    fn test_parse_multiple_statements() {
        let content = "SELECT * FROM users;\nINSERT INTO users VALUES (1, 'test');";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 2);
        assert_eq!(statements[0], "SELECT * FROM users");
        assert_eq!(statements[1], "INSERT INTO users VALUES (1, 'test')");
    }

    #[test]
    fn test_parse_line_comments() {
        let content = "-- This is a comment\nSELECT * FROM users; -- inline comment";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "SELECT * FROM users");
    }

    #[test]
    fn test_parse_block_comments() {
        let content = "/* block comment */ SELECT * FROM users; /* another comment */";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "SELECT * FROM users");
    }

    #[test]
    fn test_parse_multiline_block_comment() {
        let content = "/*\n * Multi-line\n * block comment\n */\nSELECT * FROM users;";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "SELECT * FROM users");
    }

    #[test]
    fn test_parse_string_with_semicolon() {
        let content = "INSERT INTO users VALUES (1, 'test;value');";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "INSERT INTO users VALUES (1, 'test;value')");
    }

    #[test]
    fn test_parse_double_quoted_string() {
        let content = r#"INSERT INTO users VALUES (1, "test;value");"#;
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(
            statements[0],
            r#"INSERT INTO users VALUES (1, "test;value")"#
        );
    }

    #[test]
    fn test_parse_escaped_single_quotes() {
        let content = "INSERT INTO users VALUES (1, 'test''s value');";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(
            statements[0],
            "INSERT INTO users VALUES (1, 'test''s value')"
        );
    }

    #[test]
    fn test_parse_escaped_double_quotes() {
        let content = r#"INSERT INTO users VALUES (1, "test""s value");"#;
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(
            statements[0],
            r#"INSERT INTO users VALUES (1, "test""s value")"#
        );
    }

    #[test]
    fn test_parse_multiline_statement() {
        let content = "SELECT *\nFROM users\nWHERE id = 1;";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "SELECT *\nFROM users\nWHERE id = 1");
    }

    #[test]
    fn test_parse_blank_lines() {
        let content = "SELECT * FROM users;\n\n\nINSERT INTO users VALUES (1, 'test');";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 2);
    }

    #[test]
    fn test_parse_comments_only() {
        let content = "-- comment 1\n/* comment 2 */\n-- comment 3";
        let result = parse_script(content);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0);
    }

    #[test]
    fn test_parse_unterminated_statement() {
        let content = "SELECT * FROM users";
        let result = parse_script(content);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unterminated statement"));
    }

    #[test]
    fn test_parse_unterminated_string() {
        let content = "INSERT INTO users VALUES (1, 'unterminated;";
        let result = parse_script(content);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unterminated string"));
    }

    #[test]
    fn test_parse_unterminated_block_comment() {
        let content = "/* unterminated comment\nSELECT * FROM users;";
        let result = parse_script(content);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unterminated block comment"));
    }

    #[test]
    fn test_parse_complex_script() {
        let content = r#"
-- Create table
CREATE TABLE users (
    id INT PRIMARY KEY,
    name TEXT,
    email TEXT
);

/* Insert test data */
INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');

-- Query with string containing semicolon
SELECT * FROM users WHERE email = 'test;email@example.com';
"#;
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 4);
    }

    #[test]
    fn test_parse_empty_statements() {
        let content = ";;; SELECT * FROM users; ;;;";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(statements[0], "SELECT * FROM users");
    }

    #[test]
    fn test_parse_mixed_quotes() {
        let content = r#"INSERT INTO users VALUES (1, 'single', "double");"#;
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
    }

    #[test]
    fn test_parse_comment_in_string() {
        let content = "INSERT INTO users VALUES (1, 'value with -- comment inside');";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(
            statements[0],
            "INSERT INTO users VALUES (1, 'value with -- comment inside')"
        );
    }

    #[test]
    fn test_parse_block_comment_in_string() {
        let content = "INSERT INTO users VALUES (1, 'value with /* comment */ inside');";
        let result = parse_script(content);
        assert!(result.is_ok());
        let statements = result.unwrap();
        assert_eq!(statements.len(), 1);
        assert_eq!(
            statements[0],
            "INSERT INTO users VALUES (1, 'value with /* comment */ inside')"
        );
    }
}