mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-16 21:03:18 +08:00
8becae8d0e
* Fix the release-tag pattern in version.json publicReleaseRefSpec requires `^refs/tags/v\d+\.\d+`, but every tag this repo has ever cut is unprefixed - 2.1.79, 2.1.72, 2.1.66 and so on - so the regex cannot match any of them. Checking out the real 2.1.79 tag and asking nbgv gives `2.1.79-g72a54c475f`, PublicRelease False: a tag-triggered build has always produced a -g suffixed version, which is fine while releases are pushed by hand and fatal once a workflow does it. Relaxed to `v?` so both spellings work. Same fix, same reason, as DapperAOT b5194b4 and StackExchange.Redis 611e478. Also adds the `$schema` line the file was missing. * CI: report the computed version, build on SDK 11, and test net481 **Report the computed version.** A step writes NuGetPackageVersion to the job summary and the log, before the build so it stays legible when a later step fails. Cutting a release now means reading that line off a green main run and tagging with exactly it - so the number has to be visible. A PR run builds refs/pull/N/merge, whose height includes the branch commits and the merge, so its number is *not* the one that will ship; the summary says so on anything that is not main, rather than inviting a mis-tag. This needs full history, so checkout gains fetch-depth: 0 (and moves v1 -> v4). **Build on SDK 11.** global.json already says rollForward:latestMajor, so simply installing the SDK 11 preview alongside 10 makes it the one that builds. Breakage from a new major toolchain then surfaces on an ordinary PR instead of when a release is being cut. Target frameworks are untouched - this changes what compiles the code, not what the code runs on - and global.json stays the single lever if we ever want to pin back to 10. **Test net481.** tests/Directory.Build.targets sets IsTestProject=false for net4x outside Windows, so the Ubuntu job has never run net481 - and net481 is the only TFM that compiles the ENTITY_FRAMEWORK, LINQ2SQL and OLEDB code paths. AppVeyor was quietly the only thing covering them, so retiring it (next commit) without this would drop that cover silently. New windows-job runs exactly that leg. GitHub's Windows images ship no SQL Server engine - only the OLEDB drivers - hence the install step; AppVeyor's image had an MSSQL$SQL2019 instance built in. MySQL and PostgreSQL are deliberately not provisioned there: FactMySqlAttribute and FactPostgresqlAttribute probe the server in a static constructor and skip the tests when it is unreachable, and the Ubuntu job already covers both providers against real servers. Also fixes `.NET Lib Pack`, which passed a cmd-style `%CD%\.nupkgs` to a job that has run on Ubuntu for years, and so has been writing to a directory named literally that. * Add release.yml: verify the tag, then publish to nuget.org over OIDC There has never been a workflow that publishes to nuget.org - AppVeyor pushed to two MyGet feeds on every main build, and the actual nuget.org releases were pushed by hand. That is also why the broken tag pattern went unnoticed for years. Triggered by a published GitHub Release, with workflow_dispatch as a dry run that does everything except the tag check and the push, so the pipeline can be proven (and the packages inspected, via the run artifact) without cutting a release. The guard is the point: nbgv computes the version from version.json plus commit height, so the tag name does not set it and a mistyped tag would otherwise ship a package that disagrees with its release. The step compares the two and fails loudly instead. A leading "v" is stripped, matching the `v?` pattern. Auth is Trusted Publishing (OIDC) - no long-lived key anywhere, which is the whole reason for the move. Setup lives outside this file and is recorded in the header comment: a GitHub environment named "release", a NUGET_USER secret, and a trusted-publishing policy on nuget.org for each of the six published packages. A package with no matching policy fails its push, so all six need one up front. Runs on Ubuntu, matching main.yml: that is the configuration CI actually proves, and it means bash expands the push glob (DapperAOT 13f9f0d had to unpick exactly that on Windows). Packages are uploaded as a run artifact before the push, so a failed push does not cost the build, and --skip-duplicate makes a partial push safely re-runnable. Output goes to `nupkgs`, not `.nupkgs`: upload-artifact skips hidden files by default and a dot-directory uploads as an empty artifact. No tests here - the commit being released already passed CI, and this job's only job is to reproduce that build and push it. * Retire AppVeyor, and drop MyGet from the badges release.yml now publishes to nuget.org on a tagged release, so AppVeyor's two MyGet deploy providers are both redundant and a second, unversioned place for packages to appear. The rest of AppVeyor went with them: its Windows build and test run is now main.yml's windows-job, which is the part worth keeping. That also retires the two encrypted MyGet API keys the file carried. Badges and package tables follow: the AppVeyor build badge becomes the GitHub Actions one, and the MyGet pre-release column and feed link come out of both readmes, since that feed is no longer fed by anything. build.ps1 / build.cmd are left in place - AppVeyor was their only caller, but they still work as a local build-test-pack entry point. * Stop packing Dapper.ProviderTools `dotnet pack Build.csproj` produces it, so a release workflow would push it - but it was last released as 2.0.90 in April 2021 and has had no work since. Shipping it now means a dormant package jumping to 2.1.x on the next release, and a nuget.org trusted-publishing policy that has to be kept alive for something nobody is maintaining. IsPackable=false instead, which drops the push set to the six packages the readme actually lists. The project still builds and is still referenced; un-set the property to resume shipping it. * Bump Microsoft.SourceLink.GitHub to clear the NU1902 build break Microsoft.Build.Tasks.Git 10.0.300, pulled in transitively by SourceLink, has picked up GHSA-23fw-v26w-5fgq. TreatWarningsAsErrors turns the NU1902 audit warning into an error, so Dapper and Dapper.StrongName fail to build - tests and benchmarks only warn, since they set TreatWarningsAsErrors=false. This is not caused by anything else in this PR: it reproduces on main, under SDK 10 and SDK 11 alike. It is bundled here only because it blocks the checks, and with them the first real run of the new net481 job. There is no fixed 10.x - the only versions above 10.0.300 are 11.0.100 previews - so this takes the rc. That is tolerable for this particular dependency: SourceLink is PrivateAssets=all, a build-time-only asset that never becomes a dependency of the packages we publish, and it already tracks the SDK we now build with. Verified locally: NU1902 is gone and the build exits 0, leaving the pre-existing NU1903 warnings in tests/benchmarks (SQLitePCLRaw, System.Security.Cryptography.Xml) which are warnings only and are a separate matter. * Windows: reach SQL Server over shared memory, not TCP First real run of this job: 735 of 825 net481 tests failed, every one of them "TCP Provider, error: 0 - The remote computer refused the network connection." The install itself was fine - ankane/setup-sqlserver puts in a default MSSQLSERVER instance with SQL auth - but a Windows SQL Server install leaves the TCP/IP protocol disabled, so nothing is listening on 1433 and every connection to tcp:localhost,1433 is refused. AppVeyor never hit this because it connected to `(local)\SQL2019`, not over TCP. Dropping the `tcp:`/port from both connection strings uses shared memory against the local default instance, which needs no protocol configuration. Also pins the engine to 2019: that is what the Ubuntu job's service container runs and what AppVeyor ran these tests against, and the action otherwise installs SQL Server 2025. This job exists to preserve cover that already existed - moving the engine version underneath it at the same time is a separate decision, and the OLEDB tests in particular use the legacy SQLOLEDB provider. Adds a reachability check before the tests, so the next time something is wrong with the server the job says so in one line instead of several hundred identical connection failures. * Windows: stop pinning SQL Server 2019, it hangs the install The pin added in the previous commit hung "Install SQL Server" for 28 minutes before the run was cancelled; the same step with the action's default took 2m48s on the same image a run earlier. SQL Server 2019 predates the Windows Server 2025 runner image, and its setup stalls rather than failing outright. Reverting to the action's default. The conservatism was not worth its cost: it was meant to keep this job as close as possible to what AppVeyor proved, but it cannot install at all. The net481 leg therefore runs against a newer engine than AppVeyor used - worth knowing, and called out in the comment, because the OLEDB tests reach SQL Server through the legacy SQLOLEDB provider and that combination has not been exercised before. The shared-memory connection strings and the reachability check stay: those were the actual fix for the 735 connection failures, and are independent of version.
4.7 KiB
4.7 KiB
Dapper.SqlBuilder - a simple sql formatter for .Net
Packages
| Package | NuGet Stable | NuGet Pre-release | Downloads |
|---|---|---|---|
| Dapper.SqlBuilder |
Features
Dapper.SqlBuilder contains a number of helper methods for generating sql.
The list of extension methods in Dapper.SqlBuilder right now are:
SqlBuilder AddParameters(dynamic parameters);
SqlBuilder Select(string sql, dynamic parameters = null);
SqlBuilder Where(string sql, dynamic parameters = null);
SqlBuilder OrWhere(string sql, dynamic parameters = null);
SqlBuilder OrderBy(string sql, dynamic parameters = null);
SqlBuilder GroupBy(string sql, dynamic parameters = null);
SqlBuilder Having(string sql, dynamic parameters = null);
SqlBuilder Set(string sql, dynamic parameters = null);
SqlBuilder Join(string sql, dynamic parameters = null);
SqlBuilder InnerJoin(string sql, dynamic parameters = null);
SqlBuilder LeftJoin(string sql, dynamic parameters = null);
SqlBuilder RightJoin(string sql, dynamic parameters = null);
SqlBuilder Intersect(string sql, dynamic parameters = null);
Template
SqlBuilder allows you to generate N SQL templates from a composed query, it can easily format sql when you are attaching parameters and how, e.g:
var builder = new SqlBuilder()
.Where("a = @a", new { a = 1 })
.Where("b = @b", new { b = 2 })
.OrderBy("a")
.OrderBy("b");
var counter = builder.AddTemplate("select count(*) from table /**where**/");
var selector = builder.AddTemplate("select * from table /**where**/ /**orderby**/");
var count = cnn.Query(counter.RawSql, counter.Parameters).Single();
var rows = cnn.Query(selector.RawSql, selector.Parameters);
it's same as
var count = cnn.Query("select count(*) from table where a = @a and b = @b", new { a = 1, b = 1 });
var rows = cnn.Query("select * from table where a = @a and b = @b order by a, b", new { a = 1, b = 1 });
Dynamic Filter Paging Example
var builder = new SqlBuilder();
var selectTemplate = builder.AddTemplate(@"select X.* from (
select us.*, ROW_NUMBER() OVER (/**orderby**/) AS RowNumber
from Users us
/**where**/
) as X
where RowNumber between @start and @finish", new { start, finish });
var countTemplate = builder.AddTemplate(@"select count(*) from Users /**where**/");
if (userId.HasValue())
builder.Where($"t.userId = @{nameof(userId)}", new { userId });
if (isCancel)
builder.Where($"t.isCancel = @{nameof(isCancel)}", new { isCancel });
builder.OrderBy(string.Format("t.id {0}", orderDesc ? "desc" : "asc"));
var users = conn.Query<User>(selectTemplate.RawSql, selectTemplate.Parameters);
var count = conn.ExecuteScalar<int>(countTemplate.RawSql, countTemplate.Parameters);
//..etc..
Limitations and caveats
Combining the Where and OrWhere methods
The OrWhere method currently groups all and and or clauses by type,
then join the groups with and or or depending on the first call.
This may result in possibly unexpected outcomes.
See also issue 647.
Example Where first
When providing the following clauses
sql.Where("a = @a1");
sql.OrWhere("b = @b1");
sql.Where("a = @a2");
sql.OrWhere("b = @b2");
SqlBuilder will generate sql
a = @a1 AND a = @a2 AND ( b = @b1 OR b = @b2 )
and not say
a = @a1 OR b = @b1 AND a = @a2 OR b = @b2
Example OrWhere first
When providing the following clauses
sql.OrWhere("b = @b1");
sql.Where("a = @a1");
sql.OrWhere("b = @b2");
sql.Where("a = @a2");
SqlBuilder will generate sql
a = @a1 OR a = @a2 OR ( b = @b1 OR b = @b2 )