From 7a8de2eb8b585d34335887b217007b394e065e66 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 11 Dec 2022 16:51:53 +0800 Subject: Develop secret api. v29 --- .../CrupestApi.Commons/Crud/TableInfo.cs | 73 +++++++++++++++------- 1 file changed, 49 insertions(+), 24 deletions(-) (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs') diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index 61cd04f..089ff8a 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -239,7 +239,7 @@ CREATE TABLE {tableName}( /// /// If you call this manually, it's your duty to call hooks. /// - /// + /// public (string sql, ParamList parameters) GenerateSelectSql(string? selectWhat, IWhereClause? whereClause, IOrderByClause? orderByClause = null, int? skip = null, int? limit = null, string? dbProviderId = null) { CheckRelatedColumns(whereClause); @@ -389,30 +389,16 @@ CREATE TABLE {tableName}( return result; } - public virtual int SelectCount(IDbConnection dbConnection, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) - { - var (sql, parameters) = GenerateSelectSql("COUNT(*)", where, orderBy, skip, limit); - return dbConnection.QuerySingle(sql, parameters); - - } - - public virtual IEnumerable Select(IDbConnection dbConnection, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) - { - return Select>(dbConnection, null, where, orderBy, skip, limit); - } - /// - /// Select and call hooks. + /// ConvertParameters. Select. Call hooks. /// - public virtual IEnumerable Select(IDbConnection dbConnection, string? what, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) + public virtual List SelectDynamic(IDbConnection dbConnection, string? what, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) { var (sql, parameters) = GenerateSelectSql(what, where, orderBy, skip, limit); - return dbConnection.Query(sql, parameters).Select(d => + var queryResult = dbConnection.Query(sql, ConvertParameters(parameters)); + return queryResult.Select(d => { Type dynamicType = d.GetType(); - - var result = Activator.CreateInstance(); - foreach (var column in ColumnInfos) { object? value = null; @@ -428,17 +414,56 @@ CREATE TABLE {tableName}( value = column.ColumnType.ConvertFromDatabase(value); column.Hooks.AfterSelect(column, ref value, true); } - var propertyInfo = column.PropertyInfo; - if (propertyInfo is not null) + + if (dynamicProperty is not null) { - propertyInfo.SetValue(result, value); + dynamicProperty.SetValue(d, value); } } - return result; - }); + return d; + }).ToList(); + } + + public virtual int SelectCount(IDbConnection dbConnection, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) + { + var (sql, parameters) = GenerateSelectSql("COUNT(*)", where, orderBy, skip, limit); + return dbConnection.QuerySingle(sql, parameters); + + } + + public virtual TResult MapDynamicTo(dynamic d) + { + var result = Activator.CreateInstance(); + + Type dynamicType = d.GetType(); + Type resultType = typeof(TResult); + + foreach (var column in ColumnInfos) + { + var dynamicProperty = dynamicType.GetProperty(column.ColumnName); + // TODO: Maybe we can do better to get result property in case ColumnName is set to another value. + var resultProperty = resultType.GetProperty(column.ColumnName); + if (dynamicProperty is not null && resultProperty is not null) + { + resultProperty.SetValue(result, dynamicProperty.GetValue(d)); + } + } + + return result; + } + + /// + /// Select and call hooks. + /// + public virtual List Select(IDbConnection dbConnection, string? what, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) + { + List queryResult = SelectDynamic(dbConnection, what, where, orderBy, skip, limit).ToList(); + + return queryResult.Select(MapDynamicTo).ToList(); } + // TODO: Continue here. /// /// Insert a entity and call hooks. /// -- cgit v1.2.3