-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SqliteExpression.cs
66 lines (60 loc) · 3.16 KB
/
SqliteExpression.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static class SqliteExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static SqlFunctionExpression Strftime(
ISqlExpressionFactory sqlExpressionFactory,
Type returnType,
string format,
SqlExpression timestring,
IEnumerable<SqlExpression>? modifiers = null,
RelationalTypeMapping? typeMapping = null)
{
modifiers ??= Enumerable.Empty<SqlExpression>();
// If the inner call is another strftime then shortcut a double call
if (timestring is SqlFunctionExpression rtrimFunction
&& rtrimFunction.Name == "rtrim"
&& rtrimFunction.Arguments!.Count == 2
&& rtrimFunction.Arguments[0] is SqlFunctionExpression rtrimFunction2
&& rtrimFunction2.Name == "rtrim"
&& rtrimFunction2.Arguments!.Count == 2
&& rtrimFunction2.Arguments[0] is SqlFunctionExpression strftimeFunction
&& strftimeFunction.Name == "strftime"
&& strftimeFunction.Arguments!.Count > 1)
{
// Use its timestring parameter directly in place of ours
timestring = strftimeFunction.Arguments[1];
// Prepend its modifier arguments (if any) to the current call
modifiers = strftimeFunction.Arguments.Skip(2).Concat(modifiers);
}
var finalArguments = new[] { sqlExpressionFactory.Constant(format), timestring }.Concat(modifiers);
return sqlExpressionFactory.Function(
"strftime",
finalArguments,
nullable: true,
argumentsPropagateNullability: finalArguments.Select(a => true),
returnType,
typeMapping);
}
}
}