refactor(workspace): split SQL and database driver crates

This commit is contained in:
t8y2 committed 2026-09-25 15:51:58 +08:00
1 parent b99c60aff6
commit e0da2d09d3
180 files changed
+1422 -538

No files matched your search

+409
View File
@@ -0,0 +1,409 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use serde::{Deserialize, Serialize};
use crate::sql_dialect::descriptor::DialectKind;
use crate::sql_dialect::dialect_yaml::DialectYaml;
use crate::sql_dialect::inference::{ColumnType, DefaultTypeInferenceEngine, TypeInferenceEngine};
// ============================================================================
// DML Clean Rules — lossy type mapping → pre-transform DML template
// ============================================================================
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DmlCleanRulesFile {
pub rules: Vec<DmlCleanRule>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DmlCleanRule {
pub name: String,
pub source_type: String,
pub target_type: String,
pub max_fidelity: f64,
#[serde(default)]
pub pre_transform_sql: Option<String>,
#[serde(default)]
pub description: Option<String>,
}
impl DmlCleanRule {
pub fn transform_sql(&self, table: &str, column: &str) -> Option<String> {
self.pre_transform_sql.as_ref().map(|tmpl| tmpl.replace("{table}", table).replace("{column}", column))
}
/// Check if this rule matches the source → target type pair.
pub fn matches(&self, source_type: &str, target_type: &str) -> bool {
let src_norm = normalize_type(source_type);
let tgt_norm = normalize_type(target_type);
let rule_src = normalize_type(&self.source_type);
let rule_tgt = normalize_type(&self.target_type);
src_norm == rule_src && tgt_norm == rule_tgt
}
fn applies(&self, source_type: &str, target_type: &str, fidelity: f64) -> bool {
self.matches(source_type, target_type) && fidelity < self.max_fidelity
}
}
fn normalize_type(t: &str) -> String {
t.trim().to_ascii_uppercase().split('(').next().unwrap_or("").to_string()
}
// ============================================================================
// DmlCleanRuleRegistry — global singleton, loads from YAML
// ============================================================================
pub struct DmlCleanRuleRegistry {
pub rules: Vec<DmlCleanRule>,
}
impl DmlCleanRuleRegistry {
pub fn new() -> Self {
Self { rules: Vec::new() }
}
pub fn global() -> &'static RwLock<Self> {
use std::sync::OnceLock;
static INSTANCE: OnceLock<RwLock<DmlCleanRuleRegistry>> = OnceLock::new();
INSTANCE.get_or_init(|| RwLock::new(Self::new()))
}
pub fn load(path: &Path) -> Result<Vec<DmlCleanRule>, String> {
let content = std::fs::read_to_string(path).map_err(|e| format!("Cannot read {}: {e}", path.display()))?;
let parsed: DmlCleanRulesFile =
serde_yaml::from_str(&content).map_err(|e| format!("YAML parse error in {}: {e}", path.display()))?;
Ok(parsed.rules)
}
pub fn load_default() -> Result<(), String> {
let paths = [
"plugins/mappings/rules/dml_clean_rules.yaml",
"../plugins/mappings/rules/dml_clean_rules.yaml",
"../../plugins/mappings/rules/dml_clean_rules.yaml",
];
for path in &paths {
let p = Path::new(path);
if p.exists() {
let rules = Self::load(p)?;
if let Ok(mut registry) = Self::global().write() {
registry.rules = rules;
}
return Ok(());
}
}
Ok(())
}
pub fn find_rule(&self, source_type: &str, target_type: &str) -> Option<&DmlCleanRule> {
self.rules.iter().find(|r| r.matches(source_type, target_type))
}
pub fn all_rules(&self) -> &[DmlCleanRule] {
&self.rules
}
}
impl Default for DmlCleanRuleRegistry {
fn default() -> Self {
Self::new()
}
}
// ============================================================================
// BindingEngine — auto-binds DML clean SQL to lossy type mappings
// ============================================================================
pub struct BindingEngine;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BindingResult {
pub source_type: String,
pub target_type: String,
pub fidelity: f64,
pub pre_transform_sql: Option<String>,
pub rule_name: Option<String>,
}
impl BindingEngine {
/// Compute fidelity for a single type mapping and auto-bind DML if lossy.
pub fn bind(source_type: &str, target_type: &str, table: &str, column: &str, threshold: f64) -> BindingResult {
let engine = DefaultTypeInferenceEngine;
let src_parsed = ColumnType::parse(source_type);
let tgt_parsed = ColumnType::parse(target_type);
let fidelity = engine.type_compatibility_score(&src_parsed, &tgt_parsed);
if let Ok(registry) = DmlCleanRuleRegistry::global().read() {
if let Some(rule) = registry.find_rule(source_type, target_type) {
// A pair-specific rule owns its fidelity boundary; the caller
// threshold remains the fallback for mappings without a rule.
if rule.applies(source_type, target_type, fidelity) {
return BindingResult {
source_type: source_type.to_string(),
target_type: target_type.to_string(),
fidelity,
pre_transform_sql: rule.transform_sql(table, column),
rule_name: Some(rule.name.clone()),
};
}
}
}
if fidelity >= threshold {
return BindingResult {
source_type: source_type.to_string(),
target_type: target_type.to_string(),
fidelity,
pre_transform_sql: None,
rule_name: None,
};
}
BindingResult {
source_type: source_type.to_string(),
target_type: target_type.to_string(),
fidelity,
pre_transform_sql: None,
rule_name: None,
}
}
/// Bind for a full set of column mappings.
pub fn bind_columns(
source_types: &[(String, String)], // (col_name, type)
target_types: &[(String, String)],
table: &str,
threshold: f64,
) -> Vec<BindingResult> {
let source_map: HashMap<&str, &str> = source_types.iter().map(|(n, t)| (n.as_str(), t.as_str())).collect();
target_types
.iter()
.map(|(col_name, target_type)| {
let source_type = source_map.get(col_name.as_str()).copied().unwrap_or(target_type);
Self::bind(source_type, target_type, table, col_name, threshold)
})
.collect()
}
}
// ============================================================================
// MappingCache — cache derivation results to plugins/mappings/base/
// ============================================================================
const MAPPING_BASE_DIR: &str = "plugins/mappings/base";
const MAPPING_CUSTOM_DIR: &str = "plugins/mappings/custom";
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MappingCacheFile {
pub status: String, // "UNVERIFIED" | "VERIFIED" | "REJECTED"
pub generated_at: String,
pub derived_from: MappingDerivationInfo,
pub transformations: Vec<CachedTransformation>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MappingDerivationInfo {
pub source_dialect: String,
pub target_dialect: String,
pub source_type_count: usize,
pub target_type_count: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CachedTransformation {
pub source_type: String,
pub target_type: String,
pub fidelity: f64,
pub pre_transform_sql: Option<String>,
pub rule_name: Option<String>,
}
impl MappingCacheFile {
pub fn cache_path(source: DialectKind, target: DialectKind) -> PathBuf {
let dir = Path::new(MAPPING_BASE_DIR);
let name = format!("{}_to_{}.base.yaml", source.label(), target.label());
dir.join(name)
}
pub fn custom_path(source: DialectKind, target: DialectKind, env: &str) -> PathBuf {
let dir = Path::new(MAPPING_CUSTOM_DIR);
let name = format!("{}_to_{}.{}.yaml", source.label(), target.label(), env);
dir.join(name)
}
/// Load from cache if exists. Checks custom override first, then base cache.
pub fn load(source: DialectKind, target: DialectKind, env: &str) -> Option<Self> {
let custom = Self::custom_path(source, target, env);
if custom.exists() {
let content = std::fs::read_to_string(&custom).ok()?;
return serde_yaml::from_str(&content).ok();
}
let base = Self::cache_path(source, target);
if base.exists() {
let content = std::fs::read_to_string(&base).ok()?;
return serde_yaml::from_str(&content).ok();
}
None
}
/// Save derivation result to base cache (UNVERIFIED).
pub fn save(source: DialectKind, target: DialectKind, bindings: &[BindingResult]) -> Result<(), String> {
let dir = Path::new(MAPPING_BASE_DIR);
std::fs::create_dir_all(dir).map_err(|e| format!("Cannot create mappings dir: {e}"))?;
let source_yaml = DialectYaml::from_descriptor(source);
let target_yaml = DialectYaml::from_descriptor(target);
let cache = MappingCacheFile {
status: "UNVERIFIED".to_string(),
generated_at: chrono::Utc::now().to_rfc3339(),
derived_from: MappingDerivationInfo {
source_dialect: source.label().to_string(),
target_dialect: target.label().to_string(),
source_type_count: source_yaml.types.len(),
target_type_count: target_yaml.types.len(),
},
transformations: bindings
.iter()
.map(|b| CachedTransformation {
source_type: b.source_type.clone(),
target_type: b.target_type.clone(),
fidelity: b.fidelity,
pre_transform_sql: b.pre_transform_sql.clone(),
rule_name: b.rule_name.clone(),
})
.collect(),
};
let path = Self::cache_path(source, target);
let yaml_str = serde_yaml::to_string(&cache).map_err(|e| format!("YAML serialize: {e}"))?;
std::fs::write(&path, &yaml_str).map_err(|e| format!("Write cache {}: {e}", path.display()))?;
Ok(())
}
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rule_matches_exact() {
let rule = DmlCleanRule {
name: "test".to_string(),
source_type: "BIGINT".to_string(),
target_type: "INT".to_string(),
max_fidelity: 0.8,
pre_transform_sql: Some("UPDATE {table} SET {column} = NULL".to_string()),
description: None,
};
assert!(rule.matches("BIGINT", "INT"));
assert!(!rule.matches("INT", "BIGINT"));
}
#[test]
fn rule_matches_normalized() {
let rule = DmlCleanRule {
name: "test".to_string(),
source_type: "BIGINT".to_string(),
target_type: "INT".to_string(),
max_fidelity: 0.8,
pre_transform_sql: None,
description: None,
};
assert!(rule.matches("BIGINT(20)", "INT"));
assert!(rule.matches("bigint", "int"));
}
#[test]
fn rule_transform_sql() {
let rule = DmlCleanRule {
name: "test".to_string(),
source_type: "BIGINT".to_string(),
target_type: "INT".to_string(),
max_fidelity: 0.8,
pre_transform_sql: Some("UPDATE {table} SET {column} = NULL WHERE {column} > 2147483647".to_string()),
description: None,
};
let sql = rule.transform_sql("users", "age").unwrap();
assert_eq!(sql, "UPDATE users SET age = NULL WHERE age > 2147483647");
}
#[test]
fn binding_engine_high_fidelity_no_dml() {
let result = BindingEngine::bind("INT", "INTEGER", "t", "c", 0.7);
assert!(result.pre_transform_sql.is_none());
assert!((result.fidelity - 0.9).abs() < 0.01);
}
#[test]
fn binding_engine_low_fidelity_with_dml() {
let _registry = DmlCleanRuleRegistry::new();
if let Ok(mut reg) = DmlCleanRuleRegistry::global().write() {
reg.rules = vec![DmlCleanRule {
name: "bigint_to_int".to_string(),
source_type: "BIGINT".to_string(),
target_type: "INT".to_string(),
max_fidelity: 0.9,
pre_transform_sql: Some("UPDATE {table} SET {column} = NULL WHERE {column} > 2147483647".to_string()),
description: None,
}];
}
let result = BindingEngine::bind("BIGINT", "INT", "users", "id", 0.4);
assert!(result.pre_transform_sql.is_some());
assert!(result.pre_transform_sql.unwrap().contains("2147483647"));
}
#[test]
fn rule_max_fidelity_boundary_is_exclusive() {
let rule = DmlCleanRule {
name: "boundary".to_string(),
source_type: "BIGINT".to_string(),
target_type: "INT".to_string(),
max_fidelity: 0.5,
pre_transform_sql: Some("SELECT 1".to_string()),
description: None,
};
assert!(rule.applies("BIGINT", "INT", 0.49));
assert!(!rule.applies("BIGINT", "INT", 0.5));
}
#[test]
fn default_rules_honor_configured_max_fidelity() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../plugins/mappings/rules/dml_clean_rules.yaml");
let rules = DmlCleanRuleRegistry::load(&path).unwrap();
let rule = rules.iter().find(|rule| rule.matches("BIGINT", "INT")).unwrap();
assert!(rule.applies("BIGINT", "INT", rule.max_fidelity - f64::EPSILON));
assert!(!rule.applies("BIGINT", "INT", rule.max_fidelity));
}
#[test]
fn binding_engine_no_rule_found() {
let result = BindingEngine::bind("GEOGRAPHY", "INT", "t", "c", 0.5);
assert!(result.pre_transform_sql.is_none());
assert!((result.fidelity - 0.3).abs() < 0.01);
}
#[test]
fn mapping_cache_path() {
let path = MappingCacheFile::cache_path(DialectKind::Mysql, DialectKind::Postgres);
assert!(path.to_string_lossy().contains("mysql_to_postgres"));
let custom = MappingCacheFile::custom_path(DialectKind::Mysql, DialectKind::Postgres, "prod");
assert!(custom.to_string_lossy().contains("prod"));
}
}
+6
View File
@@ -0,0 +1,6 @@
#![recursion_limit = "256"]
pub use dbx_types::{database_manifest, models, types};
pub mod dml_binding;
pub mod sql_dialect;
+87
View File
@@ -0,0 +1,87 @@
mod capabilities;
pub mod ddl_profile;
pub mod descriptor;
pub mod dialect_loader;
pub mod dialect_types;
pub mod dialect_yaml;
pub mod hot_reload;
mod identifiers;
pub mod inference;
mod table_select;
pub mod type_rewrite;
mod types;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod descriptor_snapshots;
pub use capabilities::{
firebird_rows_clause, is_schema_aware, pagination_strategy, table_pagination_strategy, uses_fetch_first,
uses_oracle_row_id, uses_single_row_insert_statements, uses_synthetic_row_id, uses_xugu_row_id, PaginationContext,
TablePaginationStrategy,
};
pub use ddl_profile::{
profile_for, AutoIncSyntax, DdlDialectProfile, IndexTypePlacement, QuoteStyle, RenameColumnSyntax, TriggerTemplate,
TypeMapEntry,
};
pub use descriptor::{
dialect_check, dialect_check_all, DialectCapabilityDescriptor, DialectInfo, DialectKind, TypeConversionRule,
TypeMappingMatrix, CAP_ADD_COLUMN, CAP_ALTER_EXISTING_COLUMN, CAP_ALTER_OWNER, CAP_ALTER_PRIMARY_KEY,
CAP_AUTO_INCREMENT, CAP_COMMENT, CAP_CREATE_FUNCTION, CAP_CREATE_INDEX, CAP_CREATE_OR_REPLACE, CAP_CREATE_SEQUENCE,
CAP_CREATE_TABLE, CAP_CREATE_TRIGGER, CAP_DROP_COLUMN, CAP_DROP_FUNCTION, CAP_DROP_INDEX, CAP_DROP_SEQUENCE,
CAP_DROP_TABLE, CAP_DROP_TRIGGER, CAP_FOREIGN_KEY, CAP_GRANT_REVOKE, CAP_IDENTITY_COLUMNS, CAP_IF_NOT_EXISTS,
CAP_INDEX_COMMENT, CAP_INDEX_FILTER, CAP_INDEX_INCLUDE, CAP_INDEX_TYPE, CAP_REBUILD_INDEX, CAP_RENAME_COLUMN,
CAP_REORDER_COLUMN, CAP_TEMPORARY_TABLE, CAP_TRANSACTIONAL_DDL, CAP_TRUNCATE_TABLE,
};
pub use identifiers::{
normalize_where_input, qualified_table_name, qualified_table_name_with_catalog, quote_table_identifier,
};
pub use identifiers::{
parse_sqlserver_linked_schema_ref, qualified_transfer_table, quote_iris_identifier, quote_transfer_identifier,
transfer_column_identifier,
};
pub use table_select::{
build_count_table_sql, build_table_data_select_sql, build_table_data_select_sql_with_database,
build_table_select_sql, DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX,
};
pub use table_select::{
database_qualified_table_name, quote_table_data_identifier, table_data_qualified_table_name, table_data_schema,
uses_connection_identifier_quote,
};
pub use type_rewrite::{
apply_auto_inc_to_column_def, column_is_auto_increment, normalize_len_params, rewrite_column_type,
split_type_base_params, type_looks_integer, AutoIncColumnBuild,
};
pub use types::*;
/// Resolve a dialect descriptor for the given kind.
pub fn resolve(kind: descriptor::DialectKind) -> descriptor::DialectCapabilityDescriptor {
lazy_init();
if let Some(desc) = dialect_loader::DialectRegistry::global().get_descriptor(kind.label()) {
return desc;
}
descriptor::DialectCapabilityDescriptor::for_dialect(kind)
}
fn lazy_init() {
use std::sync::OnceLock;
static INIT: OnceLock<()> = OnceLock::new();
INIT.get_or_init(|| {
dialect_loader::register_core_dialects();
let _ = crate::dml_binding::DmlCleanRuleRegistry::load_default();
});
}
pub fn resolve_for_db(db_type: crate::models::connection::DatabaseType) -> descriptor::DialectCapabilityDescriptor {
let kind = descriptor::DialectKind::from_database_type(db_type);
resolve(kind)
}
pub fn resolve_label(kind: descriptor::DialectKind) -> String {
if let Some(loaded) = dialect_loader::DialectRegistry::global().get(kind.label()) {
return loaded.yaml.dialect.display_name.clone().unwrap_or_else(|| kind.label().to_string());
}
kind.label().to_string()
}
@@ -0,0 +1,153 @@
use crate::models::connection::DatabaseType;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TablePaginationStrategy {
LimitOffset,
FetchFirst,
Db2FetchFirst,
SqlServerTop,
IrisTop,
InformixFirst,
FirebirdRows,
Rownum,
QuestDbLimit,
Unbounded,
AgentMaxRows,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaginationContext {
TablePreview,
BoundedRead,
UserQuery,
}
pub fn is_schema_aware(database_type: DatabaseType) -> bool {
matches!(
database_type,
DatabaseType::Postgres
| DatabaseType::SqlServer
| DatabaseType::Oracle
| DatabaseType::Redshift
| DatabaseType::Dameng
| DatabaseType::Gaussdb
| DatabaseType::Kwdb
| DatabaseType::Kingbase
| DatabaseType::Highgo
| DatabaseType::Uxdb
| DatabaseType::Vastbase
| DatabaseType::Yashandb
| DatabaseType::Oscar
| DatabaseType::Databricks
| DatabaseType::SapHana
| DatabaseType::Teradata
| DatabaseType::Vertica
| DatabaseType::Exasol
| DatabaseType::OpenGauss
| DatabaseType::OceanbaseOracle
| DatabaseType::Gbase
| DatabaseType::Databend
| DatabaseType::Jdbc
| DatabaseType::H2
| DatabaseType::Snowflake
| DatabaseType::Trino
| DatabaseType::PrestoSql
| DatabaseType::Hive
| DatabaseType::Kyuubi
| DatabaseType::Impala
| DatabaseType::Argo
| DatabaseType::Spark
| DatabaseType::Db2
| DatabaseType::Informix
| DatabaseType::Tdengine
| DatabaseType::Xugu
| DatabaseType::Sqlite
| DatabaseType::DuckDb
| DatabaseType::Iris
// Spanner supports named schemas; the PostgreSQL dialect defaults to `public`.
// GoogleSQL's default schema is the empty string, which the blank-schema filters
// in `qualified_table_name` / `table_data_qualified_table_name` drop along with
// the dot separator (`` `s`.`t` `` with an empty `s` is a Spanner syntax error).
| DatabaseType::Spanner
)
}
pub fn uses_fetch_first(database_type: DatabaseType) -> bool {
matches!(database_type, DatabaseType::Oracle | DatabaseType::Dameng | DatabaseType::Db2)
}
pub fn uses_oracle_row_id(database_type: Option<DatabaseType>) -> bool {
matches!(database_type, Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle))
}
/// Xugu exposes an unqualified ROWID pseudo-column for base, partitioned and
/// temporary tables. It is intentionally separate from Oracle's ROWIDTOCHAR
/// representation because qualified ROWID and ROWIDTOCHAR are not supported
/// by Xugu.
pub fn uses_xugu_row_id(database_type: Option<DatabaseType>) -> bool {
database_type == Some(DatabaseType::Xugu)
}
pub fn uses_synthetic_row_id(database_type: Option<DatabaseType>) -> bool {
uses_oracle_row_id(database_type) || uses_xugu_row_id(database_type)
}
/// Oracle 系方言不支持 `INSERT ... VALUES (...), (...)` 多行语法,
/// 复制为 INSERT 与导出 INSERT 都需按行生成单条语句。
pub fn uses_single_row_insert_statements(database_type: DatabaseType) -> bool {
matches!(database_type, DatabaseType::Oracle | DatabaseType::OceanbaseOracle)
}
pub fn pagination_strategy(database_type: Option<DatabaseType>, context: PaginationContext) -> TablePaginationStrategy {
match database_type {
Some(DatabaseType::Jdbc) => TablePaginationStrategy::AgentMaxRows,
Some(DatabaseType::Oracle) if matches!(context, PaginationContext::TablePreview) => {
TablePaginationStrategy::Rownum
}
// Oracle's row-limiting clause (`FETCH FIRST`/`OFFSET ... FETCH`) was
// introduced in 12c. ROWNUM remains compatible with the supported 11g
// baseline while still providing a bounded read for newer servers.
Some(DatabaseType::Oracle) if matches!(context, PaginationContext::BoundedRead) => {
TablePaginationStrategy::Rownum
}
Some(DatabaseType::Oracle) => TablePaginationStrategy::Unbounded,
Some(DatabaseType::Oscar)
if matches!(context, PaginationContext::TablePreview | PaginationContext::BoundedRead) =>
{
TablePaginationStrategy::Rownum
}
Some(DatabaseType::Oscar) => TablePaginationStrategy::Unbounded,
Some(DatabaseType::Dameng) => TablePaginationStrategy::Rownum,
Some(DatabaseType::Db2) => TablePaginationStrategy::Db2FetchFirst,
Some(DatabaseType::SqlServer) => TablePaginationStrategy::SqlServerTop,
Some(DatabaseType::Iris) => TablePaginationStrategy::IrisTop,
Some(DatabaseType::Informix) => TablePaginationStrategy::InformixFirst,
Some(DatabaseType::Firebird) => TablePaginationStrategy::FirebirdRows,
Some(DatabaseType::OceanbaseOracle) => TablePaginationStrategy::Rownum,
Some(DatabaseType::Questdb) => TablePaginationStrategy::QuestDbLimit,
_ => TablePaginationStrategy::LimitOffset,
}
}
pub fn table_pagination_strategy(database_type: Option<DatabaseType>) -> TablePaginationStrategy {
pagination_strategy(database_type, PaginationContext::TablePreview)
}
pub(super) fn is_simple_informix_identifier(name: &str) -> bool {
let mut chars = name.chars();
let Some(first) = chars.next() else {
return false;
};
(first.is_ascii_alphabetic() || first == '_')
&& chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '$')
}
pub fn firebird_rows_clause(limit: usize, offset: usize) -> String {
if offset > 0 {
let start = offset + 1;
let end = offset + limit;
format!("ROWS {start} TO {end}")
} else {
format!("ROWS {limit}")
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,68 @@
use crate::sql_dialect::descriptor::{DialectCapabilityDescriptor, DialectInfo, DialectKind, TypeMappingMatrix};
use insta::assert_json_snapshot;
#[test]
fn snapshot_mysql_descriptor() {
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::Mysql);
assert_json_snapshot!("mysql_descriptor", desc);
}
#[test]
fn snapshot_postgres_descriptor() {
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::Postgres);
assert_json_snapshot!("postgres_descriptor", desc);
}
#[test]
fn snapshot_sqlite_descriptor() {
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::Sqlite);
assert_json_snapshot!("sqlite_descriptor", desc);
}
#[test]
fn snapshot_sqlserver_descriptor() {
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::SqlServer);
assert_json_snapshot!("sqlserver_descriptor", desc);
}
#[test]
fn snapshot_oracle_descriptor() {
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::Oracle);
assert_json_snapshot!("oracle_descriptor", desc);
}
#[test]
fn snapshot_duckdb_descriptor() {
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::DuckDb);
assert_json_snapshot!("duckdb_descriptor", desc);
}
#[test]
fn snapshot_mysql_info() {
let info = DialectInfo::for_kind(DialectKind::Mysql);
assert_json_snapshot!("mysql_info", info);
}
#[test]
fn snapshot_postgres_info() {
let info = DialectInfo::for_kind(DialectKind::Postgres);
assert_json_snapshot!("postgres_info", info);
}
#[test]
fn snapshot_mysql_to_postgres_type_mapping() {
let matrix = TypeMappingMatrix::for_dialects(DialectKind::Mysql, DialectKind::Postgres);
assert_json_snapshot!("mysql_to_postgres_mapping", &matrix.rules);
}
#[test]
fn snapshot_postgres_to_mysql_type_mapping() {
let matrix = TypeMappingMatrix::for_dialects(DialectKind::Postgres, DialectKind::Mysql);
assert_json_snapshot!("postgres_to_mysql_mapping", &matrix.rules);
}
#[test]
fn snapshot_all_dialects_info() {
let all = DialectInfo::all();
assert_json_snapshot!("all_dialects_info", all);
}
@@ -0,0 +1,434 @@
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use super::descriptor::{DialectCapabilityDescriptor, DialectKind};
use super::dialect_yaml::DialectYaml;
/// Singleton dialect registry that holds all loaded dialect descriptors.
/// Thread-safe via RwLock. Access via `DialectRegistry::global()`.
pub struct DialectRegistry {
descriptors: RwLock<HashMap<String, LoadedDialect>>,
plugin_dirs: RwLock<Vec<PathBuf>>,
}
#[derive(Debug, Clone)]
pub struct LoadedDialect {
pub dialect_name: String,
pub kind: DialectKind,
pub descriptor: DialectCapabilityDescriptor,
pub yaml: DialectYaml,
pub source_path: Option<PathBuf>,
pub loaded_at: String,
}
#[derive(Debug, Clone)]
pub struct LoadError {
pub path: PathBuf,
pub error: String,
}
impl DialectRegistry {
pub fn new() -> Self {
Self { descriptors: RwLock::new(HashMap::new()), plugin_dirs: RwLock::new(Vec::new()) }
}
pub fn global() -> &'static Self {
use std::sync::OnceLock;
static INSTANCE: OnceLock<DialectRegistry> = OnceLock::new();
INSTANCE.get_or_init(DialectRegistry::new)
}
pub fn add_plugin_dir(&self, dir: PathBuf) {
if let Ok(mut dirs) = self.plugin_dirs.write() {
if !dirs.contains(&dir) {
dirs.push(dir);
}
}
}
pub fn get(&self, name: &str) -> Option<LoadedDialect> {
self.descriptors.read().ok().and_then(|d| d.get(&name.to_ascii_lowercase()).cloned())
}
pub fn get_by_kind(&self, kind: DialectKind) -> Option<LoadedDialect> {
self.descriptors.read().ok().and_then(|d| d.values().find(|ld| ld.kind == kind).cloned())
}
/// Return all loaded dialects that map to the given kind.
/// Unlike `get_by_kind` (which returns only the first match), this is
/// order-independent and is used when a type's capabilities must be
/// resolved across an entire dialect family (e.g. PostgreSQL, HighGo,
/// KingBase all share `DialectKind::Postgres`).
pub fn get_all_by_kind(&self, kind: DialectKind) -> Vec<LoadedDialect> {
self.descriptors
.read()
.ok()
.map(|d| d.values().filter(|ld| ld.kind == kind).cloned().collect())
.unwrap_or_default()
}
pub fn get_descriptor(&self, name: &str) -> Option<DialectCapabilityDescriptor> {
self.get(name).map(|ld| ld.descriptor)
}
pub fn get_descriptor_for_db(
&self,
db_type: crate::models::connection::DatabaseType,
) -> Option<DialectCapabilityDescriptor> {
if let Some(dialect_name) = crate::database_manifest::dialect_name(&db_type) {
if let Some(loaded) = self.get(dialect_name) {
return Some(loaded.descriptor);
}
}
let kind = DialectKind::from_database_type(db_type);
let label = kind.label();
let db_name = db_type.as_str();
self.get(db_name).or_else(|| self.get(label)).map(|ld| ld.descriptor)
}
pub fn get_yaml(&self, name: &str) -> Option<DialectYaml> {
self.get(name).map(|ld| ld.yaml)
}
pub fn has(&self, name: &str) -> bool {
self.descriptors.read().ok().is_some_and(|d| d.contains_key(&name.to_ascii_lowercase()))
}
pub fn all_names(&self) -> Vec<String> {
self.descriptors.read().ok().map(|d| d.keys().cloned().collect()).unwrap_or_default()
}
pub fn all_descriptors(&self) -> Vec<DialectCapabilityDescriptor> {
self.descriptors.read().ok().map(|d| d.values().map(|ld| ld.descriptor).collect()).unwrap_or_default()
}
pub fn register(
&self,
name: &str,
kind: DialectKind,
descriptor: DialectCapabilityDescriptor,
yaml: DialectYaml,
source_path: Option<PathBuf>,
) {
if let Ok(mut d) = self.descriptors.write() {
d.insert(
name.to_ascii_lowercase(),
LoadedDialect {
dialect_name: name.to_string(),
kind,
descriptor,
yaml,
source_path,
loaded_at: chrono::Utc::now().to_rfc3339(),
},
);
}
}
pub fn register_yaml(&self, path: &Path, yaml: DialectYaml, descriptor: DialectCapabilityDescriptor) {
let kind = descriptor.dialect;
let name = yaml.dialect.name.clone();
self.register(&name, kind, descriptor, yaml, Some(path.to_path_buf()));
}
pub fn register_descriptor(&self, name: &str, descriptor: DialectCapabilityDescriptor, yaml: DialectYaml) {
let kind = descriptor.dialect;
self.register(name, kind, descriptor, yaml, None);
}
pub fn unregister(&self, name: &str) -> bool {
self.descriptors.write().ok().is_some_and(|mut d| d.remove(&name.to_ascii_lowercase()).is_some())
}
pub fn is_empty(&self) -> bool {
self.descriptors.read().ok().is_none_or(|d| d.is_empty())
}
pub fn len(&self) -> usize {
self.descriptors.read().ok().map_or(0, |d| d.len())
}
}
impl Default for DialectRegistry {
fn default() -> Self {
Self::new()
}
}
/// Register all core dialect YAML definitions embedded at compile time.
/// Called once at startup by `lazy_init()`.
pub fn register_core_dialects() {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
let registry = DialectRegistry::global();
include!(concat!(env!("OUT_DIR"), "/core_dialects.rs"));
let n = registry.len();
log::info!("Registered {n} core dialects");
});
}
// ============================================================================
// DialectPluginLoader — scans plugin directories and loads YAML files
// ============================================================================
pub struct DialectPluginLoader;
#[derive(Debug)]
pub struct LoadResult {
pub loaded: Vec<DialectKind>,
pub errors: Vec<LoadError>,
pub skipped: Vec<PathBuf>,
}
impl DialectPluginLoader {
pub fn scan_and_load(registry: &DialectRegistry, plugin_dirs: &[PathBuf]) -> LoadResult {
let mut result = LoadResult { loaded: Vec::new(), errors: Vec::new(), skipped: Vec::new() };
for dir in plugin_dirs {
if !dir.exists() {
continue;
}
let entries = match fs::read_dir(dir) {
Ok(entries) => entries,
Err(e) => {
result.errors.push(LoadError { path: dir.clone(), error: format!("Cannot read directory: {e}") });
continue;
}
};
for entry in entries {
let entry = match entry {
Ok(e) => e,
Err(_) => continue,
};
let path = entry.path();
if !path.is_file() {
continue;
}
let file_name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
if !matches!(extension, "yaml" | "yml") {
continue;
}
match Self::load_file(&path) {
Ok((kind, yaml, descriptor)) => {
registry.register(file_name, kind, descriptor, yaml, Some(path.clone()));
result.loaded.push(kind);
}
Err(e) => {
result.errors.push(LoadError { path: path.clone(), error: e });
}
}
}
}
result
}
pub fn load_file(path: &Path) -> Result<(DialectKind, DialectYaml, DialectCapabilityDescriptor), String> {
let content = fs::read_to_string(path).map_err(|e| format!("Cannot read file: {e}"))?;
Self::load_from_string(&content, Some(path))
}
pub fn load_from_string(
yaml_str: &str,
source_path: Option<&Path>,
) -> Result<(DialectKind, DialectYaml, DialectCapabilityDescriptor), String> {
let parsed: DialectYaml = serde_yaml::from_str(yaml_str).map_err(|e| format!("YAML parse error: {e}"))?;
let kind = parsed.dialect_kind().ok_or_else(|| {
format!(
"Unknown dialect name '{}' in {}",
parsed.dialect.name,
source_path.map_or_else(|| "<string>".to_string(), |p| p.display().to_string())
)
})?;
let validation_errors = parsed.validate();
if !validation_errors.is_empty() {
let msgs: Vec<String> = validation_errors.iter().map(|e| e.to_string()).collect();
return Err(format!("Validation errors: {}", msgs.join("; ")));
}
let descriptor = parsed.to_descriptor(kind);
Ok((kind, parsed, descriptor))
}
pub fn load_directory(registry: &DialectRegistry, dir: &Path) -> LoadResult {
Self::scan_and_load(registry, &[dir.to_path_buf()])
}
}
// ============================================================================
// Fallback: use hardcoded descriptors when YAML is unavailable
// ============================================================================
pub fn resolve_descriptor(kind: DialectKind, registry: &DialectRegistry) -> DialectCapabilityDescriptor {
if let Some(desc) = registry.get_descriptor(kind.label()) {
return desc;
}
DialectCapabilityDescriptor::for_dialect(kind)
}
pub fn resolve_descriptor_for_db(
db_type: crate::models::connection::DatabaseType,
registry: &DialectRegistry,
) -> DialectCapabilityDescriptor {
if let Some(desc) = registry.get_descriptor_for_db(db_type) {
return desc;
}
let kind = DialectKind::from_database_type(db_type);
DialectCapabilityDescriptor::for_dialect(kind)
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
fn temp_dialect_dir() -> PathBuf {
let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let dir = std::env::temp_dir().join(format!("dbx-dialect-test-{}-{stamp}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
dir
}
fn write_temp_yaml(dir: &Path, name: &str, dialect_name: &str) -> PathBuf {
let file_path = dir.join(name);
let yaml_content = format!(
r#"
dialect:
name: "{dialect_name}"
versions:
- version: "1.0"
identifier_rules:
quote_char: "\""
max_length: 64
"#
);
let mut file = fs::File::create(&file_path).unwrap();
file.write_all(yaml_content.as_bytes()).unwrap();
file_path
}
fn cleanup_temp_dir(dir: &Path) {
let _ = fs::remove_dir_all(dir);
}
#[test]
fn load_valid_yaml_file() {
let dir = temp_dialect_dir();
let path = write_temp_yaml(&dir, "dialect_postgresql.yaml", "PostgreSQL");
let result = DialectPluginLoader::load_file(&path);
cleanup_temp_dir(&dir);
assert!(result.is_ok());
let (kind, _yaml, desc) = result.unwrap();
assert_eq!(kind, DialectKind::Postgres);
assert_eq!(desc.dialect, DialectKind::Postgres);
}
#[test]
fn load_valid_yaml_file_mysql() {
let dir = temp_dialect_dir();
let path = write_temp_yaml(&dir, "dialect_mysql.yaml", "MySQL");
let result = DialectPluginLoader::load_file(&path);
cleanup_temp_dir(&dir);
assert!(result.is_ok());
let (kind, _, _) = result.unwrap();
assert_eq!(kind, DialectKind::Mysql);
}
#[test]
fn load_invalid_dialect_name() {
let dir = temp_dialect_dir();
let path = write_temp_yaml(&dir, "dialect_unknown.yaml", "UnknownDB");
let result = DialectPluginLoader::load_file(&path);
cleanup_temp_dir(&dir);
assert!(result.is_err());
}
#[test]
fn load_invalid_yaml_syntax() {
let dir = temp_dialect_dir();
let path = dir.join("dialect_bad.yaml");
let mut file = fs::File::create(&path).unwrap();
file.write_all(b"dialect: [invalid: yaml: syntax:").unwrap();
let result = DialectPluginLoader::load_file(&path);
cleanup_temp_dir(&dir);
assert!(result.is_err());
}
#[test]
fn registry_register_and_get() {
let registry = DialectRegistry::new();
assert!(registry.is_empty());
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::Mysql);
let yaml = DialectYaml {
dialect: super::super::dialect_yaml::DialectMeta {
name: "MySQL".to_string(),
display_name: Some("MySQL".to_string()),
versions: vec![],
},
identifier_rules: super::super::dialect_yaml::IdentifierRules {
quote_char: "`".to_string(),
case_sensitive: false,
max_length: 64,
},
..Default::default()
};
registry.register_descriptor("MySQL", desc, yaml);
assert_eq!(registry.len(), 1);
assert!(registry.has("MySQL"));
assert!(!registry.has("PostgreSQL"));
let loaded = registry.get("MySQL").unwrap();
assert_eq!(loaded.dialect_name, "MySQL");
registry.unregister("MySQL");
assert!(registry.is_empty());
}
#[test]
fn registry_fallback_resolve() {
let registry = DialectRegistry::new();
let desc = resolve_descriptor(DialectKind::Mysql, &registry);
assert!(desc.has_capability(super::super::descriptor::CAP_ADD_COLUMN));
}
#[test]
fn registry_all_kinds() {
let registry = DialectRegistry::new();
let desc = DialectCapabilityDescriptor::for_dialect(DialectKind::Mysql);
let yaml = DialectYaml {
dialect: super::super::dialect_yaml::DialectMeta {
name: "MySQL".to_string(),
display_name: None,
versions: vec![],
},
identifier_rules: super::super::dialect_yaml::IdentifierRules {
quote_char: "`".to_string(),
case_sensitive: false,
max_length: 64,
},
..Default::default()
};
registry.register_descriptor("MySQL", desc, yaml);
let names = registry.all_names();
assert_eq!(names, vec!["mysql"]);
}
}
@@ -0,0 +1,135 @@
use crate::sql_dialect::descriptor::DialectKind;
use crate::sql_dialect::dialect_loader::DialectRegistry;
pub fn list_dialect_type_names(dialect_name: &str) -> Vec<String> {
super::lazy_init();
list_dialect_type_names_in(dialect_name, DialectRegistry::global())
}
fn list_dialect_type_names_in(dialect_name: &str, registry: &DialectRegistry) -> Vec<String> {
if let Some(loaded) = registry.get(dialect_name) {
return loaded.yaml.types.iter().map(|t| t.name.clone()).collect();
}
// Public callers may use stable dialect labels such as `postgres`,
// while the embedded catalog keeps the product name `PostgreSQL`.
if let Some(kind) = DialectKind::from_label(dialect_name) {
if let Some(loaded) = registry.get(core_catalog_name(kind)) {
return loaded.yaml.types.iter().map(|t| t.name.clone()).collect();
}
// Last-resort fallback: any dialect in the same family
if let Some(loaded) = registry.get_by_kind(kind) {
return loaded.yaml.types.iter().map(|t| t.name.clone()).collect();
}
}
Vec::new()
}
fn core_catalog_name(kind: DialectKind) -> &'static str {
match kind {
DialectKind::Mysql => "MySQL",
DialectKind::Postgres => "PostgreSQL",
DialectKind::Sqlite => "SQLite",
DialectKind::DuckDb => "DuckDB",
DialectKind::SqlServer => "SQL Server",
DialectKind::Oracle => "Oracle",
DialectKind::H2 => "H2",
DialectKind::ClickHouse => "ClickHouse",
DialectKind::ManticoreSearch => "ManticoreSearch",
DialectKind::Informix => "Informix",
DialectKind::Questdb => "QuestDB",
DialectKind::Soql => "SOQL",
DialectKind::Unsupported => "Unsupported",
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sql_dialect::descriptor::DialectCapabilityDescriptor;
use crate::sql_dialect::dialect_yaml::{DialectMeta, DialectType, DialectVersion, DialectYaml, IdentifierRules};
fn make_registry_with_types(dialect_name: &str, type_names: &[&str]) -> DialectRegistry {
let registry = DialectRegistry::new();
let types: Vec<DialectType> = type_names
.iter()
.map(|&name| DialectType {
name: name.to_string(),
category: "STRING".to_string(),
max_precision: None,
precision_range: None,
has_length: false,
has_precision: false,
aliases: vec![],
semantic_fidelity_base: 1.0,
})
.collect();
let yaml = DialectYaml {
dialect: DialectMeta {
name: dialect_name.to_string(),
display_name: Some(dialect_name.to_string()),
versions: vec![DialectVersion { version: "1.0".to_string(), status: "COMPATIBLE".to_string() }],
},
types,
identifier_rules: IdentifierRules { quote_char: "\"".to_string(), case_sensitive: false, max_length: 64 },
..Default::default()
};
let kind = yaml.dialect_kind().unwrap();
let descriptor = DialectCapabilityDescriptor::for_dialect(kind);
registry.register_descriptor(dialect_name, descriptor, yaml);
registry
}
#[test]
fn list_mysql_types() {
let registry = make_registry_with_types("MySQL", &["VARCHAR", "INT", "BIGINT"]);
let types = list_dialect_type_names_in("MySQL", &registry);
assert_eq!(types, vec!["VARCHAR", "INT", "BIGINT"]);
assert!(types.contains(&"VARCHAR".to_string()));
}
#[test]
fn list_dameng_types() {
let registry = make_registry_with_types(
"Dameng",
&[
"VARCHAR",
"VARCHAR2",
"INT",
"INTEGER",
"BIGINT",
"FLOAT",
"DOUBLE",
"DATE",
"TIMESTAMP",
"CLOB",
"BLOB",
"TEXT",
],
);
let types = list_dialect_type_names_in("Dameng", &registry);
assert!(!types.is_empty(), "Dameng should have types");
assert!(types.contains(&"VARCHAR2".to_string()));
assert_eq!(types.len(), 12);
}
#[test]
fn list_unknown_dialect_returns_empty() {
let registry = DialectRegistry::new();
let types = list_dialect_type_names_in("nonexistent_db", &registry);
assert!(types.is_empty());
}
#[test]
fn lists_embedded_postgres_types_by_name_and_label() {
let by_name = list_dialect_type_names("PostgreSQL");
let by_label = list_dialect_type_names("postgres");
assert!(!by_name.is_empty());
assert_eq!(by_label, by_name);
assert!(by_name.contains(&"SMALLINT".to_string()));
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,143 @@
use std::path::PathBuf;
use std::time::Duration;
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use tokio::sync::mpsc;
use super::dialect_loader::DialectPluginLoader;
/// Configuration for the hot-reload watcher.
#[derive(Debug, Clone)]
pub struct HotReloadConfig {
/// Debounce duration — multiple changes within this window are coalesced.
pub debounce_ms: u64,
/// Whether to enable polling (needed for some network filesystems).
pub enable_polling: bool,
/// Polling interval when polling is enabled.
pub poll_interval_ms: u64,
}
impl Default for HotReloadConfig {
fn default() -> Self {
Self { debounce_ms: 500, enable_polling: false, poll_interval_ms: 2000 }
}
}
/// Result of a hot-reload operation on a changed file.
#[derive(Debug, Clone)]
pub enum ReloadEvent {
/// A dialect YAML file was successfully loaded/updated.
Loaded { path: PathBuf },
/// A dialect YAML file was removed — dialect unregistered.
Removed { path: PathBuf, dialect: String },
/// A file changed but failed to load.
Error { path: PathBuf, error: String },
}
/// Hot-reload watcher that monitors `plugins/dialects/` for YAML changes
/// and updates the `DialectRegistry` in real time.
pub struct DialectHotReload {
watcher: Option<RecommendedWatcher>,
config: HotReloadConfig,
}
impl DialectHotReload {
pub fn new(config: HotReloadConfig) -> Self {
Self { watcher: None, config }
}
/// Start watching. Returns a channel receiver for reload events.
pub fn start(&mut self, watch_dirs: Vec<PathBuf>) -> Result<mpsc::UnboundedReceiver<ReloadEvent>, String> {
let (tx, rx) = mpsc::unbounded_channel();
let _debounce = Duration::from_millis(self.config.debounce_ms);
let tx_clone = tx.clone();
let mut watcher = RecommendedWatcher::new(
move |res: Result<Event, notify::Error>| match res {
Ok(event) => {
if !Self::is_relevant(&event) {
return;
}
for path in &event.paths {
let _ = tx_clone.send(match event.kind {
EventKind::Create(_) | EventKind::Modify(_) => ReloadEvent::Loaded { path: path.clone() },
EventKind::Remove(_) => ReloadEvent::Removed {
path: path.clone(),
dialect: path.file_stem().and_then(|s| s.to_str()).unwrap_or("unknown").to_string(),
},
_ => continue,
});
}
}
Err(e) => {
let _ = tx_clone
.send(ReloadEvent::Error { path: PathBuf::from("."), error: format!("Watcher error: {e}") });
}
},
Config::default()
.with_poll_interval(Duration::from_millis(self.config.poll_interval_ms))
.with_compare_contents(true),
)
.map_err(|e| format!("Failed to create file watcher: {e}"))?;
for dir in &watch_dirs {
if dir.exists() {
watcher
.watch(dir, RecursiveMode::NonRecursive)
.map_err(|e| format!("Failed to watch {}: {e}", dir.display()))?;
}
}
self.watcher = Some(watcher);
Ok(rx)
}
/// Process reload events in a loop, updating the registry.
/// This is a convenience function that runs forever on the current task.
pub async fn run_forever(
watch_dirs: Vec<PathBuf>,
registry: &'static super::dialect_loader::DialectRegistry,
) -> Result<(), String> {
let mut reload = Self::new(HotReloadConfig::default());
let mut rx = reload.start(watch_dirs)?;
while let Some(event) = rx.recv().await {
match &event {
ReloadEvent::Loaded { path } => match DialectPluginLoader::load_file(path) {
Ok((kind, yaml, descriptor)) => {
registry.register_yaml(path, yaml, descriptor);
log::info!("Hot-reload: loaded dialect {:?} from {}", kind, path.display());
}
Err(e) => {
log::warn!("Hot-reload: failed to load {}: {e}", path.display());
}
},
ReloadEvent::Removed { path, dialect } => {
registry.unregister(dialect);
log::info!("Hot-reload: unregistered dialect {dialect} ({})", path.display());
}
ReloadEvent::Error { path, error } => {
log::error!("Hot-reload: error watching {}: {error}", path.display());
}
}
}
Ok(())
}
fn is_relevant(event: &Event) -> bool {
for path in &event.paths {
if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
if matches!(ext, "yaml" | "yml") {
return true;
}
}
}
false
}
pub fn stop(&mut self) {
self.watcher = None;
}
}
@@ -0,0 +1,470 @@
use crate::models::connection::DatabaseType;
use percent_encoding::percent_decode_str;
use super::capabilities::{is_schema_aware, is_simple_informix_identifier};
pub const SQLSERVER_LINKED_SCHEMA_PREFIX: &str = "__dbx_sqlserver_linked__:";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlServerLinkedSchemaRef {
pub server: String,
pub catalog: String,
pub schema: String,
}
pub fn parse_sqlserver_linked_schema_ref(schema: &str) -> Option<SqlServerLinkedSchemaRef> {
let payload = schema.strip_prefix(SQLSERVER_LINKED_SCHEMA_PREFIX)?;
let mut parts = payload.split('|');
let server = decode_linked_schema_part(parts.next()?)?;
let catalog = decode_linked_schema_part(parts.next()?)?;
let schema = decode_linked_schema_part(parts.next()?)?;
if parts.next().is_some() || server.trim().is_empty() || catalog.trim().is_empty() || schema.trim().is_empty() {
return None;
}
Some(SqlServerLinkedSchemaRef { server, catalog, schema })
}
pub fn sqlserver_linked_table_name(linked: &SqlServerLinkedSchemaRef, table_name: &str) -> String {
[linked.server.as_str(), linked.catalog.as_str(), linked.schema.as_str(), table_name]
.into_iter()
.map(|part| quote_table_identifier(Some(DatabaseType::SqlServer), part))
.collect::<Vec<_>>()
.join(".")
}
fn decode_linked_schema_part(value: &str) -> Option<String> {
percent_decode_str(value).decode_utf8().ok().map(|value| value.into_owned())
}
pub fn qualified_table_name(database_type: Option<DatabaseType>, schema: Option<&str>, table_name: &str) -> String {
if database_type == Some(DatabaseType::Iotdb) {
let table_name = quote_table_identifier(database_type, table_name);
let schema = schema.map(str::trim).filter(|schema| !schema.is_empty());
if let Some(schema) = schema {
if table_name == schema || table_name.starts_with(&format!("{schema}.")) {
return table_name;
}
return format!("{}.{}", quote_table_identifier(database_type, schema), table_name);
}
return table_name;
}
let supports_qualifier = database_type.is_some_and(is_schema_aware)
|| matches!(
database_type,
Some(DatabaseType::Mysql | DatabaseType::Goldendb | DatabaseType::StarRocks | DatabaseType::Doris)
);
if supports_qualifier
&& database_type != Some(DatabaseType::Jdbc)
&& schema.is_some_and(|schema| !schema.trim().is_empty())
{
if database_type == Some(DatabaseType::SqlServer) {
if let Some(linked) = schema.and_then(parse_sqlserver_linked_schema_ref) {
return sqlserver_linked_table_name(&linked, table_name);
}
}
return format!(
"{}.{}",
quote_table_identifier(database_type, schema.unwrap()),
quote_table_identifier(database_type, table_name)
);
}
quote_table_identifier(database_type, table_name)
}
/// Like `qualified_table_name`, but also supports 3-part names for SQL Server
/// (`<database>.<schema>.<table>`) and Doris/StarRocks external catalogs
/// (`<catalog>.<database>.<table>`). SQL parsing stores the first segment of a
/// 3-part source in `catalog`; for SQL Server that segment is its database.
/// Doris/StarRocks use `schema` as the middle segment when present, otherwise
/// `database`, and ignore their built-in `internal` catalog.
pub fn qualified_table_name_with_catalog(
database_type: Option<DatabaseType>,
catalog: Option<&str>,
schema: Option<&str>,
database: Option<&str>,
table_name: &str,
) -> String {
let catalog = catalog.map(str::trim).filter(|catalog| !catalog.is_empty());
match (catalog, database_type) {
(Some(database), Some(DatabaseType::SqlServer)) => {
let table = qualified_table_name(database_type, schema, table_name);
format!("{}.{}", quote_table_identifier(database_type, database), table)
}
(Some(catalog), Some(DatabaseType::Doris | DatabaseType::StarRocks)) if catalog != "internal" => {
let middle = schema
.map(str::trim)
.filter(|schema| !schema.is_empty())
.or_else(|| database.map(str::trim).filter(|database| !database.is_empty()));
let table = match middle {
Some(middle) => format!(
"{}.{}",
quote_table_identifier(database_type, middle),
quote_table_identifier(database_type, table_name)
),
None => quote_table_identifier(database_type, table_name),
};
format!("{}.{}", quote_table_identifier(database_type, catalog), table)
}
_ => qualified_table_name(database_type, schema, table_name),
}
}
pub fn quote_table_identifier(database_type: Option<DatabaseType>, name: &str) -> String {
if matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss))
&& is_explicitly_quoted_identifier(name)
{
return name.to_string();
}
match database_type {
Some(DatabaseType::Iotdb) => name.to_string(),
// SOQL has no delimited identifiers: `SELECT * FROM "Account"` fails with
// MALFORMED_QUERY, and double quotes are string literals there. Object and
// field API names are always sent bare.
Some(DatabaseType::Salesforce) => name.to_string(),
// JDBC connections use the driver-reported identifier quote string
// (DatabaseMetaData.getIdentifierQuoteString()) inside the JDBC agent,
// so the Rust layer passes identifiers through unquoted.
Some(DatabaseType::Jdbc) => name.to_string(),
Some(
DatabaseType::Mysql
| DatabaseType::ClickHouse
| DatabaseType::Doris
| DatabaseType::Goldendb
| DatabaseType::StarRocks
| DatabaseType::ManticoreSearch
| DatabaseType::Hive
| DatabaseType::Kyuubi
| DatabaseType::Impala
| DatabaseType::Argo
| DatabaseType::Spark
| DatabaseType::Databricks
| DatabaseType::Databend
| DatabaseType::Tdengine
| DatabaseType::Access
| DatabaseType::Bigquery
// GoogleSQL (Spanner's default dialect) quotes identifiers with backticks;
// double quotes are string literals there, so `SELECT * FROM "users"` is a
// syntax error. PostgreSQL-dialect Spanner databases override this through
// the identifier quote reported by the agent on connect.
| DatabaseType::Spanner
| DatabaseType::Questdb,
) => {
format!("`{}`", name.replace('`', "``"))
}
Some(DatabaseType::Informix) if is_simple_informix_identifier(name) => name.to_string(),
Some(DatabaseType::Neo4j) => format!("`{}`", name.replace('`', "``")),
Some(DatabaseType::SqlServer) => format!("[{}]", name.replace(']', "]]")),
_ => format!("\"{}\"", name.replace('"', "\"\"")),
}
}
/// Quote an IRIS identifier only when its spelling requires a delimited name.
///
/// Caché/IRIS installations may disable delimited identifiers. The IRIS JDBC
/// preparser turns quoted names into `:%qpar(...)` parameters in that mode,
/// which is not valid in table references. Ordinary metadata names are
/// case-insensitive and can be sent unquoted; names containing separators or
/// other punctuation still need the identifier quote character.
pub fn quote_iris_identifier(name: &str, identifier_quote: Option<&str>) -> String {
let mut chars = name.chars();
let ordinary = chars.next().is_some_and(|first| first == '_' || first == '%' || first.is_alphabetic())
&& chars.all(|ch| ch == '_' || ch.is_alphanumeric());
if ordinary {
return name.to_string();
}
let quote = identifier_quote.map(str::trim).filter(|quote| !quote.is_empty()).unwrap_or("\"");
format!("{quote}{}{quote}", name.replace(quote, &format!("{quote}{quote}")))
}
pub fn quote_gaussdb_jdbc_identifier(name: &str, identifier_quote: &str) -> String {
if is_explicitly_quoted_identifier(name) {
return name.to_string();
}
let quote = identifier_quote.trim();
if quote.is_empty() {
return name.to_string();
}
let requires_quote = !is_simple_lower_identifier(name)
|| is_postgres_reserved_identifier(name)
|| (quote == "`" && is_mysql_only_reserved_identifier(name));
if !requires_quote {
return name.to_string();
}
format!("{quote}{}{quote}", name.replace(quote, &format!("{quote}{quote}")))
}
fn is_explicitly_quoted_identifier(name: &str) -> bool {
name.len() >= 2
&& ((name.starts_with('"') && name.ends_with('"'))
|| (name.starts_with('`') && name.ends_with('`'))
|| (name.starts_with('[') && name.ends_with(']')))
}
fn is_simple_lower_identifier(name: &str) -> bool {
let mut chars = name.chars();
let Some(first) = chars.next() else {
return false;
};
(first == '_' || first.is_ascii_lowercase())
&& chars.all(|ch| ch == '_' || ch == '$' || ch.is_ascii_lowercase() || ch.is_ascii_digit())
}
fn is_postgres_reserved_identifier(name: &str) -> bool {
matches!(
name,
"all"
| "analyse"
| "analyze"
| "and"
| "any"
| "array"
| "as"
| "asc"
| "asymmetric"
| "authorization"
| "binary"
| "both"
| "case"
| "cast"
| "check"
| "collate"
| "collation"
| "column"
| "concurrently"
| "constraint"
| "create"
| "cross"
| "current_catalog"
| "current_date"
| "current_role"
| "current_schema"
| "current_time"
| "current_timestamp"
| "current_user"
| "default"
| "deferrable"
| "desc"
| "distinct"
| "do"
| "else"
| "end"
| "except"
| "false"
| "fetch"
| "for"
| "foreign"
| "freeze"
| "from"
| "full"
| "grant"
| "group"
| "having"
| "ilike"
| "in"
| "initially"
| "inner"
| "intersect"
| "into"
| "is"
| "isnull"
| "join"
| "lateral"
| "leading"
| "left"
| "like"
| "limit"
| "localtime"
| "localtimestamp"
| "natural"
| "not"
| "notnull"
| "null"
| "offset"
| "on"
| "only"
| "or"
| "order"
| "outer"
| "overlaps"
| "placing"
| "primary"
| "references"
| "returning"
| "right"
| "select"
| "session_user"
| "similar"
| "some"
| "symmetric"
| "system_user"
| "table"
| "tablesample"
| "then"
| "to"
| "trailing"
| "true"
| "union"
| "unique"
| "user"
| "using"
| "variadic"
| "verbose"
| "when"
| "where"
| "window"
| "with"
)
}
fn is_mysql_only_reserved_identifier(name: &str) -> bool {
matches!(
name,
"accessible"
| "auto_increment"
| "change"
| "database"
| "databases"
| "delayed"
| "describe"
| "div"
| "dual"
| "enclosed"
| "escaped"
| "explain"
| "force"
| "fulltext"
| "high_priority"
| "ignore"
| "index"
| "infile"
| "key"
| "keys"
| "kill"
| "linear"
| "lines"
| "load"
| "lock"
| "low_priority"
| "master_ssl_verify_server_cert"
| "maxvalue"
| "mediumint"
| "mod"
| "no_write_to_binlog"
| "optimize"
| "optionally"
| "outfile"
| "partition"
| "purge"
| "range"
| "read_write"
| "regexp"
| "release"
| "rename"
| "replace"
| "require"
| "rlike"
| "schema"
| "schemas"
| "separator"
| "show"
| "spatial"
| "sql_big_result"
| "sql_calc_found_rows"
| "sql_small_result"
| "ssl"
| "starting"
| "straight_join"
| "terminated"
| "tinyint"
| "unlock"
| "unsigned"
| "use"
| "utc_date"
| "utc_time"
| "utc_timestamp"
| "values"
| "varbinary"
| "varchar"
| "write"
| "xor"
| "zerofill"
)
}
pub fn normalize_where_input(where_input: Option<&str>) -> String {
let trimmed = where_input.unwrap_or("").trim().trim_end_matches(';').trim();
let mut chars = trimmed.chars();
let prefix = chars.by_ref().take(5).collect::<String>();
if prefix.eq_ignore_ascii_case("where") {
chars.as_str().trim().to_string()
} else {
trimmed.to_string()
}
}
pub fn quote_transfer_identifier(name: &str, database_type: &DatabaseType) -> String {
match database_type {
DatabaseType::Mysql
| DatabaseType::ClickHouse
| DatabaseType::Doris
| DatabaseType::StarRocks
| DatabaseType::Hive
| DatabaseType::Kyuubi
| DatabaseType::Impala
| DatabaseType::Argo
| DatabaseType::Spark
| DatabaseType::Questdb => format!("`{}`", name.replace('`', "``")),
DatabaseType::SqlServer => format!("[{}]", name.replace(']', "]]")),
_ => format!("\"{}\"", name.replace('\"', "\"\"")),
}
}
pub fn transfer_column_identifier(name: &str, database_type: &DatabaseType, quote_target_column_names: bool) -> String {
if quote_target_column_names
|| !matches!(database_type, DatabaseType::Gaussdb | DatabaseType::OpenGauss)
|| !is_simple_unquoted_identifier(name)
|| is_postgres_reserved_identifier(&name.to_ascii_lowercase())
{
quote_transfer_identifier(name, database_type)
} else {
name.to_string()
}
}
fn is_simple_unquoted_identifier(name: &str) -> bool {
let mut chars = name.chars();
let Some(first) = chars.next() else {
return false;
};
(first == '_' || first.is_ascii_alphabetic())
&& chars.all(|ch| ch == '_' || ch == '$' || ch.is_ascii_alphanumeric())
}
/// Qualified table name for transfer SQL.
///
/// * Without catalog: produces `schema.table` (or just `table` for MySQL family).
/// * With catalog AND the database type supports external catalogs (Doris/StarRocks):
/// produces `catalog.schema.table` — the 3-part form those engines require to
/// address objects in an external (non-internal) catalog.
pub fn qualified_transfer_table(
table_name: &str,
schema: &str,
database_type: &DatabaseType,
catalog: Option<&str>,
) -> String {
let table = quote_transfer_identifier(table_name, database_type);
if let Some(catalog) = catalog {
format!(
"{}.{}.{}",
quote_transfer_identifier(catalog, database_type),
quote_transfer_identifier(schema, database_type),
table
)
} else if schema.is_empty()
|| matches!(database_type, DatabaseType::Mysql | DatabaseType::MongoDb | DatabaseType::Questdb)
{
table
} else {
format!("{}.{}", quote_transfer_identifier(schema, database_type), table)
}
}
@@ -0,0 +1,320 @@
use super::descriptor::{DialectKind, TypeMappingMatrix};
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnType {
pub base_type: String,
pub precision: Option<u32>,
pub scale: Option<u32>,
pub length: Option<u32>,
pub is_array: bool,
pub is_unsigned: bool,
pub extras: Vec<String>,
}
impl ColumnType {
pub fn parse(type_str: &str) -> Self {
let trimmed = type_str.trim();
let (base_type, params_str) = match trimmed.find('(') {
Some(pos) if trimmed.ends_with(')') => {
let base = trimmed[..pos].trim().to_string();
let params = trimmed[pos + 1..trimmed.len() - 1].trim();
(base, Some(params))
}
_ => (trimmed.to_string(), None),
};
let is_unsigned = base_type.to_ascii_lowercase().contains("unsigned");
let is_array = trimmed.ends_with("[]") || trimmed.to_ascii_lowercase().contains(" array");
let base_type_clean =
base_type.replace(" unsigned", "").replace(" UNSIGNED", "").replace("[]", "").trim().to_string();
let (precision, scale, length) = match params_str {
Some(params) if params.contains(',') => {
let parts: Vec<&str> = params.split(',').collect();
if parts.len() >= 2 {
let p = parts[0].trim().parse().ok();
let s = parts[1].trim().parse().ok();
(p, s, None)
} else {
(None, None, params.parse().ok())
}
}
Some(params) => {
let v = params.parse().ok();
(None, None, v)
}
None => (None, None, None),
};
Self { base_type: base_type_clean, precision, scale, length, is_array, is_unsigned, extras: Vec::new() }
}
pub fn to_string(&self, dialect: DialectKind) -> String {
let mut result = self.base_type.clone();
if self.is_unsigned && dialect == DialectKind::Mysql {
result.push_str(" UNSIGNED");
}
if let Some(len) = self.length {
result.push_str(&format!("({len})"));
} else if let Some(p) = self.precision {
if let Some(s) = self.scale {
result.push_str(&format!("({p},{s})"));
} else {
result.push_str(&format!("({p})"));
}
}
if self.is_array && (dialect == DialectKind::Postgres || dialect == DialectKind::DuckDb) {
result.push_str("[]");
}
result
}
}
pub trait TypeInferenceEngine {
fn infer_type(&self, source_type: &str, source_dialect: DialectKind, target_dialect: DialectKind) -> ColumnType;
fn suggest_type(&self, column: &ColumnType, target_dialect: DialectKind) -> String;
fn convert_default_value(
&self,
default_expr: &str,
source_dialect: DialectKind,
target_dialect: DialectKind,
) -> String;
fn type_compatibility_score(&self, source: &ColumnType, target: &ColumnType) -> f64;
}
const SCORE_IDENTICAL: f64 = 1.0;
const SCORE_ALIAS: f64 = 0.9;
const SCORE_INTEGER_FAMILY: f64 = 0.8;
const SCORE_TEXT_FAMILY: f64 = 0.7;
const SCORE_UNRELATED: f64 = 0.3;
pub struct DefaultTypeInferenceEngine;
impl DefaultTypeInferenceEngine {
fn map_type_name(&self, type_name: &str, from: DialectKind, to: DialectKind) -> String {
let matrix = TypeMappingMatrix::for_dialects(from, to);
let (converted, _) = matrix.convert_type(type_name);
converted
}
}
impl TypeInferenceEngine for DefaultTypeInferenceEngine {
fn infer_type(&self, source_type: &str, source_dialect: DialectKind, target_dialect: DialectKind) -> ColumnType {
let source_parsed = ColumnType::parse(source_type);
let mapped_base = self.map_type_name(&source_parsed.base_type, source_dialect, target_dialect);
let target_caps = crate::sql_dialect::resolve(target_dialect);
let needs_precision = mapped_base.to_ascii_lowercase().contains("decimal")
|| mapped_base.to_ascii_lowercase().contains("numeric")
|| mapped_base.to_ascii_lowercase().contains("varchar");
let (precision, scale, length) = if needs_precision {
(source_parsed.precision, source_parsed.scale, source_parsed.length)
} else if mapped_base.eq_ignore_ascii_case("integer") && source_parsed.precision.is_some() {
(None, None, None)
} else {
(source_parsed.precision, source_parsed.scale, source_parsed.length)
};
ColumnType {
base_type: mapped_base,
precision,
scale,
length,
is_array: source_parsed.is_array && target_caps.supports_array_type,
is_unsigned: source_parsed.is_unsigned && target_dialect == DialectKind::Mysql,
extras: source_parsed.extras,
}
}
fn suggest_type(&self, column: &ColumnType, target_dialect: DialectKind) -> String {
column.to_string(target_dialect)
}
fn convert_default_value(
&self,
default_expr: &str,
source_dialect: DialectKind,
target_dialect: DialectKind,
) -> String {
let trimmed = default_expr.trim();
if trimmed.is_empty() || trimmed.eq_ignore_ascii_case("null") || trimmed.eq_ignore_ascii_case("NULL") {
return String::new();
}
match (source_dialect, target_dialect) {
(DialectKind::Mysql, DialectKind::Postgres) => {
let lower = trimmed.to_ascii_lowercase();
if lower == "current_timestamp" || lower == "current_timestamp()" || lower == "now()" {
return "CURRENT_TIMESTAMP".to_string();
}
if lower.starts_with("on update ") {
return String::new();
}
}
(DialectKind::Postgres, DialectKind::Mysql) => {
let lower = trimmed.to_ascii_lowercase();
if lower == "current_timestamp" || lower == "now()" || lower == "transaction_timestamp()" {
return "CURRENT_TIMESTAMP".to_string();
}
}
(DialectKind::Mysql, DialectKind::Sqlite) => {
let lower = trimmed.to_ascii_lowercase();
if lower == "current_timestamp" || lower == "current_timestamp()" || lower == "now()" {
return "CURRENT_TIMESTAMP".to_string();
}
}
_ => {}
}
trimmed.to_string()
}
fn type_compatibility_score(&self, source: &ColumnType, target: &ColumnType) -> f64 {
let source_lower = source.base_type.to_ascii_lowercase();
let target_lower = target.base_type.to_ascii_lowercase();
if source_lower == target_lower {
return SCORE_IDENTICAL;
}
let exact_matches = [
("int", "integer"),
("integer", "int"),
("float", "real"),
("real", "float"),
("double", "double precision"),
("double precision", "double"),
("bool", "boolean"),
("boolean", "bool"),
("timestamp", "datetime"),
("datetime", "timestamp"),
];
if exact_matches.contains(&(source_lower.as_str(), target_lower.as_str())) {
return SCORE_ALIAS;
}
let integer_family = ["tinyint", "smallint", "mediumint", "int", "integer", "bigint", "serial", "bigserial"];
let text_family = ["char", "varchar", "text", "tinytext", "mediumtext", "longtext", "clob", "nclob"];
if integer_family.contains(&source_lower.as_str()) && integer_family.contains(&target_lower.as_str()) {
return SCORE_INTEGER_FAMILY;
}
if text_family.contains(&source_lower.as_str()) && text_family.contains(&target_lower.as_str()) {
return SCORE_TEXT_FAMILY;
}
SCORE_UNRELATED
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_simple_type() {
let t = ColumnType::parse("INT");
assert_eq!(t.base_type, "INT");
assert!(t.precision.is_none());
assert!(t.length.is_none());
}
#[test]
fn parse_type_with_length() {
let t = ColumnType::parse("VARCHAR(255)");
assert_eq!(t.base_type, "VARCHAR");
assert_eq!(t.length, Some(255));
}
#[test]
fn parse_type_with_precision_scale() {
let t = ColumnType::parse("DECIMAL(10,2)");
assert_eq!(t.base_type, "DECIMAL");
assert_eq!(t.precision, Some(10));
assert_eq!(t.scale, Some(2));
}
#[test]
fn parse_unsigned_type() {
let t = ColumnType::parse("INT UNSIGNED");
assert_eq!(t.base_type, "INT");
assert!(t.is_unsigned);
}
#[test]
fn infer_mysql_int_to_postgres() {
let engine = DefaultTypeInferenceEngine;
let result = engine.infer_type("INT", DialectKind::Mysql, DialectKind::Postgres);
assert_eq!(result.base_type, "INTEGER");
}
#[test]
fn infer_mysql_datetime_to_postgres() {
let engine = DefaultTypeInferenceEngine;
let result = engine.infer_type("DATETIME", DialectKind::Mysql, DialectKind::Postgres);
assert_eq!(result.base_type, "TIMESTAMP");
}
#[test]
fn infer_postgres_text_to_mysql() {
let engine = DefaultTypeInferenceEngine;
let result = engine.infer_type("TEXT", DialectKind::Postgres, DialectKind::Mysql);
assert_eq!(result.base_type, "LONGTEXT");
}
#[test]
fn type_compatibility_exact_match() {
let engine = DefaultTypeInferenceEngine;
let a = ColumnType::parse("INT");
let b = ColumnType::parse("INTEGER");
let score = engine.type_compatibility_score(&a, &b);
assert!((score - 0.9).abs() < 0.01);
}
#[test]
fn type_compatibility_integer_family() {
let engine = DefaultTypeInferenceEngine;
let a = ColumnType::parse("SMALLINT");
let b = ColumnType::parse("BIGINT");
let score = engine.type_compatibility_score(&a, &b);
assert!((score - 0.8).abs() < 0.01);
}
#[test]
fn convert_default_value_mysql_to_postgres() {
let engine = DefaultTypeInferenceEngine;
assert_eq!(
engine.convert_default_value("CURRENT_TIMESTAMP", DialectKind::Mysql, DialectKind::Postgres),
"CURRENT_TIMESTAMP"
);
assert_eq!(
engine.convert_default_value("NOW()", DialectKind::Mysql, DialectKind::Postgres),
"CURRENT_TIMESTAMP"
);
assert!(engine
.convert_default_value("ON UPDATE CURRENT_TIMESTAMP", DialectKind::Mysql, DialectKind::Postgres)
.is_empty());
}
#[test]
fn unknown_type_passthrough() {
let engine = DefaultTypeInferenceEngine;
let result = engine.infer_type("GEOGRAPHY(POINT)", DialectKind::Postgres, DialectKind::Mysql);
assert_eq!(result.base_type, "GEOGRAPHY");
}
#[test]
fn parse_array_type() {
let t = ColumnType::parse("INTEGER[]");
assert!(t.is_array);
assert_eq!(t.base_type, "INTEGER");
}
}
@@ -0,0 +1,450 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 67
expression: all
---
[
{
"kind": "mysql",
"label": "mysql",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"reorder_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"index_type",
"index_comment",
"alter_primary_key",
"foreign_key",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"alter_owner",
"grant_revoke",
"if_not_exists",
"temporary_table",
"auto_increment"
],
"max_identifier_length": 64,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 4096,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": true,
"supports_uuid_type": false,
"supports_auto_increment": true,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": false
},
{
"kind": "postgres",
"label": "postgres",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"index_type",
"index_include",
"index_filter",
"index_comment",
"alter_primary_key",
"foreign_key",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"alter_owner",
"grant_revoke",
"if_not_exists",
"create_or_replace",
"temporary_table",
"transactional_ddl",
"identity_columns"
],
"max_identifier_length": 63,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1600,
"supports_json_type": true,
"supports_array_type": true,
"supports_enum_type": true,
"supports_uuid_type": true,
"supports_auto_increment": false,
"supports_identity_columns": true,
"supports_sequences": true,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": true
},
{
"kind": "sqlite",
"label": "sqlite",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"create_index",
"drop_index",
"rebuild_index",
"index_filter",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"if_not_exists",
"auto_increment"
],
"max_identifier_length": 255,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 2000,
"supports_json_type": false,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_auto_increment": true,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": false,
"supports_full_text_index": true,
"supports_spatial_index": false,
"supports_transactional_ddl": false
},
{
"kind": "duck_db",
"label": "duckdb",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"create_index",
"drop_index",
"rebuild_index",
"create_table",
"drop_table",
"truncate_table",
"if_not_exists",
"create_or_replace",
"temporary_table"
],
"max_identifier_length": 255,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1600,
"supports_json_type": true,
"supports_array_type": true,
"supports_enum_type": true,
"supports_uuid_type": true,
"supports_auto_increment": false,
"supports_identity_columns": false,
"supports_sequences": true,
"supports_partitioning": true,
"supports_full_text_index": false,
"supports_spatial_index": true,
"supports_transactional_ddl": false
},
{
"kind": "sql_server",
"label": "sqlserver",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"index_type",
"index_include",
"index_filter",
"index_comment",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"alter_owner",
"grant_revoke",
"temporary_table",
"transactional_ddl",
"identity_columns"
],
"max_identifier_length": 128,
"supports_schemas": true,
"supports_catalogs": true,
"max_columns_per_table": 1024,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": true,
"supports_auto_increment": false,
"supports_identity_columns": true,
"supports_sequences": true,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": true
},
{
"kind": "oracle",
"label": "oracle",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"index_type",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"alter_owner",
"grant_revoke",
"if_not_exists",
"temporary_table",
"identity_columns"
],
"max_identifier_length": 30,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1000,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_auto_increment": false,
"supports_identity_columns": true,
"supports_sequences": true,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": false
},
{
"kind": "h2",
"label": "h2",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"if_not_exists",
"temporary_table",
"identity_columns"
],
"max_identifier_length": 256,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1600,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": true,
"supports_auto_increment": false,
"supports_identity_columns": true,
"supports_sequences": true,
"supports_partitioning": false,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": false
},
{
"kind": "click_house",
"label": "clickhouse",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"reorder_column",
"comment",
"create_table",
"drop_table",
"truncate_table",
"if_not_exists",
"temporary_table"
],
"max_identifier_length": 256,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1000,
"supports_json_type": true,
"supports_array_type": true,
"supports_enum_type": true,
"supports_uuid_type": true,
"supports_auto_increment": false,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": false,
"supports_transactional_ddl": false
},
{
"kind": "manticore_search",
"label": "manticoresearch",
"capabilities": [
"add_column",
"drop_column",
"create_table",
"drop_table",
"truncate_table",
"auto_increment"
],
"max_identifier_length": 256,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 512,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_auto_increment": true,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": false,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": false
},
{
"kind": "informix",
"label": "informix",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"create_index",
"drop_index",
"rebuild_index",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"grant_revoke"
],
"max_identifier_length": 128,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 32767,
"supports_json_type": false,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_auto_increment": false,
"supports_identity_columns": false,
"supports_sequences": true,
"supports_partitioning": true,
"supports_full_text_index": false,
"supports_spatial_index": false,
"supports_transactional_ddl": false
},
{
"kind": "questdb",
"label": "questdb",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"create_table",
"drop_table",
"truncate_table",
"if_not_exists"
],
"max_identifier_length": 256,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 1024,
"supports_json_type": false,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_auto_increment": false,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": true,
"supports_full_text_index": false,
"supports_spatial_index": false,
"supports_transactional_ddl": false
},
{
"kind": "soql",
"label": "soql",
"capabilities": [],
"max_identifier_length": 40,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 500,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_auto_increment": false,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": false,
"supports_full_text_index": false,
"supports_spatial_index": false,
"supports_transactional_ddl": false
}
]
@@ -0,0 +1,30 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 37
expression: desc
---
{
"dialect": "duck_db",
"flags": 469991879,
"max_identifier_length": 255,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1600,
"max_indexes_per_table": 100,
"max_query_size_bytes": 268435456,
"supports_full_text_index": false,
"supports_spatial_index": true,
"supports_partitioning": true,
"supports_table_sampling": true,
"max_foreign_key_name_length": 255,
"supports_on_update_cascade": true,
"supports_on_delete_set_null": true,
"supports_deferrable_constraints": false,
"supports_array_type": true,
"supports_json_type": true,
"supports_enum_type": true,
"supports_uuid_type": true,
"supports_identity_columns": false,
"supports_auto_increment": false,
"supports_sequences": true
}
@@ -0,0 +1,30 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 7
expression: desc
---
{
"dialect": "mysql",
"flags": 1476391935,
"max_identifier_length": 64,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 4096,
"max_indexes_per_table": 64,
"max_query_size_bytes": 16777216,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_partitioning": true,
"supports_table_sampling": false,
"max_foreign_key_name_length": 64,
"supports_on_update_cascade": true,
"supports_on_delete_set_null": true,
"supports_deferrable_constraints": false,
"supports_array_type": false,
"supports_json_type": true,
"supports_enum_type": true,
"supports_uuid_type": false,
"supports_identity_columns": false,
"supports_auto_increment": true,
"supports_sequences": false
}
@@ -0,0 +1,53 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 43
expression: info
---
{
"kind": "mysql",
"label": "mysql",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"reorder_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"index_type",
"index_comment",
"alter_primary_key",
"foreign_key",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"alter_owner",
"grant_revoke",
"if_not_exists",
"temporary_table",
"auto_increment"
],
"max_identifier_length": 64,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 4096,
"supports_json_type": true,
"supports_array_type": false,
"supports_enum_type": true,
"supports_uuid_type": false,
"supports_auto_increment": true,
"supports_identity_columns": false,
"supports_sequences": false,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": false
}
@@ -0,0 +1,103 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 55
expression: "&matrix.rules"
---
[
{
"source_type": "TINYINT(1)",
"target_type": "BOOLEAN",
"precision_loss": false,
"requires_cast": true
},
{
"source_type": "TINYINT",
"target_type": "SMALLINT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "MEDIUMINT",
"target_type": "INTEGER",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "INT",
"target_type": "INTEGER",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "BIGINT",
"target_type": "BIGINT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "FLOAT",
"target_type": "REAL",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "DOUBLE",
"target_type": "DOUBLE PRECISION",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "TINYTEXT",
"target_type": "TEXT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "TEXT",
"target_type": "TEXT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "MEDIUMTEXT",
"target_type": "TEXT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "LONGTEXT",
"target_type": "TEXT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "BLOB",
"target_type": "BYTEA",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "TINYBLOB",
"target_type": "BYTEA",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "MEDIUMBLOB",
"target_type": "BYTEA",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "LONGBLOB",
"target_type": "BYTEA",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "DATETIME",
"target_type": "TIMESTAMP",
"precision_loss": false,
"requires_cast": false
}
]
@@ -0,0 +1,30 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 31
expression: desc
---
{
"dialect": "oracle",
"flags": 402621423,
"max_identifier_length": 30,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1000,
"max_indexes_per_table": 100,
"max_query_size_bytes": 65536,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_partitioning": true,
"supports_table_sampling": true,
"max_foreign_key_name_length": 30,
"supports_on_update_cascade": true,
"supports_on_delete_set_null": true,
"supports_deferrable_constraints": true,
"supports_array_type": false,
"supports_json_type": true,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_identity_columns": true,
"supports_auto_increment": false,
"supports_sequences": true
}
@@ -0,0 +1,30 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 13
expression: desc
---
{
"dialect": "postgres",
"flags": 3221225455,
"max_identifier_length": 63,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1600,
"max_indexes_per_table": 100,
"max_query_size_bytes": 268435456,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_partitioning": true,
"supports_table_sampling": true,
"max_foreign_key_name_length": 63,
"supports_on_update_cascade": true,
"supports_on_delete_set_null": true,
"supports_deferrable_constraints": true,
"supports_array_type": true,
"supports_json_type": true,
"supports_enum_type": true,
"supports_uuid_type": true,
"supports_identity_columns": true,
"supports_auto_increment": false,
"supports_sequences": true
}
@@ -0,0 +1,56 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 49
expression: info
---
{
"kind": "postgres",
"label": "postgres",
"capabilities": [
"add_column",
"drop_column",
"rename_column",
"alter_existing_column",
"comment",
"create_index",
"drop_index",
"rebuild_index",
"index_type",
"index_include",
"index_filter",
"index_comment",
"alter_primary_key",
"foreign_key",
"create_table",
"drop_table",
"truncate_table",
"create_trigger",
"drop_trigger",
"create_function",
"drop_function",
"create_sequence",
"drop_sequence",
"alter_owner",
"grant_revoke",
"if_not_exists",
"create_or_replace",
"temporary_table",
"transactional_ddl",
"identity_columns"
],
"max_identifier_length": 63,
"supports_schemas": true,
"supports_catalogs": false,
"max_columns_per_table": 1600,
"supports_json_type": true,
"supports_array_type": true,
"supports_enum_type": true,
"supports_uuid_type": true,
"supports_auto_increment": false,
"supports_identity_columns": true,
"supports_sequences": true,
"supports_partitioning": true,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_transactional_ddl": true
}
@@ -0,0 +1,79 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 61
expression: "&matrix.rules"
---
[
{
"source_type": "SMALLINT",
"target_type": "SMALLINT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "INTEGER",
"target_type": "INT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "BIGINT",
"target_type": "BIGINT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "REAL",
"target_type": "FLOAT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "DOUBLE PRECISION",
"target_type": "DOUBLE",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "TEXT",
"target_type": "LONGTEXT",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "BYTEA",
"target_type": "BLOB",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "BOOLEAN",
"target_type": "TINYINT(1)",
"precision_loss": false,
"requires_cast": true
},
{
"source_type": "TIMESTAMP",
"target_type": "DATETIME",
"precision_loss": false,
"requires_cast": false
},
{
"source_type": "TIMESTAMPTZ",
"target_type": "DATETIME",
"precision_loss": true,
"requires_cast": true
},
{
"source_type": "UUID",
"target_type": "CHAR(36)",
"precision_loss": false,
"requires_cast": true
},
{
"source_type": "JSONB",
"target_type": "JSON",
"precision_loss": false,
"requires_cast": false
}
]
@@ -0,0 +1,30 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 19
expression: desc
---
{
"dialect": "sqlite",
"flags": 1157597639,
"max_identifier_length": 255,
"supports_schemas": false,
"supports_catalogs": false,
"max_columns_per_table": 2000,
"max_indexes_per_table": 200,
"max_query_size_bytes": 0,
"supports_full_text_index": true,
"supports_spatial_index": false,
"supports_partitioning": false,
"supports_table_sampling": false,
"max_foreign_key_name_length": 255,
"supports_on_update_cascade": true,
"supports_on_delete_set_null": true,
"supports_deferrable_constraints": false,
"supports_array_type": false,
"supports_json_type": false,
"supports_enum_type": false,
"supports_uuid_type": false,
"supports_identity_columns": false,
"supports_auto_increment": true,
"supports_sequences": false
}
@@ -0,0 +1,30 @@
---
source: crates/dbx-sql-dialect/src/sql_dialect/descriptor_snapshots.rs
assertion_line: 25
expression: desc
---
{
"dialect": "sql_server",
"flags": 3019874287,
"max_identifier_length": 128,
"supports_schemas": true,
"supports_catalogs": true,
"max_columns_per_table": 1024,
"max_indexes_per_table": 999,
"max_query_size_bytes": 268435456,
"supports_full_text_index": true,
"supports_spatial_index": true,
"supports_partitioning": true,
"supports_table_sampling": true,
"max_foreign_key_name_length": 128,
"supports_on_update_cascade": true,
"supports_on_delete_set_null": true,
"supports_deferrable_constraints": false,
"supports_array_type": false,
"supports_json_type": true,
"supports_enum_type": false,
"supports_uuid_type": true,
"supports_identity_columns": true,
"supports_auto_increment": false,
"supports_sequences": true
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,616 @@
//! Column type rewrite pipeline for cross-database DDL generation.
//!
//! Order:
//! 1. Caller-applied user field mappings (highest priority — not handled here)
//! 2. Target [`DdlDialectProfile::type_map`] (data-driven)
//! 3. [`TypeMappingMatrix`] between source/target [`DialectKind`] when available
//! 4. Generic normalization (strip display width when profile disallows it)
use crate::models::connection::DatabaseType;
use crate::sql_dialect::ddl_profile::{profile_for, DdlDialectProfile};
use crate::sql_dialect::descriptor::{DialectKind, TypeMappingMatrix};
use crate::types::ColumnInfo;
/// Split `INT(11)` / `DECIMAL(10,2)` / `VARCHAR` into base + params.
pub fn split_type_base_params(source_type: &str) -> (String, Option<String>) {
let trimmed = source_type.trim();
let upper = trimmed.to_ascii_uppercase();
match upper.find('(') {
Some(i) => {
let base = upper[..i].trim().to_string();
let end = upper.rfind(')').unwrap_or(upper.len());
let params = upper[i + 1..end].trim().to_string();
(base, Some(params))
}
None => (upper, None),
}
}
fn first_length_param(params: Option<&str>) -> Option<u32> {
params.and_then(|p| p.split(',').next()).and_then(|p| p.trim().parse::<u32>().ok())
}
/// Strip Oracle-specific length-unit qualifiers (`CHAR` / `BYTE`) from a
/// parenthesized length slice such as `(50 CHAR)` → `(50)`.
///
/// Oracle allows character columns to declare their length unit explicitly:
/// `VARCHAR2(50 CHAR)` (characters) or `VARCHAR2(50 BYTE)` (bytes). The unit
/// qualifier is Oracle-only syntax — MySQL's `VARCHAR(n)` always means
/// characters and rejects `VARCHAR(50 CHAR)` with ERROR 1064. Stripping the
/// qualifier here keeps the generated target DDL valid while preserving the
/// numeric length.
///
/// Only single numeric parameters followed by `CHAR`/`BYTE` are touched;
/// precision/scale lists (`(10,2)`) and enum/set lists (`('a','b')`) are
/// returned unchanged.
pub fn normalize_len_params(parenthesized: &str) -> String {
let Some(open) = parenthesized.find('(') else {
return parenthesized.to_string();
};
let Some(close) = parenthesized.rfind(')') else {
return parenthesized.to_string();
};
if open >= close {
return parenthesized.to_string();
}
// Keep any prefix (base type or whitespace) so a full type string such as
// `varchar(50)` round-trips as-is instead of losing its base name.
format!("{}({})", &parenthesized[..open], strip_length_unit(&parenthesized[open + 1..close]))
}
/// Strip a trailing `CHAR`/`BYTE` unit from a single length parameter,
/// e.g. `50 CHAR` → `50`. Multi-part or non-numeric parameters are unchanged.
fn strip_length_unit(params: &str) -> String {
let trimmed = params.trim();
if trimmed.is_empty() || trimmed.contains(',') {
return trimmed.to_string();
}
let upper = trimmed.to_ascii_uppercase();
for unit in ["CHAR", "BYTE"] {
if let Some(head) = upper.strip_suffix(unit) {
let head = head.trim_end();
if !head.is_empty() && head.bytes().all(|b| b.is_ascii_digit()) {
return head.to_string();
}
}
}
trimmed.to_string()
}
fn apply_template(template: &str, params: Option<&str>, max_varchar_len: Option<u32>) -> String {
if !template.contains("{}") {
return template.to_string();
}
// Prefer full params for DECIMAL(p,s); single length for TEXT({})
if template.starts_with("DECIMAL") || template.starts_with("NUMERIC") {
return match params {
Some(p) if !p.is_empty() => template.replacen("{}", p, 1),
_ => template.replace("({})", "").replace("{}", ""),
};
}
let mut len = first_length_param(params).unwrap_or(255);
if let Some(max) = max_varchar_len {
len = len.clamp(1, max);
} else {
len = len.max(1);
}
template.replacen("{}", &len.to_string(), 1)
}
/// Strip MySQL-style display width: `INT(11)` → `INT`, keep `DECIMAL(10,2)` / `VARCHAR(100)`.
///
/// When keeping length/precision params, original casing is preserved (passthrough-ish).
/// When stripping display width, the base is uppercased as a normalized form.
pub fn strip_display_width_if_needed(source_type: &str, profile: &DdlDialectProfile) -> String {
if profile.supports_display_width {
return source_type.trim().to_string();
}
let trimmed = source_type.trim();
let (base, params) = split_type_base_params(trimmed);
let Some(params) = params else {
return trimmed.to_string();
};
// Keep types where parentheses are meaningful precision/length for most targets.
let keep_params = matches!(
base.as_str(),
"VARCHAR"
| "CHARACTER VARYING"
| "CHAR"
| "CHARACTER"
| "NVARCHAR"
| "NVARCHAR2"
| "VARCHAR2"
| "DECIMAL"
| "NUMERIC"
| "NUMBER"
| "FLOAT"
| "DOUBLE"
| "TIME"
| "TIMESTAMP"
| "DATETIME"
| "ENUM"
| "SET"
);
if keep_params {
// Reconstruct from the original slice so casing stays source-faithful.
let orig_base = trimmed.split('(').next().unwrap_or(trimmed).trim();
let open = trimmed.find('(').unwrap_or(orig_base.len());
let close = trimmed.rfind(')').unwrap_or(trimmed.len());
let orig_params = if open < close { trimmed[open + 1..close].trim() } else { params.as_str() };
format!("{orig_base}({orig_params})")
} else {
// INT(11), BIGINT(20), TINYINT(2), YEAR(4), … — normalized uppercase base.
base
}
}
/// Special-case: TINYINT(1) often means boolean on MySQL-like sources.
fn tinyint1_as_boolean_key(base: &str, params: Option<&str>) -> Option<&'static str> {
if base.eq_ignore_ascii_case("TINYINT") && params == Some("1") {
Some("BOOL")
} else {
None
}
}
fn sqlserver_decimal_type(params: Option<&str>) -> String {
let Some(params) = params else {
return "DECIMAL".to_string();
};
let values = params.split(',').map(str::trim).collect::<Vec<_>>();
let Ok(precision) = values.first().copied().unwrap_or_default().parse::<u32>() else {
return "DECIMAL".to_string();
};
let precision = precision.clamp(1, 38);
if values.len() == 1 {
return format!("DECIMAL({precision})");
}
let Ok(scale) = values[1].parse::<u32>() else {
return format!("DECIMAL({precision})");
};
format!("DECIMAL({precision},{})", scale.min(precision))
}
fn sqlserver_length_type(target_type: &str, params: Option<&str>, max_length: u32) -> String {
let Some(param) = params.map(str::trim).filter(|param| !param.is_empty()) else {
return if matches!(target_type, "VARCHAR" | "NVARCHAR" | "VARBINARY") {
format!("{target_type}(MAX)")
} else {
target_type.to_string()
};
};
if param.eq_ignore_ascii_case("MAX") {
return match target_type {
"NCHAR" => "NVARCHAR(MAX)".to_string(),
"CHAR" => "VARCHAR(MAX)".to_string(),
"BINARY" => "VARBINARY(MAX)".to_string(),
_ => format!("{target_type}(MAX)"),
};
}
match param.parse::<u32>() {
Ok(length) if length > max_length => match target_type {
"NCHAR" | "NVARCHAR" => "NVARCHAR(MAX)".to_string(),
"BINARY" | "VARBINARY" => "VARBINARY(MAX)".to_string(),
_ => "VARCHAR(MAX)".to_string(),
},
Ok(length) => format!("{target_type}({})", length.max(1)),
Err(_) => format!("{target_type}(MAX)"),
}
}
fn sqlserver_temporal_type(target_type: &str, params: Option<&str>) -> String {
match params.and_then(|param| param.trim().parse::<u32>().ok()) {
Some(precision) => format!("{target_type}({})", precision.min(7)),
None => target_type.to_string(),
}
}
/// Convert common source types to native SQL Server types before the generic
/// mapping matrix runs. This is source-dialect aware because names such as
/// `timestamp` mean a date/time in MySQL/PostgreSQL but `rowversion` in SQL Server.
fn rewrite_cross_dialect_to_sqlserver(source_type: &str, source: DialectKind) -> Option<String> {
if source == DialectKind::SqlServer {
return None;
}
let source_upper = source_type.trim().to_ascii_uppercase();
// MySQL places UNSIGNED/ZEROFILL after an optional parameter list, so it
// cannot be recovered from `raw_base` alone (`INT(11) UNSIGNED`). ZEROFILL
// also implies UNSIGNED in MySQL.
let mysql_unsigned = source_upper.split_whitespace().any(|part| matches!(part, "UNSIGNED" | "ZEROFILL"));
let (raw_base, params) = split_type_base_params(source_type);
let base = raw_base
.split_whitespace()
.filter(|part| !part.eq_ignore_ascii_case("UNSIGNED") && !part.eq_ignore_ascii_case("ZEROFILL"))
.collect::<Vec<_>>()
.join(" ");
let params = params.as_deref();
let mapped = match source {
DialectKind::Mysql => match base.as_str() {
"BOOL" | "BOOLEAN" => "BIT".to_string(),
"TINYINT" if params == Some("1") && !mysql_unsigned => "BIT".to_string(),
// Preserve the complete MySQL value range. SQL Server TINYINT is
// unsigned, while MySQL TINYINT is signed unless explicitly marked.
"TINYINT" if mysql_unsigned => "TINYINT".to_string(),
"TINYINT" => "SMALLINT".to_string(),
"SMALLINT" if mysql_unsigned => "INT".to_string(),
"SMALLINT" => "SMALLINT".to_string(),
"MEDIUMINT" => "INT".to_string(),
"INT" | "INTEGER" if mysql_unsigned => "BIGINT".to_string(),
"INT" | "INTEGER" => "INT".to_string(),
"BIGINT" if mysql_unsigned => "DECIMAL(20,0)".to_string(),
"BIGINT" => "BIGINT".to_string(),
"DECIMAL" | "NUMERIC" => sqlserver_decimal_type(params),
"FLOAT" | "DOUBLE" | "DOUBLE PRECISION" | "REAL" => "FLOAT".to_string(),
"CHAR" | "VARCHAR" => sqlserver_length_type(&base, params, 8000),
"NCHAR" | "NVARCHAR" => sqlserver_length_type(&base, params, 4000),
"TINYTEXT" | "TEXT" | "MEDIUMTEXT" | "LONGTEXT" | "JSON" | "ENUM" | "SET" => "NVARCHAR(MAX)".to_string(),
"BINARY" | "VARBINARY" => sqlserver_length_type(&base, params, 8000),
"TINYBLOB" | "BLOB" | "MEDIUMBLOB" | "LONGBLOB" => "VARBINARY(MAX)".to_string(),
"DATE" => "DATE".to_string(),
"TIME" => sqlserver_temporal_type("TIME", params),
"DATETIME" | "TIMESTAMP" => sqlserver_temporal_type("DATETIME2", params),
"YEAR" => "SMALLINT".to_string(),
"GEOMETRY" => "GEOMETRY".to_string(),
_ => return None,
},
DialectKind::Postgres => match base.as_str() {
"BOOL" | "BOOLEAN" => "BIT".to_string(),
"SMALLINT" | "INT2" | "SMALLSERIAL" => "SMALLINT".to_string(),
"INTEGER" | "INT" | "INT4" | "SERIAL" => "INT".to_string(),
"BIGINT" | "INT8" | "BIGSERIAL" => "BIGINT".to_string(),
"NUMERIC" | "DECIMAL" => sqlserver_decimal_type(params),
"REAL" | "FLOAT4" => "REAL".to_string(),
"DOUBLE PRECISION" | "FLOAT8" => "FLOAT".to_string(),
"CHAR" | "CHARACTER" | "VARCHAR" | "CHARACTER VARYING" => {
sqlserver_length_type("NVARCHAR", params.or(Some("MAX")), 4000)
}
"TEXT" | "JSON" | "JSONB" | "ARRAY" => "NVARCHAR(MAX)".to_string(),
"BIT VARYING" | "VARBIT" => "VARBINARY(MAX)".to_string(),
"BYTEA" => "VARBINARY(MAX)".to_string(),
"UUID" => "UNIQUEIDENTIFIER".to_string(),
"DATE" => "DATE".to_string(),
"TIME" if source_upper.contains("WITH TIME ZONE") && !source_upper.contains("WITHOUT TIME ZONE") => {
sqlserver_temporal_type("DATETIMEOFFSET", params)
}
"TIME" | "TIME WITHOUT TIME ZONE" => sqlserver_temporal_type("TIME", params),
"TIMETZ" | "TIME WITH TIME ZONE" => sqlserver_temporal_type("DATETIMEOFFSET", params),
"TIMESTAMP" if source_upper.contains("WITH TIME ZONE") && !source_upper.contains("WITHOUT TIME ZONE") => {
sqlserver_temporal_type("DATETIMEOFFSET", params)
}
"TIMESTAMP" | "TIMESTAMP WITHOUT TIME ZONE" => sqlserver_temporal_type("DATETIME2", params),
"TIMESTAMPTZ" | "TIMESTAMP WITH TIME ZONE" => sqlserver_temporal_type("DATETIMEOFFSET", params),
"XML" => "XML".to_string(),
"MONEY" => "MONEY".to_string(),
"INET" | "CIDR" | "MACADDR" | "MACADDR8" | "INTERVAL" => "NVARCHAR(100)".to_string(),
_ if base.ends_with("[]") => "NVARCHAR(MAX)".to_string(),
_ => return None,
},
DialectKind::Sqlite | DialectKind::DuckDb => match base.as_str() {
"BOOL" | "BOOLEAN" => "BIT".to_string(),
"TINYINT" => "TINYINT".to_string(),
"SMALLINT" => "SMALLINT".to_string(),
"INT" | "INTEGER" => "BIGINT".to_string(),
"REAL" | "DOUBLE" | "DOUBLE PRECISION" | "FLOAT" => "FLOAT".to_string(),
"DECIMAL" | "NUMERIC" => sqlserver_decimal_type(params),
"CHAR" | "VARCHAR" => sqlserver_length_type("NVARCHAR", params.or(Some("MAX")), 4000),
"TEXT" | "JSON" => "NVARCHAR(MAX)".to_string(),
"BLOB" | "BYTEA" => "VARBINARY(MAX)".to_string(),
"DATE" => "DATE".to_string(),
"TIME" => sqlserver_temporal_type("TIME", params),
"DATETIME" | "TIMESTAMP" => sqlserver_temporal_type("DATETIME2", params),
"UUID" => "UNIQUEIDENTIFIER".to_string(),
_ => return None,
},
_ => return None,
};
Some(mapped)
}
/// Rewrite a source column type for `target` database.
///
/// User field mappings must be applied by the caller *before* this function.
pub fn rewrite_column_type(source_type: &str, target: DatabaseType, source_dialect: Option<DialectKind>) -> String {
let profile = profile_for(target);
if target == DatabaseType::SqlServer {
let source = source_dialect.unwrap_or(DialectKind::SqlServer);
if source == DialectKind::SqlServer {
// Same-dialect metadata already contains a valid native type. The
// generic display-width normalizer would otherwise truncate valid
// SQL Server parameters such as VARBINARY(MAX), DATETIME2(7), and
// DATETIMEOFFSET(7).
return source_type.trim().to_string();
}
if let Some(mapped) = rewrite_cross_dialect_to_sqlserver(source_type, source) {
return mapped;
}
}
let (base, params) = split_type_base_params(source_type);
// Profile type_map (includes synthetic BOOL for TINYINT(1) when mapped)
let lookup_key = tinyint1_as_boolean_key(&base, params.as_deref()).unwrap_or(base.as_str());
if let Some(template) = profile.lookup_type(lookup_key) {
return apply_template(template, params.as_deref(), profile.max_varchar_len);
}
// Also try original base if we used BOOL alias miss
if lookup_key != base && profile.lookup_type(&base).is_some() {
if let Some(template) = profile.lookup_type(&base) {
return apply_template(template, params.as_deref(), profile.max_varchar_len);
}
}
// Dialect-kind matrix
if let Some(src) = source_dialect {
let target_kind = DialectKind::from_database_type(target);
if src != target_kind {
let matrix = TypeMappingMatrix::for_dialects(src, target_kind);
let (converted, _) = matrix.convert_type(source_type);
if converted != source_type {
return strip_display_width_if_needed(&converted, &profile);
}
// No matrix rule for this cross-dialect pair, and it's a
// *varying*-length string type reported without a length — e.g.
// Postgres/Kingbase's unbounded `character varying` via
// format_type(). A bare passthrough would be invalid DDL for a
// target that requires an explicit VARCHAR length (MySQL and its
// family: VARCHAR/CHARACTER VARYING with no length is a syntax
// error there). Default to 255, matching the same fallback
// apply_template() already uses when a mapped type's length is
// missing. CHAR/CHARACTER are deliberately excluded: MySQL
// accepts a bare CHAR as CHAR(1), so defaulting it to 255 would
// silently change valid output into a different, wrong length.
// VARCHAR2/NVARCHAR2 are also excluded: they aren't valid MySQL
// type names at all, with or without a length, so defaulting
// their length fixes nothing.
if params.is_none()
&& profile.requires_explicit_varchar_length
&& profile.max_varchar_len.is_some()
&& matches!(base.as_str(), "VARCHAR" | "CHARACTER VARYING" | "NVARCHAR")
{
return format!("{}(255)", source_type.trim());
}
}
}
strip_display_width_if_needed(source_type, &profile)
}
/// Whether the column metadata indicates auto-increment / identity / serial.
pub fn column_is_auto_increment(col: &ColumnInfo) -> bool {
col.extra.as_deref().is_some_and(|extra| {
let lower = extra.to_ascii_lowercase();
lower.contains("auto_increment")
|| lower.contains("autoincrement")
|| lower.contains("identity")
|| lower.contains("serial")
})
}
/// Whether a rewritten type looks integer-like (for PK auto-inc heuristics).
pub fn type_looks_integer(mapped_type: &str) -> bool {
let lower = mapped_type.to_ascii_lowercase();
lower.contains("int")
|| lower.contains("integer")
|| lower.contains("serial")
|| lower.eq_ignore_ascii_case("counter")
|| lower.eq_ignore_ascii_case("byte")
|| lower.starts_with("decimal")
|| lower.starts_with("number")
|| lower.starts_with("numeric")
}
/// Build column type + optional auto-inc contribution using profile only (no DatabaseType branches).
pub fn apply_auto_inc_to_column_def(
profile: &DdlDialectProfile,
quoted_name: &str,
_mapped_type: &str,
col: &ColumnInfo,
is_integer_like: bool,
) -> AutoIncColumnBuild {
let wants_auto = col.is_primary_key
&& (column_is_auto_increment(col)
|| (is_integer_like
&& !matches!(
profile.auto_inc,
crate::sql_dialect::ddl_profile::AutoIncSyntax::Suffix(" AUTOINCREMENT")
)));
match profile.auto_inc {
crate::sql_dialect::ddl_profile::AutoIncSyntax::ReplaceTypeWith(type_name)
if wants_auto && col.is_primary_key =>
{
let mut def = format!("{quoted_name} {type_name}");
if !col.is_nullable {
def.push_str(" NOT NULL");
}
AutoIncColumnBuild::Complete { def, skip_default: true }
}
// SQLite's AUTOINCREMENT changes semantics (sqlite_sequence, no rowid
// reuse), so it must only be emitted for sources that explicitly ask
// for auto-increment; other suffixes keep the implicit PK heuristic.
crate::sql_dialect::ddl_profile::AutoIncSyntax::Suffix(suffix)
if col.is_primary_key && is_integer_like && (wants_auto || suffix != " AUTOINCREMENT") =>
{
AutoIncColumnBuild::AppendSuffix { suffix, skip_default: false, postgres_sequence: false }
}
crate::sql_dialect::ddl_profile::AutoIncSyntax::PostgresSequence if col.is_primary_key && is_integer_like => {
AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: false, postgres_sequence: true }
}
_ => AutoIncColumnBuild::Normal { skip_default: false },
}
}
#[derive(Debug)]
pub enum AutoIncColumnBuild {
/// Full column definition already built (e.g. COUNTER).
Complete {
def: String,
skip_default: bool,
},
/// Continue normal type def; optionally append suffix; maybe register PG sequence.
AppendSuffix {
suffix: &'static str,
skip_default: bool,
postgres_sequence: bool,
},
Normal {
skip_default: bool,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn access_rewrites_mysql_types() {
let t = DatabaseType::Access;
let src = Some(DialectKind::Mysql);
assert_eq!(rewrite_column_type("int(11)", t, src), "INTEGER");
assert_eq!(rewrite_column_type("varchar(120)", t, src), "TEXT(120)");
assert_eq!(rewrite_column_type("tinyint(2)", t, src), "BYTE");
assert_eq!(rewrite_column_type("tinyint(1)", t, src), "YESNO");
assert_eq!(rewrite_column_type("datetime", t, src), "DATETIME");
assert_eq!(rewrite_column_type("longtext", t, src), "LONGTEXT");
}
#[test]
fn sqlserver_keeps_identity_path_not_counter() {
let profile = profile_for(DatabaseType::SqlServer);
assert!(matches!(
profile.auto_inc,
crate::sql_dialect::ddl_profile::AutoIncSyntax::Suffix(s) if s.contains("IDENTITY")
));
// MySQL display width is not part of the SQL Server type.
let t = rewrite_column_type("int(11)", DatabaseType::SqlServer, Some(DialectKind::Mysql));
assert_eq!(t, "INT");
}
#[test]
fn sqlserver_rewrites_common_mysql_types() {
let target = DatabaseType::SqlServer;
let source = Some(DialectKind::Mysql);
let cases = [
("tinyint(1)", "BIT"),
("tinyint", "SMALLINT"),
("tinyint unsigned", "TINYINT"),
("tinyint(1) unsigned", "TINYINT"),
("tinyint(1) zerofill", "TINYINT"),
("smallint unsigned", "INT"),
("mediumint unsigned", "INT"),
("int(11) unsigned", "BIGINT"),
("integer zerofill", "BIGINT"),
("bigint unsigned", "DECIMAL(20,0)"),
("double", "FLOAT"),
("decimal(65,30)", "DECIMAL(38,30)"),
("varchar(9000)", "VARCHAR(MAX)"),
("nvarchar(5000)", "NVARCHAR(MAX)"),
("longtext", "NVARCHAR(MAX)"),
("json", "NVARCHAR(MAX)"),
("longblob", "VARBINARY(MAX)"),
("datetime(9)", "DATETIME2(7)"),
];
for (input, expected) in cases {
assert_eq!(rewrite_column_type(input, target, source), expected, "{input}");
}
}
#[test]
fn sqlserver_rewrites_common_postgres_types_without_rewriting_its_own_timestamp() {
let target = DatabaseType::SqlServer;
let postgres = Some(DialectKind::Postgres);
let cases = [
("serial", "INT"),
("boolean", "BIT"),
("character varying(64)", "NVARCHAR(64)"),
("text", "NVARCHAR(MAX)"),
("bytea", "VARBINARY(MAX)"),
("uuid", "UNIQUEIDENTIFIER"),
("time(4) with time zone", "DATETIMEOFFSET(4)"),
("timestamp(6) without time zone", "DATETIME2(6)"),
("timestamp(6) with time zone", "DATETIMEOFFSET(6)"),
("jsonb", "NVARCHAR(MAX)"),
];
for (input, expected) in cases {
assert_eq!(rewrite_column_type(input, target, postgres), expected, "{input}");
}
assert_eq!(rewrite_column_type("timestamp", target, Some(DialectKind::SqlServer)), "timestamp");
assert_eq!(rewrite_column_type("varbinary(max)", target, None), "varbinary(max)");
assert_eq!(rewrite_column_type("datetime2(7)", target, Some(DialectKind::SqlServer)), "datetime2(7)");
}
#[test]
fn mysql_keeps_display_width() {
let t = rewrite_column_type("int(11)", DatabaseType::Mysql, None);
assert_eq!(t, "int(11)");
}
#[test]
fn mysql_defaults_missing_length_on_cross_dialect_varchar() {
// Postgres/Kingbase report an unbounded varchar column as a bare
// `character varying` (no length) via format_type(). MySQL's
// VARCHAR/CHARACTER VARYING requires an explicit length, so passing
// that through verbatim is a syntax error (issue #8011).
let source = Some(DialectKind::Postgres);
assert_eq!(rewrite_column_type("character varying", DatabaseType::Mysql, source), "character varying(255)");
assert_eq!(rewrite_column_type("varchar", DatabaseType::Mysql, source), "varchar(255)");
}
#[test]
fn mysql_leaves_bare_char_untouched() {
// Unlike VARCHAR, a bare CHAR/CHARACTER is already valid MySQL
// syntax (implicitly CHAR(1)); defaulting it to 255 would silently
// change correct output into a different, wrong length.
let source = Some(DialectKind::Postgres);
assert_eq!(rewrite_column_type("character", DatabaseType::Mysql, source), "character");
assert_eq!(rewrite_column_type("char", DatabaseType::Mysql, source), "char");
}
#[test]
fn mysql_keeps_explicit_length_on_cross_dialect_varchar() {
let source = Some(DialectKind::Postgres);
assert_eq!(
rewrite_column_type("character varying(120)", DatabaseType::Mysql, source),
"character varying(120)"
);
}
#[test]
fn mysql_same_dialect_varchar_is_untouched_regardless_of_length() {
// Same-dialect metadata (or unknown source dialect) must never be
// rewritten — only a genuine cross-dialect gap should trigger the
// missing-length default.
assert_eq!(rewrite_column_type("varchar(255)", DatabaseType::Mysql, Some(DialectKind::Mysql)), "varchar(255)");
assert_eq!(rewrite_column_type("varchar(255)", DatabaseType::Mysql, None), "varchar(255)");
}
#[test]
fn normalize_len_params_strips_oracle_char_unit() {
assert_eq!(normalize_len_params("(6 CHAR)"), "(6)");
assert_eq!(normalize_len_params("(50 char)"), "(50)");
assert_eq!(normalize_len_params("(50 CHAR)"), "(50)");
assert_eq!(normalize_len_params("(20 BYTE)"), "(20)");
assert_eq!(normalize_len_params("(20 byte)"), "(20)");
}
#[test]
fn normalize_len_params_leaves_non_unit_params_untouched() {
// Plain length without a unit qualifier.
assert_eq!(normalize_len_params("(50)"), "(50)");
// Precision/scale must never be disturbed.
assert_eq!(normalize_len_params("(10,2)"), "(10,2)");
// Enum/set lists must never be disturbed.
assert_eq!(normalize_len_params("('a','b')"), "('a','b')");
// Non-numeric head (defensive) is left alone.
assert_eq!(normalize_len_params("(MAX CHAR)"), "(MAX CHAR)");
}
#[test]
fn normalize_len_params_handles_malformed_input() {
assert_eq!(normalize_len_params("varchar(50)"), "varchar(50)");
assert_eq!(normalize_len_params("no-parens"), "no-parens");
assert_eq!(normalize_len_params("(unclosed"), "(unclosed");
assert_eq!(normalize_len_params(")"), ")");
}
}
@@ -0,0 +1,78 @@
use serde::{Deserialize, Serialize};
use crate::models::connection::DatabaseType;
pub const DBX_ROWID_COLUMN: &str = "__DBX_ROWID";
pub const DBX_NEO4J_ELEMENT_ID_COLUMN: &str = "__DBX_ELEMENT_ID";
pub const DBX_TDENGINE_TBNAME_COLUMN: &str = "tbname";
#[derive(Debug, Clone, Copy)]
pub struct TableSelectSqlOptions<'a> {
pub database_type: Option<DatabaseType>,
pub schema: Option<&'a str>,
pub table_name: &'a str,
pub columns: &'a [String],
pub order_columns: &'a [String],
pub limit: usize,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TableDataSelectSqlOptions {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub database_type: Option<DatabaseType>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub driver_profile: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub identifier_quote: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
pub table_name: String,
/// Doris / StarRocks multi-catalog: when set to a non-`internal` catalog,
/// the FROM clause is prefixed with the catalog
/// (`<catalog>.<database>.<table>`).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub catalog: Option<String>,
/// Doris / StarRocks multi-catalog: the database under the external
/// catalog, used as the middle segment of the 3-part qualified name when
/// `schema` is absent (Doris/StarRocks have no separate schema concept).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub database: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub table_type: Option<String>,
#[serde(default)]
pub primary_keys: Vec<String>,
#[serde(default)]
pub columns: Vec<String>,
/// Database type names parallel to `columns`. Table-data callers can use
/// these to request bounded server-side projections for variable-length
/// values without changing arbitrary query execution.
#[serde(default)]
pub column_types: Vec<String>,
/// Maximum characters (text-like values) or bytes (binary values) kept in
/// a table-data preview. Omitted for exports and ordinary SQL queries.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub large_value_preview_size: Option<usize>,
#[serde(default)]
pub fallback_order_columns: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub order_by: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub offset: Option<usize>,
/// The JDBC driver advances the ResultSet before collecting the page.
/// This keeps database-specific identifier rules while avoiding SQL
/// pagination syntax that the selected driver does not support.
#[serde(default)]
pub use_driver_row_offset: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub where_input: Option<String>,
/// Time-series quick-open ergonomics: inject a rolling `time` window
/// when no WHERE was supplied. Opt-in so sampling and export callers
/// keep the historical unbounded scan semantics.
#[serde(default)]
pub inject_default_time_series_where: bool,
#[serde(default)]
pub include_row_id: bool,
}