Exemple #1
0
        protected override object ReadNext()
        {
            if (_nullMap.IsNull(CurrentField))
            {
                return(DBNull.Value);
            }

            NpgsqlRowDescription.FieldData field_descr = FieldData;
            Int32 field_value_size = PGUtil.ReadInt32(Stream) - 4;

            byte[] buffer = new byte[field_value_size];
            PGUtil.CheckedStreamRead(Stream, buffer, 0, field_value_size);
            char[] charBuffer = new char[UTF8Encoding.GetCharCount(buffer, 0, buffer.Length)];
            UTF8Encoding.GetChars(buffer, 0, buffer.Length, charBuffer, 0);
            try
            {
                return
                    (NpgsqlTypesHelper.ConvertBackendStringToSystemType(field_descr.TypeInfo, new string(charBuffer),
                                                                        field_descr.TypeSize, field_descr.TypeModifier));
            }
            catch (InvalidCastException ice)
            {
                return(ice);
            }
            catch (Exception ex)
            {
                return(new InvalidCastException(ex.Message, ex));
            }
        }
Exemple #2
0
        protected override object ReadNext()
        {
            int fieldSize = GetThisFieldCount();

            if (fieldSize >= _messageSize)
            {
                AbandonShip();
            }
            _nextFieldSize = null;

            // Check if this field is null
            if (fieldSize == -1)             // Null value
            {
                return(DBNull.Value);
            }

            NpgsqlRowDescription.FieldData field_descr = FieldData;

            if (fieldSize >= 85000)
            {
                return(ReadLargeObject(field_descr, fieldSize));
            }

            byte[] buffer = new byte[fieldSize];
            PGUtil.CheckedStreamRead(Stream, buffer, 0, fieldSize);

            try
            {
                if (field_descr.FormatCode == FormatCode.Text)
                {
                    var str = UTF8Encoding.GetString(buffer);
                    return
                        (NpgsqlTypesHelper.ConvertBackendStringToSystemType(
                             field_descr.TypeInfo,
                             str,
                             field_descr.TypeSize,
                             field_descr.TypeModifier));
                }
                else
                {
                    return
                        (NpgsqlTypesHelper.ConvertBackendBytesToSystemType(
                             field_descr.TypeInfo,
                             buffer,
                             fieldSize,
                             field_descr.TypeModifier));
                }
            }
            catch (InvalidCastException ice)
            {
                return(ice);
            }
            catch (Exception ex)
            {
                return(new InvalidCastException(ex.Message, ex));
            }
        }
Exemple #3
0
        private object ReadLargeObject(NpgsqlRowDescription.FieldData field_descr, int field_value_size)
        {
            var cms = new ChunkedMemoryStream(Stream, field_value_size);

            try
            {
                return
                    (NpgsqlTypesHelper.ConvertBackendStringToSystemType(
                         field_descr.TypeInfo,
                         new StreamReader(cms, Encoding.UTF8),
                         field_descr.TypeSize,
                         field_descr.TypeModifier));
            }
            catch (InvalidCastException ice)
            {
                return(ice);
            }
            catch (Exception ex)
            {
                return(new InvalidCastException(ex.Message, ex));
            }
        }
Exemple #4
0
        protected override object ReadNext()
        {
            if (_nullMap.IsNull(CurrentField))
            {
                return(DBNull.Value);
            }

            NpgsqlRowDescription.FieldData field_descr = FieldData;
            Int32 field_value_size = PGUtil.ReadInt32(Stream) - 4;

            if (field_value_size >= 85000)
            {
                return(ReadLargeObject(field_descr, field_value_size));
            }
            byte[] buffer = new byte[field_value_size];
            PGUtil.CheckedStreamRead(Stream, buffer, 0, field_value_size);
            var str = UTF8Encoding.GetString(buffer);

            try
            {
                return
                    (NpgsqlTypesHelper.ConvertBackendStringToSystemType(
                         field_descr.TypeInfo,
                         str,
                         field_descr.TypeSize,
                         field_descr.TypeModifier));
            }
            catch (InvalidCastException ice)
            {
                return(ice);
            }
            catch (Exception ex)
            {
                return(new InvalidCastException(ex.Message, ex));
            }
        }
        private void PrepareInternal()
        {
            if (m_Connector.BackendProtocolVersion == ProtocolVersion.Version2)
            {
                planName            = Connector.NextPlanName();
                preparedCommandText = GetCommandText(true, false);

                ExecuteBlind(m_Connector, preparedCommandText);
                prepared = PrepareStatus.V2Prepared;

                // Tell to mediator what command is being sent.
                m_Connector.Mediator.SetSqlSent(preparedCommandText, NpgsqlMediator.SQLSentType.Prepare);
            }
            else
            {
                // Use the extended query parsing...
                planName = m_Connector.NextPlanName();
                String portalName = "";

                preparedCommandText = GetCommandText(true, true);
                NpgsqlParse    parse             = new NpgsqlParse(planName, preparedCommandText, new Int32[] { });
                NpgsqlDescribe statementDescribe = new NpgsqlDescribeStatement(planName);
                IEnumerable <IServerResponseObject> responseEnum;
                NpgsqlRowDescription returnRowDesc = null;

                // Write Parse, Describe, and Sync messages to the wire.
                m_Connector.Parse(parse);
                m_Connector.Describe(statementDescribe);
                m_Connector.Sync();

                // Tell to mediator what command is being sent.
                m_Connector.Mediator.SetSqlSent(preparedCommandText, NpgsqlMediator.SQLSentType.Parse);

                // Flush and wait for response.
                responseEnum = m_Connector.ProcessBackendResponsesEnum();

                // Look for a NpgsqlRowDescription in the responses, discarding everything else.
                foreach (IServerResponseObject response in responseEnum)
                {
                    if (response is NpgsqlRowDescription)
                    {
                        returnRowDesc = (NpgsqlRowDescription)response;
                    }
                    else if (response is IDisposable)
                    {
                        (response as IDisposable).Dispose();
                    }
                }

                Int16[] resultFormatCodes;

                if (returnRowDesc != null)
                {
                    resultFormatCodes = new Int16[returnRowDesc.NumFields];

                    for (int i = 0; i < returnRowDesc.NumFields; i++)
                    {
                        NpgsqlRowDescription.FieldData returnRowDescData = returnRowDesc[i];

                        if (returnRowDescData.TypeInfo != null)
                        {
                            // Binary format?
                            // PG always defaults to text encoding.  We can fix up the row description
                            // here based on support for binary encoding.  Once this is done,
                            // there is no need to request another row description after Bind.
                            returnRowDescData.FormatCode = returnRowDescData.TypeInfo.SupportsBinaryBackendData ? FormatCode.Binary : FormatCode.Text;
                            resultFormatCodes[i]         = (Int16)returnRowDescData.FormatCode;
                        }
                        else
                        {
                            // Text format (default).
                            resultFormatCodes[i] = (Int16)FormatCode.Text;
                        }
                    }
                }
                else
                {
                    resultFormatCodes = new Int16[] { 0 };
                }

                // Save the row description for use with all future Executes.
                currentRowDescription = returnRowDesc;

                // The Bind and Execute message objects live through multiple Executes.
                // Only Bind changes at all between Executes, which is done in BindParameters().
                bind    = new NpgsqlBind(portalName, planName, new Int16[Parameters.Count], null, resultFormatCodes);
                execute = new NpgsqlExecute(portalName, 0);

                prepared = PrepareStatus.V3Prepared;
            }
        }