-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpression.cs
More file actions
85 lines (72 loc) · 2.59 KB
/
Copy pathExpression.cs
File metadata and controls
85 lines (72 loc) · 2.59 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using FizzCode.DbTools.Common;
using FizzCode.DbTools.DataDefinition.Base;
namespace FizzCode.DbTools.QueryBuilder;
public class Expression(params object[] expressionParts)
: IEnumerable<object>
{
public List<object> Values { get; } = expressionParts.ToList();
/*public static implicit operator Expression(object[] expressionParts)
{
var expression = new Expression(expressionParts);
return expression;
}*/
public IEnumerator<object> GetEnumerator()
{
return Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Values.GetEnumerator();
}
public static string GetExpression(IEnumerable<object> expressionParts, IEnumerable<QueryElement> queryElements, QueryElement? mainQueryElement = null)
{
var sb = new StringBuilder();
string? previous = null;
foreach (var obj in expressionParts)
{
if (obj is Expression expression)
{
sb.AppendSpace(GetExpression(expression.Values, queryElements, mainQueryElement));
}
else if (obj is SqlColumn sqlColumn)
{
if (previous?.EndsWith('.') != true)
{
var table = sqlColumn.Table;
var alias = "";
alias = table.GetAlias();
alias ??= mainQueryElement?.Table.SchemaAndTableName == table.SchemaAndTableName
? mainQueryElement?.Table.GetAlias()
: null;
alias ??= queryElements.Single(qe => qe.Table.SchemaAndTableName == table.SchemaAndTableName).Table.GetAlias();
Throw.InvalidOperationExceptionIfNull(alias);
sb.AppendSpace(alias);
sb.Append('.');
}
sb.Append(((QueryColumn)sqlColumn).Value);
previous = null;
}
else if (obj is string @string)
{
sb.AppendSpace(@string);
previous = @string;
}
else if (obj is int @int)
{
sb.AppendSpace(@int.ToString(CultureInfo.InvariantCulture));
// previous = @int;
}
else
{
throw new ArgumentException($"Expression part type is not handled. Type: {obj.GetType()}, Value: {obj}.");
}
}
return sb.ToString();
}
}