Skip to content

Commit 417f86e

Browse files
Implement column-as keyword for 'select' with SelectAs().
1 parent e772e29 commit 417f86e

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
lines changed

QueryBuilder.Tests/SelectTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,45 @@ public void BasicSelectEnumerable()
3535
Assert.Equal("SELECT \"id\", \"name\" FROM \"users\"", c[EngineCodes.Oracle]);
3636
}
3737

38+
[Fact]
39+
public void SelectAsOneColumn()
40+
{
41+
var query = new Query().SelectAs("Row", "Alias").From("Table");
42+
43+
var c = Compile(query);
44+
Assert.Equal("SELECT [Row] AS [Alias] FROM [Table]", c[EngineCodes.SqlServer]);
45+
Assert.Equal("SELECT `Row` AS `Alias` FROM `Table`", c[EngineCodes.MySql]);
46+
Assert.Equal("SELECT \"Row\" AS \"Alias\" FROM \"Table\"", c[EngineCodes.PostgreSql]);
47+
Assert.Equal("SELECT \"ROW\" AS \"ALIAS\" FROM \"TABLE\"", c[EngineCodes.Firebird]);
48+
Assert.Equal("SELECT \"Row\" \"Alias\" FROM \"Table\"", c[EngineCodes.Oracle]);
49+
}
50+
51+
[Fact]
52+
public void SelectAsSingletonList()
53+
{
54+
var query = new Query().SelectAs(("Row", "Alias")).From("Table");
55+
56+
var c = Compile(query);
57+
Assert.Equal("SELECT [Row] AS [Alias] FROM [Table]", c[EngineCodes.SqlServer]);
58+
Assert.Equal("SELECT `Row` AS `Alias` FROM `Table`", c[EngineCodes.MySql]);
59+
Assert.Equal("SELECT \"Row\" AS \"Alias\" FROM \"Table\"", c[EngineCodes.PostgreSql]);
60+
Assert.Equal("SELECT \"ROW\" AS \"ALIAS\" FROM \"TABLE\"", c[EngineCodes.Firebird]);
61+
Assert.Equal("SELECT \"Row\" \"Alias\" FROM \"Table\"", c[EngineCodes.Oracle]);
62+
}
63+
64+
[Fact]
65+
public void SelectAsMultipleColumns()
66+
{
67+
var query = new Query().SelectAs(("Row1", "Alias1"), ("Row2", "Alias2")).From("Table");
68+
69+
var c = Compile(query);
70+
Assert.Equal("SELECT [Row1] AS [Alias1], [Row2] AS [Alias2] FROM [Table]", c[EngineCodes.SqlServer]);
71+
Assert.Equal("SELECT `Row1` AS `Alias1`, `Row2` AS `Alias2` FROM `Table`", c[EngineCodes.MySql]);
72+
Assert.Equal("SELECT \"Row1\" AS \"Alias1\", \"Row2\" AS \"Alias2\" FROM \"Table\"", c[EngineCodes.PostgreSql]);
73+
Assert.Equal("SELECT \"ROW1\" AS \"ALIAS1\", \"ROW2\" AS \"ALIAS2\" FROM \"TABLE\"", c[EngineCodes.Firebird]);
74+
Assert.Equal("SELECT \"Row1\" \"Alias1\", \"Row2\" \"Alias2\" FROM \"Table\"", c[EngineCodes.Oracle]);
75+
}
76+
3877
[Fact]
3978
public void BasicSelectWhereBindingIsEmptyOrNull()
4079
{
@@ -74,6 +113,21 @@ public void ExpandedSelect()
74113
Assert.Equal("SELECT `users`.`id`, `users`.`name`, `users`.`age` FROM `users`", c[EngineCodes.MySql]);
75114
}
76115

116+
[Fact]
117+
public void ExpandedSelectAs()
118+
{
119+
var q = new Query().From("users").SelectAs(("users.{id,name, age}", "Alias"));
120+
var c = Compile(q);
121+
122+
// This result is weird (but valid syntax), and at least it works in
123+
// a somewhat explainable way, as opposed to regular Select() when
124+
// combining the expanded syntax and the 'as' SQLKata keyword support
125+
// which simply silently stops working when the {...} expansion is
126+
// applied.
127+
Assert.Equal("SELECT [users].[id] AS [Alias], [users].[name] AS [Alias], [users].[age] AS [Alias] FROM [users]", c[EngineCodes.SqlServer]);
128+
Assert.Equal("SELECT `users`.`id` AS `Alias`, `users`.`name` AS `Alias`, `users`.`age` AS `Alias` FROM `users`", c[EngineCodes.MySql]);
129+
}
130+
77131
[Fact]
78132
public void ExpandedSelectWithSchema()
79133
{

QueryBuilder/Clauses/ColumnClause.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
using System.Diagnostics;
2+
13
namespace SqlKata
24
{
35
public abstract class AbstractColumn : AbstractClause
46
{
7+
public string Alias { get; set; }
58
}
69

710
/// <summary>
@@ -26,6 +29,7 @@ public override AbstractClause Clone()
2629
Engine = Engine,
2730
Name = Name,
2831
Component = Component,
32+
Alias = Alias,
2933
};
3034
}
3135
}
@@ -50,6 +54,7 @@ public override AbstractClause Clone()
5054
Engine = Engine,
5155
Query = Query.Clone(),
5256
Component = Component,
57+
Alias = Alias,
5358
};
5459
}
5560
}
@@ -68,6 +73,7 @@ public class RawColumn : AbstractColumn
6873
/// <inheritdoc />
6974
public override AbstractClause Clone()
7075
{
76+
Debug.Assert(string.IsNullOrEmpty(Alias), "Raw columns cannot have an alias");
7177
return new RawColumn
7278
{
7379
Engine = Engine,

QueryBuilder/Compilers/Compiler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ public virtual string CompileColumn(SqlResult ctx, AbstractColumn column)
472472
return "(" + subCtx.RawSql + $"){alias}";
473473
}
474474

475+
if (!string.IsNullOrWhiteSpace(column.Alias))
476+
{
477+
return $"{Wrap((column as Column).Name)} {ColumnAsKeyword}{Wrap(column.Alias)}";
478+
479+
}
480+
475481
return Wrap((column as Column).Name);
476482

477483
}

QueryBuilder/Query.Select.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,42 @@ namespace SqlKata
66
{
77
public partial class Query
88
{
9+
public Query Select(params string[] columns) =>
10+
Select(columns.AsEnumerable());
911

10-
public Query Select(params string[] columns)
11-
{
12-
return Select(columns.AsEnumerable());
13-
}
12+
public Query Select(IEnumerable<string> columns) =>
13+
SelectAs(
14+
columns
15+
.Select(x => (x, null as string))
16+
.ToArray()
17+
);
18+
19+
/// <summary>
20+
/// Select a column with an alias
21+
/// </summary>
22+
/// <returns></returns>
23+
public Query SelectAs(string column, string alias) =>
24+
SelectAs((column, alias));
1425

15-
public Query Select(IEnumerable<string> columns)
26+
/// <summary>
27+
/// Select columns with an alias
28+
/// </summary>
29+
/// <returns></returns>
30+
public Query SelectAs(params (string, string)[] columns)
1631
{
1732
Method = "select";
1833

1934
columns = columns
20-
.Select(x => Helper.ExpandExpression(x))
35+
.Select(x => Helper.ExpandExpression(x.Item1).Select(y => (y, x.Item2)))
2136
.SelectMany(x => x)
2237
.ToArray();
2338

24-
2539
foreach (var column in columns)
2640
{
2741
AddComponent("select", new Column
2842
{
29-
Name = column
43+
Name = column.Item1,
44+
Alias = column.Item2
3045
});
3146
}
3247

0 commit comments

Comments
 (0)