rustcdc 0.6.7

Embeddable Rust CDC library focused on correctness-first capture primitives
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! Internal parsing helpers for DDL statement analysis.

use super::SchemaDiffOperation;
use crate::schema_history::ColumnDef;

pub(crate) fn parse_alter_clause(clause: &str) -> Option<SchemaDiffOperation> {
    let trimmed = clause.trim();
    if trimmed.is_empty() {
        return None;
    }
    let upper = trimmed.to_uppercase();

    if upper.starts_with("ADD COLUMN ") || upper.starts_with("ADD ") {
        let raw = if upper.starts_with("ADD COLUMN ") {
            &trimmed[11..]
        } else {
            &trimmed[4..]
        };
        let raw = strip_optional_keyword(raw.trim(), "IF NOT EXISTS");
        if !is_column_clause_candidate(raw) {
            return Some(SchemaDiffOperation::Unsupported {
                clause: normalize_clause_for_diff(trimmed),
            });
        }
        let column = parse_simple_column(raw)?;
        return Some(SchemaDiffOperation::AddColumn { column });
    }

    if upper.starts_with("DROP COLUMN ") || upper.starts_with("DROP ") {
        let raw = if upper.starts_with("DROP COLUMN ") {
            &trimmed[12..]
        } else {
            &trimmed[5..]
        };
        let raw = strip_optional_keyword(raw.trim(), "IF EXISTS");
        if !is_column_clause_candidate(raw) {
            return Some(SchemaDiffOperation::Unsupported {
                clause: normalize_clause_for_diff(trimmed),
            });
        }
        let name = raw
            .split_whitespace()
            .next()
            .map(normalize_identifier)
            .unwrap_or_default();
        if !name.is_empty() {
            return Some(SchemaDiffOperation::DropColumn { name });
        }
        return None;
    }

    if upper.starts_with("RENAME COLUMN ") {
        let raw = trimmed[14..].trim_start();
        let upper_raw = raw.to_uppercase();
        if let Some(to_pos) = upper_raw.find(" TO ") {
            let from = normalize_identifier(raw[..to_pos].trim());
            let to = normalize_identifier(raw[to_pos + 4..].trim());
            if !from.is_empty() && !to.is_empty() {
                return Some(SchemaDiffOperation::RenameColumn { from, to });
            }
        }
    }

    Some(SchemaDiffOperation::Unsupported {
        clause: normalize_clause_for_diff(trimmed),
    })
}

pub(crate) fn is_column_clause_candidate(raw: &str) -> bool {
    let first_token = raw.split_whitespace().next().unwrap_or("").trim();
    if first_token.is_empty() {
        return false;
    }

    // Quoted identifiers like "constraint" are valid column names.
    if first_token.starts_with('"') || first_token.starts_with('`') || first_token.starts_with('[')
    {
        return true;
    }

    let upper = first_token
        .trim_matches(|c: char| c == ',' || c == ';')
        .to_uppercase();

    !matches!(
        upper.as_str(),
        "CONSTRAINT"
            | "PRIMARY"
            | "FOREIGN"
            | "UNIQUE"
            | "CHECK"
            | "INDEX"
            | "KEY"
            | "FULLTEXT"
            | "SPATIAL"
            | "PARTITION"
            | "DEFAULT"
    )
}

pub(crate) fn normalize_clause_for_diff(clause: &str) -> String {
    let trimmed = clause.trim().trim_end_matches([';', ',']);
    let mut normalized = String::with_capacity(trimmed.len());
    let mut in_whitespace = false;
    let mut quote_state: Option<char> = None;

    let mut chars = trimmed.chars().peekable();
    while let Some(ch) = chars.next() {
        match quote_state {
            Some(']') => {
                normalized.push(ch);
                if ch == ']' {
                    if matches!(chars.peek(), Some(']')) {
                        if let Some(next) = chars.next() {
                            normalized.push(next);
                        }
                    } else {
                        quote_state = None;
                    }
                }
                continue;
            }
            Some(quote) => {
                normalized.push(ch);
                if ch == '\\' {
                    if let Some(next) = chars.next() {
                        normalized.push(next);
                    }
                    continue;
                }
                if ch == quote {
                    if matches!(chars.peek(), Some(next) if *next == quote) {
                        if let Some(next) = chars.next() {
                            normalized.push(next);
                        }
                    } else {
                        quote_state = None;
                    }
                }
                continue;
            }
            None => {}
        }

        if ch.is_whitespace() {
            if !in_whitespace {
                normalized.push(' ');
                in_whitespace = true;
            }
            continue;
        }

        in_whitespace = false;
        match ch {
            '[' => {
                quote_state = Some(']');
                normalized.push(ch);
            }
            '"' | '\'' | '`' => {
                quote_state = Some(ch);
                normalized.push(ch);
            }
            _ if ch.is_ascii_lowercase() => normalized.push(ch.to_ascii_uppercase()),
            _ => normalized.push(ch),
        }
    }

    normalized.trim().to_string()
}

pub(crate) fn split_sql_clauses(input: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut depth = 0usize;
    let mut current = String::new();

    let mut quote_state: Option<char> = None;
    let mut chars = input.chars().peekable();
    while let Some(ch) = chars.next() {
        match quote_state {
            Some(']') => {
                current.push(ch);
                if ch == ']' {
                    if matches!(chars.peek(), Some(']')) {
                        if let Some(next) = chars.next() {
                            current.push(next);
                        }
                    } else {
                        quote_state = None;
                    }
                }
                continue;
            }
            Some(quote) => {
                current.push(ch);
                if ch == '\\' {
                    if let Some(next) = chars.next() {
                        current.push(next);
                    }
                    continue;
                }
                if ch == quote {
                    if matches!(chars.peek(), Some(next) if *next == quote) {
                        if let Some(next) = chars.next() {
                            current.push(next);
                        }
                    } else {
                        quote_state = None;
                    }
                }
                continue;
            }
            None => {}
        }

        match ch {
            '"' | '\'' | '`' => {
                quote_state = Some(ch);
                current.push(ch);
            }
            '[' => {
                quote_state = Some(']');
                current.push(ch);
            }
            '(' => {
                depth = depth.saturating_add(1);
                current.push(ch);
            }
            ')' => {
                depth = depth.saturating_sub(1);
                current.push(ch);
            }
            ',' if depth == 0 => {
                if !current.trim().is_empty() {
                    out.push(current.trim().to_string());
                }
                current.clear();
            }
            _ => current.push(ch),
        }
    }

    if !current.trim().is_empty() {
        out.push(current.trim().to_string());
    }

    out
}

pub(crate) fn split_alter_table_clauses(input: &str) -> Option<&str> {
    let mut in_quote: Option<char> = None;

    for (idx, ch) in input.char_indices() {
        match in_quote {
            Some('"') => {
                if ch == '"' {
                    in_quote = None;
                }
            }
            Some('`') => {
                if ch == '`' {
                    in_quote = None;
                }
            }
            Some(']') => {
                if ch == ']' {
                    in_quote = None;
                }
            }
            _ => match ch {
                '"' => in_quote = Some('"'),
                '`' => in_quote = Some('`'),
                '[' => in_quote = Some(']'),
                c if c.is_whitespace() => {
                    return Some(input[idx..].trim_start());
                }
                _ => {}
            },
        }
    }

    None
}

pub(crate) fn strip_optional_keyword<'a>(input: &'a str, keyword: &str) -> &'a str {
    let trimmed = input.trim_start();
    if trimmed.len() < keyword.len() {
        return trimmed;
    }
    let (candidate, rest) = trimmed.split_at(keyword.len());
    if candidate.eq_ignore_ascii_case(keyword)
        && (rest.is_empty()
            || rest
                .chars()
                .next()
                .map(char::is_whitespace)
                .unwrap_or(false))
    {
        rest.trim_start()
    } else {
        trimmed
    }
}

pub(crate) fn strip_alter_target_modifiers(mut input: &str) -> &str {
    loop {
        let stripped_only = strip_optional_keyword(input, "ONLY");
        let stripped_if_exists = strip_optional_keyword(stripped_only, "IF EXISTS");
        if stripped_if_exists == input {
            break input;
        }
        input = stripped_if_exists;
    }
}

/// Helper function to extract schema-qualified table name from a DDL statement.
///
/// Handles both quoted and unquoted identifiers across different databases.
pub fn extract_qualified_name(sql: &str) -> Option<(String, String)> {
    extract_qualified_name_with_default(sql, "public")
}

/// Extract schema-qualified table name and apply the given default schema.
pub fn extract_qualified_name_with_default(
    sql: &str,
    default_schema: &str,
) -> Option<(String, String)> {
    let sql = sql.trim();

    let parts = split_qualified_identifier_parts(sql);
    match parts.as_slice() {
        [] => None,
        [table] => Some((default_schema.to_string(), normalize_identifier(table))),
        _ => {
            let schema = normalize_identifier(&parts[parts.len() - 2]);
            let table = normalize_identifier(&parts[parts.len() - 1]);
            if schema.is_empty() || table.is_empty() {
                None
            } else {
                Some((schema, table))
            }
        }
    }
}

pub(crate) fn split_qualified_identifier_parts(sql: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut quote_state: Option<char> = None;
    let mut chars = sql.trim().chars().peekable();

    while let Some(ch) = chars.next() {
        match quote_state {
            Some('"') => {
                current.push(ch);
                if ch == '"' {
                    if chars.peek() == Some(&'"') {
                        current.push(chars.next().unwrap());
                    } else {
                        quote_state = None;
                    }
                }
            }
            Some('`') => {
                current.push(ch);
                if ch == '`' {
                    if chars.peek() == Some(&'`') {
                        current.push(chars.next().unwrap());
                    } else {
                        quote_state = None;
                    }
                }
            }
            Some('[') => {
                current.push(ch);
                if ch == ']' {
                    if chars.peek() == Some(&']') {
                        current.push(chars.next().unwrap());
                    } else {
                        quote_state = None;
                    }
                }
            }
            _ => match ch {
                '"' | '`' => {
                    quote_state = Some(ch);
                    current.push(ch);
                }
                '[' => {
                    quote_state = Some('[');
                    current.push(ch);
                }
                '.' => {
                    let part = current.trim();
                    if part.is_empty() {
                        return Vec::new();
                    }
                    parts.push(part.to_string());
                    current.clear();
                }
                '(' | ',' | ';' if current.trim().is_empty() => break,
                ch if ch.is_whitespace() => {
                    if quote_state.is_none() {
                        break;
                    }
                    current.push(ch);
                }
                _ => current.push(ch),
            },
        }
    }

    let part = current.trim();
    if !part.is_empty() {
        parts.push(part.to_string());
    }

    parts
}

/// Extract primary keys from a CREATE TABLE statement.
pub fn extract_primary_keys(sql: &str) -> Vec<String> {
    let mut pks = Vec::new();
    let upper = sql.to_uppercase();

    if let Some(pk_start) = upper.find("PRIMARY KEY") {
        let after_pk = &sql[pk_start + 11..];
        let after_pk_trimmed = after_pk.trim_start();
        if after_pk_trimmed.starts_with('(') {
            if let Some(paren_end) = after_pk_trimmed.find(')') {
                let pk_cols = &after_pk_trimmed[1..paren_end];
                for col in pk_cols.split(',') {
                    let col_name = normalize_identifier(col.trim());
                    if !col_name.is_empty() {
                        pks.push(col_name);
                    }
                }
                return pks;
            }
        }
    }

    if let Some(start) = sql.find('(') {
        if let Some(end) = sql.rfind(')') {
            let col_defs = &sql[start + 1..end];
            let mut depth = 0;
            let mut current = String::new();

            for ch in col_defs.chars() {
                match ch {
                    '(' => {
                        depth += 1;
                        current.push(ch);
                    }
                    ')' => {
                        depth -= 1;
                        current.push(ch);
                    }
                    ',' if depth == 0 => {
                        maybe_push_inline_primary_key(&current, &mut pks);
                        current.clear();
                    }
                    _ => current.push(ch),
                }
            }

            maybe_push_inline_primary_key(&current, &mut pks);
        }
    }

    pks
}

pub(crate) fn maybe_push_inline_primary_key(column_def: &str, pks: &mut Vec<String>) {
    if !column_def.to_uppercase().contains("PRIMARY KEY") {
        return;
    }
    if let Some(col_name_raw) = column_def.split_whitespace().next() {
        let col_name = normalize_identifier(col_name_raw);
        if !col_name.is_empty() {
            pks.push(col_name);
        }
    }
}

/// Helper to normalize SQL identifiers (remove quotes, lowercase if unquoted).
pub fn normalize_identifier(ident: &str) -> String {
    let trimmed = ident.trim().trim_end_matches([';', ',']);

    if trimmed.starts_with('"') && trimmed.ends_with('"') && trimmed.len() >= 2 {
        return trimmed[1..trimmed.len() - 1]
            .replace("\"\"", "\"")
            .to_lowercase();
    }

    if trimmed.starts_with('`') && trimmed.ends_with('`') && trimmed.len() >= 2 {
        return trimmed[1..trimmed.len() - 1]
            .replace("``", "`")
            .to_lowercase();
    }

    if trimmed.starts_with('[') && trimmed.ends_with(']') && trimmed.len() >= 2 {
        return trimmed[1..trimmed.len() - 1]
            .replace("]]", "]")
            .to_lowercase();
    }

    trimmed.to_lowercase()
}

/// Helper to extract column definitions from a CREATE TABLE statement.
pub fn extract_columns_from_create(sql: &str) -> Vec<ColumnDef> {
    let mut columns = Vec::new();

    // Find content between first ( and last )
    if let Some(start) = sql.find('(') {
        if let Some(end) = sql.rfind(')') {
            let content = &sql[start + 1..end];

            for clause in split_sql_clauses(content) {
                let trimmed = clause.trim();
                if is_column_clause_candidate(trimmed) {
                    if let Some(col) = parse_enhanced_column(trimmed) {
                        columns.push(col);
                    }
                }
            }
        }
    }

    columns
}

pub(crate) fn parse_simple_column(def: &str) -> Option<ColumnDef> {
    let def = def.trim();
    let tokens: Vec<&str> = def.split_whitespace().collect();

    if tokens.len() < 2 {
        return None;
    }

    let name = normalize_identifier(tokens[0]);
    let data_type = tokens[1]
        .trim_matches(|c: char| c == ';' || c == ',')
        .to_uppercase();
    let nullable = !def.to_uppercase().contains("NOT NULL");

    Some(ColumnDef {
        name,
        data_type,
        nullable,
        constraints: vec![],
    })
}

pub(crate) fn parse_enhanced_column(def: &str) -> Option<ColumnDef> {
    let def = def.trim();

    // Extract column name (first quoted or unquoted token)
    let (name, rest) = extract_first_identifier(def)?;
    let name = normalize_identifier(&name);

    let upper_def = def.to_uppercase();

    // Check for generated/computed column indicators (stop parsing type for these)
    if upper_def.contains(" GENERATED ALWAYS AS ")
        || upper_def.contains(" AS ") && upper_def.contains("PERSISTED")
    {
        // For computed columns, just use a placeholder data type
        return Some(ColumnDef {
            name,
            data_type: "COMPUTED".to_string(),
            nullable: false, // computed columns are not nullable
            constraints: vec![],
        });
    }

    // Extract data type (second token or type(...) pattern)
    let rest = rest.trim_start();
    let (data_type, rest) = extract_column_type(rest)?;

    // Check for NOT NULL / UNIQUE / PRIMARY KEY
    let nullable = !rest.to_uppercase().contains("NOT NULL");

    Some(ColumnDef {
        name,
        data_type: data_type.to_uppercase(),
        nullable,
        constraints: vec![],
    })
}

pub(crate) fn extract_first_identifier(input: &str) -> Option<(String, String)> {
    let input = input.trim();

    if input.starts_with('"') {
        return extract_quoted_identifier(input, '"');
    } else if input.starts_with('`') {
        return extract_quoted_identifier(input, '`');
    } else if input.starts_with('[') {
        return extract_quoted_identifier(input, ']');
    } else {
        // Unquoted identifier: grab until whitespace/paren/comma
        let end_pos = input
            .char_indices()
            .find(|(_, c)| c.is_whitespace() || *c == '(' || *c == ',' || *c == ';')
            .map(|(idx, _)| idx)
            .unwrap_or(input.len());

        if end_pos > 0 {
            let identifier = input[..end_pos].to_string();
            let remaining = input[end_pos..].to_string();
            return Some((identifier, remaining));
        }
    }

    None
}

pub(crate) fn extract_quoted_identifier(input: &str, closing: char) -> Option<(String, String)> {
    let mut result = String::new();
    let mut chars = input.char_indices().peekable();

    if let Some((_, opening)) = chars.next() {
        result.push(opening);
    }

    while let Some((idx, ch)) = chars.next() {
        result.push(ch);
        if ch == closing {
            if matches!(chars.peek(), Some((_, next)) if *next == closing) {
                if let Some((_, escape_ch)) = chars.next() {
                    result.push(escape_ch);
                }
            } else {
                let remaining = input[idx + ch.len_utf8()..].to_string();
                return Some((result, remaining));
            }
        }
    }

    None
}

pub(crate) fn extract_column_type(input: &str) -> Option<(String, String)> {
    let input = input.trim_start();
    if input.is_empty() {
        return None;
    }

    let mut type_str = String::new();
    let mut paren_depth = 0;
    let mut end_idx = 0;

    for (i, ch) in input.chars().enumerate() {
        match ch {
            '(' => {
                paren_depth += 1;
                type_str.push(ch);
            }
            ')' => {
                paren_depth -= 1;
                type_str.push(ch);
            }
            ' ' if paren_depth == 0 => {
                end_idx = i;
                break;
            }
            ',' | ';' if paren_depth == 0 => {
                end_idx = i;
                break;
            }
            _ => type_str.push(ch),
        }
        end_idx = i + 1;
    }

    if type_str.is_empty() {
        return None;
    }

    let remaining = input[end_idx..].to_string();
    Some((type_str, remaining))
}