/
Sql.TableFunctionAttribute.cs
80 lines (66 loc) · 2.51 KB
/
Sql.TableFunctionAttribute.cs
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
using System;
using System.Linq.Expressions;
using LinqToDB.Mapping;
// ReSharper disable CheckNamespace
namespace LinqToDB
{
using Common.Internal;
using SqlProvider;
using SqlQuery;
partial class Sql
{
[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class TableFunctionAttribute : MappingAttribute
{
public TableFunctionAttribute()
{
}
public TableFunctionAttribute(string name)
{
Name = name;
}
public TableFunctionAttribute(string name, params int[] argIndices)
{
Name = name;
ArgIndices = argIndices;
}
public TableFunctionAttribute(string configuration, string name)
{
Configuration = configuration;
Name = name;
}
public TableFunctionAttribute(string configuration, string name, params int[] argIndices)
{
Configuration = configuration;
Name = name;
ArgIndices = argIndices;
}
public string? Name { get; set; }
public string? Schema { get; set; }
public string? Database { get; set; }
public string? Server { get; set; }
public string? Package { get; set; }
public int[]? ArgIndices { get; set; }
public virtual void SetTable<TContext>(DataOptions options, TContext context, ISqlBuilder sqlBuilder, MappingSchema mappingSchema, SqlTable table, MethodCallExpression methodCall, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression> converter)
{
table.SqlTableType = SqlTableType.Function;
var expressionStr = table.Expression = Name ?? methodCall.Method.Name!;
ExpressionAttribute.PrepareParameterValues(context, mappingSchema, methodCall, ref expressionStr, false, out var knownExpressions, false, out var genericTypes, converter);
if (string.IsNullOrEmpty(expressionStr))
throw new LinqToDBException($"Cannot retrieve Table Function body from expression '{methodCall}'.");
table.TableName = new SqlObjectName(
expressionStr!,
Schema : Schema ?? table.TableName.Schema,
Database: Database ?? table.TableName.Database,
Server : Server ?? table.TableName.Server,
Package : Package ?? table.TableName.Package);
table.TableArguments = ExpressionAttribute.PrepareArguments(context, string.Empty, ArgIndices, true, knownExpressions, genericTypes, converter);
}
public override string GetObjectID()
{
return $".{Configuration}.{Name}.{Schema}.{Database}.{Server}.{Package}.{IdentifierBuilder.GetObjectID(ArgIndices)}.";
}
}
}
}