Esempio n. 1
0
        private async Task FlushImpl(AsyncWrappingCommonArgs async)
        {
            var buffer = _outputBuffer.ToArray();

            _outputBuffer.Clear();
            var count = buffer.Length;

            if (_compressor != null)
            {
                count  = HandleCompression(buffer, count);
                buffer = _compressionBuffer;
            }
            if (_encryptor != null)
            {
                _encryptor.ProcessBytes(buffer, 0, count, buffer, 0);
            }
            try
            {
                await async.AsyncSyncCall(_networkStream.WriteAsync, _networkStream.Write, buffer, 0, count).ConfigureAwait(false);

                await async.AsyncSyncCall(_networkStream.FlushAsync, _networkStream.Flush).ConfigureAwait(false);
            }
            catch (IOException)
            {
                IOFailed = true;
                throw;
            }
        }
Esempio n. 2
0
        private async Task <int> ReadImpl(byte[] buffer, int offset, int count, AsyncWrappingCommonArgs async)
        {
            if (_inputBuffer.Count < count)
            {
                var readBuffer = _readBuffer;
                int read;
                try
                {
                    read = await async.AsyncSyncCall(_networkStream.ReadAsync, _networkStream.Read, readBuffer, 0, readBuffer.Length).ConfigureAwait(false);
                }
                catch (IOException)
                {
                    IOFailed = true;
                    throw;
                }
                if (read != 0)
                {
                    if (_decryptor != null)
                    {
                        _decryptor.ProcessBytes(readBuffer, 0, read, readBuffer, 0);
                    }
                    if (_decompressor != null)
                    {
                        read       = HandleDecompression(readBuffer, read);
                        readBuffer = _compressionBuffer;
                    }
                    WriteToInputBuffer(readBuffer, read);
                }
            }
            var dataLength = ReadFromInputBuffer(buffer, offset, count);

            return(dataLength);
        }
        public async Task WriteBlobBuffer(byte[] buffer, AsyncWrappingCommonArgs async)
        {
            var length = buffer.Length;             // 2 for short for buffer length

            if (length > short.MaxValue)
            {
                throw new IOException("Blob buffer too big.");
            }
            await Write(length + 2, async).ConfigureAwait(false);
            await Write(length + 2, async).ConfigureAwait(false);              //bizarre but true! three copies of the length

            await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, new[] { (byte)((length >> 0) & 0xff), (byte)((length >> 8) & 0xff) }, 0, 2).ConfigureAwait(false);

            await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, buffer, 0, length).ConfigureAwait(false);

            await WritePad((4 - length + 2)& 3, async).ConfigureAwait(false);
        }
 public async Task WriteOpaque(byte[] buffer, int length, AsyncWrappingCommonArgs async)
 {
     if (buffer != null && length > 0)
     {
         await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, buffer, 0, buffer.Length).ConfigureAwait(false);
         await WriteFill(length - buffer.Length, async).ConfigureAwait(false);
         await WritePad((4 - length)& 3, async).ConfigureAwait(false);
     }
 }
        public async Task WriteTyped(int type, byte[] buffer, AsyncWrappingCommonArgs async)
        {
            int length;

            if (buffer == null)
            {
                await Write(1, async).ConfigureAwait(false);

                await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, new[] { (byte)type }, 0, 1).ConfigureAwait(false);

                length = 1;
            }
            else
            {
                length = buffer.Length + 1;
                await Write(length, async).ConfigureAwait(false);

                await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, new[] { (byte)type }, 0, 1).ConfigureAwait(false);

                await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, buffer, 0, buffer.Length).ConfigureAwait(false);
            }
            await WritePad((4 - length)& 3, async).ConfigureAwait(false);
        }
        private async Task RollbackImpl(string savePointName, AsyncWrappingCommonArgs async)
        {
            EnsureSavePointName(savePointName);
            EnsureCompleted();
            try
            {
                var command = new FbCommand($"ROLLBACK WORK TO SAVEPOINT {savePointName}", _connection, this);
#if NET48 || NETSTANDARD2_0
                using (command)
#else
                await using (command)
#endif
                {
                    await async.AsyncSyncCall(command.ExecuteNonQueryAsync, command.ExecuteNonQuery).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw new FbException(ex.Message, ex);
            }
        }
 public async Task <byte[]> ReadBytes(byte[] buffer, int count, AsyncWrappingCommonArgs async)
 {
     if (count > 0)
     {
         var toRead        = count;
         var currentlyRead = -1;
         while (toRead > 0 && currentlyRead != 0)
         {
             toRead -= (currentlyRead = await async.AsyncSyncCall(_stream.ReadAsync, _stream.Read, buffer, count - toRead, toRead).ConfigureAwait(false));
         }
         if (currentlyRead == 0)
         {
             if (_stream is ITracksIOFailure tracksIOFailure)
             {
                 tracksIOFailure.IOFailed = true;
             }
             throw new IOException();
         }
     }
     return(buffer);
 }
 public Task Write(long value, AsyncWrappingCommonArgs async)
 {
     return(async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, TypeEncoder.EncodeInt64(value), 0, 8));
 }
 public Task Flush(AsyncWrappingCommonArgs async)
 {
     return(async.AsyncSyncCall(_stream.FlushAsync, _stream.Flush));
 }
 public Task WriteBytes(byte[] buffer, int count, AsyncWrappingCommonArgs async)
 {
     return(async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, buffer, 0, count));
 }
        private async Task ExecuteImpl(bool autoCommit, AsyncWrappingCommonArgs async)
        {
            if ((_statements?.Count ?? 0) == 0)
            {
                throw new InvalidOperationException("There are no commands for execution.");
            }

            _shouldClose = false;

            foreach (var statement in Statements)
            {
                if (!(statement.StatementType == SqlStatementType.Connect ||
                      statement.StatementType == SqlStatementType.CreateDatabase ||
                      statement.StatementType == SqlStatementType.Disconnect ||
                      statement.StatementType == SqlStatementType.DropDatabase ||
                      statement.StatementType == SqlStatementType.SetAutoDDL ||
                      statement.StatementType == SqlStatementType.SetDatabase ||
                      statement.StatementType == SqlStatementType.SetNames ||
                      statement.StatementType == SqlStatementType.SetSQLDialect))
                {
                    await ProvideCommand(async).ConfigureAwait(false);

                    _sqlCommand.CommandText = statement.Text;
                    if (_sqlTransaction == null && !(statement.StatementType == SqlStatementType.Commit || statement.StatementType == SqlStatementType.Rollback))
                    {
                        _sqlTransaction = await _sqlConnection.BeginTransactionImpl(FbTransaction.DefaultIsolationLevel, null, async).ConfigureAwait(false);
                    }
                    _sqlCommand.Transaction = _sqlTransaction;
                }

                try
                {
                    switch (statement.StatementType)
                    {
                    case SqlStatementType.AlterCharacterSet:
                    case SqlStatementType.AlterDatabase:
                    case SqlStatementType.AlterDomain:
                    case SqlStatementType.AlterException:
                    case SqlStatementType.AlterFunction:
                    case SqlStatementType.AlterIndex:
                    case SqlStatementType.AlterPackage:
                    case SqlStatementType.AlterProcedure:
                    case SqlStatementType.AlterRole:
                    case SqlStatementType.AlterSequence:
                    case SqlStatementType.AlterTable:
                    case SqlStatementType.AlterTrigger:
                    case SqlStatementType.AlterView:
                    case SqlStatementType.CommentOn:
                    case SqlStatementType.CreateCollation:
                    case SqlStatementType.CreateDomain:
                    case SqlStatementType.CreateException:
                    case SqlStatementType.CreateFunction:
                    case SqlStatementType.CreateGenerator:
                    case SqlStatementType.CreateIndex:
                    case SqlStatementType.CreatePackage:
                    case SqlStatementType.CreatePackageBody:
                    case SqlStatementType.CreateProcedure:
                    case SqlStatementType.CreateRole:
                    case SqlStatementType.CreateSequence:
                    case SqlStatementType.CreateShadow:
                    case SqlStatementType.CreateTable:
                    case SqlStatementType.CreateTrigger:
                    case SqlStatementType.CreateView:
                    case SqlStatementType.DeclareCursor:
                    case SqlStatementType.DeclareExternalFunction:
                    case SqlStatementType.DeclareFilter:
                    case SqlStatementType.DeclareStatement:
                    case SqlStatementType.DeclareTable:
                    case SqlStatementType.Delete:
                    case SqlStatementType.DropCollation:
                    case SqlStatementType.DropDomain:
                    case SqlStatementType.DropException:
                    case SqlStatementType.DropExternalFunction:
                    case SqlStatementType.DropFunction:
                    case SqlStatementType.DropFilter:
                    case SqlStatementType.DropGenerator:
                    case SqlStatementType.DropIndex:
                    case SqlStatementType.DropPackage:
                    case SqlStatementType.DropPackageBody:
                    case SqlStatementType.DropProcedure:
                    case SqlStatementType.DropSequence:
                    case SqlStatementType.DropRole:
                    case SqlStatementType.DropShadow:
                    case SqlStatementType.DropTable:
                    case SqlStatementType.DropTrigger:
                    case SqlStatementType.DropView:
                    case SqlStatementType.EventInit:
                    case SqlStatementType.EventWait:
                    case SqlStatementType.Execute:
                    case SqlStatementType.ExecuteImmediate:
                    case SqlStatementType.ExecuteProcedure:
                    case SqlStatementType.Grant:
                    case SqlStatementType.Insert:
                    case SqlStatementType.InsertCursor:
                    case SqlStatementType.Merge:
                    case SqlStatementType.Open:
                    case SqlStatementType.Prepare:
                    case SqlStatementType.Revoke:
                    case SqlStatementType.RecreateFunction:
                    case SqlStatementType.RecreatePackage:
                    case SqlStatementType.RecreatePackageBody:
                    case SqlStatementType.RecreateProcedure:
                    case SqlStatementType.RecreateTable:
                    case SqlStatementType.RecreateTrigger:
                    case SqlStatementType.RecreateView:
                    case SqlStatementType.SetGenerator:
                    case SqlStatementType.Update:
                    case SqlStatementType.Whenever:
                        OnCommandExecuting(_sqlCommand, statement.StatementType);

                        var rowsAffected = await ExecuteCommand(autoCommit, async).ConfigureAwait(false);

                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, rowsAffected);
                        break;

                    case SqlStatementType.ExecuteBlock:
                    case SqlStatementType.Select:
                        (await ProvideCommand(async).ConfigureAwait(false)).CommandText = statement.Text;

                        OnCommandExecuting(_sqlCommand, statement.StatementType);

                        var dataReader = await _sqlCommand.ExecuteReaderImpl(CommandBehavior.Default, async).ConfigureAwait(false);

                        try
                        {
                            _requiresNewConnection = false;

                            OnCommandExecuted(dataReader, statement.Text, statement.StatementType, -1);
                        }
                        finally
                        {
#if NET48 || NETSTANDARD2_0
                            dataReader.Dispose();
#else
                            await async.AsyncSyncCallNoCancellation(dataReader.DisposeAsync, dataReader.Dispose).ConfigureAwait(false);
#endif
                        }
                        break;

                    case SqlStatementType.Commit:
                        OnCommandExecuting(null, statement.StatementType);

                        await CommitTransaction(async).ConfigureAwait(false);

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Rollback:
                        OnCommandExecuting(null, statement.StatementType);

                        await RollbackTransaction(async).ConfigureAwait(false);

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.CreateDatabase:
                        OnCommandExecuting(null, statement.StatementType);

                        await CreateDatabase(statement.CleanText, async).ConfigureAwait(false);

                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.DropDatabase:
                        OnCommandExecuting(null, statement.StatementType);

                        await async.AsyncSyncCall(FbConnection.DropDatabaseAsync, FbConnection.DropDatabase, _connectionString.ToString()).ConfigureAwait(false);

                        _requiresNewConnection = true;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Connect:
                        OnCommandExecuting(null, statement.StatementType);

                        await ConnectToDatabase(statement.CleanText, async).ConfigureAwait(false);

                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Disconnect:
                        OnCommandExecuting(null, statement.StatementType);

                        await _sqlConnection.CloseImpl(async).ConfigureAwait(false);

                        FbConnection.ClearPool(_sqlConnection);
                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.SetAutoDDL:
                        OnCommandExecuting(null, statement.StatementType);

                        SetAutoDdl(statement.CleanText, ref autoCommit);
                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.SetNames:
                        OnCommandExecuting(null, statement.StatementType);

                        SetNames(statement.CleanText);
                        _requiresNewConnection = true;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.SetSQLDialect:
                        OnCommandExecuting(null, statement.StatementType);

                        SetSqlDialect(statement.CleanText);
                        _requiresNewConnection = true;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Fetch:
                    case SqlStatementType.Describe:
                        break;

                    case SqlStatementType.SetDatabase:
                    case SqlStatementType.SetStatistics:
                    case SqlStatementType.SetTransaction:
                    case SqlStatementType.ShowSQLDialect:
                        throw new NotImplementedException();
                    }
                }
                catch (Exception ex)
                {
                    await DisposeCommand(async).ConfigureAwait(false);
                    await RollbackTransaction(async).ConfigureAwait(false);
                    await CloseConnection(async).ConfigureAwait(false);

                    throw new FbException(string.Format("An exception was thrown when executing command: {1}.{0}Batch execution aborted.{0}The returned message was: {2}.",
                                                        Environment.NewLine,
                                                        statement.Text,
                                                        ex.Message),
                                          ex);
                }
            }

            await DisposeCommand(async).ConfigureAwait(false);
            await CommitTransaction(async).ConfigureAwait(false);
            await CloseConnection(async).ConfigureAwait(false);
        }
        /// <summary>
        /// Parses the isql statement CREATE DATABASE and creates the database and opens a connection to the recently created database.
        /// </summary>
        /// <param name="createDatabaseStatement">The create database statement.</param>
        private async Task CreateDatabase(string createDatabaseStatement, AsyncWrappingCommonArgs async)
        {
            // CREATE {DATABASE | SCHEMA} 'filespec'
            // [USER 'username' [PASSWORD 'password']]
            // [PAGE_SIZE [=] int]
            // [LENGTH [=] int [PAGE[S]]]
            // [DEFAULT CHARACTER SET charset]
            // [<secondary_file>];
            var pageSize = 0;
            var parser   = new SqlStringParser(createDatabaseStatement);

            parser.Tokens = StandardParseTokens;
            using (var enumerator = parser.Parse().GetEnumerator())
            {
                enumerator.MoveNext();
                if (enumerator.Current.Text.ToUpperInvariant() != "CREATE")
                {
                    throw new ArgumentException("Malformed isql CREATE statement. Expected keyword CREATE but something else was found.");
                }
                enumerator.MoveNext();                 // {DATABASE | SCHEMA}
                enumerator.MoveNext();
                _connectionString.Database = enumerator.Current.Text.Replace("'", string.Empty);
                while (enumerator.MoveNext())
                {
                    switch (enumerator.Current.Text.ToUpperInvariant())
                    {
                    case "USER":
                        enumerator.MoveNext();
                        _connectionString.UserID = enumerator.Current.Text.Replace("'", string.Empty);
                        break;

                    case "PASSWORD":
                        enumerator.MoveNext();
                        _connectionString.Password = enumerator.Current.Text.Replace("'", string.Empty);
                        break;

                    case "PAGE_SIZE":
                        enumerator.MoveNext();
                        if (enumerator.Current.Text == "=")
                        {
                            enumerator.MoveNext();
                        }
                        int.TryParse(enumerator.Current.Text, out pageSize);
                        break;

                    case "DEFAULT":
                        enumerator.MoveNext();
                        if (enumerator.Current.Text.ToUpperInvariant() != "CHARACTER")
                        {
                            throw new ArgumentException("Expected the keyword CHARACTER but something else was found.");
                        }

                        enumerator.MoveNext();
                        if (enumerator.Current.Text.ToUpperInvariant() != "SET")
                        {
                            throw new ArgumentException("Expected the keyword SET but something else was found.");
                        }

                        enumerator.MoveNext();
                        _connectionString.Charset = enumerator.Current.Text;
                        break;
                    }
                }
            }
            await async.AsyncSyncCall((cs, ps, ct) => FbConnection.CreateDatabaseAsync(cs, pageSize: ps, cancellationToken: ct), (cs, ps) => FbConnection.CreateDatabase(cs, pageSize: ps), _connectionString.ToString(), pageSize).ConfigureAwait(false);

            _requiresNewConnection = true;
            await ProvideConnection(async).ConfigureAwait(false);
        }
Esempio n. 13
0
 public Task Flush(AsyncWrappingCommonArgs async) => async.AsyncSyncCall(_stream.FlushAsync, _stream.Flush);
Esempio n. 14
0
 public Task Write(byte[] buffer, int offset, int count, AsyncWrappingCommonArgs async) => async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, buffer, offset, count);
Esempio n. 15
0
 public Task <int> Read(byte[] buffer, int offset, int count, AsyncWrappingCommonArgs async) => async.AsyncSyncCall(_stream.ReadAsync, _stream.Read, buffer, offset, count);