-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
53 lines (44 loc) · 1.83 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Carbunql;
using Carbunql.Fluent;
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Enter minimum price (or leave blank to omit):");
string? minPriceInput = Console.ReadLine();
decimal? minPrice = string.IsNullOrEmpty(minPriceInput) ? null : Convert.ToDecimal(minPriceInput);
Console.WriteLine("Enter maximum price (or leave blank to omit):");
string? maxPriceInput = Console.ReadLine();
decimal? maxPrice = string.IsNullOrEmpty(maxPriceInput) ? null : Convert.ToDecimal(maxPriceInput);
Console.WriteLine("Enter category (or leave blank to omit):");
string? category = Console.ReadLine();
Console.WriteLine("Enter in-stock status (true/false) (or leave blank to omit):");
string? inStockInput = Console.ReadLine();
bool? inStock = string.IsNullOrEmpty(inStockInput) ? null : Convert.ToBoolean(inStockInput);
var query = GenerateProductQuery(minPrice, maxPrice, category, inStock);
Console.WriteLine("Generated SQL Query:");
Console.WriteLine(query);
}
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)
.Parameter(pname, category)
.EqualIfNotNullOrEmpty("category", pname)
.EqualIfNotNullOrEmpty("in_stock", inStock);
return sq.ToText();
}
}