DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
MappingContext.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 
5 using Deveel.Data.Types;
6 
7 using IQToolkit;
8 
9 namespace Deveel.Data.Mapping {
10  public sealed class MappingContext {
11  private Dictionary<Type, ITypeMappingConfiguration> configurations;
12 
13  internal MappingContext() {
14  TableNameConvention = RuledNamingConvention.SqlNaming;
15  ColumnNameConvention = RuledNamingConvention.SqlNaming;
16  }
17 
18  public INamingConvention TableNameConvention { get; set; }
19 
20  public INamingConvention ColumnNameConvention { get; set; }
21 
22  public TypeMappingConfiguration<T> Map<T>() where T : class {
23  return Map<T>(null);
24  }
25 
26  public TypeMappingConfiguration<T> Map<T>(TypeMappingConfiguration<T> configuration) where T : class {
27  if (configurations == null)
28  configurations = new Dictionary<Type, ITypeMappingConfiguration>();
29 
30  var type = typeof (T);
31 
32  if (configuration == null) {
34  if (!configurations.TryGetValue(type, out config)) {
35  configuration = new TypeMappingConfiguration<T>();
36  configurations[type] = configuration;
37  } else {
38  configuration = (TypeMappingConfiguration<T>) config;
39  }
40  } else {
41  configurations[type] = configuration;
42  }
43 
44  return configuration;
45  }
46 
48  var model = new MappingModel();
49  if (configurations != null) {
50  foreach (var configuration in configurations.Values) {
51  var mapping = CreateTypeMapping(model, configuration);
52 
53  // TODO: make relationships...
54 
55  model.Map(mapping);
56  }
57  }
58 
59  return model;
60  }
61 
62  private string GetTableName(ITypeMappingConfiguration configuration) {
63  var tableName = configuration.TableName;
64  if (String.IsNullOrEmpty(tableName))
65  tableName = configuration.ElementType.Name;
66 
67  if (TableNameConvention != null)
68  tableName = TableNameConvention.FormatName(tableName);
69 
70  return tableName;
71  }
72 
74  var tableName = GetTableName(configuration);
75  var mapping = new TypeMapping(model, configuration.ElementType, tableName);
76 
77  foreach (var pair in configuration.Members) {
78  var member = pair.Value;
79  var memberMapping = MapMember(mapping, member);
80  mapping.AddMember(memberMapping);
81  }
82 
83  return mapping;
84  }
85 
87  // TODO: unique key, that is not UNIQUE
88  var sqlType = FormSqlType(configuration);
89  var notNull = (configuration.ColumnConstraints & ColumnConstraints.NotNull) != 0;
90  var primary = (configuration.ColumnConstraints & ColumnConstraints.PrimaryKey) != 0;
91  var unique = (configuration.ColumnConstraints & ColumnConstraints.Unique) != 0;
92  bool uniqueKey = mapping.IsUniqueKey(configuration.Member.Name);
93  var columnName = GetColumnName(configuration);
94  return new MemberMapping(mapping, configuration.Member, columnName, sqlType, notNull, primary, unique, uniqueKey);
95  }
96 
97  private string GetColumnName(IMemberMappingConfiguration configuration) {
98  var columnName = configuration.ColumnName;
99  if (String.IsNullOrEmpty(columnName))
100  columnName = configuration.Member.Name;
101 
102  if (ColumnNameConvention != null)
103  columnName = ColumnNameConvention.FormatName(columnName);
104 
105  return columnName;
106  }
107 
109  var sqlType = configuration.ColumnType;
110  if (sqlType == null)
111  sqlType = DiscoverSqlType(configuration.Member);
112 
113  var meta = new List<DataTypeMeta>();
114  if (configuration.Size != null)
115  meta.Add(new DataTypeMeta("Size", configuration.Size.Value.ToString()));
116  if (configuration.Precision != null)
117  meta.Add(new DataTypeMeta("Precision", configuration.Precision.Value.ToString()));
118 
119  return SqlType.Resolve(sqlType.Value, meta.ToArray());
120  }
121 
122  private SqlTypeCode DiscoverSqlType(MemberInfo member) {
123  var memberType = TypeHelper.GetMemberType(member);
124  if (memberType == typeof(bool))
125  return SqlTypeCode.Boolean;
126  if (memberType == typeof(byte))
127  return SqlTypeCode.TinyInt;
128  if (memberType == typeof(short))
129  return SqlTypeCode.SmallInt;
130  if (memberType == typeof(int))
131  return SqlTypeCode.Integer;
132  if (memberType == typeof (long))
133  return SqlTypeCode.BigInt;
134 
135  if (memberType == typeof(string))
136  return SqlTypeCode.VarChar;
137 
138  throw new NotSupportedException();
139  }
140  }
141 }
A long string in the system.
string GetTableName(ITypeMappingConfiguration configuration)
SqlTypeCode DiscoverSqlType(MemberInfo member)
MemberMapping MapMember(TypeMapping mapping, IMemberMappingConfiguration configuration)
bool IsUniqueKey(string memberName)
Definition: TypeMapping.cs:65
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
string GetColumnName(IMemberMappingConfiguration configuration)
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
Dictionary< Type, ITypeMappingConfiguration > configurations
TypeMapping CreateTypeMapping(MappingModel model, ITypeMappingConfiguration configuration)
static SqlType Resolve(SqlTypeCode typeCode)
Definition: SqlType.cs:441
SqlType FormSqlType(IMemberMappingConfiguration configuration)