Ejemplo n.º 1
0
        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation.</param>
        /// <param name="state">The state.</param>
        /// <returns>System.Nullable&lt;System.Int32&gt;.</returns>
        protected override int?Execute(OperationExecutionToken <SQLiteConnection, SQLiteTransaction> executionToken, OperationImplementation <SQLiteConnection, SQLiteTransaction> implementation, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteOperationExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            IDisposable?lockToken = null;

            try
            {
                switch (mode)
                {
                case LockType.Read: lockToken = SyncLock.ReaderLock(); break;

                case LockType.Write: lockToken = SyncLock.WriterLock(); break;
                }

                var rows = implementation(m_Connection, m_Transaction);
                OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                return(rows);
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
            finally
            {
                if (lockToken != null)
                {
                    lockToken.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="state">User supplied state.</param>
        /// <exception cref="ArgumentNullException">
        /// executionToken;executionToken is null.
        /// or
        /// implementation;implementation is null.
        /// </exception>
        protected override int?Execute(CommandExecutionToken <SQLiteCommand, SQLiteParameter> executionToken, CommandImplementation <SQLiteCommand> implementation, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteCommandExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            IDisposable?lockToken = null;

            try
            {
                switch (mode)
                {
                case LockType.Read: lockToken = SyncLock.ReaderLock(); break;

                case LockType.Write: lockToken = SyncLock.WriterLock(); break;
                }

                using (var cmd = new SQLiteCommand())
                {
                    cmd.Connection = m_Connection;
                    if (m_Transaction != null)
                    {
                        cmd.Transaction = m_Transaction;
                    }
                    if (DefaultCommandTimeout.HasValue)
                    {
                        cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                    }
                    cmd.CommandText = executionToken.CommandText;
                    cmd.CommandType = executionToken.CommandType;
                    foreach (var param in executionToken.Parameters)
                    {
                        cmd.Parameters.Add(param);
                    }

                    executionToken.ApplyCommandOverrides(cmd);

                    var rows = implementation(cmd);
                    executionToken.RaiseCommandExecuted(cmd, rows);
                    OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
            finally
            {
                if (lockToken != null)
                {
                    lockToken.Dispose();
                }
            }
        }