From 0955225630f3fd5ebd87f76762b1e52fe6e6f9f5 Mon Sep 17 00:00:00 2001 From: mjc Date: Mon, 26 Aug 2024 21:30:54 -0500 Subject: [PATCH] Added basic model definition classes --- src/DapperMatic/Models/Column.cs | 28 ++++++++++++++++++++++ src/DapperMatic/Models/ForeignKey.cs | 19 +++++++++++++++ src/DapperMatic/Models/Index.cs | 19 +++++++++++++++ src/DapperMatic/Models/ModelDefinition.cs | 7 ++++++ src/DapperMatic/Models/PrimaryKey.cs | 13 ++++++++++ src/DapperMatic/Models/Table.cs | 18 ++++++++++++++ src/DapperMatic/Models/UniqueConstraint.cs | 13 ++++++++++ 7 files changed, 117 insertions(+) create mode 100644 src/DapperMatic/Models/Column.cs create mode 100644 src/DapperMatic/Models/ForeignKey.cs create mode 100644 src/DapperMatic/Models/Index.cs create mode 100644 src/DapperMatic/Models/ModelDefinition.cs create mode 100644 src/DapperMatic/Models/PrimaryKey.cs create mode 100644 src/DapperMatic/Models/Table.cs create mode 100644 src/DapperMatic/Models/UniqueConstraint.cs diff --git a/src/DapperMatic/Models/Column.cs b/src/DapperMatic/Models/Column.cs new file mode 100644 index 0000000..1c5ea8e --- /dev/null +++ b/src/DapperMatic/Models/Column.cs @@ -0,0 +1,28 @@ +namespace DapperMatic.Models; + +public class Column +{ + public Column(string name, Type dotnetType) + { + Name = name; + DotnetType = dotnetType; + } + + public string Name { get; set; } + public Type DotnetType { get; set; } + public int? Length { get; set; } + public int? Precision { get; set; } + public int? Scale { get; set; } + public bool Nullable { get; set; } + public string? DefaultValue { get; set; } + public bool AutoIncrement { get; set; } + public bool PrimaryKey { get; set; } + public bool Unique { get; set; } + public bool Indexed { get; set; } + public bool ForeignKey { get; set; } + public string? ReferenceTable { get; set; } + public string? ReferenceColumn { get; set; } + public ReferentialAction? OnDelete { get; set; } + public ReferentialAction? OnUpdate { get; set; } + public string? Comment { get; set; } +} diff --git a/src/DapperMatic/Models/ForeignKey.cs b/src/DapperMatic/Models/ForeignKey.cs new file mode 100644 index 0000000..ae1fffb --- /dev/null +++ b/src/DapperMatic/Models/ForeignKey.cs @@ -0,0 +1,19 @@ +namespace DapperMatic.Models; + +public class ForeignKey +{ + public ForeignKey(string name, string column, string referenceTable, string referenceColumn) + { + Name = name; + Column = column; + ReferenceTable = referenceTable; + ReferenceColumn = referenceColumn; + } + + public string Name { get; set; } + public string Column { get; set; } + public string ReferenceTable { get; set; } + public string ReferenceColumn { get; set; } + public ReferentialAction OnDelete { get; set; } = ReferentialAction.NoAction; + public ReferentialAction OnUpdate { get; set; } = ReferentialAction.NoAction; +} diff --git a/src/DapperMatic/Models/Index.cs b/src/DapperMatic/Models/Index.cs new file mode 100644 index 0000000..f8d8caf --- /dev/null +++ b/src/DapperMatic/Models/Index.cs @@ -0,0 +1,19 @@ +namespace DapperMatic.Models; + +public class Index +{ + public Index(string name, string[] columns, bool unique = false) + { + Name = name; + Columns = columns; + Unique = unique; + } + + public string Name { get; set; } + + /// + /// Column names (optionally, appended with ` ASC` or ` DESC`) + /// + public string[] Columns { get; set; } + public bool Unique { get; set; } +} diff --git a/src/DapperMatic/Models/ModelDefinition.cs b/src/DapperMatic/Models/ModelDefinition.cs new file mode 100644 index 0000000..9c6030f --- /dev/null +++ b/src/DapperMatic/Models/ModelDefinition.cs @@ -0,0 +1,7 @@ +namespace DapperMatic.Models; + +public class ModelDefinition +{ + public Type? Type { get; set; } + public Table? Table { get; set; } +} diff --git a/src/DapperMatic/Models/PrimaryKey.cs b/src/DapperMatic/Models/PrimaryKey.cs new file mode 100644 index 0000000..a21b0a8 --- /dev/null +++ b/src/DapperMatic/Models/PrimaryKey.cs @@ -0,0 +1,13 @@ +namespace DapperMatic.Models; + +public class PrimaryKey +{ + public PrimaryKey(string name, string[] columns) + { + Name = name; + Columns = columns; + } + + public string Name { get; set; } + public string[] Columns { get; set; } +} diff --git a/src/DapperMatic/Models/Table.cs b/src/DapperMatic/Models/Table.cs new file mode 100644 index 0000000..0f385f0 --- /dev/null +++ b/src/DapperMatic/Models/Table.cs @@ -0,0 +1,18 @@ +namespace DapperMatic.Models; + +public class Table +{ + public Table(string name, string? schema) + { + Name = name; + Schema = schema; + } + + public string Name { get; set; } + public string? Schema { get; set; } + public PrimaryKey? PrimaryKey { get; set; } + public Column[] Columns { get; set; } = []; + public UniqueConstraint[] UniqueConstraints { get; set; } = []; + public Index[] Indexes { get; set; } = []; + public ForeignKey[] ForeignKeys { get; set; } = []; +} diff --git a/src/DapperMatic/Models/UniqueConstraint.cs b/src/DapperMatic/Models/UniqueConstraint.cs new file mode 100644 index 0000000..bb4e6c3 --- /dev/null +++ b/src/DapperMatic/Models/UniqueConstraint.cs @@ -0,0 +1,13 @@ +namespace DapperMatic.Models; + +public class UniqueConstraint +{ + public UniqueConstraint(string name, string[] columns) + { + Name = name; + Columns = columns; + } + + public string Name { get; set; } + public string[] Columns { get; set; } +}