Skip to content
This repository was archived by the owner on Feb 15, 2021. It is now read-only.

Commit 218704a

Browse files
israelramosmgeoperez
authored andcommitted
any method added (#20)
Closes #17 * any method added * any method moved to LiteDbSet, a little update to the sql command * Better methods * Missing file * Refactor * Refactor * Refactor done * any method added * Count, delete test added * TypeDefiniton test and mocks * Custom attribute deleted * Custom attribute and not mapped attribute tests * Upgrade deps * Before merge * Before merge
1 parent f105a62 commit 218704a

File tree

13 files changed

+634
-528
lines changed

13 files changed

+634
-528
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Unosquare.Labs.LiteLib
2+
{
3+
internal class DefinitionCacheItem
4+
{
5+
public string TableName { get; set; }
6+
7+
public string TableDefinition { get; set; }
8+
9+
public string SelectDefinition { get; set; }
10+
11+
public string InsertDefinition { get; set; }
12+
13+
public string UpdateDefinition { get; set; }
14+
15+
public string DeleteDefinition { get; set; }
16+
17+
public string DeleteDefinitionWhere { get; set; }
18+
19+
public string AnyDefinition { get; set; }
20+
21+
public string[] PropertyNames { get; set; }
22+
}
23+
}

src/Unosquare.Labs.LiteLib/Extensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Collections.Generic;
55

66
/// <summary>
7-
/// Extension methods
7+
/// Extension methods.
88
/// </summary>
99
public static class Extensions
1010
{
@@ -32,14 +32,14 @@ public static class Extensions
3232
/// Gets the type mapping.
3333
/// </summary>
3434
/// <param name="propertyType">Type of the property.</param>
35-
/// <returns>A property type of the mapping</returns>
35+
/// <returns>A property type of the mapping.</returns>
3636
public static string GetTypeMapping(this Type propertyType) => TypeMappings.ContainsKey(propertyType) ? TypeMappings[propertyType] : TextAffinity;
3737

3838
/// <summary>
3939
/// Transform a DateTime to a SQLite UTC date.
4040
/// </summary>
4141
/// <param name="utcDate">The UTC date.</param>
42-
/// <returns>UTC DateTime</returns>
42+
/// <returns>UTC DateTime.</returns>
4343
public static DateTime ToSQLiteUtcDate(this DateTime utcDate)
4444
{
4545
var startupDifference = (int)DateTime.UtcNow.Subtract(DateTime.Now).TotalHours;

src/Unosquare.Labs.LiteLib/ILiteDbSet.cs

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Threading.Tasks;
66

77
/// <summary>
8-
/// Provides basic DDL and CRUD command definitions and a DI-supplied Context
8+
/// Provides basic DDL and CRUD command definitions and a DI-supplied Context.
99
/// </summary>
1010
public interface ILiteDbSet
1111
{
@@ -48,17 +48,84 @@ public interface ILiteDbSet
4848
/// Gets the delete definition where.
4949
/// </summary>
5050
string DeleteDefinitionWhere { get; }
51+
52+
/// <summary>
53+
/// Gets any definition.
54+
/// </summary>
55+
string AnyDefinition { get; }
5156

5257
/// <summary>
5358
/// Gets or sets the parent set context.
5459
/// </summary>
5560
LiteDbContext Context { get; set; }
61+
62+
/// <summary>
63+
/// Provides and asynchronous counterpart to the Count method.
64+
/// </summary>
65+
/// <returns>A Task with the total number of rows.</returns>
66+
Task<int> CountAsync();
67+
68+
/// <summary>
69+
/// Counts the total number of rows in the table.
70+
/// </summary>
71+
/// <returns>
72+
/// The total number of rows.
73+
/// </returns>
74+
int Count();
75+
76+
/// <summary>
77+
/// Provides and asynchronous counterpart to the Count method.
78+
/// </summary>
79+
/// <param name="whereText">The where text.</param>
80+
/// <param name="whereParams">The where parameters.</param>
81+
/// <returns>
82+
/// A Task with the total number of rows.
83+
/// </returns>
84+
Task<int> CountAsync(string whereText, object whereParams = null);
85+
86+
/// <summary>
87+
/// Provides and asynchronous counterpart to the Count method.
88+
/// </summary>
89+
/// <param name="whereText">The where text.</param>
90+
/// <param name="whereParams">The where parameters.</param>
91+
/// <returns>
92+
/// The total number of rows.
93+
/// </returns>
94+
int Count(string whereText, object whereParams = null);
95+
96+
/// <summary>
97+
/// Check if the row exist in the table.
98+
/// </summary>
99+
/// <returns><c>true</c> if the query contains data, otherwise <c>false</c>.</returns>
100+
bool Any();
101+
102+
/// <summary>
103+
/// Check if the row exist in the table.
104+
/// </summary>
105+
/// <param name="whereText">The where text.</param>
106+
/// <param name="whereParams">The where parameters.</param>
107+
/// <returns><c>true</c> if the query contains data, otherwise <c>false</c>.</returns>
108+
bool Any(string whereText, object whereParams = null);
109+
110+
/// <summary>
111+
/// Check asynchronous if the row exist in the table.
112+
/// </summary>
113+
/// <param name="whereText">The where text.</param>
114+
/// <param name="whereParams">The where parameters.</param>
115+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
116+
Task<bool> AnyAsync(string whereText, object whereParams = null);
117+
118+
/// <summary>
119+
/// Check asynchronous if the table contains data.
120+
/// </summary>
121+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
122+
Task<bool> AnyAsync();
56123
}
57124

58125
/// <summary>
59-
/// Provides typed access to querying the database
126+
/// Provides typed access to querying the database.
60127
/// </summary>
61-
/// <typeparam name="T">The type of LiteModel</typeparam>
128+
/// <typeparam name="T">The type of LiteModel.</typeparam>
62129
public interface ILiteDbSet<T> : ILiteDbSet
63130
where T : ILiteModel
64131
{
@@ -103,39 +170,40 @@ public interface ILiteDbSet<T> : ILiteDbSet
103170
/// Selects a single entity from the databse given its row id.
104171
/// </summary>
105172
/// <param name="rowId">The row identifier.</param>
106-
/// <returns>A generic type</returns>
173+
/// <returns>A generic type.</returns>
107174
T Single(long rowId);
108175

109176
/// <summary>
110-
/// Counts the total number of rows in the table
177+
/// Inserts the specified entities.
111178
/// </summary>
112-
/// <returns>The total number of rows</returns>
113-
int Count();
179+
/// <param name="entities">The entities.</param>
180+
/// <exception cref="ArgumentNullException">entities.</exception>
181+
void InsertRange(IEnumerable<T> entities);
114182

115183
/// <summary>
116-
/// Provides and asynchronous counterpart to the Insert method
184+
/// Provides and asynchronous counterpart to the Insert method.
117185
/// </summary>
118186
/// <param name="entity">The entity.</param>
119-
/// <returns>A Task with the number of rows inserted</returns>
187+
/// <returns>A Task with the number of rows inserted.</returns>
120188
Task<int> InsertAsync(T entity);
121189

122190
/// <summary>
123-
/// Provides and asynchronous counterpart to the Delete method
191+
/// Provides and asynchronous counterpart to the Delete method.
124192
/// </summary>
125193
/// <param name="entity">The entity.</param>
126194
/// <returns>A Task with the number of rows deleted</returns>
127195
Task<int> DeleteAsync(T entity);
128196

129197
/// <summary>
130-
/// Provides and asynchronous counterpart to the Update method
198+
/// Provides and asynchronous counterpart to the Update method.
131199
/// </summary>
132200
/// <param name="entity">The entity.</param>
133201
/// <returns>A Task with the number of rows updated</returns>
134202
Task<int> UpdateAsync(T entity);
135203

136204
/// <summary>
137205
/// Deletes the specified where text.
138-
/// Example whereText = "X = @X" and whereParames = new { X = "hello" }
206+
/// Example whereText = "X = @X" and whereParames = new { X = "hello" }.
139207
/// </summary>
140208
/// <param name="whereText">The where text.</param>
141209
/// <param name="whereParams">The where parameters.</param>
@@ -164,18 +232,30 @@ public interface ILiteDbSet<T> : ILiteDbSet
164232
/// </summary>
165233
/// <returns>A Task of type Enumerable with a generic type.</returns>
166234
Task<IEnumerable<T>> SelectAllAsync();
167-
235+
168236
/// <summary>
169237
/// Provides and asynchronous counterpart to the Single method.
170238
/// </summary>
171239
/// <param name="rowId">The row identifier.</param>
172-
/// <returns>A Task with a generyc type.</returns>
240+
/// <returns>
241+
/// A Task with a generyc type.
242+
/// </returns>
173243
Task<T> SingleAsync(long rowId);
174244

175245
/// <summary>
176-
/// Provides and asynchronous counterpart to the Count method.
246+
/// Firsts the or default.
177247
/// </summary>
178-
/// <returns>A Task with the total number of rows.</returns>
179-
Task<int> CountAsync();
248+
/// <param name="fieldName">Name of the field.</param>
249+
/// <param name="fieldValue">The field value.</param>
250+
/// <returns> A generic type</returns>
251+
T FirstOrDefault(string fieldName, object fieldValue);
252+
253+
/// <summary>
254+
/// Firsts the or default asynchronous.
255+
/// </summary>
256+
/// <param name="fieldName">Name of the field.</param>
257+
/// <param name="fieldValue">The field value.</param>
258+
/// <returns>A Task with a generic type.</returns>
259+
Task<T> FirstOrDefaultAsync(string fieldName, object fieldValue);
180260
}
181261
}

src/Unosquare.Labs.LiteLib/LiteDbContext.cs

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@
1414
using System.Threading.Tasks;
1515
using Swan;
1616
using Swan.Reflection;
17-
#if MONO
18-
using Mono.Data.Sqlite;
19-
#elif NET46
20-
using System.Data.SQLite;
21-
#else
2217
using Microsoft.Data.Sqlite;
23-
#endif
2418

2519
/// <summary>
2620
/// A base class containing all the functionality to perform data operations on Entity Sets.
2721
/// </summary>
2822
/// <seealso cref="System.IDisposable" />
2923
public abstract class LiteDbContext : IDisposable
3024
{
31-
#region Private Declarations
32-
3325
private static readonly ConcurrentDictionary<Guid, LiteDbContext> Intances =
3426
new ConcurrentDictionary<Guid, LiteDbContext>();
3527

@@ -40,11 +32,7 @@ public abstract class LiteDbContext : IDisposable
4032
private readonly Type _contextType;
4133

4234
private bool _isDisposing; // To detect redundant calls
43-
44-
#endregion Private Declarations
45-
46-
#region Constructor
47-
35+
4836
/// <summary>
4937
/// Initializes a new instance of the <see cref="LiteDbContext" /> class.
5038
/// </summary>
@@ -58,18 +46,12 @@ protected LiteDbContext(string databaseFilePath, bool enabledLog = true)
5846

5947
databaseFilePath = Path.GetFullPath(databaseFilePath);
6048
var databaseExists = File.Exists(databaseFilePath);
61-
#if MONO
62-
Connection = new SqliteConnection($"URI=file:{databaseFilePath}");
63-
#elif NET46
64-
Connection = new SQLiteConnection($"URI=file:{databaseFilePath}");
65-
#else
6649
var builder = new SqliteConnectionStringBuilder
6750
{
68-
DataSource = databaseFilePath
51+
DataSource = databaseFilePath,
6952
};
7053

7154
Connection = new SqliteConnection(builder.ToString());
72-
#endif
7355
Connection.Open();
7456

7557
if (databaseExists == false)
@@ -82,18 +64,12 @@ protected LiteDbContext(string databaseFilePath, bool enabledLog = true)
8264
UniqueId = Guid.NewGuid();
8365
Intances[UniqueId] = this;
8466
}
85-
86-
#endregion Constructor
87-
88-
#region Events
89-
67+
9068
/// <summary>
9169
/// Occurs when [on database created].
9270
/// </summary>
9371
public event EventHandler OnDatabaseCreated = (s, e) => { };
94-
95-
#endregion Events
96-
72+
9773
#region Properties
9874

9975
/// <summary>
@@ -211,7 +187,7 @@ public Task<int> DeleteAsync(ILiteDbSet set, string whereText, object whereParam
211187
/// <param name="set">The set.</param>
212188
/// <param name="whereText">The where text.</param>
213189
/// <param name="whereParams">The where parameters.</param>
214-
/// <returns>A Task with a enumerable of type of the entity</returns>
190+
/// <returns>A Task with a enumerable of type of the entity.</returns>
215191
public Task<IEnumerable<TEntity>> SelectAsync<TEntity>(
216192
ILiteDbSet set,
217193
string whereText,
@@ -227,7 +203,7 @@ public Task<IEnumerable<TEntity>> SelectAsync<TEntity>(
227203
/// <param name="commandText">The command text.</param>
228204
/// <param name="whereParams">The where parameters.</param>
229205
/// <returns>
230-
/// An enumerable of the type of the entity
206+
/// An enumerable of the type of the entity.
231207
/// </returns>
232208
public IEnumerable<TEntity> Query<TEntity>(string commandText, object whereParams = null)
233209
{
@@ -269,7 +245,7 @@ public int Insert(object entity)
269245
/// Inserts the asynchronous without triggering events.
270246
/// </summary>
271247
/// <param name="entity">The entity.</param>
272-
/// <returns>A Task with the total number of rows inserted</returns>
248+
/// <returns>A Task with the total number of rows inserted.</returns>
273249
public async Task<int> InsertAsync(object entity)
274250
{
275251
var set = Set(entity.GetType());
@@ -285,7 +261,7 @@ public async Task<int> InsertAsync(object entity)
285261
/// Deletes the specified entity without triggering events.
286262
/// </summary>
287263
/// <param name="entity">The entity.</param>
288-
/// <returns>The affected rows count</returns>
264+
/// <returns>The affected rows count.</returns>
289265
public int Delete(object entity)
290266
{
291267
var set = Set(entity.GetType());
@@ -336,6 +312,18 @@ public Task<int> UpdateAsync(object entity)
336312

337313
return Connection.ExecuteAsync(set.UpdateDefinition, entity);
338314
}
315+
316+
internal T ExecuteScalar<T>(string commandText, object whereParams = null)
317+
{
318+
LogSqlCommand(commandText);
319+
return Connection.ExecuteScalar<T>(commandText, whereParams);
320+
}
321+
322+
internal Task<T> ExecuteScalarAsync<T>(string commandText, object whereParams = null)
323+
{
324+
LogSqlCommand(commandText);
325+
return Connection.ExecuteScalarAsync<T>(commandText, whereParams);
326+
}
339327

340328
/// <summary>
341329
/// Logs the SQL command being executed and its arguments.
@@ -354,7 +342,8 @@ internal void LogSqlCommand(string command, object arguments = null)
354342
/// </summary>
355343
private void LoadEntitySets()
356344
{
357-
var contextDbSetProperties = PropertyInfoCache.Retrieve(GetType(), () => GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
345+
var contextDbSetProperties = PropertyInfoCache
346+
.Retrieve(GetType(), () => GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
358347
.Where(
359348
p =>
360349
p.PropertyType.GetTypeInfo().IsGenericType &&
@@ -416,7 +405,7 @@ public void Dispose(bool disposing)
416405

417406
if (disposing)
418407
{
419-
Intances.TryRemove(UniqueId, out var _);
408+
Intances.TryRemove(UniqueId, out _);
420409
Connection.Close();
421410
Connection.Dispose();
422411
Connection = null;
@@ -427,10 +416,7 @@ public void Dispose(bool disposing)
427416
}
428417

429418
/// <inheritdoc />
430-
public void Dispose()
431-
{
432-
Dispose(true);
433-
}
419+
public void Dispose() => Dispose(true);
434420

435421
#endregion IDisposable Support
436422
}

0 commit comments

Comments
 (0)