5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Collections.Generic;
\r
29 using System.Collections.ObjectModel;
\r
30 using System.Data.Linq.Mapping;
\r
32 using System.Reflection;
\r
34 using System.Data.Linq.Sugar;
\r
35 using System.Data.Linq.Sugar.Expressions;
\r
37 using DbLinq.Data.Linq.Sugar.Expressions;
\r
41 namespace System.Data.Linq.Sugar.Implementation
\r
43 namespace DbLinq.Data.Linq.Sugar.Implementation
\r
46 internal class DataMapper : IDataMapper
\r
49 /// Returns a table given a type, or null if the type is not mapped
\r
51 /// <param name="tableType"></param>
\r
52 /// <param name="dataContext"></param>
\r
53 /// <returns></returns>
\r
54 public virtual string GetTableName(Type tableType, DataContext dataContext)
\r
56 var tableDescription = dataContext.Mapping.GetTable(tableType);
\r
57 if (tableDescription != null)
\r
58 return tableDescription.TableName;
\r
62 public virtual string GetColumnName(TableExpression tableExpression, MemberInfo memberInfo, DataContext dataContext)
\r
64 return GetColumnName(tableExpression.Type, memberInfo, dataContext);
\r
67 public virtual string GetColumnName(Type tableType, MemberInfo memberInfo, DataContext dataContext)
\r
69 var tableDescription = dataContext.Mapping.GetTable(tableType);
\r
70 var columnDescription = tableDescription.RowType.GetDataMember(memberInfo);
\r
71 if (columnDescription != null)
\r
72 return columnDescription.MappedName;
\r
76 public virtual IList<MemberInfo> GetPrimaryKeys(TableExpression tableExpression, DataContext dataContext)
\r
78 var tableDescription = dataContext.Mapping.GetTable(tableExpression.Type);
\r
79 if (tableDescription != null)
\r
80 return GetPrimaryKeys(tableDescription);
\r
84 public virtual IList<MemberInfo> GetPrimaryKeys(MetaTable tableDescription)
\r
86 return (from column in tableDescription.RowType.IdentityMembers select column.Member).ToList();
\r
90 /// Lists table mapped columns
\r
92 /// <param name="tableDescription"></param>
\r
93 /// <returns></returns>
\r
94 public IList<MemberInfo> GetColumns(MetaTable tableDescription)
\r
96 return (from column in tableDescription.RowType.PersistentDataMembers select column.Member).ToList();
\r
100 /// Returns association definition, if any
\r
102 /// <param name="thisTableExpression">The table referenced by the assocation (the type holding the member)</param>
\r
103 /// <param name="memberInfo">The memberInfo related to association</param>
\r
104 /// <param name="thisKey">The keys in the joined table</param>
\r
105 /// <param name="otherKey">The keys in the associated table</param>
\r
106 /// <param name="joinType"></param>
\r
107 /// <param name="joinID"></param>
\r
108 /// <param name="dataContext"></param>
\r
109 /// <returns></returns>
\r
110 public virtual Type GetAssociation(TableExpression thisTableExpression, MemberInfo memberInfo,
\r
111 out IList<MemberInfo> thisKey, out IList<MemberInfo> otherKey,
\r
112 out TableJoinType joinType, out string joinID, DataContext dataContext)
\r
114 var thisTableDescription = dataContext.Mapping.GetTable(thisTableExpression.Type);
\r
115 var thisAssociation =
\r
116 (from association in thisTableDescription.RowType.Associations
\r
117 where association.ThisMember.Member == memberInfo
\r
118 select association).SingleOrDefault();
\r
119 if (thisAssociation != null)
\r
121 // by default, join is inner
\r
122 joinType = TableJoinType.Inner;
\r
123 joinID = thisAssociation.ThisMember.MappedName;
\r
124 if (string.IsNullOrEmpty(joinID))
\r
125 throw Error.BadArgument("S0108: Association name is required to ensure join uniqueness");
\r
127 var otherType = thisAssociation.ThisMember.Type;
\r
128 if (otherType.IsGenericType) // TODO: something serious here
\r
129 otherType = otherType.GetGenericArguments()[0];
\r
131 var otherTableDescription = dataContext.Mapping.GetTable(otherType);
\r
132 bool thisKeyHasNullables, otherKeyHasNullables;
\r
133 thisKey = GetAssociationKeys(thisTableDescription, thisAssociation.ThisKey, dataContext, out thisKeyHasNullables);
\r
134 otherKey = GetAssociationKeys(otherTableDescription, thisAssociation.OtherKey, dataContext, out otherKeyHasNullables);
\r
136 // we just test here the left join (since associations are symmetric,
\r
137 // we can only find left joins here, and the otherKeyHasNullables is
\r
138 // always equal to thisKeyHasNullables)
\r
139 if (thisKeyHasNullables)
\r
140 joinType |= TableJoinType.LeftOuter;
\r
146 joinType = TableJoinType.Default;
\r
152 /// Enumerates Keys for a given table.
\r
153 /// Keys can be provided as input. If none provided, PKs are taken from table
\r
155 /// <param name="description"></param>
\r
156 /// <param name="keys">Keys to be used, leave empty to use PKs instead</param>
\r
157 /// <param name="dataContext"></param>
\r
158 /// <param name="hasNullableKeys">returned as true if some keys can be null (we then have an outer join)</param>
\r
159 /// <returns></returns>
\r
160 protected virtual IList<MemberInfo> GetAssociationKeys(MetaTable description, ReadOnlyCollection<MetaDataMember> keys,
\r
161 DataContext dataContext, out bool hasNullableKeys)
\r
163 var sourceKeys = keys;
\r
164 if (sourceKeys.Count == 0)
\r
165 sourceKeys = description.RowType.IdentityMembers;
\r
167 hasNullableKeys = false;
\r
168 var members = new List<MemberInfo>();
\r
169 foreach (var sourceKey in sourceKeys)
\r
171 members.Add(sourceKey.Member);
\r
172 if (sourceKey.CanBeNull)
\r
173 hasNullableKeys = true;
\r
178 public IList<MemberInfo> GetEntitySetAssociations(Type type)
\r
180 return type.GetProperties()
\r
181 .Where(p => p.PropertyType.IsGenericType &&
\r
182 p.PropertyType.GetGenericTypeDefinition() == typeof(System.Data.Linq.EntitySet<>) &&
\r
183 p.IsDefined(typeof(AssociationAttribute), true))
\r
184 .Cast<MemberInfo>().ToList();
\r
187 public IList<MemberInfo> GetEntityRefAssociations(Type type)
\r
189 return (from p in type.GetProperties()
\r
190 let associationAttribute = p.GetCustomAttributes(typeof(AssociationAttribute), true).FirstOrDefault() as AssociationAttribute
\r
191 let field = type.GetField(associationAttribute != null ? (associationAttribute.Storage ?? string.Empty) : string.Empty, BindingFlags.NonPublic | BindingFlags.Instance)
\r
192 where associationAttribute != null &&
\r
194 field.FieldType.IsGenericType &&
\r
195 field.FieldType.GetGenericTypeDefinition() == typeof(System.Data.Linq.EntityRef<>)
\r
197 .Cast<MemberInfo>().ToList();
\r