drittich.SimpleQuery
1.3.14
See the version list below for details.
dotnet add package drittich.SimpleQuery --version 1.3.14
NuGet\Install-Package drittich.SimpleQuery -Version 1.3.14
<PackageReference Include="drittich.SimpleQuery" Version="1.3.14" />
paket add drittich.SimpleQuery --version 1.3.14
#r "nuget: drittich.SimpleQuery, 1.3.14"
// Install drittich.SimpleQuery as a Cake Addin #addin nuget:?package=drittich.SimpleQuery&version=1.3.14 // Install drittich.SimpleQuery as a Cake Tool #tool nuget:?package=drittich.SimpleQuery&version=1.3.14
simple-query
A library for generating a simple model layer for a SQLite database
Generating the model
To generate the model code, you will run the SimpleQuery.CodeGen.exe
executable. You can find this in the C:\Users\[YOUR_USERNAME]]\.nuget\packages\drittich.simplequery\x.x.x\tools
folder. Before doing this, create an appsettings.json
file in the same folder as the executable. This file should contain the following settings:
{
"Settings": {
"ConnectionString": "",
"TargetFolder": "",
"ExcludeTables": [ ]
}
}
Populate the connection string for your SQLite database, as well as the target folder you want the files created in. If there are any tables you do not want modeled, you can add them to the ExcludeTables
list.
Make sure you reference this package in whatever project you have generated the model in.
Using the model
The SimpleQueryService
provides a simple and efficient way to interact with a SQLite database in C#. Below are examples of how to use its various methods.
Setting up SimpleQueryService
First, you need to initialize the SimpleQueryService
with the connection string to your SQLite database:
var connectionString = "Data Source=mydatabase.db;";
var queryService = new SimpleQueryService(connectionString);
This will allow you to begin querying your database using the methods provided by SimpleQueryService
.
Retrieving a Single Entity by ID
To retrieve the first entity of a specific type with a given ID, you can use the GetFirstAsync
method:
var user = await queryService.GetFirstAsync<User>(userId);
// Process the user object
Retrieving an Entity or Null if Not Found
If you want to retrieve an entity but return null
if it's not found (to avoid exceptions), use the GetFirstOrDefaultAsync
method:
var user = await queryService.GetFirstOrDefaultAsync<User>(userId);
if (user != null)
{
// Process the user object
}
else
{
Console.WriteLine("User not found.");
}
Retrieving All Entities of a Type
To get all entities of a specific type, use the GetAllAsync
method without providing any IDs:
var users = await queryService.GetAllAsync<User>();
foreach (var user in users)
{
// Process each user
}
Filtering Entities by Column Value
To find entities based on a specific column's value, use the GetAllByColumnValueAsync
method:
var usersByName = await queryService.GetAllByColumnValueAsync<User>("Name", "John Doe");
foreach (var user in usersByName)
{
// Process each matching user
}
Complex Filtering with WHERE Clause
For more complex queries that require a custom WHERE clause, use the GetAllByWhereClauseAsync
method:
var whereClause = "Age > @Age AND Active = @Active";
var parameters = new { Age = 18, Active = true };
var activeAdultUsers = await queryService.GetAllByWhereClauseAsync<User>(whereClause, parameters);
foreach (var user in activeAdultUsers)
{
// Process each matching user
}
Retrieving the First Entity Matching Column Values
To retrieve the first entity that matches a set of column values, use GetFirstByColumnValuesAsync
. This is useful when you need a single entity that matches a complex condition:
var columnValues = new Dictionary<string, object>
{
{"Name", "John Doe"},
{"Age", 30}
};
var user = await queryService.GetFirstByColumnValuesAsync<User>(columnValues);
// Process the user object
Retrieving the First Entity or Null
If you prefer to get null
instead of throwing an exception when no entities match the specified criteria, use GetFirstOrDefaultByColumnValuesAsync
:
var columnValues = new Dictionary<string, object>
{
{"Name", "Jane Doe"},
{"Active", true}
};
var user = await queryService.GetFirstOrDefaultByColumnValuesAsync<User>(columnValues);
if (user != null)
{
// Process the user object
}
else
{
Console.WriteLine("No user matches the specified criteria.");
}
Retrieving All Entities Matching Column Values
To retrieve a list of all entities that match a set of column values, you can use GetAllByColumnValuesAsync
. This method is particularly useful for more complex queries that cannot be expressed with a single column filter:
var columnValues = new Dictionary<string, object>
{
{"Department", "Engineering"},
{"YearsOfExperience", 5}
};
var employees = await queryService.GetAllByColumnValuesAsync<Employee>(columnValues);
foreach (var employee in employees)
{
// Process each employee
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Dapper (>= 2.1.0)
- Microsoft.Data.Sqlite (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.3.22 | 102 | 7/31/2024 | |
1.3.21 | 75 | 7/31/2024 | |
1.3.19 | 94 | 7/31/2024 | |
1.3.18 | 71 | 7/31/2024 | |
1.3.17 | 67 | 7/31/2024 | |
1.3.16 | 70 | 7/31/2024 | |
1.3.14 | 112 | 7/22/2024 | |
1.3.5 | 123 | 7/3/2024 | |
1.3.3 | 115 | 4/28/2024 | |
1.3.2 | 103 | 4/26/2024 | |
1.3.1 | 115 | 4/26/2024 | |
1.2.1 | 130 | 3/24/2024 | |
1.2.0 | 126 | 3/13/2024 | |
1.1.8 | 134 | 3/10/2024 | |
1.1.0 | 103 | 3/9/2024 | |
1.0.18 | 131 | 3/9/2024 | |
1.0.17 | 133 | 3/9/2024 | |
1.0.16 | 130 | 3/7/2024 | |
1.0.15 | 143 | 3/5/2024 | |
1.0.14 | 228 | 3/5/2024 | |
1.0.13 | 216 | 3/5/2024 |
Log some metrics on dispose of service