DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
MemberMappingConfiguration.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Reflection;
5 
6 using Deveel.Data.Types;
7 
8 namespace Deveel.Data.Mapping {
9  public sealed class MemberMappingConfiguration<T> : IMemberMappingConfiguration where T : class {
10  private readonly MemberInfo memberInfo;
12 
13  internal MemberMappingConfiguration(TypeMappingConfiguration<T> typeMapping, MemberInfo memberInfo) {
14  this.memberInfo = memberInfo;
15  this.typeMapping = typeMapping;
16 
17  DiscoverAttributes();
18  }
19 
20  private string ColumnName { get; set; }
21 
22  private SqlTypeCode? ColumnTypeCode { get; set; }
23 
24  private int? Size { get; set; }
25 
26  private int? Precision { get; set; }
27 
28  private ColumnConstraints ColumnConstraints { get; set; }
29 
31  get { return memberInfo; }
32  }
33 
34  private void DiscoverAttributes() {
35  string columnName = null;
36  ColumnConstraints constraints = new ColumnConstraints();
37  SqlTypeCode? typeCode = null;
38  int? size = null;
39  int? scale = null;
40 
41  var attributes = memberInfo.GetCustomAttributes(false);
42  foreach (var attribute in attributes) {
43  if (attribute is ColumnNameAttribute) {
44  var columnNameAttr = (ColumnNameAttribute) attribute;
45  ColumnName = columnNameAttr.ColumnName;
46  } else if (attribute is ColumnAttribute) {
47  var columnAttr = (ColumnAttribute) attribute;
48  columnName = columnAttr.ColumnName;
49  typeCode = columnAttr.SqlType;
50  size = columnAttr.Size;
51  scale = columnAttr.Scale;
52  } else if (attribute is ColumnConstraintAttribute) {
53  var constraintAttr = (ColumnConstraintAttribute) attribute;
54  constraints |= constraintAttr.Constraints;
55  }
56  }
57 
58  ColumnName = columnName;
59  ColumnTypeCode = typeCode;
60  ColumnConstraints = constraints;
61  Size = size;
62  Precision = scale;
63  }
64 
66  get { return ColumnName; }
67  }
68 
70  get { return ColumnTypeCode; }
71  }
72 
74  get { return ColumnConstraints; }
75  }
76 
78  get { return Size; }
79  }
80 
82  get { return Precision; }
83  }
84 
85  public MemberMappingConfiguration<T> HasName(string columnName) {
86  ColumnName = columnName;
87  return this;
88  }
89 
91  ColumnTypeCode = typeCode;
92  return this;
93  }
94 
96  Size = size;
97  return this;
98  }
99 
100  public MemberMappingConfiguration<T> IsUnique(bool flag = true) {
101  if (flag) {
103  } else {
105  }
106 
107  return this;
108  }
109 
110  public MemberMappingConfiguration<T> IsNotNull(bool flag = true) {
111  if (flag) {
113  } else {
114  ColumnConstraints &= ~(ColumnConstraints.NotNull);
115  }
116 
117  return this;
118  }
119 
120  public MemberMappingConfiguration<T> IsPrimaryKey(bool flag = true) {
121  if (flag) {
122  ColumnConstraints |= ColumnConstraints.PrimaryKey;
123  } else {
124  ColumnConstraints &= ~(ColumnConstraints.PrimaryKey);
125  }
126 
127  return this;
128  }
129 
130  public void Ignore() {
131  typeMapping.Ignore(memberInfo.Name);
132  }
133  }
134 }
MemberMappingConfiguration< T > IsUnique(bool flag=true)
MemberMappingConfiguration< T > IsNotNull(bool flag=true)
MemberMappingConfiguration< T > HasType(SqlTypeCode typeCode)
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
MemberMappingConfiguration< T > HasName(string columnName)
MemberMappingConfiguration(TypeMappingConfiguration< T > typeMapping, MemberInfo memberInfo)
MemberMappingConfiguration< T > IsPrimaryKey(bool flag=true)
readonly TypeMappingConfiguration< T > typeMapping
MemberMappingConfiguration< T > HasSize(int size)