feat(dameng): support identity columns in table editor
This commit is contained in:
14 files changed
+507
-22
No files matched your search
@@ -76,7 +76,7 @@ pub fn build_single_column_alter_sql(options: SingleColumnAlterSqlOptions) -> Ta
|
||||
StructureDialect::Mysql => statements.extend(build_mysql_existing_column_sql(&table, &options.column, "")),
|
||||
StructureDialect::Doris => statements.extend(build_doris_existing_column_sql(&table, &options.column, "")),
|
||||
StructureDialect::Postgres => statements.extend(build_postgres_existing_column_sql(&table, &options.column)),
|
||||
StructureDialect::Oracle => {
|
||||
StructureDialect::Oracle | StructureDialect::Dameng => {
|
||||
statements.extend(build_oracle_like_existing_column_sql(dialect, &table, &options.column))
|
||||
}
|
||||
StructureDialect::H2 => statements.extend(build_h2_existing_column_sql(&table, &options.column)),
|
||||
|
||||
@@ -77,10 +77,51 @@ pub(super) fn column_extra_clause(dialect: StructureDialect, column: &EditableSt
|
||||
None
|
||||
}
|
||||
}
|
||||
StructureDialect::Dameng => {
|
||||
if extra.auto_increment.unwrap_or(false) || extra.identity.is_some() {
|
||||
let seed = extra.identity.as_ref().and_then(|i| i.seed).unwrap_or(1);
|
||||
let increment = extra.identity.as_ref().and_then(|i| i.increment).unwrap_or(1);
|
||||
Some(format!("IDENTITY({seed}, {increment})"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn has_dameng_identity(column: &EditableStructureColumn) -> bool {
|
||||
column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false) || extra.identity.is_some())
|
||||
}
|
||||
|
||||
pub(super) fn is_dameng_identity_compatible_type(data_type: &str) -> bool {
|
||||
let trimmed = data_type.trim();
|
||||
let (base_type, params) = match trimmed.find('(') {
|
||||
Some(open_index) => {
|
||||
let close_index = trimmed.rfind(')').unwrap_or(trimmed.len());
|
||||
(&trimmed[..open_index], trimmed.get(open_index + 1..close_index).unwrap_or(""))
|
||||
}
|
||||
None => (trimmed, ""),
|
||||
};
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
if matches!(normalized.as_str(), "tinyint" | "smallint" | "int" | "integer" | "bigint") {
|
||||
return true;
|
||||
}
|
||||
if !matches!(normalized.as_str(), "number" | "numeric" | "decimal" | "dec") {
|
||||
return false;
|
||||
}
|
||||
let normalized_params = params.split_whitespace().collect::<String>();
|
||||
if normalized_params.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let parts = normalized_params.split(',').collect::<Vec<_>>();
|
||||
match parts.as_slice() {
|
||||
[precision] => precision.parse::<u32>().is_ok(),
|
||||
[precision, scale] => precision.parse::<u32>().is_ok() && *scale == "0",
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn column_data_type(dialect: StructureDialect, column: &EditableStructureColumn) -> String {
|
||||
if dialect == StructureDialect::ClickHouse {
|
||||
return clickhouse_column_type(column);
|
||||
@@ -282,7 +323,7 @@ pub(super) fn is_temporal_precision_type(dialect: StructureDialect, base_type: &
|
||||
| "timestamp with time zone"
|
||||
),
|
||||
StructureDialect::SqlServer => matches!(normalized.as_str(), "time" | "datetime2" | "datetimeoffset"),
|
||||
StructureDialect::Oracle => {
|
||||
StructureDialect::Oracle | StructureDialect::Dameng => {
|
||||
matches!(normalized.as_str(), "timestamp" | "timestamp with time zone" | "timestamp with local time zone")
|
||||
}
|
||||
_ => false,
|
||||
@@ -293,7 +334,7 @@ pub(super) fn is_valid_temporal_precision(params: &str, dialect: StructureDialec
|
||||
let Ok(value) = params.parse::<u8>() else {
|
||||
return false;
|
||||
};
|
||||
let max = if dialect == StructureDialect::Oracle { 9 } else { 6 };
|
||||
let max = if matches!(dialect, StructureDialect::Oracle | StructureDialect::Dameng) { 9 } else { 6 };
|
||||
value <= max && params == value.to_string()
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@ use super::column_alter::{
|
||||
build_postgres_existing_column_sql, build_questdb_existing_column_sql, build_sqlite_existing_column_sql,
|
||||
build_sqlserver_existing_column_sql, has_column_extra_change, has_existing_column_attribute_change,
|
||||
};
|
||||
use super::column_format::{column_definition, is_mysql_character_data_type};
|
||||
use super::column_format::{
|
||||
column_definition, has_dameng_identity, is_dameng_identity_compatible_type, is_mysql_character_data_type,
|
||||
};
|
||||
use super::comments::build_sqlserver_column_comment_sql;
|
||||
use super::dialect::{capabilities_for, database_label, StructureDialect};
|
||||
use super::dialect::{capabilities_for, database_label, is_oracle_like, StructureDialect};
|
||||
use super::types::{EditableStructureColumn, TableStructureSqlOptions};
|
||||
use super::util::{
|
||||
clean, is_protected_manticore_id_column, normalize_default, original_comment, original_default, qualified_table,
|
||||
@@ -20,7 +22,7 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
|
||||
let table = qualified_table(dialect, options.schema.as_deref(), &options.table_name);
|
||||
let database_label = database_label(options.database_type);
|
||||
let active_columns: Vec<_> = options.columns.iter().filter(|column| !column.marked_for_drop).collect();
|
||||
if dialect == StructureDialect::Oracle
|
||||
if is_oracle_like(dialect)
|
||||
&& active_columns.is_empty()
|
||||
&& options.columns.iter().any(|column| column.marked_for_drop)
|
||||
{
|
||||
@@ -93,6 +95,16 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
|
||||
));
|
||||
continue;
|
||||
}
|
||||
if dialect == StructureDialect::Dameng
|
||||
&& has_dameng_identity(column)
|
||||
&& !is_dameng_identity_compatible_type(&column.data_type)
|
||||
{
|
||||
warnings.push(format!(
|
||||
"Dameng identity column \"{}\" must use tinyint, smallint, int, integer, bigint, number, numeric, or decimal/dec with scale 0.",
|
||||
column.name
|
||||
));
|
||||
continue;
|
||||
}
|
||||
statements.extend(build_add_column_sql(
|
||||
dialect,
|
||||
options.database_type,
|
||||
@@ -147,7 +159,7 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
|
||||
)),
|
||||
StructureDialect::Doris => statements.extend(build_doris_existing_column_sql(&table, column, "")),
|
||||
StructureDialect::Postgres => statements.extend(build_postgres_existing_column_sql(&table, column)),
|
||||
StructureDialect::Oracle => {
|
||||
StructureDialect::Oracle | StructureDialect::Dameng => {
|
||||
statements.extend(build_oracle_like_existing_column_sql(dialect, &table, column))
|
||||
}
|
||||
StructureDialect::H2 => statements.extend(build_h2_existing_column_sql(&table, column)),
|
||||
@@ -275,7 +287,7 @@ pub(super) fn build_add_column_sql(
|
||||
table_name: &str,
|
||||
) -> Vec<String> {
|
||||
let definition = column_definition(dialect, column);
|
||||
let mut statements = if dialect == StructureDialect::Oracle || dialect == StructureDialect::Informix {
|
||||
let mut statements = if is_oracle_like(dialect) || dialect == StructureDialect::Informix {
|
||||
vec![format!("ALTER TABLE {table} ADD ({definition});")]
|
||||
} else {
|
||||
let add_keyword = if dialect == StructureDialect::SqlServer
|
||||
@@ -287,7 +299,9 @@ pub(super) fn build_add_column_sql(
|
||||
};
|
||||
vec![format!("ALTER TABLE {table} {add_keyword} {definition}{position_clause};")]
|
||||
};
|
||||
if matches!(dialect, StructureDialect::Postgres | StructureDialect::Oracle) && !clean(&column.comment).is_empty() {
|
||||
if matches!(dialect, StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng)
|
||||
&& !clean(&column.comment).is_empty()
|
||||
{
|
||||
statements.push(format!(
|
||||
"COMMENT ON COLUMN {table}.{} IS {};",
|
||||
quote_ident(dialect, &column.name),
|
||||
|
||||
@@ -19,7 +19,7 @@ pub(super) fn build_table_comment_sql(options: &TableStructureSqlOptions, warnin
|
||||
StructureDialect::Mysql => {
|
||||
vec![format!("ALTER TABLE {table} COMMENT = {quoted};")]
|
||||
}
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::H2 => {
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::H2 => {
|
||||
vec![format!("COMMENT ON TABLE {table} IS {quoted};")]
|
||||
}
|
||||
StructureDialect::ClickHouse => {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use super::column_format::{column_data_type, column_extra_clause};
|
||||
use super::column_format::{
|
||||
column_data_type, column_extra_clause, has_dameng_identity, is_dameng_identity_compatible_type,
|
||||
};
|
||||
use super::comments::{build_sqlserver_column_comment_sql, build_sqlserver_table_comment_sql};
|
||||
use super::dialect::{capabilities_for, database_label, StructureDialect};
|
||||
use super::foreign_keys::build_foreign_key_sql;
|
||||
@@ -6,7 +8,7 @@ use super::indexes::build_create_index_statements;
|
||||
use super::triggers::build_trigger_sql;
|
||||
use super::types::{TableStructureSqlOptions, TableStructureSqlResult};
|
||||
use super::util::{clean, format_default_for_sql, normalize_default, qualified_table, quote_ident, quote_string};
|
||||
use super::validation::validate_columns;
|
||||
use super::validation::{validate_columns, validate_dameng_identity};
|
||||
|
||||
pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructureSqlResult {
|
||||
let mut warnings = Vec::new();
|
||||
@@ -18,6 +20,7 @@ pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructu
|
||||
warnings.push("At least one column is required.".to_string());
|
||||
}
|
||||
validate_columns(&active_columns, &mut warnings);
|
||||
validate_dameng_identity(&options, &active_columns, &mut warnings);
|
||||
if !warnings.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
}
|
||||
@@ -25,6 +28,19 @@ pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructu
|
||||
let capabilities = capabilities_for(options.database_type);
|
||||
let dialect = capabilities.dialect;
|
||||
let table = qualified_table(dialect, options.schema.as_deref(), &options.table_name);
|
||||
if dialect == StructureDialect::Dameng {
|
||||
for column in &active_columns {
|
||||
if has_dameng_identity(column) && !is_dameng_identity_compatible_type(&column.data_type) {
|
||||
warnings.push(format!(
|
||||
"Dameng identity column \"{}\" must use tinyint, smallint, int, integer, bigint, number, numeric, or decimal/dec with scale 0.",
|
||||
column.name
|
||||
));
|
||||
}
|
||||
}
|
||||
if !warnings.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
}
|
||||
}
|
||||
let mut statements = Vec::new();
|
||||
let mut column_definitions = Vec::new();
|
||||
|
||||
@@ -73,7 +89,10 @@ pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructu
|
||||
if let Some(last) = statements.last_mut() {
|
||||
*last = last.replace(");", &format!(") COMMENT = {};", quote_string(&table_comment)));
|
||||
}
|
||||
} else if matches!(dialect, StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::H2) {
|
||||
} else if matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::H2
|
||||
) {
|
||||
statements.push(format!("COMMENT ON TABLE {table} IS {};", quote_string(&table_comment)));
|
||||
} else if dialect == StructureDialect::ClickHouse {
|
||||
statements.push(format!("ALTER TABLE {table} MODIFY COMMENT {};", quote_string(&table_comment)));
|
||||
@@ -89,7 +108,10 @@ pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructu
|
||||
}
|
||||
|
||||
if capabilities.comment
|
||||
&& matches!(dialect, StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::H2)
|
||||
&& matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::H2
|
||||
)
|
||||
{
|
||||
for column in &active_columns {
|
||||
if !clean(&column.comment).is_empty() {
|
||||
|
||||
@@ -10,6 +10,7 @@ pub(super) enum StructureDialect {
|
||||
DuckDb,
|
||||
SqlServer,
|
||||
Oracle,
|
||||
Dameng,
|
||||
H2,
|
||||
ClickHouse,
|
||||
ManticoreSearch,
|
||||
@@ -196,9 +197,21 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
||||
index_comment: true,
|
||||
..base
|
||||
},
|
||||
Some(DatabaseType::Dameng) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Dameng,
|
||||
add_column: true,
|
||||
drop_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
create_index: true,
|
||||
drop_index: true,
|
||||
rebuild_index: true,
|
||||
index_type: true,
|
||||
..base
|
||||
},
|
||||
Some(
|
||||
DatabaseType::Oracle
|
||||
| DatabaseType::Dameng
|
||||
| DatabaseType::OceanbaseOracle
|
||||
| DatabaseType::Iris
|
||||
| DatabaseType::Yashandb
|
||||
@@ -260,7 +273,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
||||
}
|
||||
|
||||
pub(super) fn is_oracle_like(dialect: StructureDialect) -> bool {
|
||||
dialect == StructureDialect::Oracle
|
||||
matches!(dialect, StructureDialect::Oracle | StructureDialect::Dameng)
|
||||
}
|
||||
|
||||
pub(super) fn database_label(database_type: Option<DatabaseType>) -> String {
|
||||
@@ -284,6 +297,7 @@ pub(super) fn dialect_label(dialect: StructureDialect) -> String {
|
||||
StructureDialect::DuckDb => "duckdb",
|
||||
StructureDialect::SqlServer => "sqlserver",
|
||||
StructureDialect::Oracle => "oracle",
|
||||
StructureDialect::Dameng => "dameng",
|
||||
StructureDialect::H2 => "h2",
|
||||
StructureDialect::ClickHouse => "clickhouse",
|
||||
StructureDialect::ManticoreSearch => "manticoresearch",
|
||||
@@ -304,6 +318,7 @@ pub(super) fn database_type_for_dialect(dialect: StructureDialect) -> Option<Dat
|
||||
StructureDialect::DuckDb => Some(DatabaseType::DuckDb),
|
||||
StructureDialect::SqlServer => Some(DatabaseType::SqlServer),
|
||||
StructureDialect::Oracle => Some(DatabaseType::Oracle),
|
||||
StructureDialect::Dameng => Some(DatabaseType::Dameng),
|
||||
StructureDialect::H2 => Some(DatabaseType::H2),
|
||||
StructureDialect::ClickHouse => Some(DatabaseType::ClickHouse),
|
||||
StructureDialect::ManticoreSearch => Some(DatabaseType::ManticoreSearch),
|
||||
|
||||
@@ -129,8 +129,10 @@ pub(super) fn build_drop_index_sql(
|
||||
if matches!(dialect, StructureDialect::Mysql | StructureDialect::SqlServer) {
|
||||
return format!("DROP INDEX {} ON {table};", quote_ident(dialect, index_name));
|
||||
}
|
||||
if matches!(dialect, StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Informix)
|
||||
&& schema.is_some_and(|schema| !schema.trim().is_empty())
|
||||
if matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Informix
|
||||
) && schema.is_some_and(|schema| !schema.trim().is_empty())
|
||||
{
|
||||
return format!("DROP INDEX {}.{};", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, index_name));
|
||||
}
|
||||
@@ -168,7 +170,9 @@ pub(super) fn build_create_index_statements(
|
||||
type_prefix = prefix;
|
||||
using_clause = using;
|
||||
}
|
||||
StructureDialect::Oracle if idx_type == "BITMAP" => type_prefix = "BITMAP ".to_string(),
|
||||
StructureDialect::Oracle | StructureDialect::Dameng if idx_type == "BITMAP" => {
|
||||
type_prefix = "BITMAP ".to_string()
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1567,6 +1567,62 @@ fn sqlserver_add_column_with_identity() {
|
||||
assert_eq!(result.statements, vec!["ALTER TABLE [dbo].[orders] ADD [id] int NOT NULL IDENTITY(10, 2);"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_add_column_with_identity() {
|
||||
let mut id = column("ID");
|
||||
id.data_type = "INT".to_string();
|
||||
id.is_nullable = false;
|
||||
id.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(10), increment: Some(2) }),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_table_structure_change_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "TEST".to_string(),
|
||||
columns: vec![id],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert_eq!(result.warnings, Vec::<String>::new());
|
||||
assert_eq!(result.statements, vec!["ALTER TABLE \"SYSDBA\".\"TEST\" ADD (\"ID\" INT IDENTITY(10, 2));"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_rejects_identity_on_incompatible_type() {
|
||||
let mut column = column("CODE");
|
||||
column.data_type = "VARCHAR(255)".to_string();
|
||||
column.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(1) }),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_table_structure_change_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "TEST".to_string(),
|
||||
columns: vec![column],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert_eq!(result.statements, Vec::<String>::new());
|
||||
assert_eq!(
|
||||
result.warnings,
|
||||
vec!["Dameng identity column \"CODE\" must use tinyint, smallint, int, integer, bigint, number, numeric, or decimal/dec with scale 0."]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_rejects_identity_on_incompatible_type() {
|
||||
let mut column = column("test");
|
||||
@@ -1665,6 +1721,88 @@ fn sqlserver_unchanged_identity_extra_does_not_mark_existing_column_changed() {
|
||||
assert_eq!(result.statements, Vec::<String>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_unchanged_identity_extra_does_not_mark_existing_column_changed() {
|
||||
let mut id = column("ID");
|
||||
id.data_type = "INT".to_string();
|
||||
id.is_nullable = false;
|
||||
id.is_primary_key = true;
|
||||
id.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(1) }),
|
||||
..Default::default()
|
||||
});
|
||||
id.original = Some(ColumnInfo {
|
||||
name: "ID".to_string(),
|
||||
data_type: "INT".to_string(),
|
||||
is_nullable: false,
|
||||
column_default: None,
|
||||
is_primary_key: true,
|
||||
extra: Some("identity".to_string()),
|
||||
comment: None,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_table_structure_change_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "TEST".to_string(),
|
||||
columns: vec![id],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert_eq!(result.warnings, Vec::<String>::new());
|
||||
assert_eq!(result.statements, Vec::<String>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_rejects_adding_second_identity_column() {
|
||||
let mut existing = column("ID");
|
||||
existing.data_type = "INT".to_string();
|
||||
existing.is_nullable = false;
|
||||
existing.is_primary_key = true;
|
||||
existing.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(1) }),
|
||||
..Default::default()
|
||||
});
|
||||
existing.original = Some(ColumnInfo {
|
||||
name: "ID".to_string(),
|
||||
data_type: "INT".to_string(),
|
||||
is_nullable: false,
|
||||
column_default: None,
|
||||
is_primary_key: true,
|
||||
extra: Some("identity".to_string()),
|
||||
comment: None,
|
||||
..Default::default()
|
||||
});
|
||||
let mut added = column("SEQ");
|
||||
added.data_type = "BIGINT".to_string();
|
||||
added.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(1) }),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_table_structure_change_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "TEST".to_string(),
|
||||
columns: vec![existing, added],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert_eq!(result.warnings, vec!["Dameng tables can have only one identity column."]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_existing_column_identity_change_warns_without_unchanged_foreign_key_warning() {
|
||||
let mut id = column("id");
|
||||
@@ -2102,6 +2240,94 @@ fn postgres_create_table_with_identity() {
|
||||
assert!(result.statements[0].contains("GENERATED BY DEFAULT AS IDENTITY"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_create_table_with_identity() {
|
||||
let mut col = column("ID");
|
||||
col.data_type = "INT".to_string();
|
||||
col.is_nullable = false;
|
||||
col.is_primary_key = true;
|
||||
col.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(100), increment: Some(5) }),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_create_table_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "USERS".to_string(),
|
||||
columns: vec![col],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert_eq!(result.warnings, Vec::<String>::new());
|
||||
assert!(result.statements[0].contains("\"ID\" INT IDENTITY(100, 5)"), "ddl: {}", result.statements[0]);
|
||||
assert!(result.statements[0].contains("PRIMARY KEY (\"ID\")"), "ddl: {}", result.statements[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_rejects_multiple_identity_columns() {
|
||||
let mut first = column("ID");
|
||||
first.data_type = "INT".to_string();
|
||||
first.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(1) }),
|
||||
..Default::default()
|
||||
});
|
||||
let mut second = column("SEQ");
|
||||
second.data_type = "BIGINT".to_string();
|
||||
second.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(1) }),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_create_table_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "USERS".to_string(),
|
||||
columns: vec![first, second],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert!(result.statements.is_empty());
|
||||
assert_eq!(result.warnings, vec!["Dameng tables can have only one identity column."]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dameng_rejects_zero_identity_increment() {
|
||||
let mut col = column("ID");
|
||||
col.data_type = "INT".to_string();
|
||||
col.extra = Some(ColumnExtra {
|
||||
auto_increment: Some(true),
|
||||
identity: Some(ColumnIdentity { generation: None, seed: Some(1), increment: Some(0) }),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let result = build_create_table_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
schema: Some("SYSDBA".to_string()),
|
||||
table_name: "USERS".to_string(),
|
||||
columns: vec![col],
|
||||
indexes: Vec::new(),
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert!(result.statements.is_empty());
|
||||
assert_eq!(result.warnings, vec!["Dameng identity column \"ID\" increment cannot be 0."]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_create_table_with_identity() {
|
||||
let mut col = column("id");
|
||||
|
||||
@@ -6,6 +6,7 @@ pub(super) fn qualified_table(dialect: StructureDialect, schema: Option<&str>, t
|
||||
dialect,
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::SqlServer
|
||||
| StructureDialect::H2
|
||||
| StructureDialect::Informix
|
||||
@@ -142,7 +143,7 @@ pub(super) fn is_temporal_type_for_default(dialect: StructureDialect, base_type:
|
||||
normalized.as_str(),
|
||||
"date" | "time" | "datetime" | "datetime2" | "smalldatetime" | "datetimeoffset"
|
||||
),
|
||||
StructureDialect::Oracle => matches!(
|
||||
StructureDialect::Oracle | StructureDialect::Dameng => matches!(
|
||||
normalized.as_str(),
|
||||
"date"
|
||||
| "timestamp"
|
||||
@@ -228,7 +229,7 @@ pub(super) fn is_string_type_for_default(dialect: StructureDialect, base_type: &
|
||||
normalized.as_str(),
|
||||
"char" | "varchar" | "nchar" | "nvarchar" | "text" | "ntext" | "xml" | "uniqueidentifier" | "sysname"
|
||||
),
|
||||
StructureDialect::Oracle => matches!(
|
||||
StructureDialect::Oracle | StructureDialect::Dameng => matches!(
|
||||
normalized.as_str(),
|
||||
"char" | "nchar" | "varchar2" | "nvarchar2" | "clob" | "nclob" | "long" | "raw" | "long raw" | "bfile"
|
||||
),
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use super::column_format::has_dameng_identity;
|
||||
use super::dialect::{capabilities_for, StructureDialect};
|
||||
use super::indexes::has_existing_index_change;
|
||||
use super::types::{EditableStructureColumn, TableStructureSqlOptions};
|
||||
use super::util::clean;
|
||||
@@ -6,6 +8,7 @@ pub(super) fn validate_draft(options: &TableStructureSqlOptions) -> Vec<String>
|
||||
let mut warnings = Vec::new();
|
||||
let active_columns: Vec<_> = options.columns.iter().filter(|column| !column.marked_for_drop).collect();
|
||||
validate_columns(&active_columns, &mut warnings);
|
||||
validate_dameng_identity(options, &active_columns, &mut warnings);
|
||||
for index in options
|
||||
.indexes
|
||||
.iter()
|
||||
@@ -24,6 +27,28 @@ pub(super) fn validate_draft(options: &TableStructureSqlOptions) -> Vec<String>
|
||||
warnings
|
||||
}
|
||||
|
||||
pub(super) fn validate_dameng_identity(
|
||||
options: &TableStructureSqlOptions,
|
||||
columns: &[&EditableStructureColumn],
|
||||
warnings: &mut Vec<String>,
|
||||
) {
|
||||
if capabilities_for(options.database_type).dialect != StructureDialect::Dameng {
|
||||
return;
|
||||
}
|
||||
|
||||
let identity_columns: Vec<_> = columns.iter().filter(|column| has_dameng_identity(column)).collect();
|
||||
if identity_columns.len() > 1 {
|
||||
warnings.push("Dameng tables can have only one identity column.".to_string());
|
||||
}
|
||||
for column in identity_columns {
|
||||
if column.extra.as_ref().and_then(|extra| extra.identity.as_ref()).and_then(|identity| identity.increment)
|
||||
== Some(0)
|
||||
{
|
||||
warnings.push(format!("Dameng identity column \"{}\" increment cannot be 0.", column.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn validate_columns(columns: &[&EditableStructureColumn], warnings: &mut Vec<String>) {
|
||||
let mut names = std::collections::HashSet::new();
|
||||
for column in columns {
|
||||
|
||||
Reference in new issue
Block a user