private void MakeDistinct([NotNull] SelectDataTable dataTable) { var hash = new List <Pair <int, SelectItem> >(); foreach (var selectItem in dataTable.Items) { var hashValue = GetHashValue(selectItem); var pair = new Pair <int, SelectItem>(hashValue, selectItem); hash.Add(pair); } hash.Sort((p0, p1) => p0.Part1 == p1.Part1 ? 0 : p0.Part1 > p1.Part1 ? 1 : -1); for (var index = hash.Count - 2; index >= 0; index--) { if (hash[index].Part1 == hash[index + 1].Part1) { hash.RemoveAt(index + 1); } } dataTable.Items.Clear(); dataTable.Items.AddRange(hash.Select(p => p.Part2)); }
public override object Evaluate([NotNull] Query query, [NotNull] QueryContext contextNode) { Assert.ArgumentNotNull(query, nameof(query)); Assert.ArgumentNotNull(contextNode, nameof(contextNode)); var result = new SelectDataTable(); BuildColumns(result); object o = contextNode; var from = From; if (from != null) { o = query.Evaluate(from, contextNode); if (o == null) { return(result); } } var list = o as QueryContext[]; if (list != null) { foreach (var context in list) { if (!AddBatchScriptItem(query, result, context.GetQueryContextItem(), list.Count())) { break; } } if (IsDistinct) { MakeDistinct(result); } if (OrderBy.Any()) { Sort(result); } return(result); } var instance = o as QueryContext; if (instance != null) { AddBatchScriptItem(query, result, instance.GetQueryContextItem(), 1); } return(result); }
private void FormatTable([NotNull] XmlTextWriter output, [NotNull] SelectDataTable selectDataTable) { Debug.ArgumentNotNull(output, nameof(output)); Debug.ArgumentNotNull(selectDataTable, nameof(selectDataTable)); output.WriteStartElement("columns"); foreach (var column in selectDataTable.Columns) { output.WriteStartElement("column"); output.WriteAttributeString("name", column.Header); output.WriteAttributeString("isreadonly", column.IsReadOnly ? "true" : "false"); output.WriteEndElement(); } output.WriteEndElement(); output.WriteStartElement("rows"); foreach (var item in selectDataTable.Items) { output.WriteStartElement("row"); output.WriteAttributeString("id", item.Item.ID.ToString()); output.WriteAttributeString("language", item.Item.Language.Name); output.WriteAttributeString("name", item.Item.Name); output.WriteAttributeString("path", item.Item.Paths.Path); foreach (var field in item.Fields) { output.WriteStartElement("value"); output.WriteAttributeString("name", field.ColumnName); if (field.Field != null) { output.WriteAttributeString("id", field.Field.ID.ToString()); } output.WriteValue(field.Value); output.WriteEndElement(); } output.WriteEndElement(); } output.WriteEndElement(); }
private void BuildColumns([NotNull] SelectDataTable result) { Debug.ArgumentNotNull(result, nameof(result)); var columnIndex = 0; foreach (var columnExpression in ColumnExpressions) { var columnName = columnExpression.ColumnName; if (string.IsNullOrEmpty(columnName)) { columnName = "Column " + columnIndex; columnIndex++; } var column = new SelectColumn { Header = columnName, IsReadOnly = string.IsNullOrEmpty(columnExpression.FieldName) }; result.Columns.Add(column); } }
private void Sort([NotNull] SelectDataTable dataTable) { dataTable.Items.Sort(Compare); }
private bool AddBatchScriptItem([NotNull] Query query, [NotNull] SelectDataTable dataTable, [NotNull] Item item, int count) { Debug.ArgumentNotNull(dataTable, nameof(dataTable)); Debug.ArgumentNotNull(item, nameof(item)); var result = true; var selectItem = new SelectItem { Item = item }; var columnIndex = 0; foreach (var columnExpression in ColumnExpressions) { var selectField = new SelectField(); if (columnExpression.FieldName != null) { selectField.Value = item[columnExpression.FieldName]; selectField.Field = item.Fields[columnExpression.FieldName]; } else if (columnExpression.Expression != null) { var value = query.EvaluateSubQuery(columnExpression.Expression, item); if (value != null) { selectField.Value = value.ToString(); } else { var function = columnExpression.Expression as Function; if (function != null && string.Compare(function.Name, "count", StringComparison.InvariantCultureIgnoreCase) == 0) { selectField.Value = count.ToString(); result = false; } else { selectField.Value = "<null>"; } } } var columnName = columnExpression.ColumnName; if (string.IsNullOrEmpty(columnName)) { columnName = "Column " + columnIndex; columnIndex++; } selectField.ColumnName = columnName; selectItem.Fields.Add(selectField); } dataTable.Items.Add(selectItem); return(result); }