Skip to main content

drasi_mysql_common/
config.rs

1// Copyright 2025 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Shared configuration types for MySQL plugins.
16
17/// Table key configuration for MySQL sources and bootstrappers.
18///
19/// Maps a table name to the columns that form its primary key,
20/// used to generate deterministic element IDs.
21#[derive(Debug, Clone, PartialEq)]
22pub struct TableKeyConfig {
23    pub table: String,
24    pub key_columns: Vec<String>,
25}
26
27/// Validates that a string is a safe SQL identifier (alphanumeric + underscore only).
28pub fn is_valid_identifier(value: &str) -> bool {
29    !value.is_empty()
30        && value
31            .chars()
32            .all(|ch| ch.is_ascii_alphanumeric() || ch == '_')
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_valid_identifiers() {
41        assert!(is_valid_identifier("users"));
42        assert!(is_valid_identifier("order_items"));
43        assert!(is_valid_identifier("Table1"));
44        assert!(is_valid_identifier("_private"));
45    }
46
47    #[test]
48    fn test_invalid_identifiers() {
49        assert!(!is_valid_identifier(""));
50        assert!(!is_valid_identifier("my table"));
51        assert!(!is_valid_identifier("table;DROP"));
52        assert!(!is_valid_identifier("my-table"));
53        assert!(!is_valid_identifier("table.name"));
54    }
55}