StringThing.MySql
0.1.2
See the version list below for details.
dotnet add package StringThing.MySql --version 0.1.2
NuGet\Install-Package StringThing.MySql -Version 0.1.2
<PackageReference Include="StringThing.MySql" Version="0.1.2" />
<PackageVersion Include="StringThing.MySql" Version="0.1.2" />
<PackageReference Include="StringThing.MySql" />
paket add StringThing.MySql --version 0.1.2
#r "nuget: StringThing.MySql, 0.1.2"
#:package StringThing.MySql@0.1.2
#addin nuget:?package=StringThing.MySql&version=0.1.2
#tool nuget:?package=StringThing.MySql&version=0.1.2
StringThing.MySql
Injection-safe interpolated SQL for MySQL/MariaDB with type-checked parameterization, built on MySqlConnector. Part of StringThing.
Install
dotnet add package StringThing.MySql
Quick start
var userId = 42;
MySql stmt = $"SELECT name FROM users WHERE id = {userId}";
await using var command = stmt.ToCommand(connection);
Parameters are named automatically using the variable name: @userId, not @p0.
Supported types
bool, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, string, char, Guid, DateTime, DateTimeOffset, DateOnly, TimeOnly, TimeSpan, byte[].
Nullable reference types (string?, byte[]?) map to NULL.
Fragments
var minAge = 18;
var status = "active";
MySqlFragment filter = $"age >= {minAge} AND status = {status}";
MySql stmt = $"SELECT * FROM users WHERE {filter}";
Multi-row insert
record InsertUser(int Id, string Name, string? Email) : IMySqlRow
{
public MySqlFragment RowValues => $"({Id}, {Name}, {Email})";
}
var users = new InsertUser[] { new(1, "alice", "alice@example.com"), new(2, "bob", null) };
MySql stmt = $"INSERT INTO users (id, name, email) VALUES {MySql.InsertRows(users)}";
IN list
var ids = new List<int> { 1, 2, 3 };
MySql stmt = $"SELECT * FROM users WHERE id IN {MySql.InList([.. ids])}";
JSON
Implement IMySqlJson on your types to store them as JSON. Use your choice of serializer:
record UserData(string Name, int Age) : IMySqlJson
{
public string ToJson() => JsonSerializer.Serialize(this);
}
MySql stmt = $"INSERT INTO data (payload) VALUES ({userData})";
Type overrides
// TEXT instead of default VARCHAR
MySql stmt = $"WHERE description = {MySql.Text(description)}";
// TIMESTAMP instead of default DATETIME
MySql stmt = $"WHERE created < {MySql.Timestamp(date)}";
Unsafe escape hatch
using StringThing.UnsafeSql;
var tableName = Sql.Unsafe("users");
MySql stmt = $"SELECT * FROM {tableName} WHERE id = {userId}";
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- MySqlConnector (>= 2.5.0)
- StringThing.Core (>= 0.1.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on StringThing.MySql:
| Package | Downloads |
|---|---|
|
StringThing.MySql.Dapper
Dapper result mapping for StringThing.MySql. Injection-safe interpolated SQL on input, Dapper mapping on output. Dapper is bundled internally. |
GitHub repositories
This package is not used by any popular GitHub repositories.