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);
            }
        }
예제 #2
0
        static ProtoDataTypes()
        {
            Array values = Enum.GetValues(typeof(ProtoDataType));

            AllTypes = new ProtoDataType[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                AllTypes[i] = (ProtoDataType)values.GetValue(i);
            }

            AllClrTypes = new Type[values.Length];
            for (int i = 0; i < AllTypes.Length; i++)
            {
                AllClrTypes[i] = ConvertProtoDataType.ToClrType(AllTypes[i]);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        public void WriteRow(IDataRecord row)
        {
            int fieldIndex = 1;

            ProtoWriter.WriteFieldHeader(3, WireType.StartGroup, writer);
            SubItemToken token = ProtoWriter.StartSubItem(rowIndex, writer);

            foreach (ProtoDataColumn column in columns)
            {
                object value = row[column.ColumnIndex];
                if (value == null || value is DBNull || (options.SerializeEmptyArraysAsNull && IsZeroLengthArray(value)))
                {
                    // don't write anything
                }
                else
                {
                    switch (column.ProtoDataType)
                    {
                    case ProtoDataType.String:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.String, writer);
                        ProtoWriter.WriteString((string)value, writer);
                        break;

                    case ProtoDataType.Short:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Variant, writer);
                        ProtoWriter.WriteInt16((short)value, writer);
                        break;

                    case ProtoDataType.Decimal:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.StartGroup, writer);
                        BclHelpers.WriteDecimal((decimal)value, writer);
                        break;

                    case ProtoDataType.Int:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Variant, writer);
                        ProtoWriter.WriteInt32((int)value, writer);
                        break;

                    case ProtoDataType.Guid:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.StartGroup, writer);
                        BclHelpers.WriteGuid((Guid)value, writer);
                        break;

                    case ProtoDataType.DateTime:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.StartGroup, writer);
                        BclHelpers.WriteDateTime((DateTime)value, writer);
                        break;

                    case ProtoDataType.Bool:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Variant, writer);
                        ProtoWriter.WriteBoolean((bool)value, writer);
                        break;

                    case ProtoDataType.Byte:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Variant, writer);
                        ProtoWriter.WriteByte((byte)value, writer);
                        break;

                    case ProtoDataType.Char:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Variant, writer);
                        ProtoWriter.WriteInt16((short)(char)value, writer);
                        break;

                    case ProtoDataType.Double:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Fixed64, writer);
                        ProtoWriter.WriteDouble((double)value, writer);
                        break;

                    case ProtoDataType.Float:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Fixed32, writer);
                        ProtoWriter.WriteSingle((float)value, writer);
                        break;

                    case ProtoDataType.Long:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.Variant, writer);
                        ProtoWriter.WriteInt64((long)value, writer);
                        break;

                    case ProtoDataType.ByteArray:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.String, writer);
                        ProtoWriter.WriteBytes((byte[])value, 0, ((byte[])value).Length, writer);
                        break;

                    case ProtoDataType.CharArray:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.String, writer);
                        ProtoWriter.WriteString(new string((char[])value), writer);
                        break;

                    case ProtoDataType.TimeSpan:
                        ProtoWriter.WriteFieldHeader(fieldIndex, WireType.StartGroup, writer);
                        BclHelpers.WriteTimeSpan((TimeSpan)value, writer);
                        break;

                    default:
                        throw new UnsupportedColumnTypeException(
                                  ConvertProtoDataType.ToClrType(column.ProtoDataType));
                    }
                }

                fieldIndex++;
            }

            ProtoWriter.EndSubItem(token, writer);
            rowIndex++;
        }
예제 #5
0
        public static void WriteRecord(ProtoWriterContext context, int recordIndex, IDataRecord record)
        {
            ProtoWriter.WriteFieldHeader(RecordFieldHeader, WireType.StartGroup, context.Writer);

            context.StartSubItem(recordIndex);

            for (var columnIndex = 0; columnIndex < context.Columns.Count; columnIndex++)
            {
                // The check whether record[columnIndex] == null is removed due to the fact that value types have to be
                // boxed into an object just to check whether the field value is null. This creates garbage, thus has
                // impact on garbage collection. Removing the check should not be a problem as IDataRecord.IsDBNull
                // should be sufficient (tested with SqlDataReader, SqlLiteDataReader, and DataTableReader).
                if (record.IsDBNull(columnIndex))
                {
                    continue;
                }

                var fieldNumber = columnIndex + 1;

                switch (context.Columns[columnIndex].ProtoDataType)
                {
                case ProtoDataType.String:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.String, context.Writer);
                    ProtoWriter.WriteString(record.GetString(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Short:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt16(record.GetInt16(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Decimal:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteDecimal(record.GetDecimal(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Int:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt32(record.GetInt32(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Guid:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteGuid(record.GetGuid(columnIndex), context.Writer);
                    break;

                case ProtoDataType.DateTime:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteDateTime(record.GetDateTime(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Bool:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteBoolean(record.GetBoolean(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Byte:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteByte(record.GetByte(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Char:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt16((short)record.GetChar(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Double:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Fixed64, context.Writer);
                    ProtoWriter.WriteDouble(record.GetDouble(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Float:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Fixed32, context.Writer);
                    ProtoWriter.WriteSingle(record.GetFloat(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Long:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt64(record.GetInt64(columnIndex), context.Writer);
                    break;

                case ProtoDataType.ByteArray:
                    var bytes = (byte[])record[columnIndex];

                    if (bytes.Length != 0 || !context.Options.SerializeEmptyArraysAsNull)
                    {
                        ProtoWriter.WriteFieldHeader(fieldNumber, WireType.String, context.Writer);
                        ProtoWriter.WriteBytes(bytes, 0, bytes.Length, context.Writer);
                    }

                    break;

                case ProtoDataType.CharArray:
                    var characters = (char[])record[columnIndex];

                    if (characters.Length != 0 || !context.Options.SerializeEmptyArraysAsNull)
                    {
                        ProtoWriter.WriteFieldHeader(fieldNumber, WireType.String, context.Writer);
                        ProtoWriter.WriteString(new string(characters), context.Writer);
                    }

                    break;

                case ProtoDataType.TimeSpan:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteTimeSpan((TimeSpan)record[columnIndex], context.Writer);
                    break;

                default:
                    throw new UnsupportedColumnTypeException(ConvertProtoDataType.ToClrType(context.Columns[columnIndex].ProtoDataType));
                }
            }

            context.EndSubItem();
        }