aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-05 13:50:52 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:52 +0800
commit9f7162f12944cc722dba9e91f4d30aa312142d49 (patch)
treeb6e68cfecdfcdb25b240277d667d26eb1d1b6806 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
parent2d63ac857ed1082f6f7d365674aa734c582a99dd (diff)
downloadcrupest-9f7162f12944cc722dba9e91f4d30aa312142d49.tar.gz
crupest-9f7162f12944cc722dba9e91f4d30aa312142d49.tar.bz2
crupest-9f7162f12944cc722dba9e91f4d30aa312142d49.zip
Develop secret api. v7
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
new file mode 100644
index 0000000..fb9e1ad
--- /dev/null
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
@@ -0,0 +1,73 @@
+namespace CrupestApi.Commons.Crud;
+
+public class TableInfo
+{
+ public TableInfo(Type entityType)
+ {
+ EntityType = entityType;
+
+ var properties = entityType.GetProperties();
+
+ var columnInfos = new List<ColumnInfo>();
+
+ bool hasPrimaryKey = false;
+ bool hasId = false;
+
+ foreach (var property in properties)
+ {
+ var columnInfo = new ColumnInfo(entityType, property.Name);
+ columnInfos.Add(columnInfo);
+ if (columnInfo.IsPrimaryKey)
+ hasPrimaryKey = true;
+ if (columnInfo.SqlColumnName.Equals("id", StringComparison.OrdinalIgnoreCase))
+ {
+ hasId = true;
+ }
+ }
+
+ if (!hasPrimaryKey)
+ {
+ if (hasId) throw new Exception("A column named id already exists but is not primary key.");
+ var columnInfo = new ColumnInfo(entityType, "id", true, true, ColumnTypeInfoRegistry.Singleton.GetRequiredByDataType(typeof(int)));
+ columnInfos.Add(columnInfo);
+ }
+
+ ColumnInfos = columnInfos;
+
+ CheckValidity();
+ }
+
+ public Type EntityType { get; }
+ public IReadOnlyList<ColumnInfo> ColumnInfos { get; }
+
+ public void CheckValidity()
+ {
+ // Check if there is only one primary key.
+ bool hasPrimaryKey = false;
+ foreach (var column in ColumnInfos)
+ {
+ if (column.IsPrimaryKey)
+ {
+ if (hasPrimaryKey) throw new Exception("Two columns are primary key.");
+ hasPrimaryKey = true;
+ }
+ }
+
+ if (!hasPrimaryKey) throw new Exception("No column is primary key.");
+
+ // Check two columns have the same sql name.
+ HashSet<string> sqlNameSet = new HashSet<string>();
+
+ foreach (var column in ColumnInfos)
+ {
+ if (sqlNameSet.Contains(column.SqlColumnName))
+ throw new Exception($"Two columns have the same sql name '{column.SqlColumnName}'.");
+ sqlNameSet.Add(column.SqlColumnName);
+ }
+ }
+
+ public string GenerateCreateTableSql()
+ {
+ throw new NotImplementedException();
+ }
+} \ No newline at end of file