public override Dataset ReadTableData(string command, FormatterManager formatter) { using var connection = CreateDbConnection(ConnectionString); connection.Open(); using var sqlCommand = CreateDbCommand(command, connection); using var reader = sqlCommand.ExecuteReader(CommandBehavior.KeyInfo); var tableName = reader.GetSchemaTable().Rows[0]["BaseTableName"].ToString(); var dataset = new Dataset(); var rows = new List <Row>(); while (reader.Read()) { var columns = new List <Column>(); for (var i = 0; i < reader.VisibleFieldCount; i++) { var column = new Column(reader.GetName(i), formatter.Format(tableName ?? "", reader.GetName(i), reader[i])); columns.Add(column); } var row = new Row { Columns = columns }; rows.Add(row); } dataset.Rows = rows; return(dataset); }
public override DatasetElement ReadTable(string command, FormatterManager formatter) { DatasetElement result = new DatasetElement(); result.Data = new XElement("data"); using (var conn = CreateDbConnection(ConnectionString)) { conn.Open(); using (var comm = CreateDbCommand(command, conn)) { var reader = comm.ExecuteReader(CommandBehavior.KeyInfo); var tableSchema = reader.GetSchemaTable(); result.Table = tableSchema.TableName; while (reader.Read()) { XElement xmlRow = new XElement("row"); result.Data.Add(xmlRow); for (int i = 0; i < reader.FieldCount; i++) { var columnName = reader.GetName(i); var value = formatter.Format(tableSchema.TableName, columnName, reader[i]); var attribute = new XAttribute(columnName, value); xmlRow.Add(attribute); } } } } return(result); }
public void Format_WhenNoFormattersAreRegistered_ReturnValueAsString() { formatterManager = new FormatterManager(); var actual = formatterManager.Format("TableB", "ColumnA", 5); actual.Should().Be("5", "because there aren't any formatters registered"); }
public void Format_When_there_are_zero_registered_formatters_Format_must_return_value_to_string() { _target = new FormatterManager(); var actual = _target.Format("TableB", "ColumnA", 5); Assert.AreEqual("5", actual); }
public static Object.Collections.DataCollection BuildDataCollection(string connectionString, string tableName, IEnumerable <string> fields, IEnumerable <FieldFilter> filters, FormatterManager formatter) { var rows = new List <Object.Collections.DataRow>(); var command = $"SELECT {string.Join(",", fields)} FROM {tableName}"; if (filters != null && filters.Any()) { filters = SpecflowDbValidatorHelper.AddQuotationMarks(filters); var filtering = new List <string>(); foreach (var filter in filters) { filtering.Add($"{filter.FieldName} IN ({filter.FieldValues})"); } command = $"{command}{Environment.NewLine}{"WHERE"} {string.Join(" AND ", filtering)}"; } using (var conn = CreateDbConnection(connectionString)) { using (var comm = CreateDbCommand(command, conn)) { conn.Open(); var reader = comm.ExecuteReader(CommandBehavior.KeyInfo); while (reader.Read()) { var dataRow = new Object.Collections.DataRow(); rows.Add(dataRow); var dataCells = new List <Object.Collections.DataCell>(); for (int i = 0; i < reader.FieldCount; i++) { var columnName = reader.GetName(i); var dbValue = reader[i]; var value = dbValue.GetType() == typeof(DBNull) ? null : formatter.Format(tableName, columnName, dbValue); var cell = new KeyValuePair <string, string>(columnName, value); var dataCell = Object.Collections.DataCell.Load(cell); dataCells.Add(dataCell); } dataRow.Values = dataCells.ToArray(); } } } return(new Object.Collections.DataCollection { Rows = rows.ToArray(), }); }
public void Format_WhenRegisteredFormatterIsNotFound_ReturnValueAsString() { var actual = formatterManager.Format("TableB", "ColumnB", 5); actual.Should().Be("5", "because a registered formatter could not be found that matches"); }