drittich.SimpleQuery 1.3.3

dotnet add package drittich.SimpleQuery --version 1.3.3
NuGet\Install-Package drittich.SimpleQuery -Version 1.3.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="drittich.SimpleQuery" Version="1.3.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add drittich.SimpleQuery --version 1.3.3
#r "nuget: drittich.SimpleQuery, 1.3.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install drittich.SimpleQuery as a Cake Addin
#addin nuget:?package=drittich.SimpleQuery&version=1.3.3

// Install drittich.SimpleQuery as a Cake Tool
#tool nuget:?package=drittich.SimpleQuery&version=1.3.3

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.3 93 4/28/2024
1.3.2 76 4/26/2024
1.3.1 88 4/26/2024
1.2.1 102 3/24/2024
1.2.0 98 3/13/2024
1.1.8 107 3/10/2024
1.1.0 80 3/9/2024
1.0.18 102 3/9/2024
1.0.17 106 3/9/2024
1.0.16 104 3/7/2024
1.0.15 108 3/5/2024
1.0.14 176 3/5/2024
1.0.13 165 3/5/2024

Refactor overloads