Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Prepared Statement functionality #896

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
expose inner, use connection with lock
Alex Norman committed Oct 18, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 582bc8c31a98fa044b2aa1d20f47a39a9f4a6171
34 changes: 24 additions & 10 deletions src/SQLiteAsync.cs
Original file line number Diff line number Diff line change
@@ -1431,8 +1431,23 @@ public void Dispose ()
/// Async wrapper for prepared statements.
/// </summary>
public class SQLiteAsyncPreparedStatement : IDisposable {
SQLiteAsyncConnection Connection { get; set; }
SQLitePreparedStatement Statement { get; set; }
SQLiteConnectionWithLock Connection { get; set; }

/// Get the inner, synchronous, statement;
public SQLitePreparedStatement Inner { get; private set; }

/// <summary>
/// Creates a prepared statement given the command text (SQL) with arguments. Place a '?'
/// in the command text for each of the arguments and then executes that command.
/// Use this method when return primitive values.
/// You can set the Trace or TimeExecution properties of the connection
/// to profile execution.
/// </summary>
/// <param name="conn">The Connection.</param>
/// <param name="query">
/// The fully escaped SQL.
/// </param>
public SQLiteAsyncPreparedStatement(SQLiteAsyncConnection conn, string query) : this(conn.GetConnection(), query) { }

/// <summary>
/// Creates a prepared statement given the command text (SQL) with arguments. Place a '?'
@@ -1445,10 +1460,10 @@ public class SQLiteAsyncPreparedStatement : IDisposable {
/// <param name="query">
/// The fully escaped SQL.
/// </param>
public SQLiteAsyncPreparedStatement(SQLiteAsyncConnection conn, string query)
public SQLiteAsyncPreparedStatement(SQLiteConnectionWithLock conn, string query)
{
Connection = conn;
Statement = new SQLitePreparedStatement(conn.GetConnection() as SQLiteConnection, query);
Inner = new SQLitePreparedStatement(Connection as SQLiteConnection, query);
}

/// <summary>
@@ -1462,7 +1477,7 @@ public SQLiteAsyncPreparedStatement(SQLiteAsyncConnection conn, string query)
/// </returns>
public Task<int> ExecuteNonQueryAsync (params object[] args)
{
return WrapAsync<int>(() => Statement.ExecuteNonQuery(args));
return WrapAsync<int>(() => Inner.ExecuteNonQuery(args));
}

/// <summary>
@@ -1481,7 +1496,7 @@ public Task<int> ExecuteNonQueryAsync (params object[] args)
/// </returns>
public Task<IEnumerable<T>> ExecuteDeferredQueryAsync<T> (params object[] args)
{
return WrapAsync<IEnumerable<T>>(() => Statement.ExecuteDeferredQuery<T>(args).ToList());
return WrapAsync<IEnumerable<T>>(() => Inner.ExecuteDeferredQuery<T>(args).ToList());
}

/// <summary>
@@ -1495,7 +1510,7 @@ public Task<IEnumerable<T>> ExecuteDeferredQueryAsync<T> (params object[] args)
/// </returns>
public Task<T> ExecuteScalarAsync<T> (params object[] args)
{
return WrapAsync<T>(() => Statement.ExecuteScalar<T>(args));
return WrapAsync<T>(() => Inner.ExecuteScalar<T>(args));
}

/// <summary>
@@ -1516,16 +1531,15 @@ protected virtual void Dispose(bool disposing)
if (disposed)
return;
if (disposing) {
Statement.Dispose();
Inner.Dispose();
}
disposed = true;
}

Task<T> WrapAsync<T> (Func<T> exe)
{
return Task.Factory.StartNew (() => {
var conn = Connection.GetConnection();
using (conn.Lock ()) {
using (Connection.Lock ()) {
return exe();
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);