remodel-core 0.1.0

Database modeling engine: conceptual (ER) to logical (relational) transformation and SQL DDL generation. A Rust reimplementation of the core engine of brModelo.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Conceptual (Entity-Relationship) model.
//!
//! The conceptual model is a directed multigraph of entities, relationships,
//! attributes, specializations, and unions. Elements are referenced by typed
//! [`u32`] handles (à la ECS) rather than by `Rc`/`Arc`, which keeps the model
//! easy to serialize, clone, and mutate without lifetime pain.
//!
//! ## Mapping from brModelo
//!
//! | brModelo class           | RemodelCore type                 |
//! |--------------------------|----------------------------------|
//! | `Entidade`               | [`Entity`]                       |
//! | `Atributo`               | [`Attribute`]                    |
//! | `Relacionamento`         | [`Relationship`]                 |
//! | `Especializacao`         | [`Specialization`]               |
//! | `Uniao`                  | [`Union`]                        |
//! | `EntidadeAssociativa`    | [`AssociativeEntity`]            |
//! | `Cardinalidade`          | [`crate::models::cardinality::Cardinality`] |
//! | `DiagramaConceitual`     | [`ConceptualModel`]              |

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::models::cardinality::Cardinality;
use crate::models::types::DataType;

/// Strongly-typed handle for an [`Entity`] inside a [`ConceptualModel`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EntityId(pub u32);

/// Strongly-typed handle for an [`Attribute`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AttributeId(pub u32);

/// Strongly-typed handle for a [`Relationship`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct RelationshipId(pub u32);

/// Strongly-typed handle for a [`Specialization`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SpecializationId(pub u32);

/// Strongly-typed handle for a [`Union`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct UnionId(pub u32);

/// Strongly-typed handle for an [`AssociativeEntity`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AssociativeEntityId(pub u32);

/// What kind of attribute this is.
///
/// Mirrors brModelo's flags on `Atributo`. A single attribute can be both
/// `Primary` and another kind in the original Java model (an attribute is
/// identified by a `boolean isIdentificador`); here that is split into
/// `is_primary` (a flag on [`Attribute`]) and `kind` (the structural kind).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum AttributeKind {
    /// A plain single-valued attribute.
    #[default]
    Simple,
    /// A composite attribute, made of nested sub-attributes.
    Composite,
    /// A multivalued attribute. The pair carries the cardinality bounds
    /// `(min, max)`; `max == None` means unbounded.
    Multivalued {
        /// Minimum number of values; `0` makes the attribute optional.
        min: u32,
        /// Maximum number of values, or `None` for unbounded (`*`).
        max: Option<u32>,
    },
    /// A derived attribute, computed from other attributes.
    Derived,
}

/// An ER entity. Holds owned attributes and a name.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entity {
    /// Unique handle within the owning model.
    pub id: EntityId,
    /// Display name (e.g. `"Customer"`).
    pub name: String,
    /// Free-form note shown in the inspector panel.
    pub note: String,
    /// IDs of attributes owned by this entity, in author order.
    pub attributes: Vec<AttributeId>,
    /// `true` for *weak entities*, which require an identifying relationship
    /// to be uniquely identified.
    pub weak: bool,
}

/// An attribute belonging to either an [`Entity`] or a [`Relationship`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Attribute {
    /// Unique handle within the owning model.
    pub id: AttributeId,
    /// Display name (e.g. `"first_name"`).
    pub name: String,
    /// Logical data type (mapped to a column type during conversion).
    pub data_type: DataType,
    /// Whether this attribute participates in the entity's primary identifier.
    pub is_primary: bool,
    /// Whether this attribute is a *partial* key — combined with the
    /// owning entity's identifying relationship to form a key (typical for
    /// weak entities).
    pub is_partial_key: bool,
    /// Whether the attribute admits a null value.
    pub is_optional: bool,
    /// Structural kind of the attribute.
    pub kind: AttributeKind,
    /// For composite attributes, the IDs of the sub-attributes that compose
    /// this one. Empty for non-composite attributes.
    pub children: Vec<AttributeId>,
}

/// One endpoint of a [`Relationship`].
///
/// The cardinality annotation follows the **"look-here"** convention used in
/// Heuser's textbook and most modern UML tools: the cardinality next to
/// entity *E* describes **how many tuples of the other entity** participate
/// per tuple of *E*.
///
/// For example, in a `wrote` relationship between `Book` and `Author`,
/// recording `Book.cardinality = ZeroToMany` means *"each book has 0..N
/// authors"* (so `Author` is the many side from `Book`'s perspective).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RelationshipEndpoint {
    /// The entity participating at this endpoint.
    pub entity: EntityId,
    /// How many tuples of the *other* entity participate per tuple of
    /// `entity`. See the type-level docs for the convention.
    pub cardinality: Cardinality,
    /// Optional role label (brModelo's `Papel`).
    pub role: Option<String>,
}

/// A relationship between two or more entities.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Relationship {
    /// Unique handle within the owning model.
    pub id: RelationshipId,
    /// Display name (e.g. `"works_for"`).
    pub name: String,
    /// Endpoints, in author order. A relationship is *binary* iff
    /// `endpoints.len() == 2`, *self* iff all endpoints share an entity.
    pub endpoints: Vec<RelationshipEndpoint>,
    /// Attributes carried by the relationship itself (descriptive attributes).
    pub attributes: Vec<AttributeId>,
}

impl Relationship {
    /// `true` if every endpoint refers to the same entity.
    pub fn is_self(&self) -> bool {
        if self.endpoints.is_empty() {
            return false;
        }
        let first = self.endpoints[0].entity;
        self.endpoints.iter().all(|e| e.entity == first)
    }

    /// `true` for binary relationships (exactly 2 endpoints).
    pub fn is_binary(&self) -> bool {
        self.endpoints.len() == 2
    }

    /// `true` if at least three distinct entity endpoints participate.
    pub fn is_nary(&self) -> bool {
        self.endpoints.len() >= 3
    }
}

/// Whether a specialization is total (every parent instance is in some child)
/// or partial (some parent instances may be in no child), and whether it is
/// disjoint (an instance can be in at most one child) or overlapping
/// (an instance can be in several children).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub struct SpecializationKind {
    /// `true` if every parent instance must belong to some child.
    pub total: bool,
    /// `true` if a parent instance may belong to multiple children at once.
    pub overlapping: bool,
}

impl SpecializationKind {
    /// Partial + disjoint: the most permissive variant (the default).
    pub const PARTIAL_DISJOINT: Self = Self { total: false, overlapping: false };
    /// Total + disjoint.
    pub const TOTAL_DISJOINT: Self = Self { total: true, overlapping: false };
    /// Partial + overlapping.
    pub const PARTIAL_OVERLAPPING: Self = Self { total: false, overlapping: true };
    /// Total + overlapping.
    pub const TOTAL_OVERLAPPING: Self = Self { total: true, overlapping: true };
}

/// IS-A specialization (generalization) connecting one parent entity to
/// several child entities. brModelo calls this `Especializacao`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Specialization {
    /// Unique handle within the owning model.
    pub id: SpecializationId,
    /// Optional name shown next to the specialization gadget.
    pub name: String,
    /// The supertype.
    pub parent: EntityId,
    /// The subtypes.
    pub children: Vec<EntityId>,
    /// Discriminator flags.
    pub kind: SpecializationKind,
}

/// Union/category construct: several parent entities feed a single category
/// child entity (brModelo's `Uniao`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Union {
    /// Unique handle within the owning model.
    pub id: UnionId,
    /// Optional name shown next to the union gadget.
    pub name: String,
    /// Parent entities whose union forms `category`.
    pub parents: Vec<EntityId>,
    /// The category entity (the union of `parents`).
    pub category: EntityId,
}

/// An associative entity wraps a relationship so that it can itself
/// participate in further relationships (brModelo's `EntidadeAssociativa`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AssociativeEntity {
    /// Unique handle within the owning model.
    pub id: AssociativeEntityId,
    /// The relationship that this associative entity wraps.
    pub relationship: RelationshipId,
    /// Display name; defaults to the wrapped relationship's name.
    pub name: String,
}

/// The full conceptual model: entities, attributes, relationships, and the
/// higher-level constructs (specialization, union, associative).
///
/// Element ordering is deterministic (author order) so that round-tripping
/// through serialization preserves the diagram.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConceptualModel {
    /// Diagram name (typically the project or schema name).
    pub name: String,
    /// Monotonic counter used to mint unique IDs.
    next_id: u32,
    /// Entities, keyed for O(1) lookup; iteration order is insertion order.
    pub entities: IndexMap<EntityId, Entity>,
    /// Attributes shared between entities and relationships.
    pub attributes: IndexMap<AttributeId, Attribute>,
    /// Relationships.
    pub relationships: IndexMap<RelationshipId, Relationship>,
    /// Specializations.
    pub specializations: IndexMap<SpecializationId, Specialization>,
    /// Unions / categories.
    pub unions: IndexMap<UnionId, Union>,
    /// Associative entities.
    pub associative_entities: IndexMap<AssociativeEntityId, AssociativeEntity>,
}

impl ConceptualModel {
    /// Create a new empty model with the given diagram name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Self::default()
        }
    }

    fn mint(&mut self) -> u32 {
        self.next_id = self.next_id.checked_add(1).expect("ID space exhausted");
        self.next_id
    }

    /// Add a new entity with the given name. Returns its handle.
    pub fn add_entity(&mut self, name: impl Into<String>) -> EntityId {
        let id = EntityId(self.mint());
        self.entities.insert(
            id,
            Entity {
                id,
                name: name.into(),
                note: String::new(),
                attributes: Vec::new(),
                weak: false,
            },
        );
        id
    }

    /// Add a new *weak* entity. Functionally identical to
    /// [`Self::add_entity`] but flips the `weak` flag, which downstream
    /// conversion uses to require an identifying relationship.
    pub fn add_weak_entity(&mut self, name: impl Into<String>) -> EntityId {
        let id = self.add_entity(name);
        self.entity_mut(id).expect("just inserted").weak = true;
        id
    }

    /// Borrow an entity by its handle.
    pub fn entity(&self, id: EntityId) -> Result<&Entity> {
        self.entities
            .get(&id)
            .ok_or_else(|| Error::UnknownReference { kind: "entity", id: format!("{}", id.0) })
    }

    /// Mutably borrow an entity by its handle.
    pub fn entity_mut(&mut self, id: EntityId) -> Result<&mut Entity> {
        self.entities
            .get_mut(&id)
            .ok_or_else(|| Error::UnknownReference { kind: "entity", id: format!("{}", id.0) })
    }

    /// Add a new attribute and attach it to `owner`.
    ///
    /// `owner` may be either an [`EntityId`] or a [`RelationshipId`]; the
    /// attribute is appended to that owner's attribute list.
    pub fn add_attribute(
        &mut self,
        owner: AttributeOwner,
        name: impl Into<String>,
        data_type: DataType,
    ) -> Result<AttributeId> {
        let id = AttributeId(self.mint());
        self.attributes.insert(
            id,
            Attribute {
                id,
                name: name.into(),
                data_type,
                is_primary: false,
                is_partial_key: false,
                is_optional: false,
                kind: AttributeKind::Simple,
                children: Vec::new(),
            },
        );
        match owner {
            AttributeOwner::Entity(eid) => self.entity_mut(eid)?.attributes.push(id),
            AttributeOwner::Relationship(rid) => self.relationship_mut(rid)?.attributes.push(id),
        }
        Ok(id)
    }

    /// Convenience: add a primary-key attribute to an entity.
    pub fn add_primary_attribute(
        &mut self,
        entity: EntityId,
        name: impl Into<String>,
        data_type: DataType,
    ) -> Result<AttributeId> {
        let id = self.add_attribute(AttributeOwner::Entity(entity), name, data_type)?;
        let attr = self.attribute_mut(id)?;
        attr.is_primary = true;
        Ok(id)
    }

    /// Borrow an attribute by its handle.
    pub fn attribute(&self, id: AttributeId) -> Result<&Attribute> {
        self.attributes
            .get(&id)
            .ok_or_else(|| Error::UnknownReference { kind: "attribute", id: format!("{}", id.0) })
    }

    /// Mutably borrow an attribute.
    pub fn attribute_mut(&mut self, id: AttributeId) -> Result<&mut Attribute> {
        self.attributes
            .get_mut(&id)
            .ok_or_else(|| Error::UnknownReference { kind: "attribute", id: format!("{}", id.0) })
    }

    /// Start building a relationship. The returned [`RelationshipBuilder`]
    /// records each endpoint and is finalized by dropping it (the relationship
    /// is inserted into the model immediately on construction; `with` mutates
    /// it in place).
    pub fn relate(
        &mut self,
        name: impl Into<String>,
        first: EntityId,
        first_card: Cardinality,
    ) -> RelationshipBuilder<'_> {
        let id = RelationshipId(self.mint());
        let rel = Relationship {
            id,
            name: name.into(),
            endpoints: vec![RelationshipEndpoint {
                entity: first,
                cardinality: first_card,
                role: None,
            }],
            attributes: Vec::new(),
        };
        self.relationships.insert(id, rel);
        RelationshipBuilder { model: self, id }
    }

    /// Borrow a relationship by its handle.
    pub fn relationship(&self, id: RelationshipId) -> Result<&Relationship> {
        self.relationships.get(&id).ok_or_else(|| Error::UnknownReference {
            kind: "relationship",
            id: format!("{}", id.0),
        })
    }

    /// Mutably borrow a relationship.
    pub fn relationship_mut(&mut self, id: RelationshipId) -> Result<&mut Relationship> {
        self.relationships.get_mut(&id).ok_or_else(|| Error::UnknownReference {
            kind: "relationship",
            id: format!("{}", id.0),
        })
    }

    /// Add a new specialization.
    pub fn add_specialization(
        &mut self,
        name: impl Into<String>,
        parent: EntityId,
        children: Vec<EntityId>,
        kind: SpecializationKind,
    ) -> Result<SpecializationId> {
        if children.len() < 2 {
            return Err(Error::InvalidSpecialization(format!(
                "specialization `{}` must have at least 2 children, got {}",
                name.into(),
                children.len()
            )));
        }
        let _ = self.entity(parent)?;
        for c in &children {
            let _ = self.entity(*c)?;
        }
        let id = SpecializationId(self.mint());
        let name = name.into();
        self.specializations.insert(id, Specialization { id, name, parent, children, kind });
        Ok(id)
    }

    /// Add a new union/category construct.
    pub fn add_union(
        &mut self,
        name: impl Into<String>,
        parents: Vec<EntityId>,
        category: EntityId,
    ) -> Result<UnionId> {
        if parents.len() < 2 {
            return Err(Error::InvalidSpecialization(format!(
                "union `{}` must have at least 2 parents, got {}",
                name.into(),
                parents.len()
            )));
        }
        let _ = self.entity(category)?;
        for p in &parents {
            let _ = self.entity(*p)?;
        }
        let id = UnionId(self.mint());
        self.unions
            .insert(id, Union { id, name: name.into(), parents, category });
        Ok(id)
    }

    /// Wrap a relationship as an associative entity.
    pub fn add_associative_entity(
        &mut self,
        relationship: RelationshipId,
    ) -> Result<AssociativeEntityId> {
        let rel = self.relationship(relationship)?;
        let name = rel.name.clone();
        let id = AssociativeEntityId(self.mint());
        self.associative_entities
            .insert(id, AssociativeEntity { id, relationship, name });
        Ok(id)
    }
}

/// Owner reference passed to [`ConceptualModel::add_attribute`].
#[derive(Debug, Clone, Copy)]
pub enum AttributeOwner {
    /// Attach the attribute to an entity.
    Entity(EntityId),
    /// Attach the attribute to a relationship (descriptive attribute).
    Relationship(RelationshipId),
}

/// Fluent builder returned by [`ConceptualModel::relate`].
///
/// Use [`with`](Self::with) to add additional endpoints (binary, ternary, …)
/// and [`carry`](Self::carry) to attach descriptive attributes.
pub struct RelationshipBuilder<'m> {
    model: &'m mut ConceptualModel,
    id: RelationshipId,
}

impl<'m> RelationshipBuilder<'m> {
    /// Add another endpoint to this relationship.
    pub fn with(self, entity: EntityId, cardinality: Cardinality) -> Self {
        if let Some(rel) = self.model.relationships.get_mut(&self.id) {
            rel.endpoints.push(RelationshipEndpoint { entity, cardinality, role: None });
        }
        self
    }

    /// Set a role label on the most recently added endpoint.
    pub fn with_role(self, role: impl Into<String>) -> Self {
        if let Some(rel) = self.model.relationships.get_mut(&self.id) {
            if let Some(last) = rel.endpoints.last_mut() {
                last.role = Some(role.into());
            }
        }
        self
    }

    /// Attach a descriptive attribute to the relationship.
    pub fn carry(self, name: impl Into<String>, data_type: DataType) -> Self {
        let _ = self
            .model
            .add_attribute(AttributeOwner::Relationship(self.id), name, data_type);
        self
    }

    /// Finalize the builder and return the relationship's handle.
    pub fn id(self) -> RelationshipId {
        self.id
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample() -> ConceptualModel {
        let mut m = ConceptualModel::new("library");
        let book = m.add_entity("Book");
        let author = m.add_entity("Author");
        m.add_primary_attribute(book, "id", DataType::Integer).unwrap();
        m.add_attribute(AttributeOwner::Entity(book), "title", DataType::Varchar(255)).unwrap();
        m.add_primary_attribute(author, "id", DataType::Integer).unwrap();
        m.relate("wrote", book, Cardinality::ZeroToMany)
            .with(author, Cardinality::OneToMany)
            .id();
        m
    }

    #[test]
    fn build_basic_model() {
        let m = sample();
        assert_eq!(m.entities.len(), 2);
        assert_eq!(m.attributes.len(), 3);
        assert_eq!(m.relationships.len(), 1);
        let rel = m.relationships.values().next().unwrap();
        assert!(rel.is_binary());
        assert!(!rel.is_self());
    }

    #[test]
    fn relationship_self_check() {
        let mut m = ConceptualModel::new("hr");
        let person = m.add_entity("Person");
        let rel = m
            .relate("manages", person, Cardinality::ZeroToOne)
            .with(person, Cardinality::ZeroToMany)
            .id();
        assert!(m.relationship(rel).unwrap().is_self());
    }

    #[test]
    fn specialization_requires_two_children() {
        let mut m = ConceptualModel::new("vehicles");
        let v = m.add_entity("Vehicle");
        let car = m.add_entity("Car");
        let err = m
            .add_specialization("kind", v, vec![car], SpecializationKind::PARTIAL_DISJOINT)
            .unwrap_err();
        assert!(matches!(err, Error::InvalidSpecialization(_)));
    }

    #[test]
    fn unknown_entity_error() {
        let m = ConceptualModel::new("x");
        let err = m.entity(EntityId(99)).unwrap_err();
        assert!(matches!(err, Error::UnknownReference { kind: "entity", .. }));
    }

    #[test]
    fn json_round_trip() {
        let m = sample();
        let s = serde_json::to_string(&m).unwrap();
        let back: ConceptualModel = serde_json::from_str(&s).unwrap();
        assert_eq!(m.entities.len(), back.entities.len());
        assert_eq!(m.attributes.len(), back.attributes.len());
        assert_eq!(m.relationships.len(), back.relationships.len());
    }
}