-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathQueryOrderConfig.cs
More file actions
103 lines (88 loc) · 4.35 KB
/
Copy pathQueryOrderConfig.cs
File metadata and controls
103 lines (88 loc) · 4.35 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using OnRamp;
using OnRamp.Config;
using System;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Beef.CodeGen.Config.Database
{
/// <summary>
/// Represents the stored procedure order-by configuration.
/// </summary>
[CodeGenClass("QueryOrder", Title = "'QueryOrder' object (database-driven)",
Description = "The `QueryOrder` object that defines the query order.")]
[CodeGenCategory("Key", Title = "Provides the _key_ configuration.")]
public class QueryOrderConfig : ConfigBase<CodeGenConfig, QueryConfig>
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <remarks><inheritdoc/></remarks>
public override string? QualifiedKeyName => BuildQualifiedKeyName("QueryOrder", Name);
#region Key
/// <summary>
/// Gets or sets the name of the column to order by.
/// </summary>
[JsonPropertyName("name")]
[CodeGenProperty("Key", Title = "The name of the `Column` to order by.", IsMandatory = true, IsImportant = true,
Description = "See also `Schema` and `Table` as these all relate.")]
public string? Name { get; set; }
/// <summary>
/// Gets or sets the name of the order by table schema.
/// </summary>
[JsonPropertyName("schema")]
[CodeGenProperty("Key", Title = "The name of order by table schema. See also `Name` and `Column` as these all relate.",
Description = "Defaults to `Query.Schema`.")]
public string? Schema { get; set; }
/// <summary>
/// Gets or sets the name of the order by table.
/// </summary>
[JsonPropertyName("table")]
[CodeGenProperty("Key", Title = "The name of the order by table.",
Description = "Defaults to `Table.Name`; i.e. primary table. See also `Schema` and `Column` as these all relate.")]
public string? Table { get; set; }
/// <summary>
/// Gets or sets the sort order option.
/// </summary>
[JsonPropertyName("order")]
[CodeGenProperty("Key", Title = "The corresponding sort order.", IsImportant = true, Options = ["Ascending", "Descending"],
Description = "Defaults to `Ascending`.")]
public string? Order { get; set; }
#endregion
/// <summary>
/// Gets the order by SQL.
/// </summary>
public string? OrderBySql { get; set; }
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override Task PrepareAsync()
{
if (Name != null && Name.StartsWith("@", StringComparison.OrdinalIgnoreCase))
Name = Name[1..];
Schema = DefaultWhereNull(Schema, () => Parent!.Schema);
Table = DefaultWhereNull(Table, () => Parent!.Name);
Order = DefaultWhereNull(Order, () => "Ascending");
var c = Root!.DbTables!.Where(x => x.Schema == Schema && x.Name == Table).SingleOrDefault()?.Columns.Where(x => x.Name == Name).SingleOrDefault();
if (c == null)
throw new CodeGenException(this, nameof(Name), $"OrderBy '{Name}' (Schema.Table '{Schema}.{Table}') not found in database.");
if (Schema == Parent!.Schema && Table == Parent!.Name)
{
if (Parent!.DbTable!.Columns.Where(x => x.Name == Name).SingleOrDefault() == null)
throw new CodeGenException(this, nameof(Name), $"OrderBy '{Name}' (Schema.Table '{Schema}.{Table}') not found in Table/Join configuration.");
OrderBySql = $"[{Parent!.Alias}].[{Name}]";
}
else
{
var t = Parent!.Joins!.Where(x => Schema == x.Schema && Table == x.Name).SingleOrDefault();
if (t != null && t.DbTable!.Columns.Where(x => x.Name == Name).SingleOrDefault() != null)
OrderBySql = $"[{t.Alias}].[{Name}]";
else
throw new CodeGenException(this, nameof(Name), $"OrderBy '{Name}' (Schema.Table '{Schema}.{Table}') not found in Table/Join configuration.");
}
OrderBySql += $" {(Order!.StartsWith("Des", StringComparison.OrdinalIgnoreCase) ? "DESC" : "ASC")}";
return Task.CompletedTask;
}
}
}