/// <summary> /// Initializes a new instance of the <see cref="ProtoDataStream"/> class. /// </summary> /// <param name="dataTable">The <see cref="DataTable"/>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( DataTable dataTable, ProtoDataWriterOptions options, int bufferSize = DefaultBufferSize) : this(dataTable.CreateDataReader(), options, bufferSize) { }
/// <summary> /// Serialize an <see cref="IDataReader"/> to a binary stream using protocol-buffers. /// </summary> /// <param name="stream">The <see cref="Stream"/> to write to.</param> /// <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> 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="DataSet"/> to a binary stream using protocol-buffers. /// </summary> /// <param name="stream">The <see cref="Stream"/> to write to.</param> /// <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> public void Serialize(Stream stream, DataSet dataSet, ProtoDataWriterOptions options) { if (stream == null) { throw new ArgumentNullException("stream"); } if (dataSet == null) { throw new ArgumentNullException("dataSet"); } using (DataTableReader reader = dataSet.CreateDataReader()) { this.Serialize(stream, reader, 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; this.resultIndex = 0; this.bufferStream = new CircularStream(bufferSize); this.writer = new ProtoWriter(this.bufferStream, null, null); }
/// <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) { Throw.IfNull(stream, nameof(stream)); Throw.IfNull(reader, nameof(reader)); options = options ?? new ProtoDataWriterOptions(); var resultIndex = 0; using (var writer = new ProtoWriter(stream, null, null)) { var context = new ProtoWriterContext(writer, options); do { ProtoWriter.WriteFieldHeader(1, WireType.StartGroup, writer); context.StartSubItem(resultIndex); context.Columns = ProtoDataColumnFactory.GetColumns(reader, options); ColumnsWriter.WriteColumns(context); var recordIndex = 0; while (reader.Read()) { RecordWriter.WriteRecord(context, recordIndex, reader); recordIndex++; } context.EndSubItem(); 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="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); SubItemToken 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> /// <param name="options">Writer options.</param> public void Serialize(Stream stream, DataTable dataTable, ProtoDataWriterOptions options) { Serialize(stream, dataTable.CreateDataReader(), 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); }
/// <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="dataSet">The <see cref="System.Data.DataSet"/>who's contents to serialize.</param> /// <param name="options">Writer options.</param> public void Serialize(Stream stream, DataSet dataSet, ProtoDataWriterOptions options) { this.Serialize(stream, dataSet.CreateDataReader(), options); }