Exemple #1
0
        public QaValidUrls(
            [Doc(nameof(DocStrings.QaValidUrls_table))][NotNull] ITable table,
            [Doc(nameof(DocStrings.QaValidUrls_urlExpression))][NotNull]
            string urlExpression)
            : base(table)
        {
            Assert.ArgumentNotNull(table, nameof(table));
            Assert.ArgumentNotNullOrEmpty(urlExpression, nameof(urlExpression));

            _table         = table;
            _urlExpression = urlExpression;

            _maximumParallelTasks = _defaultMaximumParallelTasks;

            // if the url expression is a single field, then direclty read from the field
            // (by the field index determined here)
            // Use the infrastructure for expression evaluation only if the url expression
            // is really a sql expression involving more than one field name
            // and/or functions, literals etc.,
            List <string> tokens = ExpressionUtils.GetExpressionTokens(urlExpression).ToList();

            _isSingleField = false;
            if (tokens.Count == 1)
            {
                int fieldIndex = table.FindField(urlExpression);

                if (fieldIndex < 0)
                {
                    throw new ArgumentException(
                              string.Format("Field not found in table {0}: {1}",
                                            DatasetUtils.GetName(table),
                                            urlExpression), nameof(urlExpression));
                }

                // the url expression is a single field name
                _fieldIndex        = fieldIndex;
                _isSingleField     = true;
                _affectedComponent = urlExpression.ToUpper().Trim();
            }

            if (!_isSingleField)
            {
                List <string> fieldNames = ExpressionUtils.GetExpressionFieldNames(
                    _table, _urlExpression).ToList();

                if (fieldNames.Count == 0)
                {
                    throw new ArgumentException(
                              $@"Invalid expression: {urlExpression}",
                              nameof(urlExpression));
                }

                if (fieldNames.Count == 1)
                {
                    _affectedComponent = fieldNames[0].ToUpper().Trim();
                }
            }
        }
Exemple #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));
        }
Exemple #3
0
        public static MultiTableView Create(
            [NotNull] IList <ITable> tables,
            [NotNull] IList <string> tableAliases,
            [NotNull] string expression,
            bool caseSensitive = false,
            [CanBeNull] Action <Action <string, Type>, IList <ITable> > customizeDataTable = null,
            bool useAsConstraint = true)
        {
            Assert.ArgumentNotNull(tables, nameof(tables));
            Assert.ArgumentNotNull(tableAliases, nameof(tableAliases));
            Assert.ArgumentNotNull(expression, nameof(expression));

            if (tables.Count != tableAliases.Count)
            {
                throw new ArgumentException(
                          string.Format("Equal number of tables({0}) and aliases({1}) expected",
                                        tables.Count, tableAliases.Count));
            }

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

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

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

            List <string> upperCaseTableAliases = tableAliases.Select(alias => alias.ToUpper())
                                                  .ToList();

            var tableAliasIndexes = new List <int>();

            var dataTable = new DataTable {
                CaseSensitive = caseSensitive
            };

            List <ColumnInfoFactory> factories =
                tables.Select(table => new ColumnInfoFactory(table)).ToList();

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

            const bool toUpper = true;

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

                int        tableAliasIndex;
                ColumnInfo columnInfo = TryGetColumnInfo(upperCaseTableAliases,
                                                         factories,
                                                         expressionToken,
                                                         out tableAliasIndex);
                if (columnInfo == null)
                {
                    continue;
                }

                columnInfos.Add(columnInfo);

                // add column using fully qualified name
                dataTable.Columns.Add(expressionToken, columnInfo.ColumnType);
                tableAliasIndexes.Add(tableAliasIndex);

                addedFields.Add(expressionToken);
            }

            customizeDataTable?.Invoke(
                (columnName, type) => dataTable.Columns.Add(columnName, type),
                tables);

            var dataView = new DataView(dataTable);

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

            return(new MultiTableView(columnInfos, tableAliasIndexes, dataView));
        }
Exemple #4
0
        public string ToString([NotNull] IRow row,
                               bool constraintOnly,
                               [CanBeNull] ICollection <string> addedConstraintFieldNames)
        {
            DataView dataView = ConstraintView;

            if (dataView == null)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            CreateDataRow(row, ColumnInfos);
            DataTable dataTable = dataView.Table;

            if (constraintOnly)
            {
                foreach (string fieldNameCandidate in
                         ExpressionUtils.GetExpressionTokens(dataView.RowFilter))
                {
                    int columnIndex = dataTable.Columns.IndexOf(fieldNameCandidate);

                    if (columnIndex < 0 || columnIndex >= ColumnInfos.Count)
                    {
                        continue;
                    }

                    // the candidate matches an existing column

                    if (addedConstraintFieldNames != null)
                    {
                        if (addedConstraintFieldNames.Contains(fieldNameCandidate))
                        {
                            continue;
                        }

                        addedConstraintFieldNames.Add(fieldNameCandidate);
                    }

                    if (sb.Length > 0)
                    {
                        sb.Append("; ");
                    }

                    ColumnInfo columnInfo = GetColumnInfo(columnIndex);
                    sb.AppendFormat("{0} = {1}",
                                    GetDataTableColumnName(columnIndex),
                                    columnInfo.FormatValue(row));
                }
            }
            else
            {
                var columnIndex = 0;
                foreach (ColumnInfo columnInfo in ColumnInfos)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append("; ");
                    }

                    sb.AppendFormat("{0} = {1}",
                                    GetDataTableColumnName(columnIndex),
                                    columnInfo.FormatValue(row));
                    columnIndex++;
                }
            }

            return(sb.ToString());
        }