0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Linq.Expressions;
|
|
5 using System.Reflection;
|
|
6 using System.Text;
|
|
7
|
|
8 using BLToolkit.Data;
|
|
9 using BLToolkit.Data.DataProvider;
|
|
10 using BLToolkit.Data.Linq;
|
|
11 using BLToolkit.Data.Sql;
|
|
12 using BLToolkit.Data.Sql.SqlProvider;
|
|
13
|
|
14 namespace Data.Linq.Model
|
|
15 {
|
|
16 public class NorthwindDB : DbManager
|
|
17 {
|
|
18 public NorthwindDB() : base("Northwind")
|
|
19 {
|
|
20 }
|
|
21
|
|
22 public Table<Northwind.Category> Category { get { return GetTable<Northwind.Category>(); } }
|
|
23 public Table<Northwind.Customer> Customer { get { return GetTable<Northwind.Customer>(); } }
|
|
24 public Table<Northwind.Employee> Employee { get { return GetTable<Northwind.Employee>(); } }
|
|
25 public Table<Northwind.EmployeeTerritory> EmployeeTerritory { get { return GetTable<Northwind.EmployeeTerritory>(); } }
|
|
26 public Table<Northwind.OrderDetail> OrderDetail { get { return GetTable<Northwind.OrderDetail>(); } }
|
|
27 public Table<Northwind.Order> Order { get { return GetTable<Northwind.Order>(); } }
|
|
28 public Table<Northwind.Product> Product { get { return GetTable<Northwind.Product>(); } }
|
|
29 public Table<Northwind.ActiveProduct> ActiveProduct { get { return GetTable<Northwind.ActiveProduct>(); } }
|
|
30 public Table<Northwind.DiscontinuedProduct> DiscontinuedProduct { get { return GetTable<Northwind.DiscontinuedProduct>(); } }
|
|
31 public Table<Northwind.Region> Region { get { return GetTable<Northwind.Region>(); } }
|
|
32 public Table<Northwind.Shipper> Shipper { get { return GetTable<Northwind.Shipper>(); } }
|
|
33 public Table<Northwind.Supplier> Supplier { get { return GetTable<Northwind.Supplier>(); } }
|
|
34 public Table<Northwind.Territory> Territory { get { return GetTable<Northwind.Territory>(); } }
|
|
35
|
|
36 #if !MONO
|
|
37
|
|
38 public class FreeTextKey<T>
|
|
39 {
|
|
40 public T Key;
|
|
41 public int Rank;
|
|
42 }
|
|
43
|
|
44 class FreeTextTableExpressionAttribute : TableExpressionAttribute
|
|
45 {
|
|
46 public FreeTextTableExpressionAttribute()
|
|
47 : base("")
|
|
48 {
|
|
49 }
|
|
50
|
|
51 public override void SetTable(SqlTable table, MemberInfo member, IEnumerable<Expression> expArgs, IEnumerable<ISqlExpression> sqlArgs)
|
|
52 {
|
|
53 var aargs = sqlArgs.ToArray();
|
|
54 var arr = ConvertArgs(member, aargs).ToList();
|
|
55 var method = (MethodInfo)member;
|
|
56 var sp = new MsSql2008SqlProvider();
|
|
57
|
|
58 {
|
|
59 var ttype = method.GetGenericArguments()[0];
|
|
60 var tbl = new SqlTable(ttype);
|
|
61
|
|
62 var database = tbl.Database == null ? null : sp.Convert(tbl.Database, ConvertType.NameToDatabase). ToString();
|
|
63 var owner = tbl.Owner == null ? null : sp.Convert(tbl.Owner, ConvertType.NameToOwner). ToString();
|
|
64 var physicalName = tbl.PhysicalName == null ? null : sp.Convert(tbl.PhysicalName, ConvertType.NameToQueryTable).ToString();
|
|
65
|
|
66 var name = sp.BuildTableName(new StringBuilder(), database, owner, physicalName);
|
|
67
|
|
68 arr.Add(new SqlExpression(name.ToString(), Precedence.Primary));
|
|
69 }
|
|
70
|
|
71 {
|
|
72 var field = ((ConstantExpression)expArgs.First()).Value;
|
|
73
|
|
74 if (field is string)
|
|
75 arr[0] = new SqlExpression(field.ToString(), Precedence.Primary);
|
|
76 else if (field is LambdaExpression)
|
|
77 {
|
|
78 var body = ((LambdaExpression)field).Body;
|
|
79
|
|
80 if (body is MemberExpression)
|
|
81 {
|
|
82 var name = ((MemberExpression)body).Member.Name;
|
|
83
|
|
84 name = sp.Convert(name, ConvertType.NameToQueryField).ToString();
|
|
85
|
|
86 arr[0] = new SqlExpression(name, Precedence.Primary);
|
|
87 }
|
|
88 }
|
|
89 }
|
|
90
|
|
91 table.SqlTableType = SqlTableType.Expression;
|
|
92 table.Name = "FREETEXTTABLE({6}, {2}, {3}) {1}";
|
|
93 table.TableArguments = arr.ToArray();
|
|
94 }
|
|
95 }
|
|
96
|
|
97 [FreeTextTableExpressionAttribute]
|
|
98 public Table<FreeTextKey<TKey>> FreeTextTable<TTable,TKey>(string field, string text)
|
|
99 {
|
|
100 return GetTable<FreeTextKey<TKey>>(
|
|
101 this,
|
|
102 ((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(TTable), typeof(TKey)),
|
|
103 field,
|
|
104 text);
|
|
105 }
|
|
106
|
|
107 [FreeTextTableExpressionAttribute]
|
|
108 public Table<FreeTextKey<TKey>> FreeTextTable<TTable,TKey>(Expression<Func<TTable,string>> fieldSelector, string text)
|
|
109 {
|
|
110 return GetTable<FreeTextKey<TKey>>(
|
|
111 this,
|
|
112 ((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(TTable), typeof(TKey)),
|
|
113 fieldSelector,
|
|
114 text);
|
|
115 }
|
|
116
|
|
117 #endif
|
|
118 }
|
|
119 }
|