public void ShouldNotSerializeExpressionColumn() { var isRunningOnMono = Type.GetType("Mono.Runtime") != null; if (!isRunningOnMono) { // Arrange var columnName = "foo"; var dataTable = new DataTable(); dataTable.Columns.Add(columnName, typeof(int)); dataTable.Rows.Add(1); var dataReader = dataTable.CreateDataReader(); dataReader.GetSchemaTable().Rows[0]["Expression"] = true; var options = new ProtoDataWriterOptions() { IncludeComputedColumns = false }; // Act var reader = new ProtoReader(this.Serialize(dataReader, options), null, null); // Assert var readerContext = new ProtoReaderContext(reader); readerContext.ReadExpectedFieldHeader(ResultFieldHeader); readerContext.StartSubItem(); Assert.Equal(RecordFieldHeader, reader.ReadFieldHeader()); } }
public RowWriter( ProtoWriter writer, IEnumerable<ProtoDataColumn> columns, ProtoDataWriterOptions options) { if (writer == null) { throw new ArgumentNullException("writer"); } if (columns == null) { throw new ArgumentNullException("columns"); } if (options == null) { throw new ArgumentNullException("options"); } this.writer = writer; this.columns = columns; this.options = options; rowIndex = 0; }
/// <summary> /// Initializes a new instance of the <see cref="ProtoDataStream"/> class. /// </summary> /// <param name="dataSet">The <see cref="DataSet"/>who's contents to serialize.</param> /// <param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> /// <param name="bufferSize">Buffer size to use when serializing rows. /// You should not need to change this unless you have exceptionally /// large rows or an exceptionally high number of columns.</param> public ProtoDataStream( DataSet dataSet, ProtoDataWriterOptions options, int bufferSize = DefaultBufferSize) : this(dataSet.CreateDataReader(), options, bufferSize) { }
public RowWriter( ProtoWriter writer, IEnumerable <ProtoDataColumn> columns, ProtoDataWriterOptions options) { if (writer == null) { throw new ArgumentNullException("writer"); } if (columns == null) { throw new ArgumentNullException("columns"); } if (options == null) { throw new ArgumentNullException("options"); } this.writer = writer; this.columns = columns; this.options = options; rowIndex = 0; }
public void ShouldSerializeExpressionColumn() { // Arrange var columnName = "foo"; var dataTable = new DataTable(); dataTable.Columns.Add(columnName, typeof(int)); dataTable.Rows.Add(1); var dataReader = dataTable.CreateDataReader(); dataReader.GetSchemaTable().Rows[0]["Expression"] = true; var options = new ProtoDataWriterOptions() { IncludeComputedColumns = true }; // Act this.writer.Serialize(this.stream, dataReader, options); // Assert this.stream.Position = 0; this.ReadUntilColumnName(); Assert.Equal(columnName, this.reader.ReadString()); }
public void ShouldNotSerializeExpressionColumnOnMono() { var isRunningOnMono = Type.GetType("Mono.Runtime") != null; if (isRunningOnMono) { // Arrange var columnName = "foo"; var dataTable = new DataTable(); dataTable.Columns.Add(columnName, typeof(int)); dataTable.Rows.Add(1); var dataReader = dataTable.CreateDataReader(); dataReader.GetSchemaTable().Rows[0]["Expression"] = string.Empty; var options = new ProtoDataWriterOptions() { IncludeComputedColumns = false }; // Act this.writer.Serialize(this.stream, dataReader, options); // Assert this.stream.Position = 0; this.ReadExpectedFieldHeader(ResultFieldHeader); this.StartSubItem(); Assert.Equal(RecordFieldHeader, this.reader.ReadFieldHeader()); } }
public void ShouldSerializeDataSetUsingOptions() { // Arrange var dataSet = new DataSet(); dataSet.Tables.Add(this.CreateTable(new char[0])); var stream = new MemoryStream(); var options = new ProtoDataWriterOptions() { SerializeEmptyArraysAsNull = true }; // Act DataSerializer.Serialize(stream, dataSet, options); // Assert stream.Position = 0; var dataReader = new ProtoDataReader(stream); dataReader.Read(); Assert.True(dataReader.IsDBNull(0)); }
public void ShouldSerializeExpressionColumn() { // Arrange var columnName = "foo"; var dataTable = new DataTable(); dataTable.Columns.Add(columnName, typeof(int)); dataTable.Rows.Add(1); var dataReader = dataTable.CreateDataReader(); dataReader.GetSchemaTable().Rows[0]["Expression"] = true; var options = new ProtoDataWriterOptions() { IncludeComputedColumns = true }; // Act var reader = new ProtoReader(this.Serialize(dataReader, options), null, null); // Assert var readerContext = new ProtoReaderContext(reader); readerContext.ReadUntilColumnName(); Assert.Equal(columnName, reader.ReadString()); }
public IList<ProtoDataColumn> GetColumns( IDataReader reader, ProtoDataWriterOptions options) { if (reader == null) { throw new ArgumentNullException("reader"); } if (options == null) { throw new ArgumentNullException("options"); } using (DataTable schema = reader.GetSchemaTable()) { bool schemaSupportsExpressions = schema.Columns.Contains("Expression"); var columns = new List<ProtoDataColumn>(schema.Rows.Count); for (int i = 0; i < schema.Rows.Count; i++) { // Assumption: rows in the schema table are always ordered by // Ordinal position, ascending DataRow row = schema.Rows[i]; // Skip computed columns unless requested. if (schemaSupportsExpressions) { bool isComputedColumn; if (IsRunningOnMono) { isComputedColumn = Equals(row["Expression"], string.Empty); } else { isComputedColumn = !(row["Expression"] is DBNull); } if (isComputedColumn && !options.IncludeComputedColumns) { continue; } } var col = new ProtoDataColumn { ColumnIndex = i, ProtoDataType = ConvertProtoDataType.FromClrType((Type)row["DataType"]), ColumnName = (string)row["ColumnName"] }; columns.Add(col); } return columns; } }
public IList <ProtoDataColumn> GetColumns( IDataReader reader, ProtoDataWriterOptions options) { if (reader == null) { throw new ArgumentNullException("reader"); } if (options == null) { throw new ArgumentNullException("options"); } using (DataTable schema = reader.GetSchemaTable()) { bool schemaSupportsExpressions = schema.Columns.Contains("Expression"); var columns = new List <ProtoDataColumn>(schema.Rows.Count); for (int i = 0; i < schema.Rows.Count; i++) { // Assumption: rows in the schema table are always ordered by // Ordinal position, ascending DataRow row = schema.Rows[i]; // Skip computed columns unless requested. if (schemaSupportsExpressions) { bool isComputedColumn; if (IsRunningOnMono) { isComputedColumn = Equals(row["Expression"], string.Empty); } else { isComputedColumn = !(row["Expression"] is DBNull); } if (isComputedColumn && !options.IncludeComputedColumns) { continue; } } var col = new ProtoDataColumn { ColumnIndex = i, ProtoDataType = ConvertProtoDataType.FromClrType((Type)row["DataType"]), ColumnName = (string)row["ColumnName"] }; columns.Add(col); } return(columns); } }
public static DataTable SerializeAndDeserialize(DataTable dataTable, ProtoDataWriterOptions options) { using (var stream = new MemoryStream()) { DataSerializer.Serialize(stream, dataTable, options); stream.Seek(0, SeekOrigin.Begin); return(DataSerializer.DeserializeDataTable(stream)); } }
public void ShouldReturnFalseIfOtherIsNull() { // Arrange var options = new ProtoDataWriterOptions(); // Act var result = options.Equals(null); // Assert Assert.False(result); }
private Stream Serialize(IDataReader dataReader, ProtoDataWriterOptions options = null) { var writer = new ProtoDataWriter(); var stream = new MemoryStream(); writer.Serialize(stream, dataReader); stream.Position = 0; return(stream); }
public void ShouldReturnTrueIfSameInstance() { // Arrange var options = new ProtoDataWriterOptions(); // Act var result = options.Equals(options); // Assert Assert.True(result); }
public void ShouldReturnFalseIfOtherIsNull() { // Arrange var options = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; // Act var result = options.GetHashCode(); // Assert Assert.Equal(396, result); }
public void ShouldReturnFalseIfTypeDiffers() { // Arrange var options = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; var other = new { }; // Act var result = options.Equals(other); // Assert Assert.False(result); }
public void ShouldReturnFalseIfSerializeEmptyArraysAsNullDiffers() { // Arrange var options = new ProtoDataWriterOptions() { SerializeEmptyArraysAsNull = true }; var other = new ProtoDataWriterOptions() { SerializeEmptyArraysAsNull = false }; // Act var result = options.Equals(other); // Assert Assert.False(result); }
public void ShouldReturnFalseIfIncludeComputedColumnsDiffers() { // Arrange var options = new ProtoDataWriterOptions() { IncludeComputedColumns = true }; var other = new ProtoDataWriterOptions() { IncludeComputedColumns = false }; // Act var result = options.Equals(other); // Assert Assert.False(result); }
public void ShouldReturnTrueIfIncludeComputedColumnsAndIncludeComputedColumnsAreSame() { // Arrange var options = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; var other = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; // Act var result = options.Equals(other); // Assert Assert.True(result); }
public static IList <ProtoDataColumn> GetColumns(IDataReader reader, ProtoDataWriterOptions options) { using (var schemaTable = reader.GetSchemaTable()) { var schemaSupportsExpressions = schemaTable.Columns.Contains("Expression"); var columns = new List <ProtoDataColumn>(schemaTable.Rows.Count); for (var i = 0; i < schemaTable.Rows.Count; i++) { // Assumption: rows in the schema table are always ordered by // Ordinal position, ascending var row = schemaTable.Rows[i]; // Skip computed columns unless requested. if (schemaSupportsExpressions) { bool isComputedColumn; if (IsRunningOnMono) { isComputedColumn = Equals(row["Expression"], string.Empty); } else { isComputedColumn = !(row["Expression"] is DBNull); } if (isComputedColumn && !options.IncludeComputedColumns) { continue; } } var columnName = (string)row["ColumnName"]; var dataType = (Type)row["DataType"]; var protoBufDataType = ConvertProtoDataType.FromClrType(dataType); columns.Add(new ProtoDataColumn(columnName, dataType, protoBufDataType)); } return(columns); } }
public void ShouldReturnDifferentHashCodeIfSerializeEmptyArraysAsNullDiffers() { // Arrange var options1 = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; var options2 = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = false }; // Act var result1 = options1.GetHashCode(); var result2 = options2.GetHashCode(); // Assert Assert.NotEqual(result1, result2); }
public void ShouldReturnSameHashCodeIfIncludeComputedColumnsAndSerializeEmptyArraysAsNullAreSame() { // Arrange var options1 = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; var options2 = new ProtoDataWriterOptions() { IncludeComputedColumns = true, SerializeEmptyArraysAsNull = true }; // Act var result1 = options1.GetHashCode(); var result2 = options2.GetHashCode(); // Assert Assert.Equal(result1, result2); }
public void Should_retain_binary_compatibility_with_previous_versions_when_writing() { using (DataSet dataSet = CreateTablesForBackwardsCompatibilityTest()) using (var stream = new MemoryStream()) { using (DataTableReader reader = dataSet.CreateDataReader()) { var options = new ProtoDataWriterOptions { SerializeEmptyArraysAsNull = true }; DataSerializer.Serialize(stream, reader, options); } byte[] expected = File.ReadAllBytes(PreviousVersionTestFile); stream.Seek(0, SeekOrigin.Begin); stream.GetBuffer().Take(expected.Length).Should().Have.SameSequenceAs(expected); } }
public void ShouldSerializeEmptyCharArrayAsNull() { // Arrange var dataReader = this.CreateDataReader(new char[0]); var options = new ProtoDataWriterOptions() { SerializeEmptyArraysAsNull = true }; // Act var reader = new ProtoReader(this.Serialize(dataReader, options), null, null); // Assert var readerContext = new ProtoReaderContext(reader); readerContext.ReadUntilField(); readerContext.ReadExpectedFieldHeader(3); readerContext.StartSubItem(); Assert.Equal(0, reader.ReadFieldHeader()); }
public void ShouldNotSerializeIfValueIsEmptyArray() { // Arrange var value = new char[0]; var dataReader = this.CreateDataReader(value); var options = new ProtoDataWriterOptions() { SerializeEmptyArraysAsNull = true }; // Act this.writer.Serialize(this.stream, dataReader, options); // Assert this.stream.Position = 0; this.ReadUntilField(); this.ReadExpectedFieldHeader(3); Assert.Equal(0, this.reader.ReadFieldHeader()); }
public void ShouldNotSerializeIfValueIsEmptyArray() { // Arrange var value = new char[0]; var dataReader = this.CreateDataReader(value); var options = new ProtoDataWriterOptions() { SerializeEmptyArraysAsNull = true }; // Act var reader = new ProtoReader(this.Serialize(dataReader, options), null, null); // Assert var readerContext = new ProtoReaderContext(reader); readerContext.ReadUntilField(); readerContext.ReadExpectedFieldHeader(3); Assert.Equal(0, reader.ReadFieldHeader()); }
public IEnumerable<ProtoDataColumn> GetColumns( IDataReader reader, ProtoDataWriterOptions options) { if (reader == null) throw new ArgumentNullException("reader"); if (options == null) throw new ArgumentNullException("options"); using (var schema = reader.GetSchemaTable()) { var schemaSupportsExpressions = schema.Columns.Contains("Expression"); var columns = new List<ProtoDataColumn>(schema.Rows.Count); for (var i = 0; i < schema.Rows.Count; i++) { // Assumption: rows in the schema table are always ordered by // Ordinal position, ascending var row = schema.Rows[i]; // Skip computed columns unless requested. var isComputedColumn = schemaSupportsExpressions && !(row["Expression"] is DBNull); if (isComputedColumn && !options.IncludeComputedColumns) continue; var col = new ProtoDataColumn { ColumnIndex = i, ProtoDataType = ConvertProtoDataType.FromClrType((Type)row["DataType"]), ColumnName = (string)row["ColumnName"] }; columns.Add(col); } return columns; } }
///<summary> /// Serialize an <see cref="System.Data.IDataReader"/> to a binary stream using protocol-buffers. ///</summary> ///<param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> ///<param name="reader">The <see cref="System.Data.IDataReader"/>who's contents to serialize.</param> ///<param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> public void Serialize(Stream stream, IDataReader reader, ProtoDataWriterOptions options) { if (stream == null) throw new ArgumentNullException("stream"); if (reader == null) throw new ArgumentNullException("reader"); // Null options are permitted to be passed in. options = options ?? new ProtoDataWriterOptions(); // For a (minor) performance improvement, Serialize() has been left // as a single long method with functions manually inlined. var resultIndex = 0; using (var writer = new ProtoWriter(stream, null, null)) { do { // This is the underlying protocol buffers structure we use: // // <1 StartGroup> each DataTable // <SubItem> // <2 StartGroup> each DataColumn // <SubItem> // <1 String> Column Name // <2 Variant> Column ProtoDataType (enum casted to int) // </SubItem> // <3 StartGroup> each DataRow // <SubItem> // <(# Column Index) (corresponding type)> Field Value // </SubItem> // </SubItem> // // NB if Field Value is a DataTable, the whole DataTable is // write the table ProtoWriter.WriteFieldHeader(1, WireType.StartGroup, writer); var resultToken = ProtoWriter.StartSubItem(resultIndex, writer); var columns = new ProtoDataColumnFactory().GetColumns(reader, options); new HeaderWriter(writer).WriteHeader(columns); var rowWriter = new RowWriter(writer, columns, options); // write the rows while (reader.Read()) rowWriter.WriteRow(reader); ProtoWriter.EndSubItem(resultToken, writer); resultIndex++; } while (reader.NextResult()); } }
///<summary> /// Serialize an <see cref="System.Data.IDataReader"/> to a binary stream using protocol-buffers. ///</summary> ///<param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> ///<param name="dataTable">The <see cref="System.Data.DataTable"/>who's contents to serialize.</param> public void Serialize(Stream stream, DataTable dataTable, ProtoDataWriterOptions options) { Serialize(stream, dataTable.CreateDataReader(), options); }
/// <summary> /// Initializes a new instance of the <see cref="ProtoDataStream"/> class. /// </summary> /// <param name="reader">The <see cref="IDataReader"/>who's contents to serialize.</param> /// <param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> /// <param name="bufferSize">Buffer size to use when serializing rows. /// You should not need to change this unless you have exceptionally /// large rows or an exceptionally high number of columns.</param> public ProtoDataStream( IDataReader reader, ProtoDataWriterOptions options, int bufferSize = DefaultBufferSize) { if (reader == null) throw new ArgumentNullException("reader"); if (options == null) throw new ArgumentNullException("options"); this.reader = reader; this.options = options; resultIndex = 0; columnFactory = new ProtoDataColumnFactory(); bufferStream = new CircularStream(bufferSize); writer = new ProtoWriter(bufferStream, null, null); }
///<summary> /// Serialize a <see cref="System.Data.DataTable"/> to a binary stream using protocol-buffers. ///</summary> ///<param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> ///<param name="dataTable">The <see cref="System.Data.DataTable"/> who's contents to serialize.</param> ///<param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> public static void Serialize(Stream stream, DataTable dataTable, ProtoDataWriterOptions options) { engine.Serialize(stream, dataTable, options); }
public void Should_retain_binary_compatibility_with_previous_versions_when_writing() { using (DataSet dataSet = CreateTablesForBackwardsCompatibilityTest()) using (var stream = new MemoryStream()) { using (DataTableReader reader = dataSet.CreateDataReader()) { var options = new ProtoDataWriterOptions { SerializeEmptyArraysAsNull = true }; DataSerializer.Serialize(stream, reader, options); } byte[] expected = File.ReadAllBytes(PreviousVersionTestFile); stream.Seek(0, SeekOrigin.Begin); stream.GetBuffer().Take(expected.Length).Should().Have.SameSequenceAs(expected); } }
///<summary> /// Serialize an <see cref="System.Data.IDataReader"/> to a binary stream using protocol-buffers. ///</summary> ///<param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> ///<param name="reader">The <see cref="System.Data.IDataReader"/> who's contents to serialize.</param> ///<param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> public void Serialize(Stream stream, IDataReader reader, ProtoDataWriterOptions options) { if (stream == null) throw new ArgumentNullException("stream"); if (reader == null) throw new ArgumentNullException("reader"); writer.Serialize(stream, reader, options); }
///<summary> /// Serialize a <see cref="System.Data.DataSet"/> to a binary stream using protocol-buffers. ///</summary> ///<param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> ///<param name="dataSet">The <see cref="System.Data.DataSet"/> who's contents to serialize.</param> ///<param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> public void Serialize(Stream stream, DataSet dataSet, ProtoDataWriterOptions options) { if (stream == null) throw new ArgumentNullException("stream"); if (dataSet == null) throw new ArgumentNullException("dataSet"); using (var reader = dataSet.CreateDataReader()) Serialize(stream, reader, options); }
public ProtoWriterContext(ProtoWriter writer, ProtoDataWriterOptions options) { this.Writer = writer; this.Options = options; }
/// <summary> /// Serialize a <see cref="System.Data.DataSet"/> to a binary stream using protocol-buffers. /// </summary> /// <param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> /// <param name="dataSet">The <see cref="System.Data.DataSet"/> who's contents to serialize.</param> /// <param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> public static void Serialize(Stream stream, DataSet dataSet, ProtoDataWriterOptions options) { Engine.Serialize(stream, dataSet, options); }
/// <summary> /// Serialize an <see cref="System.Data.IDataReader"/> to a binary stream using protocol-buffers. /// </summary> /// <param name="stream">The <see cref="System.IO.Stream"/> to write to.</param> /// <param name="reader">The <see cref="System.Data.IDataReader"/> who's contents to serialize.</param> /// <param name="options"><see cref="ProtoDataWriterOptions"/> specifying any custom serialization options.</param> public static void Serialize(Stream stream, IDataReader reader, ProtoDataWriterOptions options) { Engine.Serialize(stream, reader, options); }