From e45745df195c2519a67da68bf511776a0b0e4475 Mon Sep 17 00:00:00 2001 From: mk3008 Date: Thu, 18 Jul 2024 21:24:35 +0900 Subject: [PATCH] Creating a Subquery Demo Page --- demo/CarbunqlWeb/Pages/Format.razor | 70 +++++------- demo/CarbunqlWeb/Pages/SubQuery.razor | 158 +++++++++++++------------- 2 files changed, 108 insertions(+), 120 deletions(-) diff --git a/demo/CarbunqlWeb/Pages/Format.razor b/demo/CarbunqlWeb/Pages/Format.razor index c8ba2c38..d3cd3489 100644 --- a/demo/CarbunqlWeb/Pages/Format.razor +++ b/demo/CarbunqlWeb/Pages/Format.razor @@ -6,53 +6,41 @@ Format -
-@*
- - - - -
*@ -
-
- Format +Format - SQL can be formatted.(select, values, insert, create table, create index, alter table) - - - Source code - - -
-
-
-
- Demo -
-
- - Input - - -
-
- - Result - - -
+SQL can be formatted.(select, values, insert, create table, create index, alter table) -
-
+ + + +
+
+ + Input + + +
+
+ + Result + + +
+
-@code { +
+ + using Carbunql; - string fsql = string.Empty; +var sql = QueryCommandableParser.Parse("select id, val from table_a as a") + .ToText(); + +
+
- string sourcecode = @"using Carbunql; +@code { -var q = QueryCommandableParser.Parse(sql_text); -Console.WriteLine(q.ToText());"; + string fsql = string.Empty; protected override void OnInitialized() { diff --git a/demo/CarbunqlWeb/Pages/SubQuery.razor b/demo/CarbunqlWeb/Pages/SubQuery.razor index 90d9325f..0c92cb78 100644 --- a/demo/CarbunqlWeb/Pages/SubQuery.razor +++ b/demo/CarbunqlWeb/Pages/SubQuery.razor @@ -1,88 +1,92 @@ @page "/subquery" @using Carbunql; -@using Carbunql.Building; To SubQuery -
-@*
- - - - -
*@ -
-
- To SubQuery - - A select query can be converted to a sub query. - - - Source code - - -
-
-
-
- Demo -
- -
- - Alias - - -
-
-
-
- - Input - - -
-
- - Result - - -
-
-
+To SubQuery + +A select query can be converted to a sub query. + + + + +
+
+ + Alias + + +
+
+
+
+ + Input + + +
+
+ + Result + + +
+
+ +
+ + using Carbunql; + +var sql = new SelectQuery("select id, val from table_a as a") + .ToSubQuery(alias) + .ToText(); + +
+
@code { string sql = @"WITH - x AS ( - SELECT - t.id, - t.col1 AS value - FROM - table AS t - ) +monthly_sales AS ( + SELECT + store_id, + product_id, + DATE_TRUNC('month', sales_date) AS month, + SUM(sales_amount) AS total_sales + FROM + sales + GROUP BY + store_id, + product_id, + DATE_TRUNC('month', sales_date) +), +total_monthly_sales AS ( + SELECT + store_id, + month, + SUM(total_sales) AS total_sales + FROM + monthly_sales + GROUP BY + store_id, + month +) SELECT - x.id, - x.value as val + ms.store_id, + ms.product_id, + ms.month, + ms.total_sales, + tms.total_sales AS total_monthly_sales, + (ms.total_sales::FLOAT / tms.total_sales) * 100 AS sales_percentage FROM - x"; + monthly_sales ms +INNER JOIN + total_monthly_sales tms ON ms.store_id = tms.store_id AND ms.month = tms.month +ORDER BY + ms.month, + ms.product_id"; string fsql = string.Empty; - string sourcecode = @"using Carbunql; -using Carbunql.Building; - -var subsq = new SelectQuery(""select id, val from table_a""); - -var sq = new SelectQuery(); -//Convert select query to Common table -//Return value is FromClause class and SelectableTable class -var (_, t) = sq.From(subsq).As(""alias""); - -//Select all columns of the sub query -sq.Select(t); - -Console.WriteLine(q.ToCommand().CommandText);"; - string alias = "q"; protected override void OnInitialized() @@ -105,13 +109,9 @@ Console.WriteLine(q.ToCommand().CommandText);"; } try { - var sq = new SelectQuery(); - var (f, q) = sq.From(new SelectQuery(sql)).As("q"); - - sq.Select(q); - - - fsql = sq.ToCommand().CommandText; + fsql = new SelectQuery(sql) + .ToSubQuery(alias) + .ToText(); } catch (Exception ex) {