Npgsql.NpgsqlCommand.GetCommandText C# (CSharp) Method

GetCommandText() private method

Process this.commandText, trimming each distinct command and substituting paramater tokens.
private GetCommandText ( bool prepare, bool forExtendQuery ) : byte[]
prepare bool
forExtendQuery bool
return byte[]
        private byte[] GetCommandText(bool prepare, bool forExtendQuery)
        {
            NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetCommandText");

            MemoryStream commandBuilder = new MemoryStream();
            StringChunk[] chunks;

            chunks = GetDistinctTrimmedCommands(commandText);

            if (chunks.Length > 1)
            {
                if (prepare || commandType == CommandType.StoredProcedure)
                {
                    throw new NpgsqlException("Multiple queries not supported for this command type");
                }
            }

            foreach (StringChunk chunk in chunks)
            {
                if (commandBuilder.Length > 0)
                {
                    commandBuilder
                        .WriteBytes((byte)ASCIIBytes.SemiColon)
                        .WriteBytes(ASCIIByteArrays.LineTerminator);
                }

                if (prepare && ! forExtendQuery)
                {
                    commandBuilder
                        .WriteString("PREPARE ")
                        .WriteString(planName)
                        .WriteString(" AS ");
                }

                if (commandType == CommandType.StoredProcedure)
                {
                    if (! prepare && ! functionChecksDone)
                    {
                        functionNeedsColumnListDefinition = Parameters.Count != 0 && CheckFunctionNeedsColumnDefinitionList();

                        functionChecksDone = true;
                    }

                    commandBuilder.WriteString(
                        Connector.SupportsPrepare
                        ? "SELECT * FROM " // This syntax is only available in 7.3+ as well SupportsPrepare.
                        : "SELECT " //Only a single result return supported. 7.2 and earlier.
                    );

                    if (commandText[chunk.Begin + chunk.Length - 1] == ')')
                    {
                        AppendCommandReplacingParameterValues(commandBuilder, commandText, chunk.Begin, chunk.Length, prepare, forExtendQuery);
                    }
                    else
                    {
                        commandBuilder
                            .WriteString(commandText.Substring(chunk.Begin, chunk.Length))
                            .WriteBytes((byte)ASCIIBytes.ParenLeft);

                        if (prepare)
                        {
                            AppendParameterPlaceHolders(commandBuilder);
                        }
                        else
                        {
                            AppendParameterValues(commandBuilder);
                        }

                        commandBuilder.WriteBytes((byte)ASCIIBytes.ParenRight);
                    }

                    if (! prepare && functionNeedsColumnListDefinition)
                    {
                        AddFunctionColumnListSupport(commandBuilder);
                    }
                }
                else if (commandType == CommandType.TableDirect)
                {
                    commandBuilder
                        .WriteString("SELECT * FROM ")
                        .WriteString(commandText.Substring(chunk.Begin, chunk.Length));
                }
                else
                {
                    AppendCommandReplacingParameterValues(commandBuilder, commandText, chunk.Begin, chunk.Length, prepare, forExtendQuery);
                }
            }

            return commandBuilder.ToArray();
        }

Same methods

NpgsqlCommand::GetCommandText ( ) : byte[]

Usage Example

コード例 #1
0
        public void WriteToStream(Stream outputStream, Encoding encoding)
        {
            //NpgsqlEventLog.LogMsg( this.ToString() + _commandText, LogLevel.Debug  );


            String commandText = _command.GetCommandText();

            // Tell to mediator what command is being sent.

            _command.Connector.Mediator.SqlSent = commandText;

            // Send the query to server.
            // Write the byte 'Q' to identify a query message.
            outputStream.WriteByte((Byte)'Q');

            if (_protocolVersion == ProtocolVersion.Version3)
            {
                // Write message length. Int32 + string length + null terminator.
                PGUtil.WriteInt32(outputStream, 4 + encoding.GetByteCount(commandText) + 1);
            }

            // Write the query. In this case it is the CommandText text.
            // It is a string terminated by a C NULL character.
            PGUtil.WriteString(commandText, outputStream, encoding);
        }
All Usage Examples Of Npgsql.NpgsqlCommand::GetCommandText