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

Code refactoring #17

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion RqliteDotnet/IRqliteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IRqliteClient
/// <param name="level"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<QueryResults> Query(string query, ReadLevel level, CancellationToken cancellationToken);
Task<QueryResults?> Query(string query, ReadLevel level, CancellationToken cancellationToken);

/// <summary>
/// Execute command and return result
Expand Down
18 changes: 18 additions & 0 deletions RqliteDotnet/IRqliteOrmClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using RqliteDotnet.Dto;

namespace RqliteDotnet;

public interface IRqliteOrmClient : IRqliteClient
{
/// <summary>
/// Query Rqlite DB 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>
Task<List<T>> Query<T>(string query) where T: new();

Task<List<U>> QueryParams<T, U>(string query, CancellationToken cancellationToken, params T[] qps)
where T: QueryParameter
where U : new();
}
8 changes: 1 addition & 7 deletions RqliteDotnet/RqliteClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using RqliteDotnet.Dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace RqliteDotnet;

Expand All @@ -33,7 +27,7 @@ public async Task<string> Ping(CancellationToken cancellationToken = default)
}

/// <inheritdoc />
public async Task<QueryResults> Query(string query, ReadLevel level = ReadLevel.Default, CancellationToken cancellationToken = default)
public async Task<QueryResults?> Query(string query, ReadLevel level = ReadLevel.Default, CancellationToken cancellationToken = default)
{
var url = UrlBuilder.Build("/db/query?timings", query, level);

Expand Down
22 changes: 1 addition & 21 deletions RqliteDotnet/RqliteOrmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,17 @@

namespace RqliteDotnet;

public interface IRqliteOrmClient : IRqliteClient
{
/// <summary>
/// Query Rqlite DB 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>
Task<List<T>> Query<T>(string query) where T: new();

Task<List<U>> QueryParams<T, U>(string query, CancellationToken cancellationToken, params T[] qps)
where T: QueryParameter
where U : new();
}

public class RqliteOrmClient : RqliteClient, IRqliteOrmClient
{
public RqliteOrmClient(HttpClient client) : base(client) { }

public RqliteOrmClient(string uri, HttpClient? client = null) : base(uri, client) {}

/// <summary>
/// Query Rqlite DB 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>
/// <inheritdoc />
public async Task<List<T>> Query<T>(string query) where T : new()
{
var response = await Query(query);
if (response.Results!.Count > 1)

Check warning on line 16 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 16 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
throw new DataException("Query returned more than 1 result. At the moment only 1 result supported");
var res = response.Results[0];

Expand All @@ -47,8 +27,8 @@

foreach (var prop in typeof(T).GetProperties())
{
var index = res.Columns.FindIndex(c => c.ToLower() == prop.Name.ToLower());

Check warning on line 30 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 30 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
var val = GetValue(res.Types[index], res.Values[i][index]);

Check warning on line 31 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 31 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

prop.SetValue(dto, val);
}
Expand Down Expand Up @@ -78,8 +58,8 @@

foreach (var prop in typeof(U).GetProperties())
{
var index = res.Columns.FindIndex(c => c.ToLower() == prop.Name.ToLower());

Check warning on line 61 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 61 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
var val = GetValue(res.Types[index], res.Values[i][index]);

Check warning on line 62 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 62 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

prop.SetValue(dto, val);
}
Expand Down
Loading