public static WhereResult Where(ITabularReader reader, string columnIdentifier, string operatorString, string valueString, ITabularWriter writer) { int colIndex = reader.ColumnIndex(columnIdentifier); WhereResult result = new WhereResult() { ColumnIndex = colIndex, ColumnName = reader.Columns[colIndex], Op = OperatorExtensions.Parse(operatorString), Value = ConvertToBestType(valueString) }; Type t = result.Value.GetType(); if (t == typeof(bool)) { MatchBoolCompare(reader, writer, result); } else if (t == typeof(int)) { MatchIntCompare(reader, writer, result); } else if (t == typeof(DateTime)) { MatchDateTimeCompare(reader, writer, result); } else if (result.Op == Operator.Contains) { MatchContains(reader, writer, result); } else if (result.Op == Operator.StartsWith) { MatchStartsWith(reader, writer, result); } else { MatchStringCompare(reader, writer, result); } result.RowCount = reader.RowCountRead; return(result); }
private static void MatchBoolCompare(ITabularReader reader, ITabularWriter writer, WhereResult result) { bool value = (bool)result.Value; while (reader.NextRow()) { // Ensure the row has enough columns if (reader.CurrentRowColumns <= result.ColumnIndex) { continue; } // Ensure the value converts bool columnValue; if (!reader.Current(result.ColumnIndex).ToString8().TryToBoolean(out columnValue)) { continue; } int compareResult = columnValue.CompareTo(value); if (!result.Op.Matches(compareResult)) { continue; } result.MatchCount++; // If this is the matching row, write it EchoRow(reader, writer); } }
private static void MatchContains(ITabularReader reader, ITabularWriter writer, WhereResult result) { string valueString = (string)result.Value; String8 value = String8.Convert(valueString, new byte[String8.GetLength(valueString)]); while (reader.NextRow()) { // Ensure the row has enough columns if (reader.CurrentRowColumns <= result.ColumnIndex) { continue; } // Match the value if (reader.Current(result.ColumnIndex).ToString8().IndexOf(value) == -1) { continue; } result.MatchCount++; // If this is the matching row, write it EchoRow(reader, writer); } }