2017-01-07 21:54:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Linq;
|
2017-03-05 16:18:49 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
namespace SqlSugar
|
|
|
|
|
{
|
|
|
|
|
public abstract class LambadaQueryBuilder : IDMLBuilder
|
|
|
|
|
{
|
2017-04-26 01:52:29 +08:00
|
|
|
|
|
|
|
|
|
public LambadaQueryBuilder()
|
|
|
|
|
{
|
|
|
|
|
this.QueryPars = new List<SugarParameter>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#region Private Fileds
|
2017-01-07 21:54:51 +08:00
|
|
|
|
private List<JoinQueryInfo> _JoinQueryInfos;
|
|
|
|
|
private List<string> _WhereInfos;
|
2017-04-26 01:52:29 +08:00
|
|
|
|
private string _TableNameString;
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#endregion
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#region Service object
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public StringBuilder Sql { get; set; }
|
2017-03-05 16:18:49 +08:00
|
|
|
|
public SqlSugarClient Context { get; set; }
|
2017-04-09 07:30:15 +08:00
|
|
|
|
public ILambdaExpressions LambdaExpressions { get; set; }
|
2017-04-26 01:52:29 +08:00
|
|
|
|
public ISqlBuilder Builder { get; set; }
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#endregion
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#region Splicing basic
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public int? Skip { get; set; }
|
|
|
|
|
public int? Take { get; set; }
|
|
|
|
|
public string OrderByValue { get; set; }
|
2017-03-05 16:18:49 +08:00
|
|
|
|
public object SelectValue { get; set; }
|
|
|
|
|
public string SelectCacheKey { get; set; }
|
2017-03-08 14:01:41 +08:00
|
|
|
|
public string EntityName { get; set; }
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public string TableWithString { get; set; }
|
|
|
|
|
public string GroupByValue { get; set; }
|
|
|
|
|
public int WhereIndex { get; set; }
|
|
|
|
|
public int JoinIndex { get; set; }
|
2017-04-26 01:52:29 +08:00
|
|
|
|
public virtual List<SugarParameter> QueryPars { get; set; }
|
2017-04-25 01:04:55 +08:00
|
|
|
|
public virtual List<JoinQueryInfo> JoinQueryInfos
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_JoinQueryInfos = PubMethod.IsNullReturnNew(_JoinQueryInfos);
|
|
|
|
|
return _JoinQueryInfos;
|
|
|
|
|
}
|
|
|
|
|
set { _JoinQueryInfos = value; }
|
|
|
|
|
}
|
|
|
|
|
public virtual string TableShortName { get; set; }
|
|
|
|
|
public virtual List<string> WhereInfos
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_WhereInfos = PubMethod.IsNullReturnNew(_WhereInfos);
|
|
|
|
|
return _WhereInfos;
|
|
|
|
|
}
|
|
|
|
|
set { _WhereInfos = value; }
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Lambada Type
|
|
|
|
|
public ResolveExpressType SelectType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this.IsSingle() ? ResolveExpressType.SelectSingle : ResolveExpressType.SelectMultiple;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ResolveExpressType WheretType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this.IsSingle() ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
2017-04-25 01:06:18 +08:00
|
|
|
|
#region Sql Template
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public virtual string SqlTemplate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "SELECT {0} FROM {1} {2}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual string JoinTemplate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return " {0} JOIN {1} {2} ON {3} ";
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Common Methods
|
|
|
|
|
public virtual bool IsSingle()
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
2017-04-25 01:04:55 +08:00
|
|
|
|
var isSingle = Builder.LambadaQueryBuilder.JoinQueryInfos.IsNullOrEmpty();
|
|
|
|
|
return isSingle;
|
|
|
|
|
}
|
|
|
|
|
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
|
|
|
|
|
{
|
|
|
|
|
ILambdaExpressions resolveExpress = this.LambdaExpressions;
|
2017-04-26 01:52:29 +08:00
|
|
|
|
this.LambdaExpressions.Clear();
|
2017-04-25 01:04:55 +08:00
|
|
|
|
resolveExpress.JoinQueryInfos = Builder.LambadaQueryBuilder.JoinQueryInfos;
|
|
|
|
|
resolveExpress.MappingColumns = Context.MappingColumns;
|
|
|
|
|
resolveExpress.MappingTables = Context.MappingTables;
|
|
|
|
|
resolveExpress.IgnoreComumnList = Context.IgnoreComumns;
|
|
|
|
|
resolveExpress.Resolve(expression, resolveType);
|
2017-04-26 01:52:29 +08:00
|
|
|
|
this.QueryPars = new List<SugarParameter>();
|
2017-04-25 01:04:55 +08:00
|
|
|
|
this.QueryPars.AddRange(resolveExpress.Parameters);
|
|
|
|
|
var reval = resolveExpress.Result;
|
|
|
|
|
return reval;
|
|
|
|
|
}
|
|
|
|
|
public virtual string ToSqlString()
|
|
|
|
|
{
|
|
|
|
|
Sql = new StringBuilder();
|
|
|
|
|
var tableString = GetTableNameString;
|
|
|
|
|
if (this.JoinQueryInfos.IsValuable())
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
2017-04-25 01:04:55 +08:00
|
|
|
|
tableString = tableString + " " + GetJoinValueString;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2017-04-25 01:04:55 +08:00
|
|
|
|
Sql.AppendFormat(SqlTemplate, GetSelectValue, tableString, GetWhereValueString);
|
|
|
|
|
return Sql.ToString();
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2017-04-25 01:04:55 +08:00
|
|
|
|
public virtual string ToJoinString(JoinQueryInfo joinInfo)
|
|
|
|
|
{
|
|
|
|
|
return string.Format(
|
|
|
|
|
this.JoinTemplate,
|
|
|
|
|
joinInfo.JoinType.ToString(),
|
|
|
|
|
joinInfo.TableName,
|
|
|
|
|
joinInfo.ShortName + " " + joinInfo.TableWithString,
|
|
|
|
|
joinInfo.JoinWhere);
|
|
|
|
|
}
|
|
|
|
|
public virtual void Clear()
|
|
|
|
|
{
|
|
|
|
|
this.Skip = 0;
|
|
|
|
|
this.Take = 0;
|
|
|
|
|
this.Sql = null;
|
|
|
|
|
this.WhereIndex = 0;
|
|
|
|
|
this.QueryPars = null;
|
|
|
|
|
this.GroupByValue = null;
|
|
|
|
|
this._TableNameString = null;
|
|
|
|
|
this.WhereInfos = null;
|
|
|
|
|
this.JoinQueryInfos = null;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2017-03-08 14:01:41 +08:00
|
|
|
|
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#region Get SQL Partial
|
2017-03-05 16:18:49 +08:00
|
|
|
|
public virtual string GetSelectValue
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-03-05 16:18:49 +08:00
|
|
|
|
string reval = string.Empty;
|
2017-04-25 01:04:55 +08:00
|
|
|
|
if (this.SelectValue == null || this.SelectValue is string)
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
2017-03-05 16:18:49 +08:00
|
|
|
|
reval = GetSelectValueByString();
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2017-03-05 16:18:49 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reval = GetSelectValueByExpression();
|
|
|
|
|
}
|
2017-04-25 01:04:55 +08:00
|
|
|
|
if (this.SelectType == ResolveExpressType.SelectMultiple)
|
|
|
|
|
{
|
|
|
|
|
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this._JoinQueryInfos.Select(it => it.TableName));
|
2017-03-05 16:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
return reval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual string GetSelectValueByExpression()
|
|
|
|
|
{
|
|
|
|
|
var expression = this.SelectValue as Expression;
|
2017-04-25 01:04:55 +08:00
|
|
|
|
var reval = GetExpressionValue(expression, this.SelectType).GetResultString();
|
2017-03-05 16:18:49 +08:00
|
|
|
|
this.SelectCacheKey = reval;
|
|
|
|
|
return reval;
|
|
|
|
|
}
|
|
|
|
|
public virtual string GetSelectValueByString()
|
|
|
|
|
{
|
|
|
|
|
string reval;
|
|
|
|
|
if (this.SelectValue.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
string pre = null;
|
2017-03-08 14:12:14 +08:00
|
|
|
|
if (this.JoinQueryInfos.IsValuable() && this.JoinQueryInfos.Any(it => TableShortName.IsValuable()))
|
2017-03-05 16:18:49 +08:00
|
|
|
|
{
|
2017-03-08 14:12:14 +08:00
|
|
|
|
pre = Builder.GetTranslationColumnName(TableShortName) + ".";
|
2017-03-05 16:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
reval = string.Join(",", this.Context.Database.DbMaintenance.GetColumnInfosByTableName(this.EntityName).Select(it => pre + Builder.GetTranslationColumnName(it.ColumnName)));
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2017-03-05 16:18:49 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reval = this.SelectValue.ObjToString();
|
2017-03-05 17:15:36 +08:00
|
|
|
|
this.SelectCacheKey = reval;
|
2017-03-05 16:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reval;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
public virtual string GetWhereValueString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (this.WhereInfos == null) return null;
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-04-25 01:04:55 +08:00
|
|
|
|
return string.Join(" ", this.WhereInfos);
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual string GetJoinValueString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (this.JoinQueryInfos.IsNullOrEmpty()) return null;
|
2017-03-05 16:18:49 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-07 21:54:51 +08:00
|
|
|
|
return string.Join(" ", this.JoinQueryInfos.Select(it => this.ToJoinString(it)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-25 01:04:55 +08:00
|
|
|
|
public virtual string GetTableNameString
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-04-25 01:04:55 +08:00
|
|
|
|
var result = Builder.GetTranslationTableName(EntityName) + TableWithString;
|
|
|
|
|
if (this.TableShortName.IsValuable())
|
|
|
|
|
{
|
|
|
|
|
result += " " + TableShortName;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-25 01:04:55 +08:00
|
|
|
|
#endregion
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|