diff options
author | crupest <crupest@outlook.com> | 2022-12-05 16:25:43 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:52 +0800 |
commit | 875a3863cf009cf3521f5cbb06548b1de22536e2 (patch) | |
tree | 6fbc48db475a477533026410256c3e35a4e1e5a9 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs | |
parent | 9f7162f12944cc722dba9e91f4d30aa312142d49 (diff) | |
download | crupest-875a3863cf009cf3521f5cbb06548b1de22536e2.tar.gz crupest-875a3863cf009cf3521f5cbb06548b1de22536e2.tar.bz2 crupest-875a3863cf009cf3521f5cbb06548b1de22536e2.zip |
Develop secret api. v8
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index fb9e1ad..660cd4d 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -1,9 +1,20 @@ +using Dapper; +using Microsoft.Data.Sqlite; + namespace CrupestApi.Commons.Crud; public class TableInfo { + // For custom name. public TableInfo(Type entityType) + : this(entityType.Name, entityType) + { + + } + + public TableInfo(string tableName, Type entityType) { + TableName = tableName; EntityType = entityType; var properties = entityType.GetProperties(); @@ -38,6 +49,7 @@ public class TableInfo } public Type EntityType { get; } + public string TableName { get; } public IReadOnlyList<ColumnInfo> ColumnInfos { get; } public void CheckValidity() @@ -68,6 +80,36 @@ public class TableInfo public string GenerateCreateTableSql() { - throw new NotImplementedException(); + var tableName = TableName; + var columnSql = string.Join(",\n", ColumnInfos.Select(c => c.GenerateCreateTableColumnString())); + + var sql = $@" +CREATE TABLE {tableName}( + {columnSql} +); + "; + + return sql; + } + + public async Task<bool> CheckExistence(SqliteConnection connection) + { + var tableName = TableName; + var count = (await connection.QueryAsync<int>( + @"SELECT count(*) FROM sqlite_schema WHERE type = 'table' AND tbl_name = @TableName;", + new { TableName = tableName })).Single(); + if (count == 0) + { + return false; + } + else if (count > 1) + { + throw new DatabaseInternalException($"More than 1 table has name {tableName}. What happened?"); + } + else + { + return true; + } + } }
\ No newline at end of file |