buup 0.25.5

Core transformation library with zero dependencies
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
use crate::{Transform, TransformError, TransformerCategory};

/// SQL Formatter transformer
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SqlFormatter;

impl Transform for SqlFormatter {
    fn name(&self) -> &'static str {
        "SQL Formatter"
    }

    fn id(&self) -> &'static str {
        "sqlformatter"
    }

    fn description(&self) -> &'static str {
        "Formats SQL queries with proper indentation and spacing"
    }

    fn category(&self) -> TransformerCategory {
        TransformerCategory::Formatter
    }

    fn default_test_input(&self) -> &'static str {
        "SELECT id, username, email FROM users WHERE status = 'active' AND created_at > '2023-01-01' ORDER BY created_at DESC LIMIT 10"
    }

    fn transform(&self, input: &str) -> Result<String, TransformError> {
        // Skip empty input
        if input.trim().is_empty() {
            return Ok(String::new());
        }

        format_sql(input)
    }
}

enum SqlTokenType {
    Keyword,
    Identifier,
    String,
    Number,
    Operator,
    Punctuation,
    Whitespace,
    Parenthesis,
}

// Keywords that should be on their own line
const NEWLINE_KEYWORDS: [&str; 16] = [
    "FROM",
    "WHERE",
    "LEFT JOIN",
    "RIGHT JOIN",
    "INNER JOIN",
    "OUTER JOIN",
    "FULL JOIN",
    "CROSS JOIN",
    "JOIN",
    "GROUP BY",
    "HAVING",
    "ORDER BY",
    "LIMIT",
    "UNION",
    "UNION ALL",
    "INTERSECT",
];

// Keywords that start a new logical section
const MAJOR_KEYWORDS: [&str; 7] = [
    "SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "ALTER", "DROP",
];

// Format SQL query with proper indentation and spacing
fn format_sql(input: &str) -> Result<String, TransformError> {
    let mut result = String::with_capacity(input.len() * 2);
    let mut input_chars = input.chars().peekable();
    let mut indent_level: usize = 0;
    let mut at_beginning_of_line = true;
    let mut previous_token_type = SqlTokenType::Whitespace;
    let mut buffer = String::new();
    let mut in_string = false;
    let mut string_quote_char = '"';
    let mut in_comment = false;
    let mut in_multiline_comment = false;
    let mut pending_whitespace = false;

    while let Some(c) = input_chars.next() {
        // Handle strings (quoted literals)
        if (c == '\'' || c == '"') && !in_comment && !in_multiline_comment {
            if !in_string {
                // Starting a string
                in_string = true;
                string_quote_char = c;

                // Add a space before string if needed
                if !matches!(
                    previous_token_type,
                    SqlTokenType::Whitespace | SqlTokenType::Operator | SqlTokenType::Parenthesis
                ) {
                    result.push(' ');
                }

                result.push(c);
            } else if c == string_quote_char {
                // Check for escaped quotes
                if input_chars.peek() == Some(&c) {
                    // This is an escaped quote within the string
                    result.push(c);
                    input_chars.next(); // Consume the second quote
                    result.push(c);
                } else {
                    // End of string
                    in_string = false;
                    result.push(c);
                }
            } else {
                // Just a quote character inside a string delimited by a different quote
                result.push(c);
            }
            previous_token_type = SqlTokenType::String;
            continue;
        }

        // Inside a string - add all characters as-is
        if in_string {
            result.push(c);
            continue;
        }

        // Handle single-line comments
        if c == '-' && input_chars.peek() == Some(&'-') && !in_multiline_comment {
            in_comment = true;
            if !at_beginning_of_line {
                result.push(' ');
            }
            result.push(c);
            continue;
        }

        if in_comment {
            result.push(c);
            if c == '\n' {
                in_comment = false;
                at_beginning_of_line = true;

                // Apply indentation at beginning of line
                result.push_str(&"    ".repeat(indent_level));
            }
            continue;
        }

        // Handle multi-line comments
        if c == '/' && input_chars.peek() == Some(&'*') && !in_comment {
            in_multiline_comment = true;
            if !at_beginning_of_line {
                result.push(' ');
            }
            result.push(c);
            continue;
        }

        if in_multiline_comment {
            result.push(c);
            if c == '*' && input_chars.peek() == Some(&'/') {
                input_chars.next(); // Consume the '/'
                result.push('/');
                in_multiline_comment = false;
            }
            continue;
        }

        // Handle whitespace
        if c.is_whitespace() {
            if at_beginning_of_line && c != '\n' {
                // Skip leading whitespace
                continue;
            }

            if c == '\n' {
                // Handle newlines
                if !at_beginning_of_line {
                    result.push('\n');
                    at_beginning_of_line = true;

                    // Apply indentation at beginning of new line
                    result.push_str(&"    ".repeat(indent_level));
                }
            } else if !at_beginning_of_line {
                // Collapse multiple spaces into one
                pending_whitespace = true;
            }

            previous_token_type = SqlTokenType::Whitespace;
            continue;
        }

        // Handle parentheses
        if c == '(' {
            if pending_whitespace && !at_beginning_of_line {
                result.push(' ');
            }
            pending_whitespace = false;

            result.push(c);
            indent_level += 1;

            // Add newline after opening parenthesis
            result.push('\n');

            // Apply indentation for the next line
            result.push_str(&"    ".repeat(indent_level));

            // We are now at the beginning of a line
            at_beginning_of_line = true;

            previous_token_type = SqlTokenType::Parenthesis;
            continue;
        }

        if c == ')' {
            pending_whitespace = false;

            // Add newline before closing parenthesis if not at the beginning of a line
            if !at_beginning_of_line {
                result.push('\n');
            }

            indent_level = indent_level.saturating_sub(1);

            // Apply indentation for the closing parenthesis
            if at_beginning_of_line {
                // Remove previous indentation and apply the updated one
                result.truncate(result.rfind('\n').map(|pos| pos + 1).unwrap_or(0));
            }

            result.push_str(&"    ".repeat(indent_level));
            result.push(c);

            previous_token_type = SqlTokenType::Parenthesis;
            at_beginning_of_line = false;
            continue;
        }

        // Handle punctuation and operators
        if c == ',' {
            result.push(c);

            // For SELECT statements, add newline after comma
            result.push('\n');
            at_beginning_of_line = true;

            // Apply indentation for the next line
            result.push_str(&"    ".repeat(indent_level));

            previous_token_type = SqlTokenType::Punctuation;
            continue;
        }

        if "+-*/=%<>!|&".contains(c) {
            if pending_whitespace {
                result.push(' ');
            }
            pending_whitespace = false;

            result.push(c);

            // Add space after operator (but not before checking for multi-char operators)
            if !matches!(input_chars.peek(), Some(&'=') | Some(&'>') | Some(&'<')) {
                result.push(' ');
            }

            previous_token_type = SqlTokenType::Operator;
            at_beginning_of_line = false;
            continue;
        }

        // Handle keywords and identifiers
        if c.is_alphabetic() || c == '_' || c == '@' || c == '#' || c == '$' {
            buffer.clear();
            buffer.push(c);

            // Collect the entire identifier or keyword
            while let Some(&next_c) = input_chars.peek() {
                if next_c.is_alphanumeric()
                    || next_c == '_'
                    || next_c == '@'
                    || next_c == '#'
                    || next_c == '$'
                {
                    buffer.push(next_c);
                    input_chars.next();
                } else {
                    break;
                }
            }

            // Check if it's a keyword
            let upper_buffer = buffer.to_uppercase();
            let is_keyword = is_sql_keyword(&upper_buffer);

            // Handle keyword formatting
            if is_keyword {
                // Determine if we need a newline before this keyword
                let needs_newline = NEWLINE_KEYWORDS.contains(&upper_buffer.as_str())
                    || (MAJOR_KEYWORDS.contains(&upper_buffer.as_str()) && !at_beginning_of_line);

                if needs_newline && !at_beginning_of_line {
                    result.push('\n');

                    // Apply indentation for this line
                    result.push_str(&"    ".repeat(indent_level));
                } else if pending_whitespace && !at_beginning_of_line {
                    result.push(' ');
                }

                pending_whitespace = false;

                // Add the keyword in uppercase
                result.push_str(&upper_buffer);

                // Make sure there's a space after keywords
                result.push(' ');

                previous_token_type = SqlTokenType::Keyword;
            } else {
                // It's an identifier
                if pending_whitespace && !at_beginning_of_line {
                    result.push(' ');
                }
                pending_whitespace = false;

                // Add the identifier as-is
                result.push_str(&buffer);

                previous_token_type = SqlTokenType::Identifier;
            }

            at_beginning_of_line = false;
            continue;
        }

        // Handle numbers
        if c.is_numeric() || (c == '.' && input_chars.peek().is_some_and(|p| p.is_numeric())) {
            if pending_whitespace && !at_beginning_of_line {
                result.push(' ');
            }
            pending_whitespace = false;

            result.push(c);

            // Collect the rest of the number
            while let Some(&next_c) = input_chars.peek() {
                if next_c.is_numeric() || next_c == '.' {
                    result.push(next_c);
                    input_chars.next();
                } else {
                    break;
                }
            }

            previous_token_type = SqlTokenType::Number;
            at_beginning_of_line = false;
            continue;
        }

        // Handle any other characters
        if pending_whitespace && !at_beginning_of_line {
            result.push(' ');
        }
        pending_whitespace = false;

        result.push(c);
        at_beginning_of_line = false;

        // Most likely punctuation
        previous_token_type = SqlTokenType::Punctuation;
    }

    Ok(result)
}

// Check if a word is a SQL keyword
fn is_sql_keyword(word: &str) -> bool {
    // Common SQL keywords
    const KEYWORDS: [&str; 59] = [
        "SELECT",
        "FROM",
        "WHERE",
        "INSERT",
        "UPDATE",
        "DELETE",
        "DROP",
        "CREATE",
        "ALTER",
        "TABLE",
        "VIEW",
        "INDEX",
        "TRIGGER",
        "PROCEDURE",
        "FUNCTION",
        "DATABASE",
        "SCHEMA",
        "GRANT",
        "REVOKE",
        "JOIN",
        "INNER",
        "OUTER",
        "LEFT",
        "RIGHT",
        "FULL",
        "CROSS",
        "NATURAL",
        "GROUP",
        "ORDER",
        "BY",
        "HAVING",
        "UNION",
        "ALL",
        "INTERSECT",
        "EXCEPT",
        "INTO",
        "VALUES",
        "SET",
        "AS",
        "ON",
        "AND",
        "OR",
        "NOT",
        "NULL",
        "IS",
        "IN",
        "BETWEEN",
        "LIKE",
        "EXISTS",
        "CASE",
        "WHEN",
        "THEN",
        "ELSE",
        "END",
        "ASC",
        "DESC",
        "LIMIT",
        "OFFSET",
        "WITH",
    ];

    KEYWORDS.contains(&word)
}

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

    #[test]
    fn test_sql_formatter_empty() {
        let transformer = SqlFormatter;
        assert_eq!(transformer.transform("").unwrap(), "");
        assert_eq!(transformer.transform("  ").unwrap(), "");
    }

    #[test]
    fn test_sql_formatter_simple_select() {
        let transformer = SqlFormatter;
        let input = "SELECT id, name, email FROM users WHERE active = true ORDER BY name";

        // Test against the exact output format
        let expected =
            "SELECT  id,\nname,\nemail\nFROM  users\nWHERE  active =  true ORDER  BY  name";
        assert_eq!(transformer.transform(input).unwrap(), expected);
    }

    #[test]
    fn test_sql_formatter_joins() {
        let transformer = SqlFormatter;
        let input = "SELECT u.id, u.name, o.order_date FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100";

        // Test against the exact output format
        let expected = "SELECT  u.id,\nu.name,\no.order_date\nFROM  users u\nJOIN  orders o ON  u.id =  o.user_id\nWHERE  o.total >  100";
        assert_eq!(transformer.transform(input).unwrap(), expected);
    }

    #[test]
    fn test_sql_formatter_nested_queries() {
        let transformer = SqlFormatter;
        let input = "SELECT * FROM (SELECT id, COUNT(*) as count FROM orders GROUP BY id) AS subquery WHERE count > 5";

        // Test against the exact output format
        let expected = "SELECT  * \nFROM  (\n    SELECT  id,\n    COUNT(\n        * \n    ) AS  count\n    FROM  orders GROUP  BY  id\n) AS  subquery\nWHERE  count >  5";
        assert_eq!(transformer.transform(input).unwrap(), expected);
    }

    #[test]
    fn test_sql_formatter_string_literals() {
        let transformer = SqlFormatter;
        let input = "SELECT * FROM users WHERE name = 'John''s' AND department = \"Sales\"";

        // Test against the exact output format
        let expected =
            "SELECT  * \nFROM  users\nWHERE  name = 'John''s' AND  department = \"Sales\"";
        assert_eq!(transformer.transform(input).unwrap(), expected);
    }
}