1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Column model for the SDK
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Foreign key reference to another table's column
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ForeignKey {
/// Target table ID (UUID as string)
#[serde(alias = "table_id")]
pub table_id: String,
/// Column name in the target table
#[serde(alias = "column_name")]
pub column_name: String,
}
/// ODCS v3.1.0 Relationship at property level
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PropertyRelationship {
/// Relationship type (e.g., "foreignKey", "parent", "child")
#[serde(rename = "type", alias = "relationship_type")]
pub relationship_type: String,
/// Target reference (e.g., "definitions/order_id", "schema/id/properties/id")
pub to: String,
}
/// ODCS v3.1.0 logicalTypeOptions for additional type metadata
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct LogicalTypeOptions {
/// Minimum length for strings
#[serde(skip_serializing_if = "Option::is_none", alias = "min_length")]
pub min_length: Option<i64>,
/// Maximum length for strings
#[serde(skip_serializing_if = "Option::is_none", alias = "max_length")]
pub max_length: Option<i64>,
/// Regex pattern for strings
#[serde(skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
/// Format hint (e.g., "email", "uuid", "uri")
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<String>,
/// Minimum value for numbers/dates
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum: Option<serde_json::Value>,
/// Maximum value for numbers/dates
#[serde(skip_serializing_if = "Option::is_none")]
pub maximum: Option<serde_json::Value>,
/// Exclusive minimum for numbers
#[serde(skip_serializing_if = "Option::is_none", alias = "exclusive_minimum")]
pub exclusive_minimum: Option<serde_json::Value>,
/// Exclusive maximum for numbers
#[serde(skip_serializing_if = "Option::is_none", alias = "exclusive_maximum")]
pub exclusive_maximum: Option<serde_json::Value>,
/// Precision for decimals
#[serde(skip_serializing_if = "Option::is_none")]
pub precision: Option<i32>,
/// Scale for decimals
#[serde(skip_serializing_if = "Option::is_none")]
pub scale: Option<i32>,
}
impl LogicalTypeOptions {
pub fn is_empty(&self) -> bool {
self.min_length.is_none()
&& self.max_length.is_none()
&& self.pattern.is_none()
&& self.format.is_none()
&& self.minimum.is_none()
&& self.maximum.is_none()
&& self.exclusive_minimum.is_none()
&& self.exclusive_maximum.is_none()
&& self.precision.is_none()
&& self.scale.is_none()
}
}
/// Authoritative definition reference (ODCS v3.1.0)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AuthoritativeDefinition {
/// Type of the reference (e.g., "businessDefinition", "transformationImplementation")
#[serde(rename = "type", alias = "definition_type")]
pub definition_type: String,
/// URL to the authoritative definition
pub url: String,
}
/// Column model representing a field in a table
///
/// A column defines a single field with a data type, constraints, and optional metadata.
/// This model supports all ODCS v3.1.0 property fields to ensure no data loss during import/export.
///
/// # Example
///
/// ```rust
/// use data_modelling_core::models::Column;
///
/// let column = Column::new("id".to_string(), "INT".to_string());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Column {
// === Core Identity Fields ===
/// Stable technical identifier (ODCS: id)
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
/// Column name (ODCS: name)
pub name: String,
/// Business name for the column (ODCS: businessName)
#[serde(skip_serializing_if = "Option::is_none", alias = "business_name")]
pub business_name: Option<String>,
/// Column description/documentation (ODCS: description)
#[serde(default)]
pub description: String,
// === Type Information ===
/// Logical data type (ODCS: logicalType - e.g., "string", "integer", "number")
#[serde(rename = "dataType", alias = "data_type")]
pub data_type: String,
/// Physical database type (ODCS: physicalType - e.g., "VARCHAR(100)", "BIGINT")
#[serde(skip_serializing_if = "Option::is_none", alias = "physical_type")]
pub physical_type: Option<String>,
/// Physical name in the data source (ODCS: physicalName)
#[serde(skip_serializing_if = "Option::is_none", alias = "physical_name")]
pub physical_name: Option<String>,
/// Additional type options (ODCS: logicalTypeOptions)
#[serde(
skip_serializing_if = "Option::is_none",
alias = "logical_type_options"
)]
pub logical_type_options: Option<LogicalTypeOptions>,
// === Key Constraints ===
/// Whether this column is part of the primary key (ODCS: primaryKey)
#[serde(default, alias = "primary_key")]
pub primary_key: bool,
/// Position in composite primary key, 1-based (ODCS: primaryKeyPosition)
#[serde(
skip_serializing_if = "Option::is_none",
alias = "primary_key_position"
)]
pub primary_key_position: Option<i32>,
/// Whether the column contains unique values (ODCS: unique)
#[serde(default)]
pub unique: bool,
/// Whether the column allows NULL values (inverse of ODCS: required)
#[serde(default = "default_true")]
pub nullable: bool,
// === Partitioning & Clustering ===
/// Whether the column is used for partitioning (ODCS: partitioned)
#[serde(default)]
pub partitioned: bool,
/// Position in partition key, 1-based (ODCS: partitionKeyPosition)
#[serde(
skip_serializing_if = "Option::is_none",
alias = "partition_key_position"
)]
pub partition_key_position: Option<i32>,
/// Whether the column is used for clustering
#[serde(default)]
pub clustered: bool,
// === Data Classification & Security ===
/// Data classification level (ODCS: classification - e.g., "confidential", "public")
#[serde(skip_serializing_if = "Option::is_none")]
pub classification: Option<String>,
/// Whether this is a critical data element (ODCS: criticalDataElement)
#[serde(default, alias = "critical_data_element")]
pub critical_data_element: bool,
/// Name of the encrypted version of this column (ODCS: encryptedName)
#[serde(skip_serializing_if = "Option::is_none", alias = "encrypted_name")]
pub encrypted_name: Option<String>,
// === Transformation Metadata ===
/// Source objects used in transformation (ODCS: transformSourceObjects)
#[serde(
default,
skip_serializing_if = "Vec::is_empty",
alias = "transform_source_objects"
)]
pub transform_source_objects: Vec<String>,
/// Transformation logic/expression (ODCS: transformLogic)
#[serde(skip_serializing_if = "Option::is_none", alias = "transform_logic")]
pub transform_logic: Option<String>,
/// Human-readable transformation description (ODCS: transformDescription)
#[serde(
skip_serializing_if = "Option::is_none",
alias = "transform_description"
)]
pub transform_description: Option<String>,
// === Examples & Documentation ===
/// Example values for this column (ODCS: examples)
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub examples: Vec<serde_json::Value>,
/// Default value for the column
#[serde(skip_serializing_if = "Option::is_none", alias = "default_value")]
pub default_value: Option<serde_json::Value>,
// === Relationships & References ===
/// ODCS v3.1.0 relationships (property-level references)
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub relationships: Vec<PropertyRelationship>,
/// Authoritative definitions (ODCS: authoritativeDefinitions)
#[serde(
default,
skip_serializing_if = "Vec::is_empty",
alias = "authoritative_definitions"
)]
pub authoritative_definitions: Vec<AuthoritativeDefinition>,
// === Quality & Validation ===
/// Quality rules and checks (ODCS: quality)
#[serde(default)]
pub quality: Vec<HashMap<String, serde_json::Value>>,
/// Enum values if this column is an enumeration type
#[serde(default, alias = "enum_values")]
pub enum_values: Vec<String>,
// === Tags & Custom Properties ===
/// Property-level tags (ODCS: tags)
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,
/// Custom properties for format-specific metadata not covered by ODCS
#[serde(
default,
skip_serializing_if = "HashMap::is_empty",
alias = "custom_properties"
)]
pub custom_properties: HashMap<String, serde_json::Value>,
// === Legacy/Internal Fields ===
/// Whether this column is a secondary/business key
#[serde(default, alias = "secondary_key")]
pub secondary_key: bool,
/// Composite key name if this column is part of a composite key
#[serde(skip_serializing_if = "Option::is_none", alias = "composite_key")]
pub composite_key: Option<String>,
/// Foreign key reference (legacy - prefer relationships)
#[serde(skip_serializing_if = "Option::is_none", alias = "foreign_key")]
pub foreign_key: Option<ForeignKey>,
/// Additional constraints (e.g., "CHECK", "UNIQUE")
#[serde(default)]
pub constraints: Vec<String>,
/// Validation errors and warnings
#[serde(default)]
pub errors: Vec<HashMap<String, serde_json::Value>>,
/// Display order for UI rendering
#[serde(default, alias = "column_order")]
pub column_order: i32,
/// Nested data type for ARRAY<STRUCT> or MAP types
#[serde(skip_serializing_if = "Option::is_none", alias = "nested_data")]
pub nested_data: Option<String>,
}
fn default_true() -> bool {
true
}
impl Default for Column {
fn default() -> Self {
Self {
// Core Identity
id: None,
name: String::new(),
business_name: None,
description: String::new(),
// Type Information
data_type: String::new(),
physical_type: None,
physical_name: None,
logical_type_options: None,
// Key Constraints
primary_key: false,
primary_key_position: None,
unique: false,
nullable: true, // Default to nullable
// Partitioning & Clustering
partitioned: false,
partition_key_position: None,
clustered: false,
// Data Classification & Security
classification: None,
critical_data_element: false,
encrypted_name: None,
// Transformation Metadata
transform_source_objects: Vec::new(),
transform_logic: None,
transform_description: None,
// Examples & Documentation
examples: Vec::new(),
default_value: None,
// Relationships & References
relationships: Vec::new(),
authoritative_definitions: Vec::new(),
// Quality & Validation
quality: Vec::new(),
enum_values: Vec::new(),
// Tags & Custom Properties
tags: Vec::new(),
custom_properties: HashMap::new(),
// Legacy/Internal Fields
secondary_key: false,
composite_key: None,
foreign_key: None,
constraints: Vec::new(),
errors: Vec::new(),
column_order: 0,
nested_data: None,
}
}
}
impl Column {
/// Create a new column with the given name and data type
///
/// # Arguments
///
/// * `name` - The column name (must be valid according to naming conventions)
/// * `data_type` - The data type string (e.g., "INT", "VARCHAR(100)")
///
/// # Returns
///
/// A new `Column` instance with default values (nullable=true, primary_key=false).
///
/// # Example
///
/// ```rust
/// use data_modelling_core::models::Column;
///
/// let col = Column::new("user_id".to_string(), "BIGINT".to_string());
/// ```
#[allow(deprecated)]
pub fn new(name: String, data_type: String) -> Self {
Self {
// Core Identity
id: None,
name,
business_name: None,
description: String::new(),
// Type Information
data_type: normalize_data_type(&data_type),
physical_type: None,
physical_name: None,
logical_type_options: None,
// Key Constraints
primary_key: false,
primary_key_position: None,
unique: false,
nullable: true,
// Partitioning & Clustering
partitioned: false,
partition_key_position: None,
clustered: false,
// Data Classification & Security
classification: None,
critical_data_element: false,
encrypted_name: None,
// Transformation Metadata
transform_source_objects: Vec::new(),
transform_logic: None,
transform_description: None,
// Examples & Documentation
examples: Vec::new(),
default_value: None,
// Relationships & References
relationships: Vec::new(),
authoritative_definitions: Vec::new(),
// Quality & Validation
quality: Vec::new(),
enum_values: Vec::new(),
// Tags & Custom Properties
tags: Vec::new(),
custom_properties: HashMap::new(),
// Legacy/Internal Fields
secondary_key: false,
composite_key: None,
foreign_key: None,
constraints: Vec::new(),
errors: Vec::new(),
column_order: 0,
nested_data: None,
}
}
}
fn normalize_data_type(data_type: &str) -> String {
if data_type.is_empty() {
return data_type.to_string();
}
let upper = data_type.to_uppercase();
// Handle STRUCT<...>, ARRAY<...>, MAP<...> preserving inner content
if upper.starts_with("STRUCT") {
if let Some(start) = data_type.find('<')
&& let Some(end) = data_type.rfind('>')
{
let inner = &data_type[start + 1..end];
return format!("STRUCT<{}>", inner);
}
return format!("STRUCT{}", &data_type[6..]);
} else if upper.starts_with("ARRAY") {
if let Some(start) = data_type.find('<')
&& let Some(end) = data_type.rfind('>')
{
let inner = &data_type[start + 1..end];
return format!("ARRAY<{}>", inner);
}
return format!("ARRAY{}", &data_type[5..]);
} else if upper.starts_with("MAP") {
if let Some(start) = data_type.find('<')
&& let Some(end) = data_type.rfind('>')
{
let inner = &data_type[start + 1..end];
return format!("MAP<{}>", inner);
}
return format!("MAP{}", &data_type[3..]);
}
upper
}