-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDbId.cs
More file actions
68 lines (56 loc) · 2.44 KB
/
Copy pathDbId.cs
File metadata and controls
68 lines (56 loc) · 2.44 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
using TeamTools.TSQL.ExpressionEvaluator.BuiltInFunctions.ArgumentDto;
using TeamTools.TSQL.ExpressionEvaluator.BuiltInFunctions.ArgumentValidators;
using TeamTools.TSQL.ExpressionEvaluator.Routines;
using TeamTools.TSQL.ExpressionEvaluator.TypeHandling;
using TeamTools.TSQL.ExpressionEvaluator.Values;
namespace TeamTools.TSQL.ExpressionEvaluator.BuiltInFunctions.SysFunctions
{
public class DbId : SqlGenericFunctionHandler<DbId.DbIdArgs>
{
private static readonly string FuncName = "DB_ID";
private static readonly string ResultTypeName = TSqlDomainAttributes.Types.Int;
private static readonly int MinArgCount = 0;
private static readonly int MaxArgCount = 1;
private static readonly SqlIntValueRange DbIdRange = new SqlIntValueRange(0, int.MaxValue);
public DbId() : base(FuncName, MinArgCount, MaxArgCount)
{
}
public override bool ValidateArgumentValues(CallSignature<DbIdArgs> call)
{
if (call.RawArgs.Count > 0)
{
ValidationScenario
.For("DATABASE_NAME", call.RawArgs[0], call.Context)
.When(ArgumentIsValue.Validate)
.And(ArgumentIsValidStr.Validate)
.Then(s => call.ValidatedArgs.DatabaseName = s);
}
return true;
}
protected override string DoEvaluateResultType(CallSignature<DbIdArgs> call) => ResultTypeName;
protected override SqlValue DoEvaluateResultValue(CallSignature<DbIdArgs> call)
{
var value = call.Context.Converter
.ImplicitlyConvert<SqlIntTypeValue>(base.DoEvaluateResultValue(call))?
.ChangeTo(DbIdRange, call.Context.NewSource);
if (value is null)
{
return default;
}
if (call.RawArgs.Count == 0)
{
return new CurrentDatabaseId(value.TypeHandler, call.Context.NewSource);
}
if (call.ValidatedArgs.DatabaseName != null && call.ValidatedArgs.DatabaseName.IsNull)
{
call.Context.RedundantCall("DB name is NULL");
return value.TypeHandler.IntValueFactory.NewNull(call.Context.Node);
}
return value.ChangeTo(DbIdRange, value.Source);
}
public class DbIdArgs
{
public SqlStrTypeValue DatabaseName { get; set; }
}
}
}