コード例 #1
0
        public List <Condition> ChangeCondition(Condition aCondition, string aTableName)
        {
            string lColumnType = Database.Tables.FirstOrDefault(aR => aR.Name.ToLower() == aTableName.ToLower())
                                 .Columns.FirstOrDefault(aR => aR.Name.ToLower() == aCondition.ColumnName.ToLower()).Type;
            List <Condition> lConditions = new List <Condition>();

            if (aCondition.Aggregate == FunctionEnum.NONE || aCondition.Aggregate == FunctionEnum.SUM || aCondition.Aggregate == FunctionEnum.AVG)
            {
                if (IsCorrectType(lColumnType, aCondition.Value))
                {
                    lConditions.Add(aCondition);
                }

                return(lConditions);
            }

            string[] lData = Database.Tables.FirstOrDefault(aR => aR.Name == aTableName).Data.Tables[0].AsEnumerable().ToArray()
                             .Select(aR => aR[aCondition.ColumnName].ToString()).ToArray();
            List <string> lToConditionList = ParseCondition.OperatorConvertion(lData, aCondition.Operator, aCondition.Aggregate, aCondition.Value);

            foreach (var nToCondition in lToConditionList)
            {
                if (IsCorrectType(lColumnType, aCondition.Value))
                {
                    lConditions.Add(new Condition(aCondition.ColumnName, "=", nToCondition));
                }
            }
            return(lConditions);
        }
コード例 #2
0
 //TODO: agregacni funkce resit asi jenom COUNT,MIN, MAX... sum a avg nema smysl nemohl bych maskovat nic,
 //zjistim data zjistim hodnoty pro ktere plati podminka v havingu a ty nebudu maskovat
 public override void EnterSearch_condition_and([NotNull] TSqlParser.Search_condition_andContext context)
 {
     try
     {
         foreach (var nChild in context.children)
         {
             Condition lCondition = ParseCondition.Parse(nChild.GetText().ToLower());
             if (lCondition.ColumnName == null || lCondition.Value == null || lCondition.Operator == null)
             {
                 continue;
             }
             int    lColumnCount = 0;
             string lTableName   = "";
             if (AliasDic.Count == 0)
             {
                 foreach (var nTable in Database.Tables)
                 {
                     if (mMentionedTables.FirstOrDefault(aR => aR.ToLower() == nTable.Name.ToLower()) != null)
                     {
                         if (nTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lCondition.ColumnName.ToLower()) != null &&
                             !nTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lCondition.ColumnName.ToLower()).PrimaryKey)
                         {
                             if (mMentionedTables.Contains(nTable.Name))
                             {
                                 lColumnCount++;
                             }
                             lTableName = nTable.Name;
                         }
                     }
                 }
                 if (lColumnCount > 1)
                 {
                     if (OnAnalyzingError != null)
                     {
                         OnAnalyzingError(this, "Ambiguous column name. Some conditions are not involved");
                     }
                     continue;
                 }
                 if (lColumnCount == 0)
                 {
                     if (OnAnalyzingError != null)
                     {
                         OnAnalyzingError(this, "Column not found. Some conditions are not involved");
                     }
                     continue;
                 }
                 if (lColumnCount == 1)
                 {
                     ParsedQueryStructure.Table lTable = AnalyzedWorkload.FirstOrDefault(aR => aR.Name == lTableName);
                     foreach (var nCondition in ChangeCondition(lCondition, lTableName))
                     {
                         ParsedQueryStructure.Condition lNewCondition = new Condition(nCondition.ColumnName, nCondition.Operator, nCondition.Value);
                         if (lTable.Columns.FirstOrDefault(aR => aR.Name == lCondition.ColumnName) != null)
                         {
                             if (lTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lCondition.ColumnName.ToLower()).Conditions.FirstOrDefault(aR => Condition.IsSameCondition(aR, lNewCondition)) != null)
                             {
                                 continue;
                             }
                             AnalyzedWorkload.FirstOrDefault(aR => aR.Name == lTableName).Columns.FirstOrDefault(aR => aR.Name == lCondition.ColumnName)
                             .Conditions.Add(lNewCondition);
                         }
                         else
                         {
                             AnalyzedWorkload.FirstOrDefault(aR => aR.Name == lTableName).Columns.Add(new ParsedQueryStructure.Column(lCondition.ColumnName));
                             AnalyzedWorkload.FirstOrDefault(aR => aR.Name == lTableName).Columns.FirstOrDefault(aR => aR.Name == lCondition.ColumnName)
                             .Conditions.Add(lNewCondition);
                         }
                     }
                 }
             }
             else
             {
                 foreach (var nCondition in ChangeCondition(lCondition, lTableName))
                 {
                     Tuple <string, string> lColNameParts = ParseCondition.SplitAlias(nCondition.ColumnName);
                     if (lColNameParts != null)
                     {
                         string lTableFromDic = AliasDic.Where(aR => aR.Value == lColNameParts.Item1).Select(aR => aR.Key).FirstOrDefault();
                         var    lDBTable      = Database.Tables.FirstOrDefault(aR => aR.Name.ToLower() == lTableFromDic.ToLower());
                         if (lDBTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lColNameParts.Item2.ToLower()) != null &&
                             !lDBTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lColNameParts.Item2.ToLower()).PrimaryKey)
                         {
                             ParsedQueryStructure.Table     lTable        = AnalyzedWorkload.FirstOrDefault(aR => aR.Name.ToLower() == lTableFromDic.ToLower());
                             ParsedQueryStructure.Condition lNewCondition = new Condition(lColNameParts.Item2, nCondition.Operator, nCondition.Value);
                             if (lTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lColNameParts.Item2.ToLower()) != null)
                             {
                                 if (lTable.Columns.FirstOrDefault(aR => aR.Name.ToLower() == lTableFromDic.ToLower()).Conditions.FirstOrDefault(aR => Condition.IsSameCondition(aR, lNewCondition)) != null)
                                 {
                                     continue;
                                 }
                                 AnalyzedWorkload.FirstOrDefault(aR => aR.Name.ToLower() == lTableFromDic.ToLower()).Columns.FirstOrDefault(aR => aR.Name.ToLower() == lCondition.ColumnName.ToLower())
                                 .Conditions.Add(lNewCondition);
                             }
                             else
                             {
                                 AnalyzedWorkload.FirstOrDefault(aR => aR.Name.ToLower() == lTableFromDic.ToLower()).Columns.Add(new ParsedQueryStructure.Column(lColNameParts.Item2));
                                 AnalyzedWorkload.FirstOrDefault(aR => aR.Name.ToLower() == lTableFromDic.ToLower()).Columns.FirstOrDefault(aR => aR.Name.ToLower() == lColNameParts.Item2.ToLower())
                                 .Conditions.Add(lNewCondition);
                             }
                         }
                     }
                 }
             }
         }
     }
     catch { }
 }
コード例 #3
0
 public override void EnterAggregate_windowed_function([NotNull] TSqlParser.Aggregate_windowed_functionContext context)
 {
     mAggregateFunction = ParseCondition.NotifyAggregateFunction(context.GetText());
 }