Пример #1
0
 protected override System.Linq.Expressions.Expression VisitMemberAccess(System.Linq.Expressions.MemberExpression m)
 {
     if (m.Expression is System.Linq.Expressions.ParameterExpression)
     {
         string     text       = string.Empty;
         MemberInfo memberInfo = m.Member;
         if (memberInfo != null)
         {
             this._CurrentMemberInfo = memberInfo;
             this._CurrentMemberType = m.Type;
             Type         valueType = memberInfo.ReflectedType;
             DMSTableKeys key       = this.TableKeys.Where(q => q.AssemblyQualifiedName == valueType.AssemblyQualifiedName).LastOrDefault();
             if (key != null && (this.ExcuteType == DMSExcuteType.SELECT ||
                                 this.ExcuteType == DMSExcuteType.UPDATE_WHERE ||
                                 this.ExcuteType == DMSExcuteType.INSERT_SELECT))
             {
                 text = key.TableSpecialName;
                 if (this.SplitExpression.TableMapping.TokenFlag == true)
                 {
                     text = this.Provider.BuildColumnName(text);
                 }
                 this._strSql.Append(text);
                 this._strSql.Append(this.Provider.TableToken);
             }
             text = GetMemberInfoName(memberInfo.Name);
             if (this.SplitExpression.TableMapping.TokenFlag == true)
             {
                 text = this.Provider.BuildColumnName(text);
             }
             _strSql.Append(text);
         }
         return(m);
     }
     else if (m.Expression is System.Linq.Expressions.MemberExpression)
     {
         //q.UserName.Length
         string name = m.Member.Name;
         if (MemberProperties.ContainsKey(name))
         {
             this.MethodFunc(MemberProperties[name], m.Expression);
         }
         return(m);
     }
     return(base.VisitMemberAccess(m));
 }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tableSpecialName"></param>
        /// <param name="resultType"></param>
        public virtual void AnalyzeExpressionSelectPacker(string tableSpecialName, Type resultType)
        {
            StringBuilder stringBuilder = new StringBuilder();

            resultSql.Insert(0, "(");
            resultSql.Append(")");

            DMSTableKeys lastTableKeys = new DMSTableKeys()
            {
                TableName             = resultType.AssemblyQualifiedName,
                AssemblyQualifiedName = resultType.AssemblyQualifiedName,
                TableSpecialName      = tableSpecialName,
            };

            this.DMSFrame.TableExpressioin.TableKeys.Add(lastTableKeys);

            this.Clean();
        }
Пример #3
0
        private static void AnalyzeExpressionSelectPacker <T, TResult>(this DMS <T> dms)
            where T : class
            where TResult : class
        {
            //string key = string.Empty;
            //int tableIndex = this.TableIndex;
            dms.SplitExpression.DMSFrame = dms;
            dms.AnalyzeExpressionSelect();
            DMSTableKeys lastTableKeys = dms.TableExpressioin.TableKeys.LastOrDefault();

            dms.SplitExpression.AnalyzeExpressionSelectPacker(lastTableKeys.TableSpecialName, typeof(TResult));
            //this.splitExpt.DMSBase = this;
            //this.splitExpt.AnalyzeExpressionSelect(this.JoinFlag, this.currentType, ref tableIndex, ref key, ref TableKeys);
            ////加上当前Type
            //this.SelectKey = key;
            //this.splitExpt.AnalyzeExpressionSelectPacker(this.JoinFlag, typeof(TResult), typeof(T), ref tableIndex, ref TableKeys);
            //string str = this.splitExpt.ResultSql;
            //this.TopTableKeys = TableKeys.LastOrDefault();
            //this.TableIndex = tableIndex;
            dms.TableExpressioin.JoinFlag = false;
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        public virtual void AnalyzeExpressionSelect()
        {
            List <DMSTableKeys> tableKeys = this.DMSFrame.TableExpressioin.TableKeys;
            List <ParamInfo>    paramList = this.ParamList;

            if (paramList == null)
            {
                paramList = new List <ParamInfo>();
            }
            if (tableKeys == null)
            {
                tableKeys = new List <DMSTableKeys>();
            }
            else
            {
                this.DMSFrame.TableExpressioin.TableKeys = tableKeys;
            }
            this.DMSFrame.ParamIndex = paramList.Count;
            StringBuilder strSql   = new StringBuilder();
            string        ParamSql = string.Empty;
            string        tableSql = this.DMSFrame.TableExpressioin.VisitExpression(this.DMSFrame.DataType, this.DMSFrame.ExcuteType, this.DMSFrame.ParamIndex, ref ParamSql, ref ParamList);

            if (this.DMSFrame.TableExpressioin.TableKeys.Count > 0)
            {
                tableKeys.AddRange(this.DMSFrame.TableExpressioin.TableKeys);
            }
            if (this.ParamList.Count > 0)
            {
                paramList.AddRange(this.ParamList);
            }
            if (tableKeys.Count != this.DMSFrame.TableExpressioin.TableKeys.Count)
            {
                DMSTableKeys lastTableKey = tableKeys.Where(q => q.TableName == this.DMSFrame.CurrentType.AssemblyQualifiedName).LastOrDefault();
                if (lastTableKey != null)
                {
                    resultSql.Append(this.DMSFrame.Provider.As);
                    resultSql.Append(lastTableKey.TableSpecialName);
                }
            }
            this.DMSFrame.ParamIndex = paramList.Count;
            this.DMSFrame.ColumnsExpressioin.TableKeys = tableKeys;
            this.DMSFrame.WhereExpressioin.TableKeys   = tableKeys;
            this.DMSFrame.GroupByExpression.TableKeys  = tableKeys;
            this.DMSFrame.OrderByExpressioin.TableKeys = tableKeys;
            this.DMSFrame.HavingExpression.TableKeys   = tableKeys;

            ParamSql = string.Empty;
            string columnSql = this.DMSFrame.ColumnsExpressioin.VisitExpression(this.DMSFrame.DataType, this.DMSFrame.ExcuteType, this.DMSFrame.ParamIndex, ref ParamSql, ref ParamList);

            this.MemberSql           = ParamSql;
            this.DMSFrame.ParamIndex = paramList.Count;
            if (this.ParamList.Count > 0)
            {
                paramList.AddRange(this.ParamList);
            }

            ParamSql = string.Empty;
            string whereSql = this.DMSFrame.WhereExpressioin.VisitExpression(this.DMSFrame.DataType, this.DMSFrame.ExcuteType, this.DMSFrame.ParamIndex, ref ParamSql, ref ParamList);

            this.DMSFrame.ParamIndex = paramList.Count;
            if (this.ParamList.Count > 0)
            {
                paramList.AddRange(this.ParamList);
            }


            if (this.resultSql.Length == 0)
            {
                strSql.Append(this.DMSFrame.Provider.Select);
                if (this.DMSFrame.TableExpressioin.DistinctFlag)
                {
                    strSql.Append(this.DMSFrame.Provider.Distinct);
                    this.DMSFrame.TableExpressioin.DistinctFlag = false;
                }
                strSql.Append(columnSql);
                strSql.Append(this.DMSFrame.Provider.From);
            }
            strSql.Append(tableSql);
            if (!string.IsNullOrEmpty(whereSql))
            {
                if (!tableSql.Trim().EndsWith(this.DMSFrame.Provider.On.Trim()))
                {
                    strSql.Append(this.DMSFrame.Provider.Where);
                }
                strSql.Append(whereSql);
            }

            string groupby = this.DMSFrame.GroupByExpression.VisitExpression(this.DMSFrame.DataType, this.DMSFrame.ExcuteType, this.DMSFrame.ParamIndex, ref ParamSql, ref ParamList);

            this.DMSFrame.ParamIndex = paramList.Count;
            if (this.ParamList.Count > 0)
            {
                paramList.AddRange(this.ParamList);
            }

            string orderSql = this.DMSFrame.OrderByExpressioin.VisitExpression(this.DMSFrame.DataType, this.DMSFrame.ExcuteType, this.DMSFrame.ParamIndex, ref ParamSql, ref ParamList);

            this.DMSFrame.ParamIndex = paramList.Count;
            if (this.ParamList.Count > 0)
            {
                paramList.AddRange(this.ParamList);
            }

            string having = this.DMSFrame.HavingExpression.VisitExpression(this.DMSFrame.DataType, this.DMSFrame.ExcuteType, this.DMSFrame.ParamIndex, ref ParamSql, ref ParamList);

            this.DMSFrame.ParamIndex = paramList.Count;
            if (this.ParamList.Count > 0)
            {
                paramList.AddRange(this.ParamList);
            }

            if (resultSql.Length == 0)
            {
                if (!string.IsNullOrEmpty(groupby))
                {
                    strSql.Append(this.DMSFrame.Provider.GroupBy + " ");
                    strSql.Append(groupby);
                }
                if (!string.IsNullOrEmpty(having))
                {
                    strSql.Append(this.DMSFrame.Provider.Having + " ");
                    strSql.Append(having);
                }
                if (!string.IsNullOrEmpty(orderSql))
                {
                    strSql.Append(this.DMSFrame.Provider.OrderBy + " ");
                    strSql.Append(orderSql);
                }
                resultSql = new StringBuilder(strSql.ToString());
            }
            else
            {
                resultSql.Append(" " + strSql.ToString());
                strSql = new StringBuilder();
                strSql.Append(this.DMSFrame.Provider.Select);
                if (this.DMSFrame.TableExpressioin.DistinctFlag)
                {
                    strSql.Append(this.DMSFrame.Provider.Distinct);
                    this.DMSFrame.TableExpressioin.DistinctFlag = false;
                }
                strSql.Append(columnSql);
                strSql.Append(this.DMSFrame.Provider.From);
                resultSql.Insert(0, strSql.ToString());
                if (!string.IsNullOrEmpty(groupby))
                {
                    resultSql.Append(this.DMSFrame.Provider.GroupBy + " ");
                    resultSql.Append(groupby);
                }
                if (!string.IsNullOrEmpty(having))
                {
                    resultSql.Append(this.DMSFrame.Provider.Having + " ");
                    resultSql.Append(having);
                }
                if (!string.IsNullOrEmpty(orderSql))
                {
                    resultSql.Append(this.DMSFrame.Provider.OrderBy + " ");
                    resultSql.Append(orderSql);
                }
            }

            //分页需求
            if (this.DMSFrame.OrderByExpressioin.SplitPagerFlag && this.DMSFrame.OrderByExpressioin.PageIndex > 0 && this.DMSFrame.OrderByExpressioin.PageSize > 0)
            {
                DMSTableKeys key = tableKeys.LastOrDefault();
                if (key == null)
                {
                    throw new DMSFrameException("tableKeys is null");
                }
                string tableKey = key.TableSpecialName;
                if (this.TableMapping.TokenFlag == true)
                {
                    tableKey = this.DMSFrame.Provider.BuildColumnName(tableKey);
                }
                this.AnalyzeExpressionPager(this.DMSFrame.OrderByExpressioin.SplitPagerFlag, this.DMSFrame.OrderByExpressioin.PageIndex, this.DMSFrame.OrderByExpressioin.PageSize, tableKey, orderSql);
                this.DMSFrame.OrderByExpressioin.SplitPagerFlag = false;
#if DEBUG
                System.Diagnostics.Debug.WriteLine(this.DMSFrame.SplitExpression.resultSqlCount.ToString());
#endif
            }
            this.BuildParameters(paramList);
        }
Пример #5
0
        protected override System.Linq.Expressions.Expression VisitParameter(System.Linq.Expressions.ParameterExpression p)
        {
            Type         valueType     = p.Type;
            DMSTableKeys lastTableKeys = null;
            bool         isEntity      = false;
            string       text          = string.Empty;

            if (valueType.IsClass)
            {
                if (this.JoinFlag && valueType.Name.StartsWith("<>f__AnonymousType"))
                {
                    lastTableKeys = this.TableKeys.Where(q => q.AssemblyQualifiedName == valueType.AssemblyQualifiedName).FirstOrDefault();
                    if (lastTableKeys != null)
                    {
                        this._strSql.Append(lastTableKeys.TableSpecialName);
                    }
                }
                else if (valueType == typeof(DMSDataBase))
                {
                    return(p);
                }
                else
                {
                    isEntity = true;
                    text     = p.Type.GetEntityName();
                    if (this.TableFunc != null)
                    {
                        text = this.TableFunc.Compile()(p.Type);
                    }
                    lastTableKeys = new DMSTableKeys()
                    {
                        TableName             = text,
                        AssemblyQualifiedName = p.Type.AssemblyQualifiedName,
                        TableSpecialName      = this.QutoTable + this.startIndex,
                    };
                    this.TableKeys.Add(lastTableKeys);
                    this.startIndex++;
                }
            }

            if (isEntity)
            {
                string tableNames = string.Empty;
                if (!string.IsNullOrEmpty(this.Provider.TableConfiguration.Author))
                {
                    string author = this.Provider.TableConfiguration.Author;
                    if (this.SplitExpression.TableMapping.TokenFlag == true)
                    {
                        author = this.Provider.BuildColumnName(author);
                    }
                    _strSql.Append(author);
                    _strSql.Append(this.Provider.TableToken);
                    tableNames = author + this.Provider.TableToken;
                }
                else if (this._foundDMSDataBase)
                {
                    _strSql.Append(this.Provider.TableToken);
                    this._foundDMSDataBase = false;
                }
                if (this.SplitExpression.TableMapping.TokenFlag == true)
                {
                    text = this.Provider.BuildColumnName(text);
                }
                _strSql.Append(text + " ");
                if (this.ExcuteType == DMSExcuteType.SELECT)
                {
                    _strSql.Append(this.Provider.As + " ");
                    text = lastTableKeys.TableSpecialName;
                    if (this.SplitExpression.TableMapping.TokenFlag == true)
                    {
                        text = this.Provider.BuildColumnName(text);
                    }
                    _strSql.Append(text);
                    if (this.WithLock == true && this.Provider.TableConfiguration.WithLock == "true" && !string.IsNullOrEmpty(this.Provider.UnLock))
                    {
                        _strSql.Append(this.Provider.UnLock);
                    }
                }
                else if (this.ExcuteType == DMSExcuteType.UPDATE_WHERE)
                {
                    _strSql.Append(this.Provider.As + " ");
                    text = lastTableKeys.TableSpecialName;
                    if (this.SplitExpression.TableMapping.TokenFlag == true)
                    {
                        text = this.Provider.BuildColumnName(text);
                    }
                    _strSql.Append(text);
                    _strSql.Append(",");
                }
                else if (this.ExcuteType == DMSExcuteType.INSERT_SELECT)
                {
                    _strSql.Append(",");
                }
            }
            else
            {
                _strSql.Append(text);
            }

            return(base.VisitParameter(p));
        }