sql-cli 1.67.2

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
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
use crate::data::datatable::DataValue;
use crate::sql::functions::{ArgCount, FunctionCategory, FunctionSignature, SqlFunction};
use anyhow::{anyhow, Result};

/// ANSI color codes
const ANSI_RESET: &str = "\x1b[0m";

/// Named color mappings
fn get_color_code(color_name: &str) -> Option<&'static str> {
    match color_name.to_lowercase().as_str() {
        // Foreground colors (30-37)
        "black" => Some("\x1b[30m"),
        "red" => Some("\x1b[31m"),
        "green" => Some("\x1b[32m"),
        "yellow" => Some("\x1b[33m"),
        "blue" => Some("\x1b[34m"),
        "magenta" | "purple" => Some("\x1b[35m"),
        "cyan" => Some("\x1b[36m"),
        "white" => Some("\x1b[37m"),
        // Bright foreground colors (90-97)
        "bright_black" | "gray" | "grey" => Some("\x1b[90m"),
        "bright_red" => Some("\x1b[91m"),
        "bright_green" => Some("\x1b[92m"),
        "bright_yellow" => Some("\x1b[93m"),
        "bright_blue" => Some("\x1b[94m"),
        "bright_magenta" | "bright_purple" => Some("\x1b[95m"),
        "bright_cyan" => Some("\x1b[96m"),
        "bright_white" => Some("\x1b[97m"),
        _ => None,
    }
}

/// Background color mappings
fn get_bg_color_code(color_name: &str) -> Option<&'static str> {
    match color_name.to_lowercase().as_str() {
        // Background colors (40-47)
        "black" => Some("\x1b[40m"),
        "red" => Some("\x1b[41m"),
        "green" => Some("\x1b[42m"),
        "yellow" => Some("\x1b[43m"),
        "blue" => Some("\x1b[44m"),
        "magenta" | "purple" => Some("\x1b[45m"),
        "cyan" => Some("\x1b[46m"),
        "white" => Some("\x1b[47m"),
        // Bright background colors (100-107)
        "bright_black" | "gray" | "grey" => Some("\x1b[100m"),
        "bright_red" => Some("\x1b[101m"),
        "bright_green" => Some("\x1b[102m"),
        "bright_yellow" => Some("\x1b[103m"),
        "bright_blue" => Some("\x1b[104m"),
        "bright_magenta" | "bright_purple" => Some("\x1b[105m"),
        "bright_cyan" => Some("\x1b[106m"),
        "bright_white" => Some("\x1b[107m"),
        _ => None,
    }
}

/// ANSI_COLOR(color_name, text) - Apply foreground color to text
pub struct AnsiColorFunction;

impl SqlFunction for AnsiColorFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_COLOR",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(2),
            description: "Apply ANSI foreground color to text",
            returns: "Colored text with ANSI escape codes",
            examples: vec![
                "SELECT ANSI_COLOR('red', 'ERROR')",
                "SELECT ANSI_COLOR('green', 'SUCCESS')",
                "SELECT ANSI_COLOR('yellow', amount) FROM sales",
                "SELECT ANSI_COLOR('bright_blue', order_id) FROM orders",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let color_name = match &args[0] {
            DataValue::String(s) => s.as_str(),
            DataValue::InternedString(s) => s.as_str(),
            DataValue::Null => return Ok(args[1].clone()),
            _ => return Err(anyhow!("ANSI_COLOR: first argument must be a color name")),
        };

        let text = match &args[1] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[1].to_string(),
        };

        let color_code = get_color_code(color_name).ok_or_else(|| {
            anyhow!(
                "ANSI_COLOR: unknown color '{}'. Available: black, red, green, yellow, blue, magenta, cyan, white, bright_* variants",
                color_name
            )
        })?;

        Ok(DataValue::String(format!(
            "{}{}{}",
            color_code, text, ANSI_RESET
        )))
    }
}

/// ANSI_BG(color_name, text) - Apply background color to text
pub struct AnsiBgFunction;

impl SqlFunction for AnsiBgFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_BG",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(2),
            description: "Apply ANSI background color to text",
            returns: "Text with colored background using ANSI escape codes",
            examples: vec![
                "SELECT ANSI_BG('red', 'CRITICAL')",
                "SELECT ANSI_BG('green', 'ACTIVE')",
                "SELECT ANSI_BG('yellow', 'WARNING')",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let color_name = match &args[0] {
            DataValue::String(s) => s.as_str(),
            DataValue::InternedString(s) => s.as_str(),
            DataValue::Null => return Ok(args[1].clone()),
            _ => return Err(anyhow!("ANSI_BG: first argument must be a color name")),
        };

        let text = match &args[1] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[1].to_string(),
        };

        let color_code = get_bg_color_code(color_name).ok_or_else(|| {
            anyhow!(
                "ANSI_BG: unknown color '{}'. Available: black, red, green, yellow, blue, magenta, cyan, white, bright_* variants",
                color_name
            )
        })?;

        Ok(DataValue::String(format!(
            "{}{}{}",
            color_code, text, ANSI_RESET
        )))
    }
}

/// ANSI_RGB(r, g, b, text) - Apply RGB foreground color to text
pub struct AnsiRgbFunction;

impl SqlFunction for AnsiRgbFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_RGB",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(4),
            description: "Apply RGB foreground color to text (0-255 for each component)",
            returns: "Text colored with RGB value using ANSI escape codes",
            examples: vec![
                "SELECT ANSI_RGB(255, 0, 0, 'Bright Red')",
                "SELECT ANSI_RGB(0, 255, 0, 'Bright Green')",
                "SELECT ANSI_RGB(255, 165, 0, 'Orange')",
                "SELECT ANSI_RGB(138, 43, 226, 'Blue Violet')",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let r = match &args[0] {
            DataValue::Integer(n) => {
                if *n < 0 || *n > 255 {
                    return Err(anyhow!("ANSI_RGB: red value must be 0-255, got {}", n));
                }
                *n as u8
            }
            _ => return Err(anyhow!("ANSI_RGB: red value must be an integer")),
        };

        let g = match &args[1] {
            DataValue::Integer(n) => {
                if *n < 0 || *n > 255 {
                    return Err(anyhow!("ANSI_RGB: green value must be 0-255, got {}", n));
                }
                *n as u8
            }
            _ => return Err(anyhow!("ANSI_RGB: green value must be an integer")),
        };

        let b = match &args[2] {
            DataValue::Integer(n) => {
                if *n < 0 || *n > 255 {
                    return Err(anyhow!("ANSI_RGB: blue value must be 0-255, got {}", n));
                }
                *n as u8
            }
            _ => return Err(anyhow!("ANSI_RGB: blue value must be an integer")),
        };

        let text = match &args[3] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[3].to_string(),
        };

        // ANSI 24-bit true color: ESC[38;2;r;g;bm
        Ok(DataValue::String(format!(
            "\x1b[38;2;{};{};{}m{}{}",
            r, g, b, text, ANSI_RESET
        )))
    }
}

/// ANSI_RGB_BG(r, g, b, text) - Apply RGB background color to text
pub struct AnsiRgbBgFunction;

impl SqlFunction for AnsiRgbBgFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_RGB_BG",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(4),
            description: "Apply RGB background color to text (0-255 for each component)",
            returns: "Text with RGB background using ANSI escape codes",
            examples: vec![
                "SELECT ANSI_RGB_BG(255, 0, 0, 'Red Background')",
                "SELECT ANSI_RGB_BG(0, 128, 0, 'Dark Green BG')",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let r = match &args[0] {
            DataValue::Integer(n) => {
                if *n < 0 || *n > 255 {
                    return Err(anyhow!("ANSI_RGB_BG: red value must be 0-255, got {}", n));
                }
                *n as u8
            }
            _ => return Err(anyhow!("ANSI_RGB_BG: red value must be an integer")),
        };

        let g = match &args[1] {
            DataValue::Integer(n) => {
                if *n < 0 || *n > 255 {
                    return Err(anyhow!("ANSI_RGB_BG: green value must be 0-255, got {}", n));
                }
                *n as u8
            }
            _ => return Err(anyhow!("ANSI_RGB_BG: green value must be an integer")),
        };

        let b = match &args[2] {
            DataValue::Integer(n) => {
                if *n < 0 || *n > 255 {
                    return Err(anyhow!("ANSI_RGB_BG: blue value must be 0-255, got {}", n));
                }
                *n as u8
            }
            _ => return Err(anyhow!("ANSI_RGB_BG: blue value must be an integer")),
        };

        let text = match &args[3] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[3].to_string(),
        };

        // ANSI 24-bit true color background: ESC[48;2;r;g;bm
        Ok(DataValue::String(format!(
            "\x1b[48;2;{};{};{}m{}{}",
            r, g, b, text, ANSI_RESET
        )))
    }
}

/// ANSI_BOLD(text) - Make text bold
pub struct AnsiBoldFunction;

impl SqlFunction for AnsiBoldFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_BOLD",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(1),
            description: "Make text bold using ANSI escape codes",
            returns: "Bold text",
            examples: vec![
                "SELECT ANSI_BOLD('Important')",
                "SELECT ANSI_BOLD(total) FROM sales",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[0].to_string(),
        };

        Ok(DataValue::String(format!("\x1b[1m{}{}", text, ANSI_RESET)))
    }
}

/// ANSI_ITALIC(text) - Make text italic
pub struct AnsiItalicFunction;

impl SqlFunction for AnsiItalicFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_ITALIC",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(1),
            description: "Make text italic using ANSI escape codes",
            returns: "Italic text",
            examples: vec!["SELECT ANSI_ITALIC('Emphasized')"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[0].to_string(),
        };

        Ok(DataValue::String(format!("\x1b[3m{}{}", text, ANSI_RESET)))
    }
}

/// ANSI_UNDERLINE(text) - Underline text
pub struct AnsiUnderlineFunction;

impl SqlFunction for AnsiUnderlineFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_UNDERLINE",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(1),
            description: "Underline text using ANSI escape codes",
            returns: "Underlined text",
            examples: vec!["SELECT ANSI_UNDERLINE('Important')"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[0].to_string(),
        };

        Ok(DataValue::String(format!("\x1b[4m{}{}", text, ANSI_RESET)))
    }
}

/// ANSI_BLINK(text) - Make text blink (slow)
pub struct AnsiBlinkFunction;

impl SqlFunction for AnsiBlinkFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_BLINK",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(1),
            description: "Make text blink using ANSI escape codes",
            returns: "Blinking text",
            examples: vec!["SELECT ANSI_BLINK('ALERT')"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[0].to_string(),
        };

        Ok(DataValue::String(format!("\x1b[5m{}{}", text, ANSI_RESET)))
    }
}

/// ANSI_REVERSE(text) - Reverse video (swap fg/bg)
pub struct AnsiReverseFunction;

impl SqlFunction for AnsiReverseFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_REVERSE",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(1),
            description: "Reverse video (swap foreground and background colors)",
            returns: "Text with reversed colors",
            examples: vec!["SELECT ANSI_REVERSE('Highlighted')"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[0].to_string(),
        };

        Ok(DataValue::String(format!("\x1b[7m{}{}", text, ANSI_RESET)))
    }
}

/// ANSI_STRIKETHROUGH(text) - Strikethrough text
pub struct AnsiStrikethroughFunction;

impl SqlFunction for AnsiStrikethroughFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "ANSI_STRIKETHROUGH",
            category: FunctionCategory::Terminal,
            arg_count: ArgCount::Fixed(1),
            description: "Add strikethrough to text using ANSI escape codes",
            returns: "Strikethrough text",
            examples: vec!["SELECT ANSI_STRIKETHROUGH('Deprecated')"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::InternedString(s) => s.to_string(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => args[0].to_string(),
        };

        Ok(DataValue::String(format!("\x1b[9m{}{}", text, ANSI_RESET)))
    }
}