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
|
|
|
|
|
{
|
|
|
|
|
public LambadaQueryBuilder()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2017-03-05 16:18:49 +08:00
|
|
|
|
|
|
|
|
|
private List<SugarParameter> _QueryPars;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
private List<JoinQueryInfo> _JoinQueryInfos;
|
|
|
|
|
private List<string> _WhereInfos;
|
|
|
|
|
private string _TableNameString;
|
|
|
|
|
|
|
|
|
|
public StringBuilder Sql { get; set; }
|
2017-03-05 16:18:49 +08:00
|
|
|
|
public SqlSugarClient Context { get; set; }
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
2017-03-05 16:18:49 +08:00
|
|
|
|
public ISqlBuilder Builder { get; set; }
|
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; }
|
|
|
|
|
public ResolveExpressType ResolveType { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual string SqlTemplate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "SELECT {0} FROM {1} {2}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual string JoinTemplate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return " {0} JOIN {1} {2} ON {3} ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual string GetTableNameString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-03-08 14:01:41 +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-03-08 14:01:41 +08:00
|
|
|
|
|
|
|
|
|
public virtual string TableShortName { get; set; }
|
|
|
|
|
|
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;
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
if (ResolveType == ResolveExpressType.SelectMultiple) {
|
|
|
|
|
this.SelectCacheKey = this.SelectCacheKey+string.Join("-",this._JoinQueryInfos.Select(it => it.TableName));
|
|
|
|
|
}
|
|
|
|
|
return reval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual string GetSelectValueByExpression()
|
|
|
|
|
{
|
|
|
|
|
var expression = this.SelectValue as Expression;
|
|
|
|
|
ILambdaExpressions resolveExpress = this.Context.LambdaExpressions;
|
|
|
|
|
var isSingle= Builder.LambadaQueryBuilder.JoinQueryInfos.IsValuable();
|
|
|
|
|
resolveExpress.Resolve(expression, ResolveType);
|
|
|
|
|
this.QueryPars.AddRange(resolveExpress.Parameters);
|
|
|
|
|
var reval= resolveExpress.Result.GetResultString();
|
|
|
|
|
this.SelectCacheKey = reval;
|
|
|
|
|
resolveExpress.Clear();
|
|
|
|
|
return reval;
|
|
|
|
|
}
|
|
|
|
|
public virtual string GetSelectValueByString()
|
|
|
|
|
{
|
|
|
|
|
string reval;
|
|
|
|
|
if (this.SelectValue.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
string pre = null;
|
|
|
|
|
if (this.JoinQueryInfos.IsValuable() && this.JoinQueryInfos.Any(it => it.PreShortName.IsValuable()))
|
|
|
|
|
{
|
|
|
|
|
pre = Builder.GetTranslationColumnName(this.JoinQueryInfos.Single(it => it.PreShortName.IsValuable()).PreShortName) + ".";
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2017-03-05 16:18:49 +08:00
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public virtual string GetWhereValueString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (this.WhereInfos == null) return null;
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-03-05 16:18:49 +08:00
|
|
|
|
return " WHERE " + 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)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string ToSqlString()
|
|
|
|
|
{
|
|
|
|
|
Sql = new StringBuilder();
|
|
|
|
|
var tableString = GetTableNameString;
|
2017-03-05 16:18:49 +08:00
|
|
|
|
if (this.JoinQueryInfos.IsValuable())
|
|
|
|
|
{
|
2017-01-07 21:54:51 +08:00
|
|
|
|
tableString = tableString + " " + GetJoinValueString;
|
|
|
|
|
}
|
2017-03-05 16:18:49 +08:00
|
|
|
|
Sql.AppendFormat(SqlTemplate, GetSelectValue, tableString, GetWhereValueString);
|
2017-01-07 21:54:51 +08:00
|
|
|
|
return Sql.ToString();
|
|
|
|
|
}
|
|
|
|
|
public virtual string ToJoinString(JoinQueryInfo joinInfo)
|
|
|
|
|
{
|
|
|
|
|
return string.Format(
|
|
|
|
|
this.JoinTemplate,
|
2017-03-05 16:18:49 +08:00
|
|
|
|
joinInfo.JoinIndex == 1 ? (joinInfo.PreShortName + " " + joinInfo.JoinType.ToString() + " ") : (joinInfo.JoinType.ToString() + " JOIN "),
|
2017-01-07 21:54:51 +08:00
|
|
|
|
joinInfo.TableName,
|
2017-03-08 14:01:41 +08:00
|
|
|
|
joinInfo.ShortName + " " + joinInfo.TableWithString,
|
2017-01-07 21:54:51 +08:00
|
|
|
|
joinInfo.JoinWhere);
|
|
|
|
|
}
|
|
|
|
|
public virtual List<string> WhereInfos
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_WhereInfos = PubMethod.IsNullReturnNew(_WhereInfos);
|
|
|
|
|
return _WhereInfos;
|
|
|
|
|
}
|
|
|
|
|
set { _WhereInfos = value; }
|
|
|
|
|
}
|
2017-03-05 16:18:49 +08:00
|
|
|
|
|
|
|
|
|
public virtual List<SugarParameter> QueryPars
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_QueryPars = PubMethod.IsNullReturnNew(_QueryPars);
|
|
|
|
|
return _QueryPars;
|
|
|
|
|
}
|
|
|
|
|
set { _QueryPars = value; }
|
|
|
|
|
}
|
|
|
|
|
public virtual List<JoinQueryInfo> JoinQueryInfos
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_JoinQueryInfos = PubMethod.IsNullReturnNew(_JoinQueryInfos);
|
|
|
|
|
return _JoinQueryInfos;
|
|
|
|
|
}
|
|
|
|
|
set { _JoinQueryInfos = value; }
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|