-
Notifications
You must be signed in to change notification settings - Fork 4
SelectClause
Select columns and constants can be set in the Select clause. It is also possible to give it a different name.
There are multiple ways to specify the Select clause, but the most frequent pattern is column selection.
In addition to column selection, it supports constants, calculation formulas, and inline queries.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("table_a").As("a");
sq.Select(a, "a_id");
sq.Select(a, "value").As("val");
sq.Select(10).As("val_10");
Console.WriteLine(sq.ToText());
}
}
If you want to select a column, set the From clause and get the SelectableTable in advance.
Then use the Select method to select the table and column names. You can specify an alias name by using the chain method As.
Alias name is optional.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("table_a").As("a");
sq.Select(a, "a_id");
sq.Select(a, "value").As("val");
Console.WriteLine(sq.ToText());
}
}
SELECT
a.a_id,
a.value AS val
FROM
table_a AS a
NOTE: The above description is an abbreviation. To be verbose, you need to use the ColumnValue class.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("tabla_a").As("a");
sq.Select(new ColumnValue(a, "a_id")).As("a_id");
sq.Select(new ColumnValue(a, "value")).As("val");
Console.WriteLine(sq.ToText());
}
}
Use the SelectAll method to select all columns. Please specify the SelectableTable class as the first argument.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("tabla_a").As("a");
sq.SelectAll(a);
Console.WriteLine(sq.ToText());
}
}
SELECT
a.*
FROM
tabla_a AS a
Used without arguments to create a wildcard selection query. This method is not recommended because column information will be missing, making it difficult to reuse the same instance.
SelectableTable is not used when selecting constants.
Write the value you want to select in the Select method as is, and also specify an alias name using method chaining.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
sq.Select(1).As("v1");
Console.WriteLine(sq.ToText());
}
}
SELECT
1 AS v1
NOTE: The above description is an abbreviation. To be verbose, you need to use the LiteralValue class.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
sq.Select(new LiteralValue("1")).As("v1");
Console.WriteLine(sq.ToText());
}
}
You need to register parameters using the AddParameter method in advance. The return is the parameter name and the type is string. Please note that Carbunql does not manage placeholder identifiers, so you need to specify an identifier that matches the execution environment.
You can then select by passing the parameter name to the Select method.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var prm = sq.AddParameter(":prm", 1);
sq.Select(prm).As("val");
Console.WriteLine(sq.ToText());
}
}
/*
:prm = 1
*/
SELECT
:prm AS val
Select queries can be used as inline queries by using the ToValue method.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var x = new SelectQuery("select x.value from table_x");
var sq = new SelectQuery();
sq.Select(x.ToValue()).As("val");
Console.WriteLine(sq.ToText());
}
}
SELECT
(
SELECT
x.value
FROM
table_x
) AS val
You can add operators and operands using the AddOperatableValue method of the ValueBase class. Note that added expressions are always added at the end.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("tabla_a").As("a");
sq.Select(() => {
var v = new LiteralValue("1");
v.AddOperatableValue("+", new ColumnValue(f, "value1"));
v.AddOperatableValue("-", new ColumnValue(f, "value2"));
return v;
}).As("val");
Console.WriteLine(sq.ToText());
}
}
SELECT
1 + a.value1 - a.value2 AS val
FROM
tabla_a AS a
If you want to use parentheses, use the ToGroup method.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("tabla_a").As("a");
sq.Select(() => {
var v1 = new LiteralValue("1");
var v2 = new ColumnValue(f, "value1");
v2.AddOperatableValue("-", new ColumnValue(f, "value2"));
v1.AddOperatableValue("*", v2.ToGroup());
return v1;
}).As("val");
Console.WriteLine(sq.ToText());
}
}
SELECT
1 * (a.value1 - a.value2) AS val
FROM
tabla_a AS a
Express it using the FunctionValue class.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
sq.Select(new FunctionValue("now")).As("dt");
Console.WriteLine(sq.ToText());
}
}
SELECT
NOW() AS dt
Specify the arguments as follows.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
sq.Select(new FunctionValue("concat", new ValueCollection { "'a'", "'b'", "'c'" })).As("txt");
Console.WriteLine(sq.ToText());
}
}
SELECT
CONCAT('a', 'b', 'c') AS txt
It can be defined using the OverClause class.
using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Values;
using Carbunql.Clauses;
public class Program
{
public static void Main()
{
var sq = new SelectQuery();
var (f, a) = sq.From("table_a").As("a");
sq.Select(new FunctionValue("row_number",
() =>
{
var ov = new OverClause();
ov.AddPartition(new ColumnValue(a, "name"));
ov.AddOrder(new ColumnValue(a, "a_id").ToSortable());
return ov;
}
)).As("row_num");
Console.WriteLine(sq.ToText());
}
}
SELECT
ROW_NUMBER() OVER(
PARTITION BY
a.name
ORDER BY
a.a_id
) AS row_num
FROM
table_a AS a
It is possible to write, but it is on hold because it is very redundant and the specifications are likely to change.
SelectableTable holding column information like a subquery If you specify this in the Select method, all columns will be selected. At this time, the "*" keyword is not used, and each column is expanded.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var x = new SelectQuery("select a.id, a.value from table_a as a");
var sq = new SelectQuery();
var (f, a) = sq.From(x).As("a");
sq.Select(a);
Console.WriteLine(sq.ToText());
}
}
SELECT
a.id,
a.value
FROM
(
SELECT
a.id,
a.value
FROM
table_a AS a
) AS a
When subqueries are joined to tables, duplicate column names may occur. In this case, the one written first takes precedence by default.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var tableA = new SelectQuery("select a.a_id, a.value from table_a as a");
var tableB = new SelectQuery("select b.a_id, b.value, b.remarks from table_b as b");
var sq = new SelectQuery();
var (f, a) = sq.From(tableA).As("a");
var b = f.InnerJoin(tableB).As("b").On(a, "a_id");
sq.Select(a);
sq.Select(b);
Console.WriteLine(sq.ToText());
}
}
SELECT
a.a_id,
a.value,
b.remarks
FROM
(
SELECT
a.a_id,
a.value
FROM
table_a AS a
) AS a
INNER JOIN (
SELECT
b.a_id,
b.value,
b.remarks
FROM
table_b AS b
) AS b ON a.a_id = b.a_id
You can obtain a list of selected column names using the GetColumnName method. If an alias name is specified for the column, the alias name is retrieved.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var tableA = new SelectQuery("select a.a_id, a.value from table_a as a");
var tableB = new SelectQuery("select b.a_id, b.value, b.remarks from table_b as b");
var sq = new SelectQuery();
var (f, a) = sq.From(tableA).As("a");
var b = f.InnerJoin(tableB).As("b").On(a, "a_id");
sq.Select(a);
sq.Select(b);
foreach (var x in sq.GetColumnNames())
{
Console.WriteLine(x);
}
}
}
a_id
value
remarks
If you want to get the selected items rather than just the column name, use GetSelectableItems.
using System;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var tableA = new SelectQuery("select a.a_id, a.value from table_a as a");
var tableB = new SelectQuery("select b.a_id, b.value, b.remarks from table_b as b");
var sq = new SelectQuery();
var (f, a) = sq.From(tableA).As("a");
var b = f.InnerJoin(tableB).As("b").On(a, "a_id");
sq.Select(a);
sq.Select(b);
foreach (var x in sq.GetSelectableItems())
{
Console.WriteLine(x.ToText());
}
}
}
a.a_id
a.value
b.remarks
SelectableItem is stored in SelectClause. You can remove it by specifying the Remove method.
using System;
using System.Linq;
using Carbunql;
using Carbunql.Building;
public class Program
{
public static void Main()
{
var tableA = new SelectQuery("select a.a_id, a.value from table_a as a");
var tableB = new SelectQuery("select b.a_id, b.value, b.remarks from table_b as b");
var sq = new SelectQuery();
var (f, a) = sq.From(tableA).As("a");
var b = f.InnerJoin(tableB).As("b").On(a, "a_id");
sq.Select(a);
sq.Select(b);
var item = sq.GetSelectableItems().Where(x => x.Alias == "a_id").First();
sq.SelectClause!.Remove(item);
Console.WriteLine(sq.ToText());
}
}
SELECT
a.value,
b.remarks
FROM
(
SELECT
a.a_id,
a.value
FROM
table_a AS a
) AS a
INNER JOIN (
SELECT
b.a_id,
b.value,
b.remarks
FROM
table_b AS b
) AS b ON a.a_id = b.a_id