Ejemplo n.º 1
0
        /// <summary>
        /// Prepares the necessary byte buffers from the given CommandText
        /// </summary>
        /// <returns>Array of byte buffers, one for each SQL command</returns>
        /// <remarks>
        /// Converts the CommandText into an array of tokens
        /// using TokenizeSql and then into one or more byte buffers that can be
        /// sent to the server.  If the server supports batching (and we  have enabled it),
        /// then all commands result in a single byte array, otherwise a single buffer
        /// is created for each SQL command (as separated by ';').
        /// The SQL text is converted to bytes using the active encoding for the server.
        /// </remarks>
        private ArrayList PrepareSqlBuffers(string sql)
        {
            ArrayList    buffers = new ArrayList();
            PacketWriter writer  = new PacketWriter();

            writer.Encoding = connection.Encoding;
            writer.Version  = connection.driver.Version;

            // if we are executing as a stored procedure, then we need to add the call
            // keyword.
            if (CommandType == CommandType.StoredProcedure)
            {
                if (storedProcedure == null)
                {
                    storedProcedure = new StoredProcedure(connection);
                }
                sql = storedProcedure.Prepare(this);
            }

            // tokenize the SQL
            sql = sql.TrimStart(';').TrimEnd(';');
            ArrayList tokens = TokenizeSql(sql);

            foreach (string token in tokens)
            {
                if (token.Trim().Length == 0)
                {
                    continue;
                }
                if (token == ";" && !connection.driver.SupportsBatch)
                {
                    MemoryStream ms = (MemoryStream)writer.Stream;
                    if (ms.Length > 0)
                    {
                        buffers.Add(ms);
                    }

                    writer          = new PacketWriter();
                    writer.Encoding = connection.Encoding;
                    writer.Version  = connection.driver.Version;
                    continue;
                }
                else if (token[0] == parameters.ParameterMarker)
                {
                    if (SerializeParameter(writer, token))
                    {
                        continue;
                    }
                }

                // our fall through case is to write the token to the byte stream
                writer.WriteStringNoNull(token);
            }

            // capture any buffer that is left over
            MemoryStream mStream = (MemoryStream)writer.Stream;

            if (mStream.Length > 0)
            {
                buffers.Add(mStream);
            }

            return(buffers);
        }