/// <summary> /// 追加列名 /// </summary> /// <param name="aliases">表别名</param> /// <param name="expression">列名表达式</param> /// <returns>返回解析到的表别名</returns> public string AppendMember(TableAliasCache aliases, Expression expression) { Expression exp = expression; LambdaExpression lambdaExpression = exp as LambdaExpression; if (lambdaExpression != null) { exp = lambdaExpression.Body; } MemberExpression memExp = exp as MemberExpression; if (expression.NodeType == ExpressionType.Constant || memExp.Expression.NodeType == ExpressionType.Constant) { PartialVisitor visitor = new PartialVisitor(); var eval = visitor.Eval(memExp ?? expression); string value = eval.NodeType == ExpressionType.Constant ? ExpressionVisitorBase.GetSqlValue((eval as ConstantExpression).Value) : string.Empty; _builder.Append(value); return(value); } else { string alias = aliases == null ? null : aliases.GetTableAlias(memExp); this.AppendMember(alias, memExp.Member.Name); return(alias); } }
//private string GetSqlValue(string value, MemberExpression node = null) //{ // if (value == null) return "NULL"; // MemberExpression memberExp = node; // // make sure always visit ConstantExpression Last ******* // //if (memberExp == null) // //{ // // while (_stcMember.Count > 0) // // { // // var temp = _stcMember.Pop(); // // if (memberExp == null) memberExp = temp; // // } // //} // //bool isNv = true; // //if (memberExp != null) // //{ // // //DbType from memberInfo // // var rInfo = TypeRuntimeInfoCache.GetRuntimeInfo(memberExp.Member.DeclaringType); // // var wrapper = rInfo.GetWrapper(memberExp.Member.Name) as MemberAccessWrapper; // // if (wrapper != null && wrapper.Column != null) // // { // // isNv = wrapper.Column.DbType == DbType.String || wrapper.Column.DbType == DbType.StringFixedLength; // // } // //} // //// N'信息科技' 或者 'Google' // //return string.Format("{0}'{1}'", isNv ? "N" : string.Empty, value.ToString().Replace("'", "''")); //} private static string GetSqlValue(IEnumerable value, MemberExpression node = null) { if (value == null) { return("NULL"); } MemberExpression memberExp = node; var iterator = value.GetEnumerator(); List <string> stack = new List <string>(); while (iterator.MoveNext()) { stack.Add(ExpressionVisitorBase.GetSqlValue(iterator.Current, memberExp)); } // =>a,b,c string sql = string.Join(",", stack); return(sql); }
/// <summary> /// 取值对应的sql片断 /// </summary> public static string GetSqlValue(object value, MemberExpression node = null) { if (value == null) { return("NULL"); } Type type = value.GetType(); if (type == typeof(string)) { return(string.Format("N'{0}'", value.ToString().Replace("'", "''"))); } //return this.GetSqlValue(value as string, node); if (type == typeof(Guid)) { return(string.Format("'{0}'", value)); } if (type == typeof(DateTime)) { return(string.Format("'{0}'", value)); } if (type == typeof(bool)) { return(Convert.ToBoolean(value) ? "1" : "0"); } if (typeof(IEnumerable).IsAssignableFrom(type)) { return(ExpressionVisitorBase.GetSqlValue(value as IEnumerable, node)); } return(value.ToString()); }
/// <summary> /// 获取 Length 属性对应的SQL函数 /// </summary> /// <param name="node">字段或者属性节点</param> /// <param name="visitorBase">访问器</param> /// <returns></returns> public abstract string GetLenFuncName(MemberExpression node, ExpressionVisitorBase visitor);
/// <summary> /// 访问表示字段或者属性的属性的节点 a.Name.Length /// </summary> /// <param name="node">字段或者属性节点</param> /// <param name="visitorBase">访问器</param> /// <returns></returns> public abstract Expression VisitMemberMember(MemberExpression node, ExpressionVisitorBase visitor);
/// <summary> /// 访问表示 null 合并运算的节点 a ?? b /// </summary> /// <param name="b">二元表达式节点</param> /// <param name="visitorBase">访问器</param> /// <returns></returns> public abstract Expression VisitCoalesce(BinaryExpression b, ExpressionVisitorBase visitor);
/// <summary> /// 访问表示 null 判断运算的节点 a.Name == null /// </summary> /// <param name="b">二元表达式节点</param> /// <param name="visitorBase">访问器</param> /// <returns></returns> public abstract Expression VisitEqualNull(BinaryExpression b, ExpressionVisitorBase visitor);
/// <summary> /// 访问表示方法调用的节点 /// </summary> /// <param name="node">方法调用节点</param> /// <param name="visitorBase">访问器</param> /// <returns></returns> public abstract Expression VisitMethodCall(MethodCallExpression node, ExpressionVisitorBase visitor);