private manytoone SetIfNullable(manytoone mto, IForeignKeyColumnInfo[] iForeignKeyColumnInfo) { bool notnull = true; foreach (var fk in iForeignKeyColumnInfo) { var meta = currentContext.GetTableMetaData(fk.ForeignKeyTableCatalog, fk.ForeignKeyTableSchema, fk.ForeignKeyTableName); if (null != meta) { var cinfo = meta.GetColumnMetadata(fk.ForeignKeyColumnName); if (true.ParseFromDb(cinfo.Nullable) == true) { notnull = false; break; } } } mto.notnull = notnull; mto.notnullSpecified = mto.notnull; return(mto); }
protected override void OnProcess(GenerationContext context) { this.context = context; foreach (var e in context.Model.GetEntities()) { logger.Debug(string.Format("{0} working on:", GetType().Name) + e.table); if (IsManyToManyJoin(e)) { var kc = GetKeyColumns(context.GetTableMetaData(e.schema, e.name)); bool hasbag = false; if (kc.Length == 2)//bag: key spawn two columns { hasbag = true; manytoone[] refs = context.Model.GetManyToOnesOfEntity(e.name); for( int i=0;i<refs.Length;++i ) { int other = i == 0?1:0; set set = new set(); set.table = e.table; set.schema = e.schema; set.catalog = e.catalog; set.name = context.NamingStrategy.GetNameForCollection(refs[other].@class,0); set.key = new key() { column1 = refs[i].column }; set.Item = new manytomany() { @class = refs[other].@class ,Items = new column[]{ new column(){ name = refs[other].column}} }; context.Model.AddCollectionToEntity(refs[i].@class,set); logger.Warn(string.Format("Collection {0} on entity {1} is a many to many, need 'inverse' to be specified?",set.name,refs[i].@class)); } } else if( kc.Length==1 ) //idbag: a single column with the pair id. { hasbag = true; manytoone[] refs = context.Model.GetManyToOnesOfEntity(e.name); for (int i = 0; i < refs.Length; ++i) { int other = i == 0 ? 1 : 0; idbag idbag = new idbag(); idbag.collectionid = new collectionid() { column1 = kc[0].Name , type = typeConverter.GetNHType(kc[0]) , length = kc[0].ColumnSize > 0 ? kc[0].ColumnSize.ToString() : null , generator = new generator() { @class = "native" } }; logger.Warn(string.Format("IdBag collection {0} on entity {1}: collection key generator defaults to native",idbag.name,refs[i].@class)); idbag.table = e.table; idbag.schema = e.schema; idbag.catalog = e.catalog; idbag.name = context.NamingStrategy.GetNameForCollection(refs[other].@class, 0); idbag.key = new key() { column1 = refs[i].column }; idbag.Item = new manytomany() { @class = refs[other].@class , Items = new column[] { new column() { name = refs[other].column } } }; context.Model.AddCollectionToEntity(refs[i].@class, idbag); logger.Warn(string.Format("Collection {0} on entity {1} is a many to many, need 'inverse' to be specified?", idbag.name, refs[i].@class)); } } if (hasbag) { RemoveCurrentCollections(e); context.Model.RemoveEntity(e.name); } } } }
protected override void OnProcess(GenerationContext context) { this.context = context; foreach (var e in context.Model.GetEntities()) { logger.Debug(string.Format("{0} working on:", GetType().Name) + e.table); if (IsManyToManyJoin(e)) { var kc = GetKeyColumns(context.GetTableMetaData(e.schema, e.table)); bool hasbag = false; if (kc.Length == 2)//bag: key spawn two columns { hasbag = true; manytoone[] refs = context.Model.GetManyToOnesOfEntity(e.name); for (int i = 0; i < refs.Length; ++i) { int other = i == 0?1:0; set set = new set(); set.table = e.table; set.schema = e.schema; set.catalog = e.catalog; set.name = context.NamingStrategy.GetNameForCollection(refs[other].@class, 0); set.key = new key() { column1 = refs[i].column }; set.Item = new manytomany() { @class = refs[other].@class , Items = new column[] { new column() { name = refs[other].column } } }; context.Model.AddCollectionToEntity(refs[i].@class, set); logger.Warn(string.Format("Collection {0} on entity {1} is a many to many, need 'inverse' to be specified?", set.name, refs[i].@class)); } } else if (kc.Length == 1) //idbag: a single column with the pair id. { hasbag = true; manytoone[] refs = context.Model.GetManyToOnesOfEntity(e.name); for (int i = 0; i < refs.Length; ++i) { int other = i == 0 ? 1 : 0; idbag idbag = new idbag(); idbag.collectionid = new collectionid() { column1 = kc[0].Name , type = typeConverter.GetNHType(kc[0]) , length = kc[0].ColumnSize > 0 ? kc[0].ColumnSize.ToString() : null , generator = new generator() { @class = "native" } }; logger.Warn(string.Format("IdBag collection {0} on entity {1}: collection key generator defaults to native", idbag.name, refs[i].@class)); idbag.table = e.table; idbag.schema = e.schema; idbag.catalog = e.catalog; idbag.name = context.NamingStrategy.GetNameForCollection(refs[other].@class, 0); idbag.key = new key() { column1 = refs[i].column }; idbag.Item = new manytomany() { @class = refs[other].@class , Items = new column[] { new column() { name = refs[other].column } } }; context.Model.AddCollectionToEntity(refs[i].@class, idbag); logger.Warn(string.Format("Collection {0} on entity {1} is a many to many, need 'inverse' to be specified?", idbag.name, refs[i].@class)); } } if (hasbag) { RemoveCurrentCollections(e); context.Model.RemoveEntity(e.name); } } } }
private void CheckIfCanBeMap(set set, @class entity) { if (set.Item is onetomany) { var collectedEntity = context.Model.GetClassFromEntityName((set.Item as onetomany).@class); if (collectedEntity.Item is compositeid) { var cid = collectedEntity.Item as compositeid; List <string> setColumn = new List <string>(); if (!string.IsNullOrEmpty(set.key.column1)) { setColumn.Add(set.key.column1); } if (null != set.key.column) { setColumn.AddRange(set.key.column.Select(q => q.name)); } List <string> collectedKeyColumns = new List <string>(); collectedKeyColumns.AddRange(cid.Items.OfType <keyproperty>().Select(q => q.column1 ?? q.name)); List <string> nonOverlappingColumns; if (CheckOverlapping(setColumn, collectedKeyColumns, out nonOverlappingColumns)) { setToRemove.Add(set); entitiesToRemove.Add(collectedEntity.name); map map = new map(); map.name = set.name; map.table = set.table; map.key = set.key; var meta = context.GetTableMetaData(collectedEntity.schema, collectedEntity.table ?? collectedEntity.name); if (nonOverlappingColumns.Count == 1) { map.Item = new index() { column1 = nonOverlappingColumns[0], type = typeConverter.GetNHType(meta.GetColumnMetadata(nonOverlappingColumns[0])) }; } else { compositeindex ci = new compositeindex(); ci.@class = context.NamingStrategy.GetClassNameForComponentKey(map.name ?? map.table); ci.Items = nonOverlappingColumns.Select(q => new keyproperty() { name = context.NamingStrategy.GetPropertyNameFromColumnName(q) , column1 = context.NamingStrategy.GetPropertyNameFromColumnName(q) != q ? q : null , type1 = typeConverter.GetNHType(meta.GetColumnMetadata(q)) , length = meta.GetColumnMetadata(q).ColumnSize == 0 ? null:meta.GetColumnMetadata(q).ColumnSize.ToString() }).ToArray(); map.Item = ci; } property[] props = collectedEntity.Items.OfType <property>().ToArray(); if (props.Length == 1) { //use an element element e = new element(); e.column = props[0].column ?? props[0].name; e.type1 = props[0].type1; e.type = props[0].type; e.precision = props[0].precision; e.length = props[0].length; map.Item1 = e; } else { //use a composite element compositeelement ce = new compositeelement(); ce.@class = context.NamingStrategy.GetClassNameForCollectionComponent(collectedEntity.table ?? collectedEntity.name); ce.Items = context.Model.GetPropertyOfEntity(collectedEntity.name); map.Item1 = ce; } context.Model.AddCollectionToEntity(entity.name, map); } } } }