-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Dapper-like ORM methods to a separate client, update readme a…
…nd bump version in nuspec
- Loading branch information
Showing
5 changed files
with
67 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Data; | ||
|
||
namespace RqliteDotnet; | ||
|
||
public class RqliteOrmClient : RqliteClient | ||
{ | ||
public RqliteOrmClient(string uri, HttpClient? client = null) : base(uri, client) {} | ||
|
||
/// <summary> | ||
/// Query Rqlite and return result as an instance of T | ||
/// </summary> | ||
/// <param name="query">Query to execute</param> | ||
/// <typeparam name="T">Type of result object</typeparam> | ||
/// <returns></returns> | ||
public async Task<List<T>> Query<T>(string query) where T: new() | ||
{ | ||
var response = await Query(query); | ||
if (response.Results!.Count > 1) | ||
throw new DataException("Query returned more than 1 result. At the moment only 1 result supported"); | ||
var res = response.Results[0]; | ||
|
||
if (!string.IsNullOrEmpty(res.Error)) | ||
throw new InvalidOperationException(res.Error); | ||
var list = new List<T>(); | ||
|
||
for (int i = 0; i < res.Values.Count; i++) | ||
{ | ||
var dto = new T(); | ||
|
||
foreach (var prop in typeof(T).GetProperties()) | ||
{ | ||
var index = res.Columns.FindIndex(c => c.ToLower() == prop.Name.ToLower()); | ||
var x = GetValue(res.Types[index], res.Values[i][index]); | ||
|
||
prop.SetValue(dto, x); | ||
} | ||
|
||
list.Add(dto); | ||
} | ||
|
||
return list; | ||
} | ||
} |