DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DeveelDbMapping.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq.Expressions;
5 using System.Reflection;
6 
7 using Deveel.Data.Mapping;
8 
9 using IQToolkit.Data;
10 using IQToolkit.Data.Common;
11 
12 namespace Deveel.Data.Linq {
13  class DeveelDbMapping : BasicMapping {
14  public DeveelDbMapping(MappingModel model) {
15  Model = model;
16  }
17 
18  public MappingModel Model { get; private set; }
19 
20  public override string GetColumnName(MappingEntity entity, MemberInfo member) {
21  var dbMapping = (DbMappingEntity) entity;
22  var memberMapping = dbMapping.TypeMapping.GetMember(member.Name);
23  if (memberMapping == null)
24  return null;
25 
26  return memberMapping.ColumnName;
27  }
28 
29  public override MappingEntity GetEntity(Type elementType, string tableId) {
30  var typeMapping = Model.GetMapping(elementType);
31  if (typeMapping == null)
32  throw new QueryException(String.Format("The type '{0}' is not mapped by the model", elementType));
33 
34  return new DbMappingEntity(tableId, typeMapping);
35  }
36 
37  public override bool IsPrimaryKey(MappingEntity entity, MemberInfo member) {
38  var dbMapping = (DbMappingEntity) entity;
39  return dbMapping.TypeMapping.IsPrimaryKey(member.Name) ||
40  dbMapping.TypeMapping.IsUniqueKey(member.Name);
41  }
42 
43  public override string GetTableName(MappingEntity entity) {
44  var dbMapping = (DbMappingEntity)entity;
45  return dbMapping.TypeMapping.TableName;
46  }
47 
48  public override string GetTableId(Type type) {
49  return type.FullName;
50  }
51 
52  public override bool IsGenerated(MappingEntity entity, MemberInfo member) {
53  var dbMapping = (DbMappingEntity)entity;
54  return dbMapping.TypeMapping.IsUniqueKey(member.Name);
55  }
56 
57  public override bool IsColumn(MappingEntity entity, MemberInfo member) {
58  var dbMapping = (DbMappingEntity)entity;
59  return dbMapping.TypeMapping.IsMemberMapped(member.Name) &&
60  dbMapping.TypeMapping.IsColumn(member.Name);
61  }
62 
63  public override bool IsAssociationRelationship(MappingEntity entity, MemberInfo member) {
64  return base.IsAssociationRelationship(entity, member);
65  }
66 
67  public override IEnumerable<MemberInfo> GetAssociationKeyMembers(MappingEntity entity, MemberInfo member) {
68  return base.GetAssociationKeyMembers(entity, member);
69  }
70 
71  public override IEnumerable<MemberInfo> GetAssociationRelatedKeyMembers(MappingEntity entity, MemberInfo member) {
72  return base.GetAssociationRelatedKeyMembers(entity, member);
73  }
74 
75  public override bool IsRelationshipSource(MappingEntity entity, MemberInfo member) {
76  // TODO:
77  return false;
78  }
79 
80  public override bool IsRelationshipTarget(MappingEntity entity, MemberInfo member) {
81  // TODO:
82  return false;
83  }
84 
85  public override string GetColumnDbType(MappingEntity entity, MemberInfo member) {
86  var dbMapping = (DbMappingEntity)entity;
87  var mapppedMember = dbMapping.TypeMapping.GetMember(member.Name);
88  if (mapppedMember == null)
89  return null;
90 
91  // TODO: Check if here we should provide the full version of the type of just the name
92  return mapppedMember.ColumnType.TypeCode.ToString();
93  }
94 
95  #region DbMappingEntity
96 
97  class DbMappingEntity : MappingEntity {
98  public DbMappingEntity(string tableId, TypeMapping typeMapping) {
99  this.tableId = tableId;
100  TypeMapping = typeMapping;
101  }
102 
103  private readonly string tableId;
104 
105  public TypeMapping TypeMapping { get; private set; }
106 
107  public override string TableId {
108  get { return tableId; }
109  }
110 
111  public override Type ElementType {
112  get { return TypeMapping.Type; }
113  }
114 
115  public override Type EntityType {
116  get { return TypeMapping.Type; }
117  }
118  }
119 
120  #endregion
121  }
122 }
DbMappingEntity(string tableId, TypeMapping typeMapping)
override string GetColumnName(MappingEntity entity, MemberInfo member)
override string GetTableName(MappingEntity entity)
override bool IsRelationshipTarget(MappingEntity entity, MemberInfo member)
DeveelDbMapping(MappingModel model)
override bool IsColumn(MappingEntity entity, MemberInfo member)
override bool IsGenerated(MappingEntity entity, MemberInfo member)
override bool IsPrimaryKey(MappingEntity entity, MemberInfo member)
override MappingEntity GetEntity(Type elementType, string tableId)
override IEnumerable< MemberInfo > GetAssociationRelatedKeyMembers(MappingEntity entity, MemberInfo member)
override bool IsRelationshipSource(MappingEntity entity, MemberInfo member)
override string GetColumnDbType(MappingEntity entity, MemberInfo member)
override string GetTableId(Type type)
override bool IsAssociationRelationship(MappingEntity entity, MemberInfo member)
override IEnumerable< MemberInfo > GetAssociationKeyMembers(MappingEntity entity, MemberInfo member)