예제 #1
0
 public PreparedCommandSqlParser(StatementPreparer preparer, List <ParsedStatement> statements, List <int> statementStartEndIndexes, ByteBufferWriter writer)
     : base(preparer)
 {
     m_statements = statements;
     m_statementStartEndIndexes = statementStartEndIndexes;
     m_writer = writer;
 }
예제 #2
0
 public ParameterSqlParser(StatementPreparer preparer, ByteBufferWriter writer)
     : base(preparer)
 {
     m_writer = writer;
 }
예제 #3
0
        private async Task DoPrepareAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            var statementPreparer = new StatementPreparer(CommandText, m_parameterCollection, CreateStatementPreparerOptions());
            var parsedStatements  = statementPreparer.SplitStatements();

            if (parsedStatements.Statements.Count > 1)
            {
                throw new NotSupportedException("Multiple semicolon-delimited SQL statements are not supported by MySqlCommand.Prepare");
            }

            var columnsAndParameters     = new ResizableArray <byte>();
            var columnsAndParametersSize = 0;

            var preparedStatements = new List <PreparedStatement>(parsedStatements.Statements.Count);

            foreach (var statement in parsedStatements.Statements)
            {
                await Connection.Session.SendAsync(new PayloadData(statement.StatementBytes), ioBehavior, cancellationToken).ConfigureAwait(false);

                var payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                var response = StatementPrepareResponsePayload.Create(payload.AsSpan());

                ColumnDefinitionPayload[] parameters = null;
                if (response.ParameterCount > 0)
                {
                    parameters = new ColumnDefinitionPayload[response.ParameterCount];
                    for (var i = 0; i < response.ParameterCount; i++)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payload.ArraySegment.Count);
                        Buffer.BlockCopy(payload.ArraySegment.Array, payload.ArraySegment.Offset, columnsAndParameters.Array, columnsAndParametersSize, payload.ArraySegment.Count);
                        parameters[i]             = ColumnDefinitionPayload.Create(new ResizableArraySegment <byte>(columnsAndParameters, columnsAndParametersSize, payload.ArraySegment.Count));
                        columnsAndParametersSize += payload.ArraySegment.Count;
                    }
                    if (!Connection.Session.SupportsDeprecateEof)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        EofPayload.Create(payload.AsSpan());
                    }
                }

                ColumnDefinitionPayload[] columns = null;
                if (response.ColumnCount > 0)
                {
                    columns = new ColumnDefinitionPayload[response.ColumnCount];
                    for (var i = 0; i < response.ColumnCount; i++)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payload.ArraySegment.Count);
                        Buffer.BlockCopy(payload.ArraySegment.Array, payload.ArraySegment.Offset, columnsAndParameters.Array, columnsAndParametersSize, payload.ArraySegment.Count);
                        columns[i] = ColumnDefinitionPayload.Create(new ResizableArraySegment <byte>(columnsAndParameters, columnsAndParametersSize, payload.ArraySegment.Count));
                        columnsAndParametersSize += payload.ArraySegment.Count;
                    }
                    if (!Connection.Session.SupportsDeprecateEof)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        EofPayload.Create(payload.AsSpan());
                    }
                }

                preparedStatements.Add(new PreparedStatement(response.StatementId, statement, columns, parameters));
            }

            Connection.Session.AddPreparedStatement(CommandText, new PreparedStatements(preparedStatements, parsedStatements));
        }
예제 #4
0
        private async Task PrepareAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            if (Connection == null)
            {
                throw new InvalidOperationException("Connection property must be non-null.");
            }
            if (Connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Connection must be Open; current state is {0}".FormatInvariant(Connection.State));
            }
            if (string.IsNullOrWhiteSpace(CommandText))
            {
                throw new InvalidOperationException("CommandText must be specified");
            }
            if (m_connection?.HasActiveReader ?? false)
            {
                throw new InvalidOperationException("Cannot call Prepare when there is an open DataReader for this command; it must be closed first.");
            }
            if (Connection.IgnorePrepare)
            {
                return;
            }

            if (CommandType != CommandType.Text)
            {
                throw new NotSupportedException("Only CommandType.Text is currently supported by MySqlCommand.Prepare");
            }

            var statementPreparer = new StatementPreparer(CommandText, Parameters, CreateStatementPreparerOptions());
            var parsedStatements  = statementPreparer.SplitStatements();

            if (parsedStatements.Statements.Count > 1)
            {
                throw new NotSupportedException("Multiple semicolon-delimited SQL statements are not supported by MySqlCommand.Prepare");
            }

            var columnsAndParameters     = new ResizableArray <byte>();
            var columnsAndParametersSize = 0;

            var preparedStatements = new List <PreparedStatement>(parsedStatements.Statements.Count);

            foreach (var statement in parsedStatements.Statements)
            {
                await Connection.Session.SendAsync(new PayloadData(statement.StatementBytes), ioBehavior, cancellationToken).ConfigureAwait(false);

                var payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                var response = StatementPrepareResponsePayload.Create(payload);

                ColumnDefinitionPayload[] parameters = null;
                if (response.ParameterCount > 0)
                {
                    parameters = new ColumnDefinitionPayload[response.ParameterCount];
                    for (var i = 0; i < response.ParameterCount; i++)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payload.ArraySegment.Count);
                        Buffer.BlockCopy(payload.ArraySegment.Array, payload.ArraySegment.Offset, columnsAndParameters.Array, columnsAndParametersSize, payload.ArraySegment.Count);
                        parameters[i]             = ColumnDefinitionPayload.Create(new ResizableArraySegment <byte>(columnsAndParameters, columnsAndParametersSize, payload.ArraySegment.Count));
                        columnsAndParametersSize += payload.ArraySegment.Count;
                    }
                    if (!Connection.Session.SupportsDeprecateEof)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        EofPayload.Create(payload);
                    }
                }

                ColumnDefinitionPayload[] columns = null;
                if (response.ColumnCount > 0)
                {
                    columns = new ColumnDefinitionPayload[response.ColumnCount];
                    for (var i = 0; i < response.ColumnCount; i++)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payload.ArraySegment.Count);
                        Buffer.BlockCopy(payload.ArraySegment.Array, payload.ArraySegment.Offset, columnsAndParameters.Array, columnsAndParametersSize, payload.ArraySegment.Count);
                        columns[i] = ColumnDefinitionPayload.Create(new ResizableArraySegment <byte>(columnsAndParameters, columnsAndParametersSize, payload.ArraySegment.Count));
                        columnsAndParametersSize += payload.ArraySegment.Count;
                    }
                    if (!Connection.Session.SupportsDeprecateEof)
                    {
                        payload = await Connection.Session.ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

                        EofPayload.Create(payload);
                    }
                }

                preparedStatements.Add(new PreparedStatement(response.StatementId, statement, columns, parameters));
            }

            m_parsedStatements = parsedStatements;
            m_statements       = preparedStatements;
        }