Skip to content

Commit

Permalink
Extract Dapper-like ORM methods to a separate client, update readme a…
Browse files Browse the repository at this point in the history
…nd bump version in nuspec
  • Loading branch information
arthrp committed Feb 27, 2022
1 parent 347615b commit 960ffd9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ var queryResults = await client.Query("select * from foo");

var executeResults = await client.Execute("insert into foo (name) values('test')");
```

There is also a basic ORM client similar to Dapper:
```csharp
public class FooResultDto
{
public int Id { get; set; }
public string Name { get; set; }
}

var rqClient = new RqliteOrmClient("http://localhost:6000");

var queryresults = await rqClient.Query<FooResultDto>("select * from foo"); //Returns List<FooResultDto>
```
You can see more examples in unit tests. NB: please report performance problems if any.
2 changes: 1 addition & 1 deletion RqliteDotnet.Test/RqliteClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task QueryWithGenerics_Works()
{
var client = HttpClientMock.GetQueryMock();

var rqClient = new RqliteClient("http://localhost:6000", client);
var rqClient = new RqliteOrmClient("http://localhost:6000", client);
var queryresults = await rqClient.Query<FooResultDto>("select * from foo");

Assert.AreEqual(1, queryresults.Count);
Expand Down
41 changes: 6 additions & 35 deletions RqliteDotnet/RqliteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace RqliteDotnet;

public class RqliteClient
{
private readonly HttpClient _httpClient;
protected readonly HttpClient _httpClient;

public RqliteClient(string uri, HttpClient? client = null)
{
Expand All @@ -33,7 +33,7 @@ public async Task<QueryResults> Query(string query)
var result = JsonSerializer.Deserialize<QueryResults>(str, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
return result;
}

public async Task<ExecuteResults> Execute(string command)
{
var request = new HttpRequestMessage(HttpMethod.Post, "/db/execute?timings");
Expand All @@ -42,7 +42,7 @@ public async Task<ExecuteResults> Execute(string command)
var result = await _httpClient.SendTyped<ExecuteResults>(request);
return result;
}

public async Task<ExecuteResults> Execute(IEnumerable<string> commands, DbFlag? flags)
{
var parameters = GetParameters(flags);
Expand All @@ -54,7 +54,7 @@ public async Task<ExecuteResults> Execute(IEnumerable<string> commands, DbFlag?
var result = await _httpClient.SendTyped<ExecuteResults>(request);
return result;
}

public async Task<QueryResults> QueryParams<T>(string query, params T[] qps) where T: QueryParameter
{
var request = new HttpRequestMessage(HttpMethod.Post, "/db/query?timings");
Expand All @@ -76,35 +76,6 @@ public async Task<QueryResults> QueryParams<T>(string query, params T[] qps) whe
return result;
}

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;
}

private string GetParameters(DbFlag? flags)
{
if (flags == null) return "";
Expand All @@ -117,14 +88,14 @@ private string GetParameters(DbFlag? flags)

if ((flags & DbFlag.Transaction) == DbFlag.Transaction)
{
result.Append("&transation");
result.Append("&transaction");
}

if (result.Length > 0) result[0] = '?';
return result.ToString();
}

private object GetValue(string valType, JsonElement el)
protected object GetValue(string valType, JsonElement el)
{
object? x = valType switch
{
Expand Down
5 changes: 3 additions & 2 deletions RqliteDotnet/RqliteDotnet.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>RqliteDotnet</id>
<version>1.0.0</version>
<authors>arthrp</authors>
<version>1.0.1</version>
<authors>arthrp, otoolep et al.</authors>
<description>Client for Rqlite - lightweight distributed database</description>
<language>en-US</language>
<license type="expression">MIT</license>
<projectUrl>https://github.com/rqlite/rqlite-dotnet/</projectUrl>
</metadata>
</package>
43 changes: 43 additions & 0 deletions RqliteDotnet/RqliteOrmClient.cs
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;
}
}

0 comments on commit 960ffd9

Please sign in to comment.