|
|
@ -86,14 +86,11 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<CreateTablesResult> CreateTablesAsync (params Type[] types) |
|
|
public Task<CreateTablesResult> CreateTablesAsync (params Type[] types) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
CreateTablesResult result = new CreateTablesResult (); |
|
|
CreateTablesResult result = new CreateTablesResult (); |
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
foreach (Type type in types) { |
|
|
foreach (Type type in types) |
|
|
|
|
|
{ |
|
|
|
|
|
int aResult = conn.CreateTable (type); |
|
|
int aResult = conn.CreateTable (type); |
|
|
result.Results[type] = aResult; |
|
|
result.Results[type] = aResult; |
|
|
} |
|
|
} |
|
|
@ -105,11 +102,9 @@ namespace SQLite |
|
|
public Task<int> DropTableAsync<T> () |
|
|
public Task<int> DropTableAsync<T> () |
|
|
where T : new () |
|
|
where T : new () |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.DropTable<T> (); |
|
|
return conn.DropTable<T> (); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -117,11 +112,9 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<int> InsertAsync (object item) |
|
|
public Task<int> InsertAsync (object item) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Insert (item); |
|
|
return conn.Insert (item); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -129,11 +122,9 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<int> UpdateAsync (object item) |
|
|
public Task<int> UpdateAsync (object item) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Update (item); |
|
|
return conn.Update (item); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -141,11 +132,9 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<int> DeleteAsync (object item) |
|
|
public Task<int> DeleteAsync (object item) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Delete (item); |
|
|
return conn.Delete (item); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -167,11 +156,9 @@ namespace SQLite |
|
|
public Task<T> FindAsync<T> (object pk) |
|
|
public Task<T> FindAsync<T> (object pk) |
|
|
where T : new () |
|
|
where T : new () |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Find<T> (pk); |
|
|
return conn.Find<T> (pk); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -193,11 +180,9 @@ namespace SQLite |
|
|
public Task<T> FindAsync<T> (Expression<Func<T, bool>> predicate) |
|
|
public Task<T> FindAsync<T> (Expression<Func<T, bool>> predicate) |
|
|
where T : new () |
|
|
where T : new () |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Find<T> (predicate); |
|
|
return conn.Find<T> (predicate); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -205,11 +190,9 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<int> ExecuteAsync (string query, params object[] args) |
|
|
public Task<int> ExecuteAsync (string query, params object[] args) |
|
|
{ |
|
|
{ |
|
|
return Task<int>.Factory.StartNew(() => |
|
|
return Task<int>.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Execute (query, args); |
|
|
return conn.Execute (query, args); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -217,11 +200,9 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<int> InsertAllAsync (IEnumerable items) |
|
|
public Task<int> InsertAllAsync (IEnumerable items) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.InsertAll (items); |
|
|
return conn.InsertAll (items); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -230,19 +211,15 @@ namespace SQLite |
|
|
[Obsolete("Will cause a deadlock if any call in action ends up in a different thread. Use RunInTransactionAsync(Action<SQLiteConnection>) instead.")] |
|
|
[Obsolete("Will cause a deadlock if any call in action ends up in a different thread. Use RunInTransactionAsync(Action<SQLiteConnection>) instead.")] |
|
|
public Task RunInTransactionAsync (Action<SQLiteAsyncConnection> action) |
|
|
public Task RunInTransactionAsync (Action<SQLiteAsyncConnection> action) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = this.GetConnection (); |
|
|
var conn = this.GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
conn.BeginTransaction (); |
|
|
conn.BeginTransaction (); |
|
|
try |
|
|
try { |
|
|
{ |
|
|
|
|
|
action (this); |
|
|
action (this); |
|
|
conn.Commit (); |
|
|
conn.Commit (); |
|
|
} |
|
|
} |
|
|
catch (Exception) |
|
|
catch (Exception) { |
|
|
{ |
|
|
|
|
|
conn.Rollback (); |
|
|
conn.Rollback (); |
|
|
throw; |
|
|
throw; |
|
|
} |
|
|
} |
|
|
@ -285,11 +262,9 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<T> ExecuteScalarAsync<T> (string sql, params object[] args) |
|
|
public Task<T> ExecuteScalarAsync<T> (string sql, params object[] args) |
|
|
{ |
|
|
{ |
|
|
return Task<T>.Factory.StartNew(() => |
|
|
return Task<T>.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
var command = conn.CreateCommand (sql, args); |
|
|
var command = conn.CreateCommand (sql, args); |
|
|
return command.ExecuteScalar<T> (); |
|
|
return command.ExecuteScalar<T> (); |
|
|
} |
|
|
} |
|
|
@ -299,11 +274,9 @@ namespace SQLite |
|
|
public Task<List<T>> QueryAsync<T> (string sql, params object[] args) |
|
|
public Task<List<T>> QueryAsync<T> (string sql, params object[] args) |
|
|
where T : new () |
|
|
where T : new () |
|
|
{ |
|
|
{ |
|
|
return Task<List<T>>.Factory.StartNew(() => |
|
|
return Task<List<T>>.Factory.StartNew (() => { |
|
|
{ |
|
|
|
|
|
var conn = GetConnection (); |
|
|
var conn = GetConnection (); |
|
|
using (conn.Lock()) |
|
|
using (conn.Lock ()) { |
|
|
{ |
|
|
|
|
|
return conn.Query<T> (sql, args); |
|
|
return conn.Query<T> (sql, args); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -351,10 +324,8 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<List<T>> ToListAsync () |
|
|
public Task<List<T>> ToListAsync () |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock()) |
|
|
|
|
|
{ |
|
|
|
|
|
return _innerQuery.ToList (); |
|
|
return _innerQuery.ToList (); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -362,10 +333,8 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<int> CountAsync () |
|
|
public Task<int> CountAsync () |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock()) |
|
|
|
|
|
{ |
|
|
|
|
|
return _innerQuery.Count (); |
|
|
return _innerQuery.Count (); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -373,10 +342,8 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<T> ElementAtAsync (int index) |
|
|
public Task<T> ElementAtAsync (int index) |
|
|
{ |
|
|
{ |
|
|
return Task.Factory.StartNew(() => |
|
|
return Task.Factory.StartNew (() => { |
|
|
{ |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock()) |
|
|
|
|
|
{ |
|
|
|
|
|
return _innerQuery.ElementAt (index); |
|
|
return _innerQuery.ElementAt (index); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -384,10 +351,8 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<T> FirstAsync () |
|
|
public Task<T> FirstAsync () |
|
|
{ |
|
|
{ |
|
|
return Task<T>.Factory.StartNew(() => |
|
|
return Task<T>.Factory.StartNew(() => { |
|
|
{ |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock()) |
|
|
|
|
|
{ |
|
|
|
|
|
return _innerQuery.First (); |
|
|
return _innerQuery.First (); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -395,10 +360,8 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public Task<T> FirstOrDefaultAsync () |
|
|
public Task<T> FirstOrDefaultAsync () |
|
|
{ |
|
|
{ |
|
|
return Task<T>.Factory.StartNew(() => |
|
|
return Task<T>.Factory.StartNew(() => { |
|
|
{ |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock ()) { |
|
|
using (((SQLiteConnectionWithLock)_innerQuery.Connection).Lock()) |
|
|
|
|
|
{ |
|
|
|
|
|
return _innerQuery.FirstOrDefault (); |
|
|
return _innerQuery.FirstOrDefault (); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -453,13 +416,11 @@ namespace SQLite |
|
|
|
|
|
|
|
|
public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connectionString) |
|
|
public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connectionString) |
|
|
{ |
|
|
{ |
|
|
lock (_entriesLock) |
|
|
lock (_entriesLock) { |
|
|
{ |
|
|
|
|
|
Entry entry; |
|
|
Entry entry; |
|
|
string key = connectionString.ConnectionString; |
|
|
string key = connectionString.ConnectionString; |
|
|
|
|
|
|
|
|
if (!_entries.TryGetValue(key, out entry)) |
|
|
if (!_entries.TryGetValue (key, out entry)) { |
|
|
{ |
|
|
|
|
|
entry = new Entry (connectionString); |
|
|
entry = new Entry (connectionString); |
|
|
_entries[key] = entry; |
|
|
_entries[key] = entry; |
|
|
} |
|
|
} |
|
|
@ -473,10 +434,8 @@ namespace SQLite |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public void Reset () |
|
|
public void Reset () |
|
|
{ |
|
|
{ |
|
|
lock (_entriesLock) |
|
|
lock (_entriesLock) { |
|
|
{ |
|
|
foreach (var entry in _entries.Values) { |
|
|
foreach (var entry in _entries.Values) |
|
|
|
|
|
{ |
|
|
|
|
|
entry.OnApplicationSuspended (); |
|
|
entry.OnApplicationSuspended (); |
|
|
} |
|
|
} |
|
|
_entries.Clear (); |
|
|
_entries.Clear (); |
|
|
|