Carbunql 0.8.11
See the version list below for details.
dotnet add package Carbunql --version 0.8.11
NuGet\Install-Package Carbunql -Version 0.8.11
<PackageReference Include="Carbunql" Version="0.8.11" />
paket add Carbunql --version 0.8.11
#r "nuget: Carbunql, 0.8.11"
// Install Carbunql as a Cake Addin #addin nuget:?package=Carbunql&version=0.8.11 // Install Carbunql as a Cake Tool #tool nuget:?package=Carbunql&version=0.8.11
Carbunql
This C# library allows you to flexibly edit Raw SQL as object models. It supports easy addition and modification of columns and search conditions, as well as transformations to CTEs (Common Table Expressions), subqueries, and even insert and update queries.
By using Carbunql, you can maximize the reusability of Raw SQL and efficiently manage your queries.
Demo
This is a sample that accepts any search conditions and adds them as search conditions. If no conditions are entered, they will be excluded from the search conditions.
Source code
I will only extract the parts that process SQL. Please refer to the link below for the complete source. https://github.com/mk3008/Carbunql/blob/main/demo/DynamicFiltering/Program.cs
private static string GenerateProductQuery(decimal? minPrice, decimal? maxPrice, string? category, bool? inStock)
{
var sql = """
SELECT
p.product_id,
p.product_name,
p.price,
p.category,
p.in_stock
FROM
product as p
""";
var pname = ":category";
// Convert the selection query to an object
var sq = SelectQuery.Parse(sql)
.GreaterThanOrEqualIfNotNullOrEmpty("price", minPrice)
.LessThanOrEqualIfNotNullOrEmpty("price", maxPrice)
.AddParameter(pname, category)
.EqualIfNotNullOrEmpty("category", pname)
.EqualIfNotNullOrEmpty("in_stock", inStock);
return sq.ToText();
}
Parameter
Enter minimum price (or leave blank to omit):
Enter maximum price (or leave blank to omit):
100
Enter category (or leave blank to omit):
tea
Enter in-stock status (true/false) (or leave blank to omit):
true
Generated SQL
You can see that only the specified search criteria has been inserted.
/*
:category = 'tea'
*/
SELECT
p.product_id,
p.product_name,
p.price,
p.category,
p.in_stock
FROM
product AS p
WHERE
p.price <= 100
AND p.category = :category
AND p.in_stock = True
Advanced Demo
This is a sample that displays an aggregate report for a specified year and month.
You can see that even complex processing using CTE and UNION can be written simply.
Source code
I will only extract the parts that process SQL. Please refer to the link below for the complete source. https://github.com/mk3008/Carbunql/blob/main/demo/DynamicCTE/Program.cs
public static string GenerateReportQuery(bool includeSummary, DateTime summaryMonth)
{
string dailySummaryQuery = """
SELECT
sale_date
, sum(amount) AS amount_total
, '' as caption
, 1 as sort_number
FROM
salse
GROUP BY
sale_date
""";
var dailySummary = FluentTable.Create(dailySummaryQuery, "daily_summary", "d");
string monthlySummaryQuery = """
SELECT
date_trunc('month', sale_date) + '1 month -1 day' as sale_date
, sum(amount) AS amount_total
, 'monthly total' as caption
, 2 as sort_number
FROM
salse
GROUP BY
date_trunc('month', sale_date) + '1 month -1 day'
""";
var monthlySummary = FluentTable.Create(monthlySummaryQuery, "monthly_summary", "m");
// Create daily summary query
var sq = new SelectQuery()
.From(dailySummary)
.SelectAll(dailySummary);
if (includeSummary)
{
// Add monthly summary query with UNION ALL
sq.UnionAll(() =>
{
var xsq = new SelectQuery()
.From(monthlySummary)
.SelectAll(monthlySummary);
return xsq;
});
}
// Add date filter condition
var saleDate = ":sale_date";
sq.AddParameter(saleDate, summaryMonth)
.BetweenInclusiveStart("sale_date", saleDate, $"{saleDate}::timestamp + '1 month'");
// Convert the entire query to a CTE
sq = sq.ToCTEQuery("final", "f");
// Add sorting conditions
sq.RemoveSelect("sort_number")
.OrderBy("sale_date")
.OrderBy("sort_number");
return sq.ToText();
}
Parameter
Which month to summarize? (yyyy-mm-dd)
2024-08-01
Include monthly summary rows? (true/false)
true
Generated SQL
A query that combines a daily aggregation query and a monthly aggregation query will be output.
Please also note that the search conditions are inserted in two places. Carbunql will insert the search conditions in the appropriate positions without you having to specify them in detail.
/*
:sale_date = '2024/08/01 0:00:00'
*/
WITH
daily_summary AS (
SELECT
sale_date,
SUM(amount) AS amount_total,
'' AS caption,
1 AS sort_number
FROM
salse
WHERE
:sale_date <= salse.sale_date
AND salse.sale_date < :sale_date::timestamp + '1 month'
GROUP BY
sale_date
),
monthly_summary AS (
SELECT
DATE_TRUNC('month', sale_date) + '1 month -1 day' AS sale_date,
SUM(amount) AS amount_total,
'monthly total' AS caption,
2 AS sort_number
FROM
salse
WHERE
:sale_date <= salse.sale_date
AND salse.sale_date < :sale_date::timestamp + '1 month'
GROUP BY
DATE_TRUNC('month', sale_date) + '1 month -1 day'
),
final AS (
SELECT
d.sale_date,
d.amount_total,
d.caption,
d.sort_number
FROM
daily_summary AS d
UNION ALL
SELECT
m.sale_date,
m.amount_total,
m.caption,
m.sort_number
FROM
monthly_summary AS m
)
SELECT
f.sale_date,
f.amount_total,
f.caption
FROM
final AS f
ORDER BY
f.sale_date,
f.sort_number
Features
- Convert Raw SQL to object models.
- Convert object models back to Raw SQL.
- Format Raw SQL.
- Convert select queries to various query types:
- Insert queries
- Update queries
- Delete queries
- Merge queries
- Table creation queries
- Edit columns and search conditions in select queries.
- No DBMS environment or entity classes required.
- Perform basic syntax checks.
- The Carbunql.Dapper library allows you to run queries with Dapper.
You can try out some of the processing on the online demo site.
https://mk3008.github.io/Carbunql/
Constraints
- Comments are removed when modeling.
Getting started
Install the package from NuGet.
PM> Install-Package Carbunql
https://www.nuget.org/packages/Carbunql/
Model a select query
Just pass the select query string to the constructor of the SelectQuery class.
using Carbunql;
var text = "select s.sale_id, s.store_id, date_trunc('month', s.sale_date) as allocate_ym, s.sale_price from sales as s";
var sq = SelectQuery.Parse(text);
Return the model to a select query
Use the ToText
or ToOneLineText
method.
The ToText
method will return a formatted select query. Parameter information will also be added as a comment.
The ToOneLineText
method will output a single line without formatting. Use the ToOneLineText method if performance is important.
using Carbunql;
var text = "select s.sale_id, s.store_id, date_trunc('month', s.sale_date) as allocate_ym, s.sale_price from sales as s";
var sq = SelectQuery.Parse(text);
var query = sq.ToOneLineText();
Execute the query
If you use Carbunql.Dapper, you can execute it directly without writing it back to RawSQL.
This is the recommended method because parameter information is also passed.
PM> Install-Package Carbunql.Dapper
The full source code for the demo can be found below. https://github.com/mk3008/Carbunql/blob/main/test/Carbunql.Dapper.Test/DapperTest.cs
using var cn = PostgresDB.ConnectionOpenAsNew(Logger);
var sql = @"with
data_ds(c1, c2) as (
values
(1,1)
, (1,2)
, (2,1)
, (2,2)
)
select
*
from
data_ds
where
c1 = :val
";
var sq = SelectQuery.Parse(sql);
sq.AddParameter(":val", 1);
using var r = cn.ExecuteReader(sq);
var cnt = 0;
while (r.Read())
{
cnt++;
}
Assert.Equal(2, cnt);
Create an empty select query
If you do not specify arguments in the constructor, a model without SELECT and FROM clauses will be created. Please add SELECT and FROM clauses manually.
using Carbunql;
var sq = new SelectQuery();
Add a FROM clause
If you added an empty select query, use the From
function to manually add a FROM clause. The first argument is the table name, and the second argument is the alias name.
[!NOTE] Don't forget to import the namespace Carbunql.Fluent.
using Carbunql;
using Carbunql.Fluent;
var sq = new SelectQuery()
.From("customer", "c");
Add a column to select
You can add a column to select by using the Select
function. The first argument is the column name, and the second argument is the column alias name. The column alias name is optional.
using Carbunql;
using Carbunql.Fluent;
var sq = new SelectQuery()
.From("customer", "c")
.Select("c.customer_id")
.Select("c.first_name || c.last_name", "customer_name");
Add search conditions
You can add search conditions by using the AddWhere
method.
The first argument is the name of the column to which you want to add a condition.
The second argument is a delegate (or lambda expression) that takes the column source and column name as input and generates the condition part of the SQL.
using Carbunql;
var text = "select s.sale_id, s.store_id, date_trunc('month', s.sale_date) as allocate_ym, s.sale_price from sales as s";
var sq = SelectQuery.Parse(text)
.AddWhere("sale_id", (source, column) => $"{source.Alias}.{column} = 1");
The above code can be written even more succinctly if we import the namespace Carbunql.Fluent:
using Carbunql;
using Carbunql.Fluent;
var text = "select s.sale_id, s.store_id, date_trunc('month', s.sale_date) as allocate_ym, s.sale_price from sales as s";
var sq = SelectQuery.Parse(text)
.Equal("sale_id", 1);
About the AddWhere function
Please note the following specifications.
- It is added with AND conditions.
- If the column name to be searched does not exist, an error will occur.
- The search target is the entire query. The query reference order is analyzed, and the conditions are inserted only for the query source at the deepest position.
For example, the search condition for the column sale_id
is inserted only for s
in the subquery.
It is not inserted for the query source subq
. This is because it references a query source to which the search condition has already been applied.
select
subq.sale_id
from
(
select
s.sale_id
from
sale as s --the deepest query source to which sale_id belongs
) subq
In this way, since the reference order of the query source is analyzed, the search condition is inserted in the optimal position without any consideration of the insertion location.
However, since this library does not reference the DBMS (table definition), it cannot detect column names that are not explicitly stated in the select query.
If you try to insert a search condition for the column price
into the select query above, an error will occur even if the column is defined in the DBMS.
In this case, make it clear that there is a column name in the select query. If you write it like this, the column price
will be detected.
select
subq.sale_id
from
(
select
s.sale_id
, s.price
from
sale as s
) subq
Also, when searching for a column, it checks which query source it is attributed to. For this reason, if the alias name of the query source is omitted, it may not be detected correctly.
For example, in the case of a single query source like the one below, the query source is only s
, so it is possible to identify the column price
.
select
sale_id
, price
from
sale as s
However, if a table is joined, parsing will fail even if the SQL is executable because it does not refer to the DBMS (table definition). In the following query, it is not possible to determine whether the column "price" belongs to s
or c
based on the information in the select query alone.
select
sale_id
, price
from
sale as s
inner join customer as c on s.customer_id = c.customer_id
Referenced Libraries
ZString / MIT License
https://github.com/Cysharp/ZString
https://github.com/Cysharp/ZString/blob/master/LICENSE
Copyright (c) 2020 Cysharp, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Dapper / Apache License 2.0
https://github.com/DapperLib/Dapper
https://github.com/DapperLib/Dapper/blob/main/License.txt
The Dapper library and tools are licenced under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
The Dapper logo is copyright Marc Gravell 2021 onwards; it is fine to use the Dapper logo when referencing the Dapper library and utilities, but the Dapper logo (including derivatives) must not be used in a way that misrepresents an external product or library as being affiliated or endorsed with Dapper. For example, you must not use the Dapper logo as the package icon on your own external tool (even if it uses Dapper internally), without written permission. If in doubt: ask.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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. |
-
net6.0
- MessagePack (>= 2.5.124)
- ZString (>= 2.5.0)
-
net8.0
- MessagePack (>= 2.5.124)
- ZString (>= 2.5.0)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Carbunql:
Package | Downloads |
---|---|
RedOrb
simply object relation mapping framework. |
|
Carbunql.Dapper
A utility for using the query class generated by Carbunql with Dapper as it is. |
|
Carbunql.Postgres
Type-safe SelectQuery Builder using Expression.For Postgres. |
|
Carbunql.TypeSafe
This library allows for TypeSafe SQL building, enables independent definition and reuse of subqueries and CTEs, and supports unit testing without the need for tables. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
0.8.14 | 95 | 10/30/2024 | |
0.8.13.1 | 709 | 10/21/2024 | |
0.8.13 | 125 | 10/20/2024 | |
0.8.12 | 111 | 10/3/2024 | |
0.8.11 | 548 | 9/1/2024 | |
0.8.10 | 180 | 8/18/2024 | |
0.8.9.1 | 129 | 8/18/2024 | |
0.8.9 | 128 | 8/15/2024 | |
0.8.8 | 131 | 8/9/2024 | |
0.8.7 | 75 | 8/3/2024 | |
0.8.6 | 1,488 | 7/24/2024 | |
0.8.5 | 101 | 7/17/2024 | |
0.8.4.1 | 127 | 7/13/2024 | |
0.7.8.1 | 165 | 5/12/2024 | |
0.7.8 | 135 | 5/1/2024 | |
0.7.7 | 169 | 4/17/2024 | |
0.7.6 | 148 | 4/1/2024 | |
0.7.5 | 117 | 3/30/2024 | |
0.7.4 | 137 | 3/26/2024 | |
0.7.3 | 135 | 3/24/2024 | |
0.7.2 | 346 | 3/15/2024 | |
0.7.1 | 1,942 | 3/9/2024 | |
0.6.4 | 153 | 2/25/2024 | |
0.6.3 | 163 | 2/20/2024 | |
0.6.2 | 144 | 2/20/2024 | |
0.6.1.1 | 307 | 2/13/2024 | |
0.5.7 | 197 | 1/20/2024 | |
0.5.6 | 590 | 12/3/2023 | |
0.5.5 | 169 | 11/26/2023 | |
0.5.4 | 156 | 11/25/2023 | |
0.5.3 | 148 | 11/25/2023 | |
0.5.2 | 152 | 11/20/2023 | |
0.5.1 | 235 | 11/14/2023 | |
0.5.0 | 204 | 9/25/2023 | |
0.4.5 | 451 | 9/21/2023 | |
0.4.4 | 156 | 9/17/2023 | |
0.4.3 | 159 | 9/17/2023 | |
0.4.2 | 168 | 9/16/2023 | |
0.4.1 | 179 | 9/7/2023 | |
0.3.2 | 296 | 8/26/2023 | |
0.3.1 | 169 | 8/25/2023 | |
0.3.0 | 254 | 8/17/2023 | |
0.2.8 | 346 | 7/11/2023 | |
0.2.7 | 212 | 6/20/2023 | |
0.2.6 | 207 | 5/10/2023 | |
0.2.5 | 212 | 4/30/2023 | |
0.2.4 | 225 | 4/17/2023 | |
0.2.3 | 421 | 3/30/2023 | |
0.2.2 | 229 | 3/30/2023 | |
0.2.1 | 272 | 3/25/2023 | |
0.2.0 | 273 | 3/24/2023 | |
0.1.1 | 282 | 3/5/2023 | |
0.1.0 | 317 | 2/28/2023 |