Пример #1
0
        // this for now, but a expression tree for parsing boolean logic would probably be the way to do this.
        // only handles the case of one comparison... i.e. column = something
        private bool processWhere(IntSchRow row, List <string> whereClause)
        {
            string conditionOnColumn = whereClause[0];
            string conditionOperator = whereClause[1];
            object givenConditionObj = whereClause[2];

            IntSchValue storedValue = row.GetValueInColumn(conditionOnColumn);
            // make a IntSchValue object out of the user given condition that can be used for comparison to the stored value
            IntSchValue givenCondition = new IntSchValue(givenConditionObj, columns[conditionOnColumn].DataType);

            int comparisonResult = storedValue.CompareTo(givenCondition);

            switch (conditionOperator)
            {
            case ">": return(comparisonResult < 0);    // value > condition

            case ">=": return(comparisonResult <= 0);  // value >= condition

            case "<": return(comparisonResult > 0);    // value < condition

            case "<=": return(comparisonResult >= 0);  // value <= condition

            case "=": return(comparisonResult == 0);   // value == condition

            default: throw new Exception("Unsupported condition operator");
            }
        }
Пример #2
0
        // insert a record into the table
        public void Insert(List <string> columnNames, List <string> columnValues)
        {
            printList(columnNames, "Log insert column names");
            printList(columnValues, "Log insert column values");

            if (columnNames.Count == columnValues.Count)
            {
                IntSchRow row = new IntSchRow();

                for (int i = 0; i < columnNames.Count; ++i)
                {
                    TypeEnum    columnType = columns[columnNames[i]].DataType;
                    IntSchValue val        = new IntSchValue(columnValues[i], columnType);

                    columns[columnNames[i]].AddValueToColumn(val);
                    row.SetValueInColumn(columnNames[i], val);
                }

                rows.Add(row);
            }
        }
Пример #3
0
        // aggregations: pairs of column, function
        internal List <IntSchRow> GroupBy(List <IntSchRow> rows, string colToGroupOn, List <KeyValuePair <string, string> > aggregations)
        {
            List <IntSchRow>           rowResult = new List <IntSchRow>();
            IntSchValueEqualityByValue comparer  = new IntSchValueEqualityByValue();

            var query = rows.GroupBy(
                row => row.GetValueInColumn(colToGroupOn),
                (groupValue, groupRows) =>
            {
                Console.WriteLine("Group value " + groupValue.ToString());

                List <KeyValuePair <string, object> > aggregationResults = new List <KeyValuePair <string, object> >();

                foreach (KeyValuePair <string, string> entry in aggregations)
                {
                    string columnName = entry.Key;
                    string function   = entry.Value;

                    switch (function)
                    {
                    case "count":
                        int count = groupRows.Count();
                        aggregationResults.Add(new KeyValuePair <string, object>("count(" + columnName + ")", count));
                        break;

                    case "max":
                        float max = groupRows.Max(row =>
                        {
                            IntSchValue intSchValue = row.GetValueInColumn(columnName);
                            switch (intSchValue.Type)
                            {
                            case TypeEnum.Integer:
                                return((int)Convert.ChangeType(intSchValue.Value, typeof(int)));

                            case TypeEnum.Float:
                                return((float)Convert.ChangeType(intSchValue.Value, typeof(float)));

                            default:
                                break;
                            }
                            return(0);
                        });

                        aggregationResults.Add(new KeyValuePair <string, object>("max(" + columnName + ")", max));

                        break;

                    case "min":
                        float min = groupRows.Min(row =>
                        {
                            IntSchValue intSchValue = row.GetValueInColumn(columnName);
                            switch (intSchValue.Type)
                            {
                            case TypeEnum.Integer:
                                return((int)Convert.ChangeType(intSchValue.Value, typeof(int)));

                            case TypeEnum.Float:
                                return((float)Convert.ChangeType(intSchValue.Value, typeof(float)));

                            default:
                                break;
                            }
                            return(0);
                        });

                        aggregationResults.Add(new KeyValuePair <string, object>("min(" + columnName + ")", min));

                        break;

                    case "avg":
                        float avg = groupRows.Average(row =>
                        {
                            IntSchValue intSchValue = row.GetValueInColumn(columnName);
                            switch (intSchValue.Type)
                            {
                            case TypeEnum.Integer:
                                return((int)Convert.ChangeType(intSchValue.Value, typeof(int)));

                            case TypeEnum.Float:
                                return((float)Convert.ChangeType(intSchValue.Value, typeof(float)));

                            default:
                                break;
                            }
                            return(0);
                        });

                        aggregationResults.Add(new KeyValuePair <string, object>("avg(" + columnName + ")", avg));

                        break;

                    case "sum":
                        float sum = groupRows.Sum(row =>
                        {
                            IntSchValue intSchValue = row.GetValueInColumn(columnName);
                            switch (intSchValue.Type)
                            {
                            case TypeEnum.Integer:
                                return((int)Convert.ChangeType(intSchValue.Value, typeof(int)));

                            case TypeEnum.Float:
                                return((float)Convert.ChangeType(intSchValue.Value, typeof(float)));

                            default:
                                break;
                            }
                            return(0);
                        });

                        aggregationResults.Add(new KeyValuePair <string, object>("sum(" + columnName + ")", sum));

                        break;

                    default:
                        break;
                    }
                }

                return(new
                {
                    Key = groupValue,
                    Aggregations = aggregationResults
                });
            },
                comparer
                );

            foreach (var result in query)
            {
                IntSchRow row = new IntSchRow();
                row.SetValueInColumn(colToGroupOn, result.Key);
                foreach (KeyValuePair <string, object> aggregation in result.Aggregations)
                {
                    string columnName = aggregation.Key;
                    row.SetValueInColumn(columnName, new IntSchValue(aggregation.Value, TypeEnum.Integer));
                }
                rowResult.Add(row);
            }

            foreach (IntSchRow r in rowResult)
            {
                Console.WriteLine(r);
            }

            return(rowResult);
        }