Browse Source

Select <ViewModel>() Auto Fill

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
6b9b23809c
  1. 23
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  2. 16
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs
  3. 90
      Src/Asp.Net/SqlSugar/Infrastructure/Mapper.cs
  4. 2
      Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs
  5. 1
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs
  6. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

23
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -427,6 +427,21 @@ namespace SqlSugar
{
return _Select<TResult>(expression);
}
public virtual ISugarQueryable<TResult> Select<TResult>()
{
var isJoin = this.QueryBuilder.JoinExpression!=null;
if (isJoin)
{
var selectValue = new SugarMapper(this.Context).GetSelectValue<TResult>(this.QueryBuilder);
return this.Select<TResult>(selectValue);
}
else
{
return this.Select<TResult>(this.SqlBuilder.SqlSelectAll);
}
}
public virtual ISugarQueryable<TResult> Select<TResult>(string selectValue)
{
var result = InstanceFactory.GetQueryable<TResult>(this.Context.CurrentConnectionConfig);
@ -988,7 +1003,7 @@ namespace SqlSugar
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
var result= Min<TResult>(lamResult.GetResultString());
var result = Min<TResult>(lamResult.GetResultString());
QueryBuilder.SelectValue = null;
return result;
}
@ -1002,7 +1017,7 @@ namespace SqlSugar
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
var reslut= Max<TResult>(lamResult.GetResultString());
var reslut = Max<TResult>(lamResult.GetResultString());
QueryBuilder.SelectValue = null;
return reslut;
}
@ -1010,7 +1025,7 @@ namespace SqlSugar
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
var reslut= Sum<TResult>(lamResult.GetResultString());
var reslut = Sum<TResult>(lamResult.GetResultString());
QueryBuilder.SelectValue = null;
return reslut;
}
@ -1184,7 +1199,7 @@ namespace SqlSugar
{
if (result.HasValue())
{
if (UtilMethods.GetRootBaseType(entityType).HasValue() &&UtilMethods.GetRootBaseType(entityType) == UtilConstants.ModelType)
if (UtilMethods.GetRootBaseType(entityType).HasValue() && UtilMethods.GetRootBaseType(entityType) == UtilConstants.ModelType)
{
foreach (var item in result)
{

16
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
@ -41,6 +42,8 @@ namespace SqlSugar
public object SelectValue { get; set; }
public string SelectCacheKey { get; set; }
public string EntityName { get; set; }
public Type EntityType { get; set; }
public string TableWithString { get; set; }
public string GroupByValue { get; set; }
@ -49,6 +52,7 @@ namespace SqlSugar
public int JoinIndex { get; set; }
public bool IsDisabledGobalFilter { get; set; }
public virtual List<SugarParameter> Parameters { get; set; }
public Expression JoinExpression { get; set; }
public Dictionary<string, string> EasyJoinInfos
{
get
@ -328,6 +332,17 @@ namespace SqlSugar
return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize+1, pageIndex * pageSize);
}
public virtual string GetSelectByItems(List<KeyValuePair<string, object>> items)
{
var array = items.Select(it => {
dynamic dynamicObj = this.Context.Utilities.DeserializeObject<dynamic>(this.Context.Utilities.SerializeObject(it.Value));
var dbName =Builder.GetTranslationColumnName( (string)(dynamicObj.dbName));
var asName = Builder.GetTranslationColumnName((string)(dynamicObj.asName));
return string.Format("{0}.{1} AS {2}",it.Key,dbName,asName);
});
return string.Join(",",array);
}
public virtual string ToJoinString(JoinQueryInfo joinInfo)
{
return string.Format(
@ -485,6 +500,7 @@ namespace SqlSugar
return this.GroupByValue;
}
}
#endregion
private string GetTableName(string entityName)

90
Src/Asp.Net/SqlSugar/Infrastructure/Mapper.cs

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class SugarMapper
{
private SqlSugarClient _context;
public SugarMapper(SqlSugarClient context)
{
_context = context;
}
public string GetSelectValue<TResult>(QueryBuilder queryBuilder)
{
string result = string.Empty;
var veiwModel = _context.EntityMaintenance.GetEntityInfo<TResult>();
var exp = (queryBuilder.JoinExpression as LambdaExpression);
List<KeyValuePair<string, object>> selectItems = new List<KeyValuePair<string, object>>();
foreach (var viewColumn in veiwModel.Columns)
{
var exParsmeters = exp.Parameters.Select(it => new { shortName = it.Name, type = it.Type }).ToList();
foreach (var expPars in exParsmeters)
{
var columns = _context.EntityMaintenance.GetEntityInfo(expPars.type).Columns.Where(it => it.IsIgnore == false);
var joinModelDbColumns = columns.Select(it =>
new { asName = it.PropertyName, dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList();
var joinModelProperties = columns.Select(it =>
new { asName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList();
var joinModelDbColumnsWithType = columns.Select(it =>
new { asName = (expPars.type.Name + it.PropertyName), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList();
var joinModelPropertiesWithTye = columns.Select(it =>
new { asName = (expPars.type.Name + _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type)), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList();
var joinModelDbColumns_WithType = columns.Select(it =>
new { asName = (expPars.type.Name +"_"+ it.PropertyName), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList();
var joinModelProperties_WithTye = columns.Select(it =>
new { asName = (expPars.type.Name +"_"+ _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type)), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList();
if (joinModelDbColumns.Any(it => it.asName.Equals(viewColumn.PropertyName,StringComparison.CurrentCultureIgnoreCase)))
{
var value = joinModelDbColumns.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase));
if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase))
selectItems.Add(new KeyValuePair<string, object>(expPars.shortName,value));
break;
}
if (joinModelProperties.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)))
{
var value = joinModelProperties.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase));
if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase))
selectItems.Add(new KeyValuePair<string, object>(expPars.shortName, value));
break;
}
if (joinModelDbColumnsWithType.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)))
{
var value = joinModelDbColumnsWithType.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase));
if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase))
selectItems.Add(new KeyValuePair<string, object>(expPars.shortName, value));
break;
}
if (joinModelPropertiesWithTye.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)))
{
var value = joinModelPropertiesWithTye.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase));
if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase))
selectItems.Add(new KeyValuePair<string, object>(expPars.shortName, value));
break;
}
if (joinModelDbColumns_WithType.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)))
{
var value = joinModelDbColumns_WithType.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase));
if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase))
selectItems.Add(new KeyValuePair<string, object>(expPars.shortName, value));
break;
}
if (joinModelProperties_WithTye.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)))
{
var value = joinModelProperties_WithTye.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase));
if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase))
selectItems.Add(new KeyValuePair<string, object>(expPars.shortName, value));
break;
}
}
}
result = queryBuilder.GetSelectByItems(selectItems);
return result;
}
}
}

2
Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs

@ -315,6 +315,7 @@ namespace SqlSugar
List<SugarParameter> paramters = new List<SugarParameter>();
queryable.SqlBuilder.QueryBuilder.JoinQueryInfos = this.GetJoinInfos(queryable.SqlBuilder, joinExpression, ref paramters, ref shortName, types);
queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
queryable.SqlBuilder.QueryBuilder.JoinExpression = joinExpression;
if (paramters != null)
{
queryable.SqlBuilder.QueryBuilder.Parameters.AddRange(paramters);
@ -326,6 +327,7 @@ namespace SqlSugar
string shortName = string.Empty;
queryable.SqlBuilder.QueryBuilder.EasyJoinInfos = this.GetEasyJoinInfo(joinExpression, ref shortName, queryable.SqlBuilder, types);
queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
queryable.SqlBuilder.QueryBuilder.JoinExpression=joinExpression;
}
#endregion

1
Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

@ -74,6 +74,7 @@ namespace SqlSugar
Task<bool> AnyAsync();
ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression);
ISugarQueryable<TResult> Select<TResult>();
ISugarQueryable<TResult> Select<TResult>(string select);
ISugarQueryable<T> Select(string select);
ISugarQueryable<T> MergeTable();

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -110,6 +110,7 @@
<Compile Include="Infrastructure\DependencyManagement.cs" />
<Compile Include="ExternalServiceInterface\ISerializeService.cs" />
<Compile Include="Entities\DefaultServices.cs" />
<Compile Include="Infrastructure\Mapper.cs" />
<Compile Include="Realization\Oracle\Deleteable\OracleDeleteable.cs" />
<Compile Include="Realization\Oracle\Insertable\OracleInsertable.cs" />
<Compile Include="Realization\Oracle\Updateable\OracleUpdateable.cs" />

Loading…
Cancel
Save