Exemple #1
0
        void CreateMessagesSchemaOnly(CommandBehavior behavior)
        {
            Contract.Requires((behavior & CommandBehavior.SchemaOnly) != 0);

            ProcessRawQuery();

            for (var i = 0; i < _queries.Count; i++)
            {
                ParseMessage    parseMessage;
                DescribeMessage describeMessage;
                if (i == 0)
                {
                    parseMessage    = _connector.ParseMessage;
                    describeMessage = _connector.DescribeMessage;
                }
                else
                {
                    parseMessage    = new ParseMessage();
                    describeMessage = new DescribeMessage();
                }

                _connector.AddMessage(parseMessage.Populate(_queries[i], _connector.TypeHandlerRegistry));
                _connector.AddMessage(describeMessage.Populate(StatementOrPortal.Statement));
            }

            _connector.AddMessage(SyncMessage.Instance);
        }
Exemple #2
0
        void CreateMessagesNonPrepared(CommandBehavior behavior)
        {
            Contract.Requires((behavior & CommandBehavior.SchemaOnly) == 0);

            ProcessRawQuery();

            var portalNames = _queries.Count > 1
                ? Enumerable.Range(0, _queries.Count).Select(i => "MQ" + i).ToArray()
                : null;

            for (var i = 0; i < _queries.Count; i++)
            {
                var query = _queries[i];

                ParseMessage    parseMessage;
                DescribeMessage describeMessage;
                BindMessage     bindMessage;
                if (i == 0)
                {
                    parseMessage    = _connector.ParseMessage;
                    describeMessage = _connector.DescribeMessage;
                    bindMessage     = _connector.BindMessage;
                }
                else
                {
                    parseMessage    = new ParseMessage();
                    describeMessage = new DescribeMessage();
                    bindMessage     = new BindMessage();
                }

                _connector.AddMessage(parseMessage.Populate(query, _connector.TypeHandlerRegistry));
                _connector.AddMessage(describeMessage.Populate(StatementOrPortal.Statement));

                bindMessage.Populate(
                    _connector.TypeHandlerRegistry,
                    query.InputParameters,
                    _queries.Count == 1 ? "" : portalNames[i]
                    );
                if (AllResultTypesAreUnknown)
                {
                    bindMessage.AllResultTypesAreUnknown = AllResultTypesAreUnknown;
                }
                else if (i == 0 && UnknownResultTypeList != null)
                {
                    bindMessage.UnknownResultTypeList = UnknownResultTypeList;
                }
                _connector.AddMessage(bindMessage);
            }

            if (_queries.Count == 1)
            {
                _connector.AddMessage(_connector.ExecuteMessage.Populate("", (behavior & CommandBehavior.SingleRow) != 0 ? 1 : 0));
            }
            else
            {
                for (var i = 0; i < _queries.Count; i++)
                {
                    // TODO: Verify SingleRow behavior for multiqueries
                    _connector.AddMessage(new ExecuteMessage(portalNames[i], (behavior & CommandBehavior.SingleRow) != 0 ? 1 : 0));
                    _connector.AddMessage(new CloseMessage(StatementOrPortal.Portal, portalNames[i]));
                }
            }
            _connector.AddMessage(SyncMessage.Instance);
        }
Exemple #3
0
        /// <summary>
        /// Creates a prepared version of the command on a PostgreSQL server.
        /// </summary>
        public override void Prepare()
        {
            Prechecks();
            if (Parameters.Any(p => !p.IsTypeExplicitlySet))
            {
                throw new InvalidOperationException("NpgsqlCommand.Prepare method requires all parameters to have an explicitly set type.");
            }

            _connector = Connection.Connector;
            Log.Debug("Prepare command", _connector.Id);

            using (_connector.StartUserAction())
            {
                DeallocatePrepared();
                ProcessRawQuery();

                for (var i = 0; i < _queries.Count; i++)
                {
                    var             query = _queries[i];
                    ParseMessage    parseMessage;
                    DescribeMessage describeMessage;
                    if (i == 0)
                    {
                        parseMessage    = _connector.ParseMessage;
                        describeMessage = _connector.DescribeMessage;
                    }
                    else
                    {
                        parseMessage    = new ParseMessage();
                        describeMessage = new DescribeMessage();
                    }

                    query.PreparedStatementName = _connector.NextPreparedStatementName();
                    _connector.AddMessage(parseMessage.Populate(query, _connector.TypeHandlerRegistry));
                    _connector.AddMessage(describeMessage.Populate(StatementOrPortal.Statement,
                                                                   query.PreparedStatementName));
                }

                _connector.AddMessage(SyncMessage.Instance);
                _connector.SendAllMessages();

                _queryIndex = 0;

                while (true)
                {
                    var msg = _connector.ReadSingleMessage();
                    switch (msg.Code)
                    {
                    case BackendMessageCode.CompletedResponse: // prepended messages, e.g. begin transaction
                    case BackendMessageCode.ParseComplete:
                    case BackendMessageCode.ParameterDescription:
                        continue;

                    case BackendMessageCode.RowDescription:
                        var description = (RowDescriptionMessage)msg;
                        FixupRowDescription(description, _queryIndex == 0);
                        _queries[_queryIndex++].Description = description;
                        continue;

                    case BackendMessageCode.NoData:
                        _queries[_queryIndex++].Description = null;
                        continue;

                    case BackendMessageCode.ReadyForQuery:
                        Contract.Assume(_queryIndex == _queries.Count);
                        IsPrepared = true;
                        return;

                    default:
                        throw _connector.UnexpectedMessageReceived(msg.Code);
                    }
                }
            }
        }