Пример #1
0
        private static ColumnInfo TryGetColumnInfo(
            [NotNull] IList <string> aliases,
            [NotNull] IList <ColumnInfoFactory> factories,
            [NotNull] string expressionToken,
            out int tableAliasIndex)
        {
            int tableSeparatorIndex = expressionToken.IndexOf(_tableSeparator, 0);

            tableAliasIndex = -1;

            if (tableSeparatorIndex < 0)
            {
                // there is no separator in the token --> ignore
                return(null);
            }

            string tableNameOrAlias = expressionToken.Substring(0, tableSeparatorIndex);

            int aliasIndex = aliases.IndexOf(tableNameOrAlias);

            if (aliasIndex < 0)
            {
                return(null);
            }

            // the qualified field name references one of the aliases ("L.FIELD1")

            string tokenWithoutQualifier =
                expressionToken.Substring(tableSeparatorIndex + 1);

            ColumnInfoFactory factory = factories[aliasIndex];

            ColumnInfo columnInfo = factory.GetColumnInfo(tokenWithoutQualifier);

            if (columnInfo == null)
            {
                return(null);
            }

            tableAliasIndex = aliasIndex;
            return(columnInfo);
        }
Пример #2
0
        public static TableView Create([NotNull] ITable table,
                                       [CanBeNull] string expression,
                                       bool useAsConstraint,
                                       bool caseSensitive = false)
        {
            Assert.ArgumentNotNull(table, nameof(table));

            if (expression == null || expression.Trim().Length == 0)
            {
                // this filter helper won't do much
                return(new TableView(new ColumnInfo[] { }, null));
            }

            // read/remove case sensitivity override from expression
            bool?caseSensitivityOverride;

            expression = ExpressionUtils.ParseCaseSensitivityHint(expression,
                                                                  out caseSensitivityOverride);

            if (caseSensitivityOverride != null)
            {
                caseSensitive = caseSensitivityOverride.Value;
            }

            var columnInfos = new List <ColumnInfo>();
            var addedFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            IFields fields = table.Fields;

            if (table.HasOID)
            {
                string oidFieldName = table.OIDFieldName.ToUpper();
                int    fieldIndex   = table.FindField(oidFieldName);
                IField field        = fields.Field[fieldIndex];

                columnInfos.Add(new FieldColumnInfo(table, field, fieldIndex));
                addedFields.Add(oidFieldName);
            }

            var factory = new ColumnInfoFactory(table);

            foreach (string token in ExpressionUtils.GetExpressionTokens(expression))
            {
                if (addedFields.Contains(token))
                {
                    continue;
                }

                ColumnInfo columnInfo = factory.GetColumnInfo(token);

                if (columnInfo != null)
                {
                    columnInfos.Add(columnInfo);
                    addedFields.Add(token);
                }
            }

            var dataTable = new DataTable(DatasetUtils.GetName(table))
            {
                CaseSensitive = caseSensitive
            };

            foreach (ColumnInfo columnInfo in columnInfos)
            {
                dataTable.Columns.Add(columnInfo.ColumnName, columnInfo.ColumnType);
            }

            var dataView = new DataView(dataTable);

            if (useAsConstraint)
            {
                dataView.RowFilter = expression;
            }

            return(new TableView(columnInfos, dataView));
        }