Skip to content

Commit 04bb43f

Browse files
author
zzzprojects
committed
Improving Performance + ValueNullable + EvalReadAccess
Improving Performance + ValueNullable + EvalReadAccess
1 parent 53794ff commit 04bb43f

Some content is hidden

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

61 files changed

+1286
-120
lines changed

src/Z.Expressions.SqlServer.Eval/Helper/DataTableHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Data;
1212
using System.Runtime.InteropServices;
1313
using System.Text.RegularExpressions;
14+
using Z.Expressions.CodeCompiler;
1415

1516
namespace Z.Expressions.SqlServer.Eval
1617
{
@@ -50,6 +51,33 @@ public static DataTable GetDataTable(object value)
5051
dt.ImportRow(dr);
5152
}
5253
}
54+
else if (value is IEnumerable<FakeAnonymousType>)
55+
{
56+
var list = (IEnumerable<FakeAnonymousType>)value;
57+
58+
bool isFirst = true;
59+
60+
dt = new DataTable();
61+
62+
foreach (var item in list)
63+
{
64+
if (isFirst)
65+
{
66+
foreach (var property in item.Properties)
67+
{
68+
dt.Columns.Add(property.Key);
69+
}
70+
isFirst = false;
71+
}
72+
73+
var row = dt.NewRow();
74+
dt.Rows.Add(row);
75+
foreach (var property in item.Properties)
76+
{
77+
row[property.Key] = property.Value;
78+
}
79+
}
80+
}
5381
else if (value is IEnumerable && value.GetType().IsGenericType && value.GetType().GetGenericArguments().Length == 1)
5482
{
5583
var genericType = value.GetType().GetGenericArguments()[0];

src/Z.Expressions.SqlServer.Eval/Helper/SqlTypeHelper.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static object ConvertToType(object value)
4747
return valueDouble.IsNull ? (double?) null : valueDouble.Value;
4848
case "SqlGuid":
4949
var valueGuid = (SqlGuid) value;
50-
return valueGuid.IsNull ? Guid.Empty : valueGuid.Value;
50+
return valueGuid.IsNull ? (Guid?) null : valueGuid.Value;
5151
case "SqlInt16":
5252
var valueInt16 = (SqlInt16) value;
5353
return valueInt16.IsNull ? (short?) null : valueInt16.Value;
@@ -75,5 +75,48 @@ public static object ConvertToType(object value)
7575

7676
return rValue;
7777
}
78+
79+
public static Type GetNullableType(object value)
80+
{
81+
switch (value.GetType().Name)
82+
{
83+
case "SqlBinary":
84+
return typeof (byte[]);
85+
case "SqlBoolean":
86+
return typeof (bool?);
87+
case "SqlByte":
88+
return typeof (byte?);
89+
case "SqlBytes":
90+
return typeof (byte[]);
91+
case "SqlChars":
92+
return typeof (char[]);
93+
case "SqlDateTime":
94+
return typeof (DateTime?);
95+
case "SqlDecimal":
96+
return typeof (decimal?);
97+
case "SqlDouble":
98+
return typeof (double?);
99+
case "SqlGuid":
100+
return typeof (Guid?);
101+
case "SqlInt16":
102+
return typeof (short?);
103+
case "SqlInt32":
104+
return typeof (int?);
105+
case "SqlInt64":
106+
return typeof (long?);
107+
case "SqlMoney":
108+
return typeof (decimal?);
109+
case "SqlSingle":
110+
return typeof (float?);
111+
case "SqlString":
112+
return typeof (string);
113+
case "SqlXml":
114+
return typeof (string);
115+
case "DBNull":
116+
return null;
117+
}
118+
119+
return typeof (object);
120+
}
78121
}
79122
}

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/Eval.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections;
1010
using System.Collections.Generic;
11+
using System.Dynamic;
1112
using System.Security.Principal;
1213
using Microsoft.SqlServer.Server;
1314

@@ -19,9 +20,11 @@ public partial struct SQLNET
1920
{
2021
/// <summary>Eval the code or expression and return an object value.</summary>
2122
/// <returns>The object value from the evaluated code or expression.</returns>
22-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
23+
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.None)]
2324
public object Eval()
2425
{
26+
27+
2528
var item = Item;
2629
var now = DateTime.Now;
2730

@@ -113,15 +116,15 @@ public object Eval()
113116

114117
/// <summary>Eval the code or expression and return an object value.</summary>
115118
/// <returns>The object value from the evaluated code or expression.</returns>
116-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
119+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
117120
public object eval()
118121
{
119122
return Eval();
120123
}
121124

122125
/// <summary>Eval the code or expression and return an object value.</summary>
123126
/// <returns>The object value from the evaluated code or expression.</returns>
124-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
127+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
125128
public object EVAL()
126129

127130
{

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalBigInt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial struct SQLNET
1616
{
1717
/// <summary>Eval the code or expression and return a big int value.</summary>
1818
/// <returns>The big int value from the evaluated code or expression.</returns>
19-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
19+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
2020
public long? EvalBigInt()
2121
{
2222
var value = Eval();
@@ -26,7 +26,7 @@ public partial struct SQLNET
2626

2727
/// <summary>Eval the code or expression and return a big int value.</summary>
2828
/// <returns>The big int value from the evaluated code or expression.</returns>
29-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
29+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3030
public long? evalbigint()
3131

3232
{
@@ -35,7 +35,7 @@ public partial struct SQLNET
3535

3636
/// <summary>Eval the code or expression and return a big int value.</summary>
3737
/// <returns>The big int value from the evaluated code or expression.</returns>
38-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
38+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3939
public long? EVALBIGINT()
4040
{
4141
return EvalBigInt();

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalBit.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial struct SQLNET
1616
{
1717
/// <summary>Eval the code or expression and return a bit value.</summary>
1818
/// <returns>The bit value from the evaluated code or expression.</returns>
19-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
19+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
2020
public bool? EvalBit()
2121
{
2222
var value = Eval();
@@ -26,7 +26,7 @@ public partial struct SQLNET
2626

2727
/// <summary>Eval the code or expression and return a bit value.</summary>
2828
/// <returns>The bit value from the evaluated code or expression.</returns>
29-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
29+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3030
public bool? evalbit()
3131

3232
{
@@ -35,7 +35,7 @@ public partial struct SQLNET
3535

3636
/// <summary>Eval the code or expression and return a bit value.</summary>
3737
/// <returns>The bit value from the evaluated code or expression.</returns>
38-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
38+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3939
public bool? EVALBIT()
4040
{
4141
return EvalBit();

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalInt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial struct SQLNET
1616
{
1717
/// <summary>Eval the code or expression and return an int value.</summary>
1818
/// <returns>The int value from the evaluated code or expression.</returns>
19-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
19+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
2020
public int? EvalInt()
2121
{
2222
var value = Eval();
@@ -26,15 +26,15 @@ public partial struct SQLNET
2626

2727
/// <summary>Eval the code or expression and return an int value.</summary>
2828
/// <returns>The int value from the evaluated code or expression.</returns>
29-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
29+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3030
public int? evalint()
3131
{
3232
return EvalInt();
3333
}
3434

3535
/// <summary>Eval the code or expression and return an int value.</summary>
3636
/// <returns>The int value from the evaluated code or expression.</returns>
37-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
37+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3838
public int? EVALINT()
3939
{
4040
return EvalInt();

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalSQLNET.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public partial struct SQLNET
1515
{
1616
/// <summary>Eval the code or expression and return a SQLNET object value.</summary>
1717
/// <returns>The SQLNET object value from the evaluated code or expression.</returns>
18-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
18+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
1919
public SQLNET EvalSQLNET()
2020
{
2121
var value = Eval();
@@ -29,15 +29,15 @@ public SQLNET EvalSQLNET()
2929

3030
/// <summary>Eval the code or expression and return a SQLNET object value.</summary>
3131
/// <returns>The SQLNET object value from the evaluated code or expression.</returns>
32-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
32+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3333
public SQLNET evalsqlnet()
3434
{
3535
return EvalSQLNET();
3636
}
3737

3838
/// <summary>Eval the code or expression and return a SQLNET object value.</summary>
3939
/// <returns>The SQLNET object value from the evaluated code or expression.</returns>
40-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
40+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
4141
public SQLNET EVALSQLNET()
4242
{
4343
return EvalSQLNET();

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalSmallInt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial struct SQLNET
1616
{
1717
/// <summary>Eval the code or expression and return a small int value.</summary>
1818
/// <returns>The small int value from the evaluated code or expression.</returns>
19-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
19+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
2020
public short? EvalSmallInt()
2121
{
2222
var value = Eval();
@@ -26,15 +26,15 @@ public partial struct SQLNET
2626

2727
/// <summary>Eval the code or expression and return a small int value.</summary>
2828
/// <returns>The small int value from the evaluated code or expression.</returns>
29-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
29+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3030
public short? evalsmallint()
3131
{
3232
return EvalSmallInt();
3333
}
3434

3535
/// <summary>Eval the code or expression and return a small int value.</summary>
3636
/// <returns>The small int value from the evaluated code or expression.</returns>
37-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
37+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3838
public short? EVALSMALLINT()
3939
{
4040
return EvalSmallInt();

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalString.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public partial struct SQLNET
1717
{
1818
/// <summary>Eval the code or expression and return a string value.</summary>
1919
/// <returns>The string value from the evaluated code or expression.</returns>
20-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
20+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
2121
[return: SqlFacet(MaxSize = -1)]
2222
public SqlString EvalString()
2323
{
@@ -28,7 +28,7 @@ public SqlString EvalString()
2828

2929
/// <summary>Eval the code or expression and return a string value.</summary>
3030
/// <returns>The string value from the evaluated code or expression.</returns>
31-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
31+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3232
[return: SqlFacet(MaxSize = -1)]
3333
public SqlString evalstring()
3434
{
@@ -37,7 +37,7 @@ public SqlString evalstring()
3737

3838
/// <summary>Eval the code or expression and return a string value.</summary>
3939
/// <returns>The string value from the evaluated code or expression.</returns>
40-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
40+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
4141
[return: SqlFacet(MaxSize = -1)]
4242
public SqlString EVALSTRING()
4343
{

src/Z.Expressions.SqlServer.Eval/SQLNET/Eval/EvalTinyInt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial struct SQLNET
1616
{
1717
/// <summary>Eval the code or expression and return a tiny int value.</summary>
1818
/// <returns>The tiny int value from the evaluated code or expression.</returns>
19-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
19+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
2020
public byte? EvalTinyInt()
2121
{
2222
var value = Eval();
@@ -26,15 +26,15 @@ public partial struct SQLNET
2626

2727
/// <summary>Eval the code or expression and return a tiny int value.</summary>
2828
/// <returns>The tiny int value from the evaluated code or expression.</returns>
29-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
29+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3030
public byte? evaltinyint()
3131
{
3232
return EvalTinyInt();
3333
}
3434

3535
/// <summary>Eval the code or expression and return a tiny int value.</summary>
3636
/// <returns>The tiny int value from the evaluated code or expression.</returns>
37-
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
37+
[SqlMethod(DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None)]
3838
public byte? EVALTINYINT()
3939
{
4040
return EvalTinyInt();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 & Issues: https://github.com/zzzprojects/Eval-SQL.NET/issues
4+
// License: https://github.com/zzzprojects/Eval-SQL.NET/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
using Microsoft.SqlServer.Server;
9+
10+
// ReSharper disable InconsistentNaming
11+
12+
namespace Z.Expressions.SqlServer.Eval
13+
{
14+
public partial struct SQLNET
15+
{
16+
/// <summary>Eval the code or expression and return an object value.</summary>
17+
/// <returns>The object value from the evaluated code or expression.</returns>
18+
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
19+
public object EvalReadAccess()
20+
{
21+
return Eval();
22+
}
23+
24+
/// <summary>Eval the code or expression and return an object value.</summary>
25+
/// <returns>The object value from the evaluated code or expression.</returns>
26+
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
27+
public object evalreadaccess()
28+
{
29+
return Eval();
30+
}
31+
32+
/// <summary>Eval the code or expression and return an object value.</summary>
33+
/// <returns>The object value from the evaluated code or expression.</returns>
34+
[SqlMethod(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
35+
public object EVALREADACCESS()
36+
37+
{
38+
return Eval();
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)