aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-05 20:28:58 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:52 +0800
commit3d55a8a37e0e56e56a0bdad4fbb7a69a5d36d54b (patch)
tree01b0098fd4cb3fb5ee05aa7434b51d9a7e084b0e /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
parentdb0932004e2d7462288044e4dd9c353d9b534793 (diff)
downloadcrupest-3d55a8a37e0e56e56a0bdad4fbb7a69a5d36d54b.tar.gz
crupest-3d55a8a37e0e56e56a0bdad4fbb7a69a5d36d54b.tar.bz2
crupest-3d55a8a37e0e56e56a0bdad4fbb7a69a5d36d54b.zip
Develop secret api. v10
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs57
1 files changed, 57 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
index 38daa3f..04d7f40 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
@@ -6,6 +6,8 @@ namespace CrupestApi.Commons.Crud;
public class TableInfo
{
+ private readonly Lazy<List<string>> _lazyColumnNameList;
+
// For custom name.
public TableInfo(Type entityType)
: this(entityType.Name, entityType)
@@ -47,11 +49,14 @@ public class TableInfo
ColumnInfos = columnInfos;
CheckValidity();
+
+ _lazyColumnNameList = new Lazy<List<string>>(() => ColumnInfos.Select(c => c.SqlColumnName).ToList());
}
public Type EntityType { get; }
public string TableName { get; }
public IReadOnlyList<ColumnInfo> ColumnInfos { get; }
+ public IReadOnlyList<string> ColumnNameList => _lazyColumnNameList.Value;
public void CheckValidity()
{
@@ -130,6 +135,58 @@ CREATE TABLE {tableName}(
{
return true;
}
+ }
+
+ public string GenerateSelectSql(WhereClause? whereClause, OrderByClause? orderByClause, int? skip, int? limit, out DynamicParameters parameters)
+ {
+ if (whereClause is not null)
+ {
+ var relatedFields = ((IWhereClause)whereClause).GetRelatedColumns();
+ if (relatedFields is not null)
+ {
+ foreach (var field in relatedFields)
+ {
+ if (!ColumnNameList.Contains(field))
+ {
+ throw new ArgumentException($"Field {field} is not in the table.");
+ }
+ }
+ }
+ }
+
+ parameters = new DynamicParameters();
+
+ StringBuilder result = new StringBuilder()
+ .Append("SELECT * FROM ")
+ .Append(TableName);
+
+ if (whereClause is not null)
+ {
+ result.Append(' ');
+ result.Append(whereClause.GenerateSql(parameters));
+ }
+
+ if (orderByClause is not null)
+ {
+ result.Append(' ');
+ result.Append(orderByClause.GenerateSql());
+ }
+
+
+ if (limit is not null)
+ {
+ result.Append(" LIMIT @Limit");
+ parameters.Add("Limit", limit.Value);
+ }
+
+ if (skip is not null)
+ {
+ result.Append(" OFFSET @Skip");
+ parameters.Add("Skip", skip.Value);
+ }
+
+ result.Append(';');
+ return result.ToString();
}
} \ No newline at end of file