protected override int? Execute(CommandExecutionToken<SQLiteCommand, SQLiteParameter> executionToken, CommandImplementation<SQLiteCommand> implementation, object state)
{
if (executionToken == null)
throw new ArgumentNullException("executionToken", "executionToken is null.");
if (implementation == null)
throw new ArgumentNullException("implementation", "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 con = CreateConnection())
{
using (var cmd = new SQLiteCommand())
{
cmd.Connection = con;
if (DefaultCommandTimeout.HasValue)
cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
cmd.CommandText = executionToken.CommandText;
//TODO: add potential check for this type.
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();
}
}