Skip to content

Commit 120b164

Browse files
author
zzzprojects
committed
ZZZ Projects - Release Stable Version
ZZZ Projects - Release Stable Version
1 parent fb1630e commit 120b164

File tree

63 files changed

+2824
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2824
-0
lines changed

Diff for: src/Z.Expressions.SqlServer.Eval.sln

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "Z.Expressions.SqlServer.Eval", "Z.Expressions.SqlServer.Eval\Z.Expressions.SqlServer.Eval.sqlproj", "{A494B55E-94A3-4914-A03F-4D0432D16420}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{A494B55E-94A3-4914-A03F-4D0432D16420}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A494B55E-94A3-4914-A03F-4D0432D16420}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A494B55E-94A3-4914-A03F-4D0432D16420}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{A494B55E-94A3-4914-A03F-4D0432D16420}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{A494B55E-94A3-4914-A03F-4D0432D16420}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{A494B55E-94A3-4914-A03F-4D0432D16420}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Description: Evaluate C# code and expression in T-SQL stored procedure, function and trigger.
2+
// Website & Documentation: https://github.com/zzzprojects/Eval-SQL.NET
3+
// Forum: https://zzzprojects.uservoice.com/forums/328452-eval-sql-net
4+
// License: http://www.zzzprojects.com/license-agreement/
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright (c) 2015 ZZZ Projects. All rights reserved.
7+
8+
using System;
9+
using System.Collections;
10+
using System.Collections.Generic;
11+
using System.Linq.Expressions;
12+
using System.Reflection;
13+
using Z.Expressions.CodeAnalysis.CSharp;
14+
using Z.Expressions.CodeCompiler.CSharp;
15+
16+
namespace Z.Expressions
17+
{
18+
/// <summary>An eval expression compiler used to compile the code or expression.</summary>
19+
internal static partial class EvalCompiler
20+
{
21+
/// <summary>Property info for the IDictionary Item indexer used in ResolveParameter methods.</summary>
22+
private static readonly PropertyInfo DictionaryItemPropertyInfo = typeof (IDictionary).GetProperty("Item", new[] {typeof (string)});
23+
24+
/// <summary>Compile the code or expression and return a TDelegate of type Func or Action to execute.</summary>
25+
/// <param name="context">The eval context used to compile the code or expression.</param>
26+
/// <param name="code">The code or expression to compile.</param>
27+
/// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
28+
/// <param name="resultType">Type of the compiled code or expression result.</param>
29+
/// <returns>A TDelegate of type Func or Action that represents the compiled code or expression.</returns>
30+
internal static EvalDelegate Compile(EvalContext context, string code, IDictionary<string, Type> parameterTypes, Type resultType)
31+
{
32+
var cacheKey = ResolveCacheKey(context, typeof (Func<IDictionary, object>), code, parameterTypes);
33+
34+
EvalDelegate cachedDelegate;
35+
if (EvalManager.CacheDelegate.TryGetValue(cacheKey, out cachedDelegate))
36+
{
37+
return cachedDelegate;
38+
}
39+
40+
// Options
41+
var scope = new ExpressionScope
42+
{
43+
AliasExtensionMethods = context.AliasExtensionMethods,
44+
AliasNames = context.AliasNames,
45+
AliasStaticMembers = context.AliasStaticMembers,
46+
AliasTypes = context.AliasTypes,
47+
BindingFlags = context.BindingFlags,
48+
UseCaretForExponent = context.UseCaretForExponent
49+
};
50+
51+
// ADD global constants
52+
if (context.AliasGlobalConstants.Count > 0)
53+
{
54+
scope.Constants = new Dictionary<string, ConstantExpression>(context.AliasGlobalConstants);
55+
}
56+
57+
// ADD global variables
58+
if (context.AliasGlobalVariables.Count > 0)
59+
{
60+
foreach (var keyValue in context.AliasGlobalVariables)
61+
{
62+
scope.CreateLazyVariable(keyValue.Key, new Lazy<Expression>(() =>
63+
{
64+
var innerParameter = scope.CreateVariable(keyValue.Value.GetType(), keyValue.Key);
65+
var innerExpression = Expression.Assign(innerParameter, Expression.Constant(keyValue.Value));
66+
scope.Expressions.Add(innerExpression);
67+
return innerParameter;
68+
}));
69+
}
70+
}
71+
72+
// Resolve Parameter
73+
var parameterExpressions = ResolveParameter(scope, parameterTypes);
74+
75+
// CodeAnalysis
76+
var syntaxRoot = SyntaxParser.ParseText(code);
77+
78+
// CodeCompiler
79+
var expression = ExpressionParser.ParseSyntax(scope, syntaxRoot, resultType);
80+
81+
// Compile the expression
82+
var compiled = Expression.Lambda<Func<IDictionary, object>>(expression, parameterExpressions).Compile();
83+
84+
var evalDelegate = new EvalDelegate(cacheKey, compiled);
85+
EvalManager.CacheDelegate.TryAdd(cacheKey, evalDelegate);
86+
87+
return evalDelegate;
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Description: Evaluate C# code and expression in T-SQL stored procedure, function and trigger.
2+
// Website & Documentation: https://github.com/zzzprojects/Eval-SQL.NET
3+
// Forum: https://zzzprojects.uservoice.com/forums/328452-eval-sql-net
4+
// License: http://www.zzzprojects.com/license-agreement/
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright (c) 2015 ZZZ Projects. All rights reserved.
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace Z.Expressions
13+
{
14+
internal static partial class EvalCompiler
15+
{
16+
/// <summary>Resolve and return the cache key used in the EvalManager cache.</summary>
17+
/// <param name="context">The eval context used to compile the code or expression.</param>
18+
/// <param name="tdelegate">Type of the delegate (Func or Action) to use to compile the code or expression.</param>
19+
/// <param name="code">The code or expression to compile.</param>
20+
/// <param name="parameterTypes">List of parameter types used to compile the code or expression.</param>
21+
/// <returns>A string representing a unique key for the combinaison delegate/code/parameter types.</returns>
22+
private static string ResolveCacheKey(EvalContext context, Type tdelegate, string code, IDictionary<string, Type> parameterTypes)
23+
{
24+
// Concatenate:
25+
// - CacheKey Prefix
26+
// - Code or expression
27+
// - Delegate
28+
// - Parameter Types
29+
return string.Concat(context.CacheKeyPrefix,
30+
";",
31+
code,
32+
";",
33+
tdelegate.FullName,
34+
";",
35+
parameterTypes == null ? "" : string.Join(";", parameterTypes.Values.Select(x => x.FullName)));
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Description: Evaluate C# code and expression in T-SQL stored procedure, function and trigger.
2+
// Website & Documentation: https://github.com/zzzprojects/Eval-SQL.NET
3+
// Forum: https://zzzprojects.uservoice.com/forums/328452-eval-sql-net
4+
// License: http://www.zzzprojects.com/license-agreement/
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright (c) 2015 ZZZ Projects. All rights reserved.
7+
8+
using System;
9+
using System.Collections;
10+
using System.Collections.Generic;
11+
using System.Linq.Expressions;
12+
using Z.Expressions.CodeCompiler.CSharp;
13+
14+
namespace Z.Expressions
15+
{
16+
internal static partial class EvalCompiler
17+
{
18+
/// <summary>Resolve parameters used for the code or expression.</summary>
19+
/// <param name="scope">The expression scope for the code or expression to compile.</param>
20+
/// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
21+
/// <returns>A ParameterExpression list used in code or expression to compile.</returns>
22+
private static List<ParameterExpression> ResolveParameter(ExpressionScope scope, IDictionary<string, Type> parameterTypes)
23+
{
24+
var parameters = new List<ParameterExpression>();
25+
26+
var parameterDictionary = scope.CreateParameter(typeof (IDictionary));
27+
parameters.Add(parameterDictionary);
28+
29+
foreach (var parameter in parameterTypes)
30+
{
31+
scope.CreateLazyVariable(parameter.Key, new Lazy<Expression>(() =>
32+
{
33+
var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);
34+
35+
Expression innerExpression = Expression.Property(parameterDictionary, DictionaryItemPropertyInfo, Expression.Constant(parameter.Key));
36+
37+
innerExpression = innerExpression.Type != parameter.Value ?
38+
Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
39+
Expression.Assign(innerParameter, innerExpression);
40+
41+
scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));
42+
43+
return innerParameter;
44+
}));
45+
}
46+
47+
return parameters;
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Description: Evaluate C# code and expression in T-SQL stored procedure, function and trigger.
2+
// Website & Documentation: https://github.com/zzzprojects/Eval-SQL.NET
3+
// Forum: https://zzzprojects.uservoice.com/forums/328452-eval-sql-net
4+
// License: http://www.zzzprojects.com/license-agreement/
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright (c) 2015 ZZZ Projects. All rights reserved.
7+
8+
namespace Z.Expressions
9+
{
10+
/// <summary>Values that represent the ParameterKind for the EvalCompiler.</summary>
11+
public enum EvalCompilerParameterKind
12+
{
13+
None,
14+
Dictionary,
15+
Enumerable,
16+
SingleDictionary,
17+
SingleObject,
18+
Typed,
19+
Untyped
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Description: Evaluate C# code and expression in T-SQL stored procedure, function and trigger.
2+
// Website & Documentation: https://github.com/zzzprojects/Eval-SQL.NET
3+
// Forum: https://zzzprojects.uservoice.com/forums/328452-eval-sql-net
4+
// License: http://www.zzzprojects.com/license-agreement/
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright (c) 2015 ZZZ Projects. All rights reserved.
7+
8+
using System;
9+
using System.Collections.Generic;
10+
11+
namespace Z.Expressions
12+
{
13+
public partial class EvalContext
14+
{
15+
/// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
16+
/// <param name="code">The code or expression to compile.</param>
17+
/// <param name="parameterTypes">Parameter types used to compile the code or expression.</param>
18+
/// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
19+
public EvalDelegate Compile(string code, IDictionary<string, Type> parameterTypes)
20+
{
21+
return EvalCompiler.Compile(this, code, parameterTypes, typeof (object));
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)