A small library for building SQL queries in a better way than regular string concatenation.
Clauses can be added in any order.
Mostly for use directly by ADO.NET or micro-ORMs such as Dapper.
A small helper project that allows transforming ODataQueryOptions<T>
directly into
a SelectStatement
.
NuGet Awesome.Data.Sql.Builder
Install-Package Awesome.Data.Sql.Builder
NuGet Awesome.Data.Sql.Builder.OData
Install-Package Awesome.Data.Sql.Builder.OData
Here is a sample unit test from the project.
var statement = SqlStatements.Select(new[] { "u.ID", "u.Name", "u.EmailAddress" })
.From("Users u")
.Where("u.IsCool = TRUE")
.Where("u.Name LIKE @Query")
.OrderBy("u.Name", false)
.Limit(3).Offset(6);
var sql = statement.ToSql();
Assert.That(
sql,
SqlCompareConstraint.EqualTo(@"SELECT
u.ID, u.Name, u.EmailAddress
FROM
Users u
WHERE
u.IsCool = TRUE AND
u.Name LIKE @Query
ORDER BY
u.Name DESC
LIMIT 3 OFFSET 6"));
var statement = new SelectStatement(new[] { "u.ID" })
.From("Users u")
.Limit(3)
.Offset(6);
var sql = new SqlServer2012SqlRenderer().RenderSelect(statement);
Assert.That(
sql,
SqlCompareConstraint.EqualTo(@"SELECT
u.ID
FROM
Users u
OFFSET 6 ROWS
FETCH NEXT 3 ROWS ONLY"));
var options = ODataQueryOptionsHelper.Build<TestDTO>(
"$select=Id,Name,Contact/FirstName,Contact/BirthDate,Contact/Address/City&" +
"$expand=Contact,Contact/Address&" +
"$top=10&" +
"$skip=20");
var result = new ODataQueryOptionsToSqlStatement().ToSelect(options).First();
Assert.That(
result.ColumnsList,
Is.EquivalentTo(new[]
{
"Id",
"Name",
"Contact/FirstName",
"Contact/BirthDate",
"Contact/Address/City"
}));
Assert.That(result.LimitClause, Is.EqualTo("10"));
Assert.That(result.OffsetClause, Is.EqualTo("20"));
Only supports $select
, $top
, $skip
and $inlinecount=allpages
for now!
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Make sure you add a unit test!
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author: cdroulers
LGPL: See LICENSE
- Support for different SQL providers (SQL Server in particular) in a pluggable way.
- First public version. Supports all basic SQL operations for PostgreSQL.
- ???
- First public version. Supports
$select
,$top
,$skip
and$inlinecount=allpages
.
- Support basic
$filter
operations