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