• DMSLinq表达式框架实现(五) DMSTableExpression实现


      1  /// <summary>
      2     /// 表的表达式处理实现
      3     /// </summary>
      4     public class DMSTableExpression : DMSExpressionVisitor, IDMSTableExpressionParser
      5     {
      6         /// <summary>
      7         /// 
      8         /// </summary>
      9         public IDMSDbProvider DbProvider
     10         {
     11             get;
     12             set;
     13         }
     14         private List<SingleTableExpression> SingleExpression
     15         {
     16             get;
     17             set;
     18         }
     19         /// <summary>
     20         /// 
     21         /// </summary>
     22         public Expression DMSExpression { get; set; }
     23         /// <summary>
     24         /// 
     25         /// </summary>
     26         public Dictionary<string, string> KeyValue { get; set; }
     27         /// <summary>
     28         /// 
     29         /// </summary>
     30         public Dictionary<string, string> TableNameAlias { get; set; }
     31 
     32         private StringBuilder _ResultSql = new StringBuilder();
     33         /// <summary>
     34         /// 
     35         /// </summary>
     36         /// <param name="keyTable"></param>
     37         /// <returns></returns>
     38         public string AnalyzeExpression(ref Dictionary<string, string> keyTable)
     39         {
     40             this._ResultSql = new StringBuilder();
     41             if (DMSExpression != null)
     42             {
     43                 this.Visit(DMSExpression);
     44             }
     45             keyTable = this.KeyValue;
     46 
     47             if (keyTable.Count > 0)
     48             {
     49                 string lastKey = keyTable.Last().Key;
     50                 if (TableNameAlias.ContainsKey(lastKey))
     51                 {
     52                     keyTable[lastKey] = TableNameAlias[lastKey];
     53                 }
     54             }
     55             return this._ResultSql.ToString();
     56         }
     57         private bool IsAs = true;
     58         /// <summary>
     59         /// 
     60         /// </summary>
     61         /// <returns></returns>
     62         public string AnalyzeExpression()
     63         {
     64             this._ResultSql = new StringBuilder();
     65             this.IsAs = false;
     66             if (DMSExpression != null)
     67             {
     68                 this.Visit(DMSExpression);
     69             }
     70             return this._ResultSql.ToString();
     71         }
     72         /// <summary>
     73         /// 
     74         /// </summary>
     75         /// <param name="dms"></param>
     76         /// <param name="type"></param>
     77         public void Append(IDMSBase dms, Type type)
     78         {
     79             if (this.KeyValue == null)
     80                 KeyValue = new Dictionary<string, string>();
     81             if (SingleExpression == null)
     82                 SingleExpression = new List<SingleTableExpression>();
     83             if (TableNameAlias == null)
     84                 TableNameAlias = new Dictionary<string, string>();
     85 
     86             if (typeof(DMSWhere) == type
     87                || typeof(DMSOn) == type
     88                || typeof(DMSSelect) == type)
     89             {
     90                 if (SingleExpression.Find(q => q.Key == dms.CurrentType && q.TableType == type) != null)
     91                 {
     92                     return;
     93                 }
     94                 SingleExpression.Add(new SingleTableExpression() { Key = dms.CurrentType, TableType = type });
     95             }
     96             if (!type.IsGenericType && type.IsClass)
     97             {
     98                 NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(object), Expression.New(type));
     99                 if (this.DMSExpression != null)
    100                 {
    101                     NewArrayExpression newArrayExpression2 = this.DMSExpression as NewArrayExpression;
    102                     IEnumerable<Expression> initializers2 = newArrayExpression2.Expressions.Concat(newArrayExpression.Expressions);
    103                     newArrayExpression = Expression.NewArrayInit(typeof(object), initializers2);
    104                     this.DMSExpression = dms.ModifyExpression(newArrayExpression);
    105                 }
    106                 else
    107                 {
    108                     this.DMSExpression = dms.ModifyExpression(newArrayExpression);
    109                 }
    110             }
    111             else
    112             {
    113                 NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(object), Expression.Constant(type.ToString()));
    114                 if (this.DMSExpression != null)
    115                 {
    116                     Expression exp = this.DMSExpression;
    117                     NewArrayExpression newArrayExpression2 = exp as NewArrayExpression;
    118                     if (newArrayExpression2 != null)
    119                     {
    120                         IEnumerable<Expression> initializers2 = newArrayExpression2.Expressions.Concat(newArrayExpression.Expressions);
    121                         newArrayExpression = Expression.NewArrayInit(typeof(object), initializers2);
    122                         this.DMSExpression = dms.ModifyExpression(newArrayExpression);
    123                     }
    124                 }
    125                 else
    126                 {
    127                     this.DMSExpression = dms.ModifyExpression(newArrayExpression);
    128                 }
    129             }
    130         }
    131         /// <summary>
    132         /// 
    133         /// </summary>
    134         /// <param name="nex"></param>
    135         /// <returns></returns>
    136         protected override NewExpression VisitNew(NewExpression nex)
    137         {
    138             Type type = nex.Type;
    139 
    140             if (!type.IsGenericType && type.IsClass)
    141             {
    142                 AdjustConstant(type);
    143             }
    144             #region MyRegion
    145             //else
    146             //{
    147             //    foreach (Expression exp in nex.Arguments)
    148             //    {
    149             //        this.Visit(exp);
    150             //        if (!this._ResultSql.ToString().EndsWith(","))
    151             //        {
    152             //            this._ResultSql.Append(",");
    153             //        }
    154             //    }
    155             //    if (this._ResultSql.ToString().EndsWith(","))
    156             //    {
    157             //        this._ResultSql.Remove(this._ResultSql.Length - 1, 1);
    158             //    }
    159             //    this._ResultSql.Append(" ");
    160             //} 
    161             #endregion
    162             return nex;
    163         }
    164         private void AdjustConstant(Type type)
    165         {
    166             string text = string.Empty;
    167             bool IsEntity = false;
    168             //是否是IEntity
    169             if (type.GetInterface(typeof(IEntity).FullName, false) != null)
    170             {
    171                 if (!this.KeyValue.ContainsKey(type.ToString()))
    172                 {
    173                     string AliasName = "t" + this.KeyValue.Count;
    174                     if (TableNameAlias.ContainsKey(type.ToString()))
    175                     {
    176                         AliasName = TableNameAlias[type.ToString()];
    177                     }
    178                     this.KeyValue.Add(type.ToString(), AliasName);
    179                 }
    180                 text = type.GetEntityName();
    181                 if (string.IsNullOrEmpty(text)) {
    182                     text = type.Name;
    183                 }
    184                 IsEntity = true;
    185             }
    186             else if (type.GetInterface(typeof(IDMSParser).FullName, false) != null)//是否是IDMSParser
    187             {
    188                 text = type.GetDMSName();
    189                 if (type == typeof(DMSSelect))
    190                 {
    191                     text = "";
    192                 }
    193             }
    194             else
    195             {
    196                 if (!this.KeyValue.ContainsKey(type.ToString()))
    197                 {
    198                     string AliasName = "t" + this.KeyValue.Count;
    199                     if (TableNameAlias.ContainsKey(type.ToString()))
    200                     {
    201                         AliasName = TableNameAlias[type.ToString()];
    202                     }
    203                     this.KeyValue.Add(type.ToString(), AliasName);
    204                 }
    205             }
    206             if (!string.IsNullOrEmpty(text) && this.DbProvider != null && type.GetInterface(typeof(IDMSParser).FullName, false) == null)
    207             {
    208                 text = this.DbProvider.BuildTableName(text);
    209             }
    210             this._ResultSql.Append(text);
    211             if (IsEntity && IsAs)
    212             {
    213                 this._ResultSql.Append(" AS ");
    214                 text = this.KeyValue[type.ToString()];
    215                 if (!string.IsNullOrEmpty(text) && this.DbProvider != null)
    216                 {
    217                     text = this.DbProvider.BuildTableName(text);
    218                 }
    219                 this._ResultSql.Append(text);
    220             }
    221             this._ResultSql.Append(" ");
    222         }
    223         /// <summary>
    224         /// 
    225         /// </summary>
    226         /// <param name="c"></param>
    227         /// <returns></returns>
    228         protected override Expression VisitConstant(ConstantExpression c)
    229         {
    230 
    231             if (c.Value != null)
    232             {
    233                 if (c.Type.IsArray)
    234                 {
    235                     Array array = c.Value as Array;
    236                     foreach (object current in array)
    237                     {
    238                         if (current != null)
    239                         {
    240                             Type type = current.GetType();
    241                             if (type == typeof(string))
    242                             {
    243                                 if (!this.KeyValue.ContainsKey(current.ToString()))
    244                                 {
    245                                     string AliasName = "t" + this.KeyValue.Count;
    246                                     if (this.TableNameAlias.ContainsKey(current.ToString()))
    247                                     {
    248                                         AliasName = this.TableNameAlias[current.ToString()];
    249                                     }
    250                                     this.KeyValue.Add(current.ToString(), AliasName);
    251                                 }
    252                             }
    253                             else
    254                             {
    255                                 AdjustConstant(type);
    256                             }
    257                         }
    258                     }
    259                 }
    260                 else
    261                 {
    262                     if (!this.KeyValue.ContainsKey(c.Value.ToString()))
    263                     {
    264                         this.KeyValue.Add(c.Value.ToString(), "t" + this.KeyValue.Count);
    265                     }
    266                 }
    267             }
    268             return base.VisitConstant(c);
    269         }
    270         /// <summary>
    271         /// 
    272         /// </summary>
    273         /// <param name="na"></param>
    274         /// <returns></returns>
    275         protected override Expression VisitNewArray(NewArrayExpression na)
    276         {
    277             foreach (Expression current in na.Expressions)
    278             {
    279                 this.Visit(current);
    280                 this._ResultSql.Append(" ");
    281             }
    282             return na;
    283         }
    284     }
    千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs
  • 相关阅读:
    uoj#207 共价大爷游长沙
    bzoj4006 [JLOI2015]管道连接
    bzoj2595 [Wc2008]游览计划
    uoj#300.【CTSC2017】吉夫特
    bzoj2565 最长双回文串
    bzoj2342 [Shoi2011]双倍回文
    bzoj3676 [Apio2014]回文串
    [转载]物理大神的八卦完整版——大爱物理
    低层次数论书籍大杂烩
    控制论课题
  • 原文地址:https://www.cnblogs.com/kingkoo/p/2500713.html
Copyright © 2020-2023  润新知