protected virtual void CreateTableForProperty(IPropertyMap propertyMap, IDomainMap targetDomainMap, bool generateMappings) { IClassMap classMap = propertyMap.ClassMap; ITableMap tableMap = null; ISourceMap sourceMap = null; ISourceMap addToSourceMap = null; IClassMap classMapClone = null; IPropertyMap propertyMapClone = null; string name = ""; sourceMap = classMap.GetSourceMap(); if (sourceMap == null) { sourceMap = classMap.DomainMap.GetSourceMap(); if (sourceMap == null) { throw new Exception("No default data source specified for domain model! Can't add table for class!"); } } addToSourceMap = targetDomainMap.GetSourceMap(sourceMap.Name); if (addToSourceMap == null) { addToSourceMap = (ISourceMap) sourceMap.Clone(); addToSourceMap.DomainMap = targetDomainMap; } if (propertyMap.Table.Length > 0) { name = propertyMap.Table; } else { name = GetTableNameForProperty(propertyMap); } tableMap = addToSourceMap.GetTableMap(name); if (tableMap == null) { tableMap = new TableMap(); tableMap.Name = name; tableMap.SourceMap = addToSourceMap; } if (generateMappings & propertyMap.Table.Length < 1) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); classMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == targetDomainMap.Source)) { classMapClone.Source = addToSourceMap.Name; } classMapClone.DomainMap = targetDomainMap; } propertyMapClone = classMapClone.GetPropertyMap(propertyMap.Name); if (propertyMapClone == null) { propertyMapClone = (IPropertyMap) propertyMap.Clone(); propertyMapClone.ClassMap = classMapClone; } propertyMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == classMapClone.Source)) { if (!(addToSourceMap.Name == targetDomainMap.Source)) { propertyMapClone.Source = addToSourceMap.Name; } } } }
protected virtual void CreateColumnForProperty(IPropertyMap propertyMap, IDomainMap targetDomainMap, bool generateMappings, IClassMap ownerClassMap) { IClassMap classMap = propertyMap.ClassMap; ITableMap tableMap = null; ISourceMap sourceMap = null; ISourceMap addToSourceMap = null; IClassMap classMapClone = null; IPropertyMap propertyMapClone = null; IColumnMap columnMap = null; string classTableName = ""; string name = ""; string tableName = ""; bool allowNulls = false; IClassMap refClassMap = null; string forTableName = ""; string forColumnName = ""; int cnt = 0; bool found = false; string primColName = ""; string ownerClassTableName = ""; string foreignKeyName = ""; sourceMap = classMap.GetSourceMap(); if (sourceMap == null) { sourceMap = classMap.DomainMap.GetSourceMap(); if (sourceMap == null) { throw new Exception("No default data source specified for domain model! Can't add table for class!"); } } addToSourceMap = targetDomainMap.GetSourceMap(sourceMap.Name); if (addToSourceMap == null) { addToSourceMap = (ISourceMap) sourceMap.Clone(); addToSourceMap.DomainMap = targetDomainMap; } if (propertyMap.ClassMap.Table.Length > 0) { classTableName = propertyMap.ClassMap.Table; } else { classTableName = GetTableNameForClass(propertyMap.ClassMap); } if (propertyMap.Table.Length > 0) { tableName = propertyMap.Table; } else if (propertyMap.IsCollection) { tableName = GetTableNameForProperty(propertyMap); } else { if (IsOutbrokenByInheritance(propertyMap, ownerClassMap)) { ownerClassTableName = GetTableNameForClass(ownerClassMap, true); tableName = ownerClassTableName; } else { tableName = classTableName; } } tableMap = addToSourceMap.GetTableMap(tableName); if (tableMap == null) { tableMap = new TableMap(); tableMap.Name = tableName; tableMap.SourceMap = addToSourceMap; } if (propertyMap.Column.Length > 0) { name = propertyMap.Column; } else { name = GetColumnNameForProperty(propertyMap); } columnMap = tableMap.GetColumnMap(name); if (columnMap == null) { columnMap = new ColumnMap(); columnMap.Name = name; columnMap.TableMap = tableMap; } columnMap.DataType = GetColumnTypeForProperty(propertyMap); columnMap.Length = GetColumnLengthForProperty(propertyMap); columnMap.Precision = GetColumnPrecisionForProperty(propertyMap); columnMap.Scale = GetColumnScaleForProperty(propertyMap); allowNulls = propertyMap.IsNullable; if (propertyMap.IsIdentity) { allowNulls = false; } columnMap.AllowNulls = allowNulls; if (propertyMap.IsIdentity) { columnMap.IsPrimaryKey = true; columnMap.AllowNulls = false; if (propertyMap.ReferenceType == ReferenceType.None) { if (columnMap.DataType == DbType.Int16 || columnMap.DataType == DbType.Int32 || columnMap.DataType == DbType.Int64) { if (propertyMap.ClassMap.GetIdentityPropertyMaps().Count == 1) { if (propertyMap.GetIsAssignedBySource()) { columnMap.IsAutoIncrease = true; columnMap.Seed = 1; columnMap.Increment = 1; } } } } } if (!(propertyMap.ReferenceType == ReferenceType.None)) { foreignKeyName = "FK_" + columnMap.TableMap.Name + "_" + columnMap.Name; refClassMap = propertyMap.GetReferencedClassMap(); if (!(refClassMap == null)) { foreach (IPropertyMap idProp in refClassMap.GetIdentityPropertyMaps()) { if (cnt > 0) { if (idProp.Column.Length > 0) { name = idProp.Column; } else { name = GetColumnNameForProperty(idProp); } foreignKeyName += "_" + name; } } } primColName = ""; if (refClassMap.TypeColumn.Length > 0) { primColName = refClassMap.TypeColumn; } else { if (!(refClassMap.InheritanceType == InheritanceType.None)) { primColName = GetTypeColumnNameForClass(refClassMap); } } if (primColName.Length > 0) { name = propertyMap.Name + "_" + primColName; foreignKeyName += "_" + name; } else { name = propertyMap.Name; } } if (!(propertyMap.ReferenceType == ReferenceType.None)) { columnMap.IsForeignKey = true; columnMap.ForeignKeyName = foreignKeyName; refClassMap = propertyMap.GetReferencedClassMap(); if (!(refClassMap == null)) { if (refClassMap.Table.Length > 0) { forTableName = refClassMap.Table; } else { forTableName = GetTableNameForClass(refClassMap); } columnMap.PrimaryKeyTable = forTableName; foreach (IPropertyMap idProp in refClassMap.GetIdentityPropertyMaps()) { if (idProp.Column.Length > 0) { forColumnName = idProp.Column; } else { forColumnName = GetColumnNameForProperty(idProp); } break; } columnMap.PrimaryKeyColumn = forColumnName; } } if (generateMappings & propertyMap.Column.Length < 1) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); if (tableMap.Name == classTableName) { classMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == targetDomainMap.Source)) { classMapClone.Source = addToSourceMap.Name; } } classMapClone.DomainMap = targetDomainMap; } propertyMapClone = classMapClone.GetPropertyMap(propertyMap.Name); if (propertyMapClone == null) { propertyMapClone = (IPropertyMap) propertyMap.Clone(); propertyMapClone.ClassMap = classMapClone; } propertyMapClone.Column = columnMap.Name; if (!(tableMap.Name == classTableName)) { propertyMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == classMapClone.Source)) { if (!(addToSourceMap.Name == targetDomainMap.Source)) { propertyMapClone.Source = addToSourceMap.Name; } } } } if (!(propertyMap.ReferenceType == ReferenceType.None)) { refClassMap = propertyMap.GetReferencedClassMap(); if (!(refClassMap == null)) { foreach (IPropertyMap idProp in refClassMap.GetIdentityPropertyMaps()) { if (cnt > 0) { if (idProp.Column.Length > 0) { name = idProp.Column; } else { name = GetColumnNameForProperty(idProp); } columnMap = tableMap.GetColumnMap(name); if (columnMap == null) { columnMap = new ColumnMap(); columnMap.Name = name; columnMap.TableMap = tableMap; } columnMap.DataType = GetColumnTypeForProperty(idProp); columnMap.Length = GetColumnLengthForProperty(idProp); columnMap.Precision = GetColumnPrecisionForProperty(idProp); columnMap.Scale = GetColumnScaleForProperty(idProp); columnMap.IsForeignKey = true; columnMap.ForeignKeyName = foreignKeyName; columnMap.PrimaryKeyTable = forTableName; if (idProp.Column.Length > 0) { forColumnName = idProp.Column; } else { forColumnName = GetColumnNameForProperty(idProp); } columnMap.PrimaryKeyColumn = forColumnName; if (generateMappings) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); if (tableMap.Name == classTableName) { classMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == targetDomainMap.Source)) { classMapClone.Source = addToSourceMap.Name; } } classMapClone.DomainMap = targetDomainMap; } propertyMapClone = classMapClone.GetPropertyMap(propertyMap.Name); if (propertyMapClone == null) { propertyMapClone = (IPropertyMap) propertyMap.Clone(); propertyMapClone.ClassMap = classMapClone; if (!(tableMap.Name == classTableName)) { propertyMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == classMapClone.Source)) { if (!(addToSourceMap.Name == targetDomainMap.Source)) { propertyMapClone.Source = addToSourceMap.Name; } } } } found = false; foreach (string iterName in propertyMapClone.AdditionalColumns) { if (iterName == columnMap.Name) { found = true; } } if (!(found)) { propertyMapClone.AdditionalColumns.Add(columnMap.Name); } } } cnt += 1; } if (refClassMap.InheritanceType != InheritanceType.None) { if (refClassMap.TypeColumn.Length > 0) { primColName = refClassMap.TypeColumn; } else { primColName = GetTypeColumnNameForClass(refClassMap); } name = propertyMap.Name + "_" + primColName; columnMap = tableMap.GetColumnMap(name); if (columnMap == null) { columnMap = new ColumnMap(); columnMap.Name = name; columnMap.TableMap = tableMap; } columnMap.DataType = DbType.AnsiStringFixedLength; columnMap.Length = 1; columnMap.Precision = 1; columnMap.Scale = 0; columnMap.AllowNulls = true; if (propertyMap.IsIdentity) { columnMap.AllowNulls = false; } columnMap.IsForeignKey = true; columnMap.ForeignKeyName = foreignKeyName; columnMap.PrimaryKeyTable = forTableName; columnMap.PrimaryKeyColumn = primColName; if (generateMappings) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); if (tableMap.Name == classTableName) { classMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == targetDomainMap.Source)) { classMapClone.Source = addToSourceMap.Name; } } classMapClone.DomainMap = targetDomainMap; } propertyMapClone = classMapClone.GetPropertyMap(propertyMap.Name); if (propertyMapClone == null) { propertyMapClone = (IPropertyMap) propertyMap.Clone(); propertyMapClone.ClassMap = classMapClone; if (!(tableMap.Name == classTableName)) { propertyMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == classMapClone.Source)) { if (!(addToSourceMap.Name == targetDomainMap.Source)) { propertyMapClone.Source = addToSourceMap.Name; } } } } found = false; foreach (string iterName in propertyMapClone.AdditionalColumns) { if (iterName == columnMap.Name) { found = true; } } if (!(found)) { propertyMapClone.AdditionalColumns.Add(columnMap.Name); } } } } } }
protected virtual void CreateIDColumnForProperty(IPropertyMap propertyMap, IDomainMap targetDomainMap, bool generateMappings, IClassMap ownerClassMap) { IClassMap classMap = propertyMap.ClassMap; ITableMap tableMap = null; ISourceMap sourceMap = null; ISourceMap addToSourceMap = null; IClassMap classMapClone = null; IPropertyMap propertyMapClone = null; IColumnMap columnMap = null; string classTableName = ""; string ownerClassTableName = ""; string name = ""; string forColName = ""; string tableName = ""; string foreignKeyName = ""; bool isClassTableOrConcrete = false; sourceMap = classMap.GetSourceMap(); if (sourceMap == null) { sourceMap = classMap.DomainMap.GetSourceMap(); if (sourceMap == null) { throw new Exception("No default data source specified for domain model! Can't add table for class!"); } } addToSourceMap = targetDomainMap.GetSourceMap(sourceMap.Name); if (addToSourceMap == null) { addToSourceMap = (ISourceMap) sourceMap.Clone(); addToSourceMap.DomainMap = targetDomainMap; } if (propertyMap.ClassMap.Table.Length > 0) { classTableName = propertyMap.ClassMap.Table; } else { classTableName = GetTableNameForClass(propertyMap.ClassMap); } if (propertyMap.Table.Length > 0) { tableName = propertyMap.Table; } else if (propertyMap.IsCollection) { tableName = GetTableNameForProperty(propertyMap); } else { if (IsOutbrokenByInheritance(propertyMap, ownerClassMap)) { isClassTableOrConcrete = true; ownerClassTableName = GetTableNameForClass(ownerClassMap, true); tableName = ownerClassTableName; } else { tableName = classTableName; } } tableMap = addToSourceMap.GetTableMap(tableName); if (tableMap == null) { tableMap = new TableMap(); tableMap.Name = tableName; tableMap.SourceMap = addToSourceMap; } foreignKeyName = "FK_" + tableMap.Name; foreach (IPropertyMap idPropertyMap in propertyMap.ClassMap.GetIdentityPropertyMaps()) { if (idPropertyMap.Column.Length > 0) { forColName = idPropertyMap.Column; } else { forColName = GetColumnNameForProperty(idPropertyMap); } foreignKeyName += "_" + forColName; } if (!(classMap.InheritanceType == InheritanceType.None)) { if (classMap.TypeColumn.Length > 0) { foreignKeyName += "_" + classMap.TypeColumn; } else { foreignKeyName += "_" + GetTypeColumnNameForClass(classMap); } } foreach (IPropertyMap idPropertyMap in propertyMap.ClassMap.GetIdentityPropertyMaps()) { if (idPropertyMap.Column.Length > 0) { forColName = idPropertyMap.Column; } else { forColName = GetColumnNameForProperty(idPropertyMap); } if (propertyMap.IdColumn.Length > 0) { name = propertyMap.IdColumn; } else { name = forColName; } columnMap = tableMap.GetColumnMap(name); if (columnMap == null) { columnMap = new ColumnMap(); columnMap.Name = name; columnMap.TableMap = tableMap; } columnMap.DataType = GetColumnTypeForProperty(idPropertyMap); columnMap.Length = GetColumnLengthForProperty(idPropertyMap); columnMap.Precision = GetColumnPrecisionForProperty(idPropertyMap); columnMap.Scale = GetColumnScaleForProperty(idPropertyMap); if (!(propertyMap.IsCollection)) { columnMap.IsPrimaryKey = true; columnMap.AllowNulls = false; } if (propertyMap.IsCollection || propertyMap.Table.Length > 0 || isClassTableOrConcrete) { columnMap.IsForeignKey = true; columnMap.PrimaryKeyTable = classTableName; columnMap.PrimaryKeyColumn = forColName; columnMap.ForeignKeyName = foreignKeyName; } if (generateMappings & propertyMap.IdColumn.Length < 1) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); classMapClone.DomainMap = targetDomainMap; } propertyMapClone = classMapClone.GetPropertyMap(propertyMap.Name); if (propertyMapClone == null) { propertyMapClone = (IPropertyMap) propertyMap.Clone(); propertyMapClone.ClassMap = classMapClone; } propertyMapClone.IdColumn = columnMap.Name; if (tableMap.Name != classTableName) { propertyMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == classMapClone.Source)) { if (!(addToSourceMap.Name == targetDomainMap.Source)) { propertyMapClone.Source = addToSourceMap.Name; } } } } } if (classMap.InheritanceType != InheritanceType.None) { if (classMap.TypeColumn.Length > 0) { name = classMap.TypeColumn; } else { name = GetTypeColumnNameForClass(classMap); } columnMap = tableMap.GetColumnMap(name); if (columnMap == null) { columnMap = new ColumnMap(); columnMap.Name = name; columnMap.TableMap = tableMap; } columnMap.DataType = DbType.AnsiStringFixedLength; columnMap.Length = 1; columnMap.Precision = 1; columnMap.Scale = 0; if (!(propertyMap.IsCollection)) { columnMap.IsPrimaryKey = true; columnMap.ForeignKeyName = foreignKeyName; columnMap.AllowNulls = false; } columnMap.IsForeignKey = true; columnMap.PrimaryKeyTable = classTableName; columnMap.PrimaryKeyColumn = name; if (generateMappings) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); classMapClone.DomainMap = targetDomainMap; } propertyMapClone = classMapClone.GetPropertyMap(propertyMap.Name); if (propertyMapClone == null) { propertyMapClone = (IPropertyMap) propertyMap.Clone(); propertyMapClone.ClassMap = classMapClone; } foreach (string iterName in propertyMapClone.AdditionalIdColumns) { if (iterName == columnMap.Name) { return; } } propertyMapClone.AdditionalIdColumns.Add(columnMap.Name); } } }