123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881 |
- using Ant.Core;
- using Ant.Data;
- using Ant.DbExpressions;
- using Ant.ORM;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace Ant.SQLite
- {
- partial class SqlGenerator : DbExpressionVisitor<DbExpression>
- {
- public const string ParameterPrefix = "@P_";
- internal ISqlBuilder _sqlBuilder = new SqlBuilder();
- List<DbParam> _parameters = new List<DbParam>();
- static readonly Dictionary<string, Action<DbMethodCallExpression, SqlGenerator>> MethodHandlers = InitMethodHandlers();
- static readonly Dictionary<string, Action<DbAggregateExpression, SqlGenerator>> AggregateHandlers = InitAggregateHandlers();
- static readonly Dictionary<MethodInfo, Action<DbBinaryExpression, SqlGenerator>> BinaryWithMethodHandlers = InitBinaryWithMethodHandlers();
- static readonly Dictionary<Type, string> CastTypeMap = null;
- static readonly List<string> CacheParameterNames = null;
- static SqlGenerator()
- {
- Dictionary<Type, string> castTypeMap = new Dictionary<Type, string>();
- castTypeMap.Add(typeof(string), "TEXT");
- castTypeMap.Add(typeof(byte), "INTEGER");
- castTypeMap.Add(typeof(Int16), "INTEGER");
- castTypeMap.Add(typeof(int), "INTEGER");
- castTypeMap.Add(typeof(long), "INTEGER");
- //castTypeMap.Add(typeof(decimal), "DECIMAL(19,0)");//I think this will be a bug.
- castTypeMap.Add(typeof(double), "REAL");
- castTypeMap.Add(typeof(float), "REAL");
- castTypeMap.Add(typeof(bool), "INTEGER");
- //castTypeMap.Add(typeof(DateTime), "DATETIME");
- //castTypeMap.Add(typeof(Guid), "UNIQUEIDENTIFIER");
- CastTypeMap = Utils.Clone(castTypeMap);
- int cacheParameterNameCount = 2 * 12;
- List<string> cacheParameterNames = new List<string>(cacheParameterNameCount);
- for (int i = 0; i < cacheParameterNameCount; i++)
- {
- string paramName = ParameterPrefix + i.ToString();
- cacheParameterNames.Add(paramName);
- }
- CacheParameterNames = cacheParameterNames;
- }
- public ISqlBuilder SqlBuilder { get { return this._sqlBuilder; } }
- public List<DbParam> Parameters { get { return this._parameters; } }
- public static SqlGenerator CreateInstance()
- {
- return new SqlGenerator();
- }
- public override DbExpression Visit(DbEqualExpression exp)
- {
- DbExpression left = exp.Left;
- DbExpression right = exp.Right;
- left = DbExpressionExtensions.ParseDbExpression(left);
- right = DbExpressionExtensions.ParseDbExpression(right);
- //明确 left right 其中一边一定为 null
- if (DbExpressionExtensions.AffirmExpressionRetValueIsNull(right))
- {
- left.Accept(this);
- this._sqlBuilder.Append(" IS NULL");
- return exp;
- }
- if (DbExpressionExtensions.AffirmExpressionRetValueIsNull(left))
- {
- right.Accept(this);
- this._sqlBuilder.Append(" IS NULL");
- return exp;
- }
- left.Accept(this);
- this._sqlBuilder.Append(" = ");
- right.Accept(this);
- return exp;
- }
- public override DbExpression Visit(DbNotEqualExpression exp)
- {
- DbExpression left = exp.Left;
- DbExpression right = exp.Right;
- left = DbExpressionExtensions.ParseDbExpression(left);
- right = DbExpressionExtensions.ParseDbExpression(right);
- //明确 left right 其中一边一定为 null
- if (DbExpressionExtensions.AffirmExpressionRetValueIsNull(right))
- {
- left.Accept(this);
- this._sqlBuilder.Append(" IS NOT NULL");
- return exp;
- }
- if (DbExpressionExtensions.AffirmExpressionRetValueIsNull(left))
- {
- right.Accept(this);
- this._sqlBuilder.Append(" IS NOT NULL");
- return exp;
- }
- left.Accept(this);
- this._sqlBuilder.Append(" <> ");
- right.Accept(this);
- return exp;
- }
- public override DbExpression Visit(DbNotExpression exp)
- {
- this._sqlBuilder.Append("NOT ");
- this._sqlBuilder.Append("(");
- exp.Operand.Accept(this);
- this._sqlBuilder.Append(")");
- return exp;
- }
- public override DbExpression Visit(DbAndExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " & ");
- return exp;
- }
- public override DbExpression Visit(DbAndAlsoExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " AND ");
- return exp;
- }
- public override DbExpression Visit(DbOrExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " | ");
- return exp;
- }
- public override DbExpression Visit(DbOrElseExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " OR ");
- return exp;
- }
- // +
- public override DbExpression Visit(DbAddExpression exp)
- {
- MethodInfo method = exp.Method;
- if (method != null)
- {
- Action<DbBinaryExpression, SqlGenerator> handler;
- if (BinaryWithMethodHandlers.TryGetValue(method, out handler))
- {
- handler(exp, this);
- return exp;
- }
- throw UtilExceptions.NotSupportedMethod(exp.Method);
- }
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " + ");
- return exp;
- }
- // -
- public override DbExpression Visit(DbSubtractExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " - ");
- return exp;
- }
- // *
- public override DbExpression Visit(DbMultiplyExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " * ");
- return exp;
- }
- // /
- public override DbExpression Visit(DbDivideExpression exp)
- {
- Stack<DbExpression> operands = GatherBinaryExpressionOperand(exp);
- this.ConcatOperands(operands, " / ");
- return exp;
- }
- // <
- public override DbExpression Visit(DbLessThanExpression exp)
- {
- exp.Left.Accept(this);
- this._sqlBuilder.Append(" < ");
- exp.Right.Accept(this);
- return exp;
- }
- // <=
- public override DbExpression Visit(DbLessThanOrEqualExpression exp)
- {
- exp.Left.Accept(this);
- this._sqlBuilder.Append(" <= ");
- exp.Right.Accept(this);
- return exp;
- }
- // >
- public override DbExpression Visit(DbGreaterThanExpression exp)
- {
- exp.Left.Accept(this);
- this._sqlBuilder.Append(" > ");
- exp.Right.Accept(this);
- return exp;
- }
- // >=
- public override DbExpression Visit(DbGreaterThanOrEqualExpression exp)
- {
- exp.Left.Accept(this);
- this._sqlBuilder.Append(" >= ");
- exp.Right.Accept(this);
- return exp;
- }
- public override DbExpression Visit(DbAggregateExpression exp)
- {
- Action<DbAggregateExpression, SqlGenerator> aggregateHandler;
- if (!AggregateHandlers.TryGetValue(exp.Method.Name, out aggregateHandler))
- {
- throw UtilExceptions.NotSupportedMethod(exp.Method);
- }
- aggregateHandler(exp, this);
- return exp;
- }
- public override DbExpression Visit(DbTableExpression exp)
- {
- this.QuoteName(exp.Table.Name);
- return exp;
- }
- public override DbExpression Visit(DbColumnAccessExpression exp)
- {
- this.QuoteName(exp.Table.Name);
- this._sqlBuilder.Append(".");
- this.QuoteName(exp.Column.Name);
- return exp;
- }
- public override DbExpression Visit(DbFromTableExpression exp)
- {
- this.AppendTableSegment(exp.Table);
- this.VisitDbJoinTableExpressions(exp.JoinTables);
- return exp;
- }
- public override DbExpression Visit(DbJoinTableExpression exp)
- {
- DbJoinTableExpression joinTablePart = exp;
- string joinString = null;
- if (joinTablePart.JoinType == JoinType.InnerJoin)
- {
- joinString = " INNER JOIN ";
- }
- else if (joinTablePart.JoinType == JoinType.LeftJoin)
- {
- joinString = " LEFT JOIN ";
- }
- else
- throw new NotSupportedException("JoinType: " + joinTablePart.JoinType);
- this._sqlBuilder.Append(joinString);
- this.AppendTableSegment(joinTablePart.Table);
- this._sqlBuilder.Append(" ON ");
- joinTablePart.Condition.Accept(this);
- this.VisitDbJoinTableExpressions(joinTablePart.JoinTables);
- return exp;
- }
- public override DbExpression Visit(DbSubQueryExpression exp)
- {
- this._sqlBuilder.Append("(");
- exp.SqlQuery.Accept(this);
- this._sqlBuilder.Append(")");
- return exp;
- }
- public override DbExpression Visit(DbSqlQueryExpression exp)
- {
- this.BuildGeneralSql(exp);
- return exp;
- }
- public override DbExpression Visit(DbInsertExpression exp)
- {
- this._sqlBuilder.Append("INSERT INTO ");
- this.QuoteName(exp.Table.Name);
- this._sqlBuilder.Append("(");
- bool first = true;
- foreach (var item in exp.InsertColumns)
- {
- if (first)
- first = false;
- else
- {
- this._sqlBuilder.Append(",");
- }
- this.QuoteName(item.Key.Name);
- }
- this._sqlBuilder.Append(")");
- this._sqlBuilder.Append(" VALUES(");
- first = true;
- foreach (var item in exp.InsertColumns)
- {
- if (first)
- first = false;
- else
- {
- this._sqlBuilder.Append(",");
- }
- item.Value.Accept(this);
- }
- this._sqlBuilder.Append(")");
- return exp;
- }
- public override DbExpression Visit(DbUpdateExpression exp)
- {
- this._sqlBuilder.Append("UPDATE ");
- this.QuoteName(exp.Table.Name);
- this._sqlBuilder.Append(" SET ");
- bool first = true;
- foreach (var item in exp.UpdateColumns)
- {
- if (first)
- first = false;
- else
- this._sqlBuilder.Append(",");
- this.QuoteName(item.Key.Name);
- this._sqlBuilder.Append("=");
- item.Value.Accept(this);
- }
- this.BuildWhereState(exp.Condition);
- return exp;
- }
- public override DbExpression Visit(DbDeleteExpression exp)
- {
- this._sqlBuilder.Append("DELETE FROM ");
- this.QuoteName(exp.Table.Name);
- this.BuildWhereState(exp.Condition);
- return exp;
- }
- public override DbExpression Visit(DbCaseWhenExpression exp)
- {
- this._sqlBuilder.Append("CASE");
- foreach (var whenThen in exp.WhenThenPairs)
- {
- this._sqlBuilder.Append(" WHEN ");
- whenThen.When.Accept(this);
- this._sqlBuilder.Append(" THEN ");
- whenThen.Then.Accept(this);
- }
- this._sqlBuilder.Append(" ELSE ");
- exp.Else.Accept(this);
- this._sqlBuilder.Append(" END");
- return exp;
- }
- public override DbExpression Visit(DbConvertExpression exp)
- {
- DbExpression stripedExp = DbExpressionHelper.StripInvalidConvert(exp);
- if (stripedExp.NodeType != DbExpressionType.Convert)
- {
- stripedExp.Accept(this);
- return exp;
- }
- exp = (DbConvertExpression)stripedExp;
- string dbTypeString;
- if (TryGetCastTargetDbTypeString(exp.Operand.Type, exp.Type, out dbTypeString, false))
- {
- this.BuildCastState(exp.Operand, dbTypeString);
- }
- else
- {
- Type targetUnType = Utils.GetUnderlyingType(exp.Type);
- if (targetUnType == UtilConstants.TypeOfDateTime)
- {
- /* DATETIME('2016-08-06 09:01:24') */
- this._sqlBuilder.Append("DATETIME(");
- exp.Operand.Accept(this);
- this._sqlBuilder.Append(")");
- }
- else
- exp.Operand.Accept(this);
- }
- return exp;
- }
- public override DbExpression Visit(DbMethodCallExpression exp)
- {
- Action<DbMethodCallExpression, SqlGenerator> methodHandler;
- if (!MethodHandlers.TryGetValue(exp.Method.Name, out methodHandler))
- {
- throw UtilExceptions.NotSupportedMethod(exp.Method);
- }
- methodHandler(exp, this);
- return exp;
- }
- public override DbExpression Visit(DbMemberExpression exp)
- {
- MemberInfo member = exp.Member;
- if (member.DeclaringType == UtilConstants.TypeOfDateTime)
- {
- if (member == UtilConstants.PropertyInfo_DateTime_Now)
- {
- this._sqlBuilder.Append("DATETIME('NOW','LOCALTIME')");
- return exp;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_UtcNow)
- {
- this._sqlBuilder.Append("DATETIME()");
- return exp;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Today)
- {
- this._sqlBuilder.Append("DATE('NOW','LOCALTIME')");
- return exp;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Date)
- {
- this._sqlBuilder.Append("DATETIME(DATE(");
- exp.Expression.Accept(this);
- this._sqlBuilder.Append("))");
- return exp;
- }
- if (IsDbFunction_DATEPART(exp))
- {
- return exp;
- }
- }
- DbParameterExpression newExp;
- if (DbExpressionExtensions.TryParseToParameterExpression(exp, out newExp))
- {
- return newExp.Accept(this);
- }
- if (member.Name == "Length" && member.DeclaringType == UtilConstants.TypeOfString)
- {
- this._sqlBuilder.Append("LENGTH(");
- exp.Expression.Accept(this);
- this._sqlBuilder.Append(")");
- return exp;
- }
- else if (member.Name == "Value" && Utils.IsNullable(exp.Expression.Type))
- {
- exp.Expression.Accept(this);
- return exp;
- }
- throw new NotSupportedException(string.Format("'{0}.{1}' is not supported.", member.DeclaringType.FullName, member.Name));
- }
- public override DbExpression Visit(DbConstantExpression exp)
- {
- if (exp.Value == null || exp.Value == DBNull.Value)
- {
- this._sqlBuilder.Append("NULL");
- return exp;
- }
- var objType = exp.Value.GetType();
- if (objType == UtilConstants.TypeOfBoolean)
- {
- this._sqlBuilder.Append(((bool)exp.Value) ? "1" : "0");
- return exp;
- }
- else if (objType == UtilConstants.TypeOfString)
- {
- this._sqlBuilder.Append("'", exp.Value, "'");
- return exp;
- }
- else if (objType.IsEnum)
- {
- this._sqlBuilder.Append(((int)exp.Value).ToString());
- return exp;
- }
- this._sqlBuilder.Append(exp.Value);
- return exp;
- }
- public override DbExpression Visit(DbParameterExpression exp)
- {
- object paramValue = exp.Value;
- Type paramType = exp.Type;
- if (paramType.IsEnum)
- {
- paramType = UtilConstants.TypeOfInt32;
- if (paramValue != null)
- {
- paramValue = (int)paramValue;
- }
- }
- if (paramValue == null)
- paramValue = DBNull.Value;
- DbParam p;
- if (paramValue == DBNull.Value)
- {
- p = this._parameters.Where(a => Utils.AreEqual(a.Value, paramValue) && a.Type == paramType).FirstOrDefault();
- }
- else
- p = this._parameters.Where(a => Utils.AreEqual(a.Value, paramValue)).FirstOrDefault();
- if (p != null)
- {
- this._sqlBuilder.Append(p.Name);
- return exp;
- }
- string paramName = GenParameterName(this._parameters.Count);
- p = DbParam.Create(paramName, paramValue, paramType);
- if (paramValue.GetType() == UtilConstants.TypeOfString)
- {
- if (((string)paramValue).Length <= 4000)
- p.Size = 4000;
- }
- this._parameters.Add(p);
- this._sqlBuilder.Append(paramName);
- return exp;
- }
- void AppendTableSegment(DbTableSegment seg)
- {
- seg.Body.Accept(this);
- this._sqlBuilder.Append(" AS ");
- this.QuoteName(seg.Alias);
- }
- internal void AppendColumnSegment(DbColumnSegment seg)
- {
- seg.Body.Accept(this);
- this._sqlBuilder.Append(" AS ");
- this.QuoteName(seg.Alias);
- }
- void AppendOrdering(DbOrdering ordering)
- {
- if (ordering.OrderType == OrderType.Asc)
- {
- ordering.Expression.Accept(this);
- this._sqlBuilder.Append(" ASC");
- return;
- }
- else if (ordering.OrderType == OrderType.Desc)
- {
- ordering.Expression.Accept(this);
- this._sqlBuilder.Append(" DESC");
- return;
- }
- throw new NotSupportedException("OrderType: " + ordering.OrderType);
- }
- void VisitDbJoinTableExpressions(List<DbJoinTableExpression> tables)
- {
- foreach (var table in tables)
- {
- table.Accept(this);
- }
- }
- void BuildGeneralSql(DbSqlQueryExpression exp)
- {
- this._sqlBuilder.Append("SELECT ");
- List<DbColumnSegment> columns = exp.ColumnSegments;
- for (int i = 0; i < columns.Count; i++)
- {
- DbColumnSegment column = columns[i];
- if (i > 0)
- this._sqlBuilder.Append(",");
- this.AppendColumnSegment(column);
- }
- this._sqlBuilder.Append(" FROM ");
- exp.Table.Accept(this);
- this.BuildWhereState(exp.Condition);
- this.BuildGroupState(exp);
- this.BuildOrderState(exp.Orderings);
- if (exp.SkipCount == null && exp.TakeCount == null)
- return;
- int skipCount = exp.SkipCount ?? 0;
- long takeCount = long.MaxValue;
- if (exp.TakeCount != null)
- takeCount = exp.TakeCount.Value;
- this._sqlBuilder.Append(" LIMIT ", takeCount.ToString(), " OFFSET ", skipCount.ToString());
- }
- void BuildWhereState(DbExpression whereExpression)
- {
- if (whereExpression != null)
- {
- this._sqlBuilder.Append(" WHERE ");
- whereExpression.Accept(this);
- }
- }
- void BuildOrderState(List<DbOrdering> orderings)
- {
- if (orderings.Count > 0)
- {
- this._sqlBuilder.Append(" ORDER BY ");
- this.ConcatOrderings(orderings);
- }
- }
- void ConcatOrderings(List<DbOrdering> orderings)
- {
- for (int i = 0; i < orderings.Count; i++)
- {
- if (i > 0)
- {
- this._sqlBuilder.Append(",");
- }
- this.AppendOrdering(orderings[i]);
- }
- }
- void BuildGroupState(DbSqlQueryExpression exp)
- {
- var groupSegments = exp.GroupSegments;
- if (groupSegments.Count == 0)
- return;
- this._sqlBuilder.Append(" GROUP BY ");
- for (int i = 0; i < groupSegments.Count; i++)
- {
- if (i > 0)
- this._sqlBuilder.Append(",");
- groupSegments[i].Accept(this);
- }
- if (exp.HavingCondition != null)
- {
- this._sqlBuilder.Append(" HAVING ");
- exp.HavingCondition.Accept(this);
- }
- }
- void ConcatOperands(IEnumerable<DbExpression> operands, string connector)
- {
- this._sqlBuilder.Append("(");
- bool first = true;
- foreach (DbExpression operand in operands)
- {
- if (first)
- first = false;
- else
- this._sqlBuilder.Append(connector);
- operand.Accept(this);
- }
- this._sqlBuilder.Append(")");
- return;
- }
- void QuoteName(string name)
- {
- if (string.IsNullOrEmpty(name))
- throw new ArgumentException("name");
- this._sqlBuilder.Append("[", name, "]");
- }
- void BuildCastState(DbExpression castExp, string targetDbTypeString)
- {
- this._sqlBuilder.Append("CAST(");
- castExp.Accept(this);
- this._sqlBuilder.Append(" AS ", targetDbTypeString, ")");
- }
- bool IsDbFunction_DATEPART(DbMemberExpression exp)
- {
- MemberInfo member = exp.Member;
- if (member == UtilConstants.PropertyInfo_DateTime_Year)
- {
- DbFunction_DATEPART(this, "Y", exp.Expression);
- return true;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Month)
- {
- DbFunction_DATEPART(this, "m", exp.Expression);
- return true;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Day)
- {
- DbFunction_DATEPART(this, "d", exp.Expression);
- return true;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Hour)
- {
- DbFunction_DATEPART(this, "H", exp.Expression);
- return true;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Minute)
- {
- DbFunction_DATEPART(this, "M", exp.Expression);
- return true;
- }
- if (member == UtilConstants.PropertyInfo_DateTime_Second)
- {
- DbFunction_DATEPART(this, "S", exp.Expression);
- return true;
- }
- /* SQLite is not supports MILLISECOND */
- if (member == UtilConstants.PropertyInfo_DateTime_DayOfWeek)
- {
- DbFunction_DATEPART(this, "w", exp.Expression);
- return true;
- }
- return false;
- }
- #region BinaryWithMethodHandlers
- static Dictionary<MethodInfo, Action<DbBinaryExpression, SqlGenerator>> InitBinaryWithMethodHandlers()
- {
- var binaryWithMethodHandlers = new Dictionary<MethodInfo, Action<DbBinaryExpression, SqlGenerator>>();
- binaryWithMethodHandlers.Add(UtilConstants.MethodInfo_String_Concat_String_String, StringConcat);
- binaryWithMethodHandlers.Add(UtilConstants.MethodInfo_String_Concat_Object_Object, StringConcat);
- var ret = Utils.Clone(binaryWithMethodHandlers);
- return ret;
- }
- static void StringConcat(DbBinaryExpression exp, SqlGenerator generator)
- {
- MethodInfo method = exp.Method;
- List<DbExpression> operands = new List<DbExpression>();
- operands.Add(exp.Right);
- DbExpression left = exp.Left;
- DbAddExpression e = null;
- while ((e = (left as DbAddExpression)) != null && (e.Method == UtilConstants.MethodInfo_String_Concat_String_String || e.Method == UtilConstants.MethodInfo_String_Concat_Object_Object))
- {
- operands.Add(e.Right);
- left = e.Left;
- }
- operands.Add(left);
- DbExpression whenExp = null;
- List<DbExpression> operandExps = new List<DbExpression>(operands.Count);
- for (int i = operands.Count - 1; i >= 0; i--)
- {
- DbExpression operand = operands[i];
- DbExpression opBody = operand;
- if (opBody.Type != UtilConstants.TypeOfString)
- {
- // 需要 cast type
- opBody = DbExpression.Convert(opBody, UtilConstants.TypeOfString);
- }
- DbExpression equalNullExp = DbExpression.Equal(opBody, UtilConstants.DbConstant_Null_String);
- if (whenExp == null)
- whenExp = equalNullExp;
- else
- whenExp = DbExpression.AndAlso(whenExp, equalNullExp);
- DbExpression thenExp = DbConstantExpression.StringEmpty;
- DbCaseWhenExpression.WhenThenExpressionPair whenThenPair = new DbCaseWhenExpression.WhenThenExpressionPair(equalNullExp, thenExp);
- List<DbCaseWhenExpression.WhenThenExpressionPair> whenThenExps = new List<DbCaseWhenExpression.WhenThenExpressionPair>(1);
- whenThenExps.Add(whenThenPair);
- DbExpression elseExp = opBody;
- DbCaseWhenExpression caseWhenExpression = DbExpression.CaseWhen(whenThenExps, elseExp, UtilConstants.TypeOfString);
- operandExps.Add(caseWhenExpression);
- }
- generator._sqlBuilder.Append("CASE", " WHEN ");
- whenExp.Accept(generator);
- generator._sqlBuilder.Append(" THEN ");
- DbConstantExpression.Null.Accept(generator);
- generator._sqlBuilder.Append(" ELSE ");
- generator._sqlBuilder.Append("(");
- for (int i = 0; i < operandExps.Count; i++)
- {
- if (i > 0)
- generator._sqlBuilder.Append(" || ");
- operandExps[i].Accept(generator);
- }
- generator._sqlBuilder.Append(")");
- generator._sqlBuilder.Append(" END");
- }
- #endregion
- }
- }
|