Пример #1
0
        /// <summary>
        /// Ctr.
        /// </summary>
        /// <param name="connection">Parent connection</param>
        /// <param name="inputStream">Incoming data stream from server</param>
        /// <param name="holders">Array of holder objects that must be disposed when reader is closed</param>
        public PqlDataReader(PqlDataConnection connection, BufferedReaderStream inputStream, params IDisposable[] holders)
        {
            m_protocolUtility = new PqlProtocolUtility(connection, inputStream, holders);
            try
            {
                m_reader = inputStream.MyReader;
                m_scheme = m_protocolUtility.ReadResponseHeaders(inputStream);

                if (m_scheme.Fields != null && m_scheme.Fields.Length > 0)
                {
                    m_currentRow = new RowData(m_scheme.Fields.Select(x => x.DataType).ToArray());
                    m_state = ReaderState.New;
                }
                else
                {
                    m_state = ReaderState.Closed;
                }

                connection.SwitchToFetchingState();
            }
            catch
            {
                m_protocolUtility.Dispose();
                throw;
            }
        }
Пример #2
0
        public DataGenBulk(int count, int firstId)
        {
            Count = count;
            FirstId = firstId;

            FieldNames = new[]
            {
                "id", "fieldByte1", "fieldGuid2", "fieldString3", "fieldBinary4", "fieldDecimal5", "fieldBool6",
                "fieldBool15", "fieldDecimal14"
            };
            m_rowData =
                new RowData(new[]
                {
                    DbType.Int64, DbType.Byte, DbType.Guid, DbType.String, DbType.Binary, DbType.Currency, DbType.Boolean,
                    DbType.Boolean, DbType.Decimal
                });
        }
Пример #3
0
        /// <summary>
        /// Returns a <see cref="T:System.Data.DataTable"/> that describes the column metadata of the <see cref="T:System.Data.IDataReader"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Data.DataTable"/> that describes the column metadata.
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Data.IDataReader"/> is closed. </exception>
        /// <filterpriority>2</filterpriority>
        public override DataTable GetSchemaTable()
        {
            var dataTable = new DataTable("SchemaTable")
            {
                Locale = CultureInfo.InvariantCulture, MinimumCapacity = m_scheme.FieldCount
            };
            var cColumnName               = new DataColumn(SchemaTableColumn.ColumnName, typeof(string));
            var cColumnOrdinal            = new DataColumn(SchemaTableColumn.ColumnOrdinal, typeof(int));
            var cColumnSize               = new DataColumn(SchemaTableColumn.ColumnSize, typeof(int));
            var cNumericPrecision         = new DataColumn(SchemaTableColumn.NumericPrecision, typeof(short));
            var cNumericScale             = new DataColumn(SchemaTableColumn.NumericScale, typeof(short));
            var cDataType                 = new DataColumn(SchemaTableColumn.DataType, typeof(Type));
            var cProviderSpecificDataType = new DataColumn(SchemaTableOptionalColumn.ProviderSpecificDataType, typeof(Type));
            var cNonVersionedProviderType = new DataColumn(SchemaTableColumn.NonVersionedProviderType, typeof(int));
            var cProviderType             = new DataColumn(SchemaTableColumn.ProviderType, typeof(int));
            var cIsLong                          = new DataColumn(SchemaTableColumn.IsLong, typeof(bool));
            var cAllowDbNull                     = new DataColumn(SchemaTableColumn.AllowDBNull, typeof(bool));
            var cIsReadOnly                      = new DataColumn(SchemaTableOptionalColumn.IsReadOnly, typeof(bool));
            var cIsRowVersion                    = new DataColumn(SchemaTableOptionalColumn.IsRowVersion, typeof(bool));
            var sIsUnique                        = new DataColumn(SchemaTableColumn.IsUnique, typeof(bool));
            var sIsKey                           = new DataColumn(SchemaTableColumn.IsKey, typeof(bool));
            var cIsAutoIncrement                 = new DataColumn(SchemaTableOptionalColumn.IsAutoIncrement, typeof(bool));
            var sIsHidden                        = new DataColumn(SchemaTableOptionalColumn.IsHidden, typeof(bool));
            var cBaseCatalogName                 = new DataColumn(SchemaTableOptionalColumn.BaseCatalogName, typeof(string));
            var cBaseSchemaName                  = new DataColumn(SchemaTableColumn.BaseSchemaName, typeof(string));
            var cBaseTableName                   = new DataColumn(SchemaTableColumn.BaseTableName, typeof(string));
            var cBaseColumnName                  = new DataColumn(SchemaTableColumn.BaseColumnName, typeof(string));
            var cBaseServerName                  = new DataColumn(SchemaTableOptionalColumn.BaseServerName, typeof(string));
            var cIsAliased                       = new DataColumn(SchemaTableColumn.IsAliased, typeof(bool));
            var sIsExpression                    = new DataColumn(SchemaTableColumn.IsExpression, typeof(bool));
            var cIsIdentity                      = new DataColumn("IsIdentity", typeof(bool));
            var cDataTypeName                    = new DataColumn("DataTypeName", typeof(string));
            var cUdtAssemblyQualifiedName        = new DataColumn("UdtAssemblyQualifiedName", typeof(string));
            var cXmlSchemaCollectionDatabase     = new DataColumn("XmlSchemaCollectionDatabase", typeof(string));
            var cXmlSchemaCollectionOwningSchema = new DataColumn("XmlSchemaCollectionOwningSchema", typeof(string));
            var cXmlSchemaCollectionName         = new DataColumn("XmlSchemaCollectionName", typeof(string));
            var cIsColumnSet                     = new DataColumn("IsColumnSet", typeof(bool));

            cColumnOrdinal.DefaultValue = 0;
            cIsLong.DefaultValue        = false;

            var columns = dataTable.Columns;

            columns.Add(cColumnName);
            columns.Add(cColumnOrdinal);
            columns.Add(cColumnSize);
            columns.Add(cNumericPrecision);
            columns.Add(cNumericScale);
            columns.Add(sIsUnique);
            columns.Add(sIsKey);
            columns.Add(cBaseServerName);
            columns.Add(cBaseCatalogName);
            columns.Add(cBaseColumnName);
            columns.Add(cBaseSchemaName);
            columns.Add(cBaseTableName);
            columns.Add(cDataType);
            columns.Add(cAllowDbNull);
            columns.Add(cProviderType);
            columns.Add(cIsAliased);
            columns.Add(sIsExpression);
            columns.Add(cIsIdentity);
            columns.Add(cIsAutoIncrement);
            columns.Add(cIsRowVersion);
            columns.Add(sIsHidden);
            columns.Add(cIsLong);
            columns.Add(cIsReadOnly);
            columns.Add(cProviderSpecificDataType);
            columns.Add(cDataTypeName);
            columns.Add(cXmlSchemaCollectionDatabase);
            columns.Add(cXmlSchemaCollectionOwningSchema);
            columns.Add(cXmlSchemaCollectionName);
            columns.Add(cUdtAssemblyQualifiedName);
            columns.Add(cNonVersionedProviderType);
            columns.Add(cIsColumnSet);

            foreach (var field in m_scheme.Fields)
            {
                var systemType = RowData.DeriveSystemType(field.DataType);

                var row = dataTable.NewRow();
                row[cColumnName]               = field.Name;
                row[cColumnOrdinal]            = field.Ordinal;
                row[cColumnSize]               = 0;
                row[cDataType]                 = systemType;
                row[cProviderSpecificDataType] = systemType;
                row[cNonVersionedProviderType] = field.DataType;
                row[cDataTypeName]             = GetDataTypeName(field.Ordinal);
                row[cProviderType]             = field.DataType;
                row[cNumericPrecision]         = DBNull.Value;
                row[cNumericScale]             = DBNull.Value;
                row[cAllowDbNull]              = true;
                row[cIsAliased]                = false;
                row[sIsKey]           = false;
                row[sIsHidden]        = false;
                row[sIsExpression]    = false;
                row[cIsIdentity]      = false;
                row[cIsAutoIncrement] = false;
                row[cIsLong]          = false;
                row[sIsUnique]        = false;
                row[cIsRowVersion]    = false;
                row[cIsReadOnly]      = false;
                row[cIsColumnSet]     = false;
                dataTable.Rows.Add(row);
                row.AcceptChanges();
            }

            foreach (DataColumn dataColumn in columns)
            {
                dataColumn.ReadOnly = true;
            }

            return(dataTable);
        }
Пример #4
0
 public override Type GetFieldType(int i)
 {
     return(RowData.DeriveSystemType(m_scheme.RequireField(i).DataType));
 }
        private static Action <BinaryWriter, object> GetEnumeratorWriter(DbType dbType)
        {
            switch (dbType)
            {
            //case DbType.VarNumeric:
            //    break;
            case DbType.AnsiString:
            case DbType.String:
            case DbType.AnsiStringFixedLength:
            case DbType.StringFixedLength:
            case DbType.Xml:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <string>)untyped;
                    while (typed.MoveNext())
                    {
                        var data = typed.Current;
                        var len = data == null ? -1 : data.Length;
                        RowData.Write7BitEncodedInt(writer, len);
                        for (var i = 0; i < len; i++)
                        {
                            RowData.Write7BitEncodedInt(writer, data[i]);
                        }
                    }
                });

            case DbType.Binary:
            case DbType.Object:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <byte[]>)untyped;
                    while (typed.MoveNext())
                    {
                        var data = typed.Current;
                        var len = data == null ? -1 : data.Length;
                        RowData.Write7BitEncodedInt(writer, len);
                        if (len > 0)
                        {
                            writer.Write(data, 0, data.Length);
                        }
                    }
                });

            case DbType.Byte:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Byte>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Boolean:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Boolean>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Time:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <TimeSpan>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current.Ticks);
                    }
                });

            case DbType.Date:
            case DbType.DateTime2:
            case DbType.DateTime:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <DateTime>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current.ToBinary());
                    }
                });

            case DbType.Currency:
            case DbType.Decimal:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Decimal>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Double:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Double>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Guid:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Guid>)untyped;
                    var curr = new RowData.ValueHolder16Bytes();
                    while (typed.MoveNext())
                    {
                        curr.AsGuid = typed.Current;
                        writer.Write(curr.Lo);
                        writer.Write(curr.Hi);
                    }
                });

            case DbType.Int16:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Int16>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Int32:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Int32>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Int64:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Int64>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.SByte:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <SByte>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.Single:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <Single>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.UInt16:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <UInt16>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.UInt32:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <UInt32>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.UInt64:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <UInt64>)untyped;
                    while (typed.MoveNext())
                    {
                        writer.Write(typed.Current);
                    }
                });

            case DbType.DateTimeOffset:
                return((writer, untyped) =>
                {
                    var typed = (IEnumerator <DateTimeOffset>)untyped;
                    var curr = new RowData.ValueHolder16Bytes();
                    while (typed.MoveNext())
                    {
                        curr.AsDateTimeOffset = typed.Current;
                        writer.Write(curr.Lo);
                        writer.Write(curr.Hi);
                    }
                });

            default:
                throw new DataException("Invalid DbType: " + dbType);
            }
        }
        private static void WritePrimitiveValue(BinaryWriter writer, DbType dbType, object value)
        {
            switch (dbType)
            {
            //case DbType.VarNumeric:
            //    break;
            case DbType.AnsiString:
            case DbType.String:
            case DbType.AnsiStringFixedLength:
            case DbType.StringFixedLength:
            case DbType.Xml:
            {
                var data = (string)value;
                var len  = data.Length;        // we don't expect null values here
                RowData.Write7BitEncodedInt(writer, len);
                for (var i = 0; i < len; i++)
                {
                    RowData.Write7BitEncodedInt(writer, data[i]);
                }
            }
            break;

            case DbType.Binary:
            case DbType.Object:
            {
                var data = (byte[])value;
                var len  = data.Length;        // we don't expect null values here
                RowData.Write7BitEncodedInt(writer, len);
                if (len > 0)
                {
                    writer.Write(data, 0, data.Length);
                }
            }
            break;

            case DbType.Byte:
                writer.Write((Byte)value);
                break;

            case DbType.Boolean:
                writer.Write((bool)value);
                break;

            case DbType.Time:
                writer.Write(((TimeSpan)value).Ticks);
                break;

            case DbType.Date:
            case DbType.DateTime2:
            case DbType.DateTime:
                writer.Write(((DateTime)value).ToBinary());
                break;

            case DbType.Currency:
            case DbType.Decimal:
                writer.Write((Decimal)value);
                break;

            case DbType.Double:
                writer.Write((Double)value);
                break;

            case DbType.Guid:
            {
                var curr = new RowData.ValueHolder16Bytes {
                    AsGuid = (Guid)value
                };
                writer.Write(curr.Lo);
                writer.Write(curr.Hi);
            }
            break;

            case DbType.Int16:
                writer.Write((Int16)value);
                break;

            case DbType.Int32:
                writer.Write((Int32)value);
                break;

            case DbType.Int64:
                writer.Write((Int64)value);
                break;

            case DbType.SByte:
                writer.Write((SByte)value);
                break;

            case DbType.Single:
                writer.Write((Single)value);
                break;

            case DbType.UInt16:
                writer.Write((UInt16)value);
                break;

            case DbType.UInt32:
                writer.Write((UInt32)value);
                break;

            case DbType.UInt64:
                writer.Write((UInt64)value);
                break;


            case DbType.DateTimeOffset:
            {
                var curr = new RowData.ValueHolder16Bytes {
                    AsDateTimeOffset = (DateTimeOffset)value
                };
                writer.Write(curr.Lo);
                writer.Write(curr.Hi);
            }
            break;

            default:
                throw new DataException("Invalid DbType: " + dbType);
            }
        }
        /// <summary>
        /// Validates consistency of configuration values on this parameter instance.
        /// </summary>
        public void Validate()
        {
            IsValidatedCollection = false;

            if (Direction != ParameterDirection.Input)
            {
                throw new DataException("Direction must be set to " + ParameterDirection.Input);
            }

            if (string.IsNullOrEmpty(ParameterName))
            {
                throw new DataException("ParameterName is null or empty");
            }

            if (ParameterName.Length < 2 || ParameterName[0] != '@')
            {
                throw new DataException("ParameterName must have at least two characters and start with '@'");
            }

            for (var index = 1; index < ParameterName.Length; index++)
            {
                var c = ParameterName[index];
                if (!Char.IsLetterOrDigit(c))
                {
                    throw new DataException("After initial '@', ParameterName can only have letters and digits: " + ParameterName);
                }
            }

            // validate DbType enum
            var expectedType = RowData.DeriveSystemType(DbType);

            // perform basic sanity check for most collection and value types
            if (Value != null && Value != DBNull.Value)
            {
                var vtype = Value.GetType();

                if (DbType == DbType.Binary || DbType == DbType.Object || vtype.IsValueType)
                {
                }
                else if (vtype.IsArray)
                {
                    if (((Array)Value).Rank != 1)
                    {
                        throw new DataException("Parameter value cannot be a multidimensional array");
                    }
                    vtype = vtype.GetElementType();
                    IsValidatedCollection = true;
                }
                else if (vtype != typeof(string))
                {
                    // check if Value is of supported collection type
                    var interfaces = vtype.GetInterfaces();
                    foreach (var intf in interfaces)
                    {
                        if (intf.IsGenericType)
                        {
                            var basetype = intf.GetGenericTypeDefinition();
                            if (basetype == typeof(ICollection <>) || basetype == typeof(IReadOnlyCollection <>))
                            {
                                vtype = intf.GetGenericArguments()[0];
                                if (vtype == expectedType)
                                {
                                    IsValidatedCollection = true;
                                    break;
                                }
                            }
                        }
                    }

                    // do not support basic ICollection in order to avoid unboxing when writing values to request stream
                    // boxing-unboxing can seriously affect client performance on large value sets
                    if (!IsValidatedCollection && Value is ICollection)
                    {
                        throw new DataException(
                                  "To provide a collection of values as a parameter's value, use strongly typed 1-dimensional arrays"
                                  + " or generic collections implementing ICollection<T>, IReadOnlyCollection<T>.");
                    }
                }

                if (vtype != expectedType)
                {
                    throw new DataException(
                              string.Format(
                                  "Value contains an instance of type {0}, which is different from what is implied by DbType ({1}): {2}",
                                  vtype.FullName, DbType, expectedType.FullName));
                }
            }
        }
 public void PrepareBuffersForSelect()
 {
     ClauseEvaluationContext.ChangeBuffer = null;
     OutputDataBuffer = new RowData(ResponseHeaders.Fields.Select(x => x.DataType).ToArray());
 }
        /// <summary>
        /// Ctr.
        /// </summary>
        /// <param name="countToRead">Number of <see cref="RowData"/> items to read, can be zero</param>
        /// <param name="fieldTypes">Expected field types</param>
        /// <param name="stream">Incoming data stream</param>
        /// <param name="driverRowData">Buffer to put field values into, after each call to <see cref="MoveNext"/></param>
        public InputDataStreamEnumerator(int countToRead, DbType[] fieldTypes, Stream stream, DriverRowData driverRowData)
        {
            if (countToRead < 0)
            {
                throw new ArgumentOutOfRangeException("countToRead", countToRead, "Count may not be negative");
            }

            if (fieldTypes == null)
            {
                throw new ArgumentNullException("fieldTypes");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (driverRowData == null)
            {
                throw new ArgumentNullException("driverRowData");
            }

            if (fieldTypes.Length == 0)
            {
                throw new ArgumentException("Count of fields in client row buffer must be greater than zero, first column must contain primary key value");
            }

            if (driverRowData.FieldTypes.Length != fieldTypes.Length)
            {
                throw new ArgumentException(string.Format("Count of fields in client row buffer ({0}) must be equal to count of fields in driver buffer ({1})"
                    , fieldTypes.Length, driverRowData.FieldTypes.Length));
            }

            for (var ordinal = 0; ordinal < driverRowData.FieldTypes.Length; ordinal++)
            {
                if (driverRowData.FieldTypes[ordinal] != fieldTypes[ordinal])
                {
                    throw new ArgumentException(string.Format("Field type mismatch at ordinal {0}. Client type: {1}, driver type: {2}",
                        ordinal, fieldTypes[ordinal], m_driverRowData.FieldTypes[ordinal]));
                }
            }

            m_countToRead = countToRead;
            m_driverRowData = driverRowData;
            m_reader = new BinaryReader(stream, Encoding.UTF8, true);
            m_readerBuffer = new RowData(fieldTypes);
            m_pkFieldType = m_readerBuffer.FieldRepresentationTypes[0];
        }