0
|
1 using System;
|
|
2 using System.Linq;
|
|
3 using System.Reflection;
|
|
4
|
|
5 namespace BLToolkit.Data.Linq
|
|
6 {
|
|
7 using Data.Sql;
|
|
8 using Reflection;
|
|
9
|
|
10 [SerializableAttribute]
|
|
11 [AttributeUsageAttribute(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
|
|
12 public class SqlFunctionAttribute : Attribute
|
|
13 {
|
|
14 public SqlFunctionAttribute()
|
|
15 {
|
|
16 }
|
|
17
|
|
18 public SqlFunctionAttribute(string name)
|
|
19 {
|
|
20 Name = name;
|
|
21 }
|
|
22
|
|
23 public SqlFunctionAttribute(string name, params int[] argIndices)
|
|
24 {
|
|
25 Name = name;
|
|
26 ArgIndices = argIndices;
|
|
27 }
|
|
28
|
|
29 public SqlFunctionAttribute(string sqlProvider, string name)
|
|
30 {
|
|
31 SqlProvider = sqlProvider;
|
|
32 Name = name;
|
|
33 }
|
|
34
|
|
35 public SqlFunctionAttribute(string sqlProvider, string name, params int[] argIndices)
|
|
36 {
|
|
37 SqlProvider = sqlProvider;
|
|
38 Name = name;
|
|
39 ArgIndices = argIndices;
|
|
40 }
|
|
41
|
|
42 public string SqlProvider { get; set; }
|
|
43 public string Name { get; set; }
|
|
44 public bool ServerSideOnly { get; set; }
|
|
45 public bool PreferServerSide { get; set; }
|
|
46 public int[] ArgIndices { get; set; }
|
|
47
|
|
48 protected ISqlExpression[] ConvertArgs(MemberInfo member, ISqlExpression[] args)
|
|
49 {
|
|
50 if (member is MethodInfo)
|
|
51 {
|
|
52 var method = (MethodInfo)member;
|
|
53
|
|
54 if (method.DeclaringType.IsGenericType)
|
|
55 args = args.Concat(method.DeclaringType.GetGenericArguments().Select(t => (ISqlExpression)SqlDataType.GetDataType(t))).ToArray();
|
|
56
|
|
57 if (method.IsGenericMethod)
|
|
58 args = args.Concat(method.GetGenericArguments().Select(t => (ISqlExpression)SqlDataType.GetDataType(t))).ToArray();
|
|
59 }
|
|
60
|
|
61 if (ArgIndices != null)
|
|
62 {
|
|
63 var idxs = new ISqlExpression[ArgIndices.Length];
|
|
64
|
|
65 for (var i = 0; i < ArgIndices.Length; i++)
|
|
66 idxs[i] = args[ArgIndices[i]];
|
|
67
|
|
68 return idxs;
|
|
69 }
|
|
70
|
|
71 return args;
|
|
72 }
|
|
73
|
|
74 public virtual ISqlExpression GetExpression(MemberInfo member, params ISqlExpression[] args)
|
|
75 {
|
|
76 return new SqlFunction(TypeHelper.GetMemberType(member), Name ?? member.Name, ConvertArgs(member, args));
|
|
77 }
|
|
78 }
|
|
79 }
|