Exemplo n.º 1
0
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, SimpleTable expandTable)
        {
            if (sourceTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: source table null"));
            }
            if (expandTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: aggregate table null"));
            }

            var newTable = sourceTable.Copy();

            // create columns
            foreach (var map in expandTable)
            {
                var source      = map["source"];
                var destination = map["destination"];
                newTable.AddColumnName(destination);

                var expression = ExpressionCache.Compile(source);

                if (expression == null)
                {
                    return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: evaluator returns null", "Source: " + source));
                }
            }

            var fieldSource = new FieldDataSource();

            foreach (var newRow in newTable)
            {
                fieldSource.Row = newRow;

                foreach (var map in expandTable)
                {
                    var source      = map["source"];
                    var destination = map["destination"];

                    var expression = ExpressionCache.Compile(source);

                    var result = expression.Evaluate(new Context()
                    {
                        FieldSource = fieldSource
                    });

                    if (result.IsError)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, result.String));
                    }

                    newRow[destination] = ToString(result);
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }
Exemplo n.º 2
0
        public static StandardResult <SimpleTable> Execute(SimpleTable table, string includeExpression)
        {
            if (string.IsNullOrWhiteSpace(includeExpression))
            {
                return(StandardResult <SimpleTable> .ReturnResult(table.Copy()));
            }

            var newTable = new SimpleTable();

            foreach (var column in table.ColumnNames)
            {
                newTable.AddColumnName(column);
            }

            var fieldSource = new FieldDataSource();

            var expression = ExpressionCache.Compile(includeExpression);

            foreach (var sourceRow in table)
            {
                fieldSource.Row = sourceRow;

                if (string.IsNullOrWhiteSpace(includeExpression))
                {
                    continue;
                }

                var result = expression.Evaluate(new Context()
                {
                    FieldSource = fieldSource
                });

                if (!expression.IsValid)
                {
                    return(StandardResult <SimpleTable> .ReturnError("FilterTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, "invalid result: " + result.String));
                }

                if (!result.IsBoolean)
                {
                    return(StandardResult <SimpleTable> .ReturnError("FilterTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, "result was not boolean: " + result.String));
                }

                if (!result.Boolean)
                {
                    continue;
                }

                var newRow = newTable.CreateRow();

                for (int i = 0; i < sourceRow.ColumnCount; i++)
                {
                    var value = sourceRow[i];

                    if (value == null)
                    {
                        continue;
                    }

                    newRow[i] = value;
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }