private void ProcessRelationEnd(Relation rel, RelationEnd relEnd) { var otherEnd = rel.GetOtherEnd(relEnd); string propertyName = rel.GetRelationFkNameToEnd(otherEnd); bool needPositionStorage = rel.NeedsPositionStorage(relEnd.GetRole()); string positionColumnName = Construct.ListPositionColumnName(otherEnd, prefix); GenerateProperty( propertyName, needPositionStorage, positionColumnName); }
private void GetRelationColumnNames(ObjectClass objClass, List <string> columns) { foreach (var relEnd in schema.GetQuery <RelationEnd>().Where(e => e.Type == objClass)) { if (relEnd.Parent.HasStorage(relEnd.GetRole())) { columns.Add(Construct.ForeignKeyColumnName(relEnd.Parent.GetOtherEnd(relEnd))); if (relEnd.Parent.NeedsPositionStorage(relEnd.GetRole())) { columns.Add(Construct.ListPositionColumnName(relEnd.Parent.GetOtherEnd(relEnd))); } } } }
protected virtual void ApplyPropertyMappings() { var relevantRelations = cls.GetRelations() // Managed by a cache .Where(r => (r.A.Type.ID == cls.ID && r.Storage == StorageType.MergeIntoA) || (r.B.Type.ID == cls.ID && r.Storage == StorageType.MergeIntoB)) .ToList() .OrderBy(r => r.GetAssociationName()); foreach (var rel in relevantRelations) { string propertyName; string columnName; if (rel.A.Type == cls && rel.NeedsPositionStorage(RelationEndRole.A) && rel.A.Navigator != null) { propertyName = Construct.ListPositionPropertyName(rel.A); columnName = Construct.ListPositionColumnName(rel.B); this.WriteLine("<ScalarProperty Name=\"{0}\" ColumnName=\"{1}\" />", propertyName, columnName); } if (rel.B.Type == cls && rel.NeedsPositionStorage(RelationEndRole.B) && rel.B.Navigator != null) { propertyName = Construct.ListPositionPropertyName(rel.B); columnName = Construct.ListPositionColumnName(rel.A); this.WriteLine("<ScalarProperty Name=\"{0}\" ColumnName=\"{1}\" />", propertyName, columnName); } } foreach (var prop in cls.Properties.OfType <ValueTypeProperty>().Where(p => !p.IsList).OrderBy(p => p.Name)) { ModelMslEntityTypeMappingScalarProperty.Call(Host, ctx, prop, prop.Name, String.Empty); } foreach (var prop in cls.Properties.OfType <CompoundObjectProperty>().Where(p => !p.IsList).OrderBy(p => p.Name)) { ModelMslEntityTypeMappingComplexProperty.Call(Host, ctx, prop, prop.Name, String.Empty); } }
public static void Call(Arebis.CodeGeneration.IGenerationHost _host, string prefix, ValueTypeProperty prop, string propName, string columnName, bool forceDefinition, string implementationSuffix, bool needsConcurrency) { if (_host == null) { throw new ArgumentNullException("_host"); } // shortcut unmapped properties if (prop.IsCalculated) { _host.WriteOutput(string.Format("<!-- ValueTypeProperty {0} is calculated and persisted -->\n", prop.Name)); } propName = string.IsNullOrEmpty(propName) ? prop.Name : propName; columnName = string.IsNullOrEmpty(columnName) ? propName : columnName; var optimisticLock = needsConcurrency && propName == "ChangedOn"; string typeAttr = String.Empty; if (prop is DateTimeProperty) { typeAttr = "type=\"Timestamp\""; } string ceClassAttr; if (prop.IsList && !forceDefinition) { // set the proper type for collection entries ceClassAttr = String.Format("class=\"{0}.{1}{2}+{1}Proxy,Zetbox.Objects.NHibernateImpl\"", prop.GetCollectionEntryNamespace(), prop.GetCollectionEntryClassName(), implementationSuffix); } else { // not needed ceClassAttr = String.Empty; } string ceReverseKeyColumnName = prop.GetCollectionEntryReverseKeyColumnName(); string listPositionColumnName = Construct.ListPositionColumnName(prop); Call(_host, prefix, propName, columnName, prop.IsList && !forceDefinition, typeAttr, ceClassAttr, ceReverseKeyColumnName, listPositionColumnName, optimisticLock); }
protected virtual void ApplyObjectReferenceProperty(string prefix, ObjectReferenceProperty prop) { this.WriteLine("<!-- ObjectReferenceProperty -->"); var rel = Zetbox.App.Extensions.RelationExtensions.Lookup(ctx, prop); var relEnd = rel.GetEnd(prop); var otherEnd = rel.GetOtherEnd(relEnd); string nameAttr = String.Format("name=\"{0}\"", prop.Name); string classAttr = String.Format("class=\"{0}\"", ObjectClassHbm.GetAssemblyQualifiedProxy(otherEnd.Type, this.Settings)); //string tableAttr = String.Format("table =\"`{0}`\" ", rel.GetAssociationName()); switch (rel.GetRelationType()) { case RelationType.one_one: if (rel.HasStorage(relEnd.GetRole())) { string columnAttr = String.Format("column=\"`{0}`\"", Construct.ForeignKeyColumnName(otherEnd, prefix)); this.WriteObjects(" <many-to-one ", nameAttr, " ", columnAttr, " ", classAttr, " unique=\"true\" "); if (prop.EagerLoading) { // TODO: re-think and re-test eagerloading //this.WriteObjects("fetch=\"join\" "); } this.WriteLine("/>"); } else { this.WriteObjects(" <one-to-one ", nameAttr, " ", classAttr, " constrained=\"false\" ", // constrained must be false, because else the reference is not optional(!) // TODO: re-think and re-test eagerloading //prop.EagerLoading ? "fetch=\"join\" " : String.Empty, "property-ref=\"" + (otherEnd.Navigator != null ? otherEnd.Navigator.Name : "(no nav)") + "\" />"); } break; case RelationType.one_n: if (otherEnd.Multiplicity.UpperBound() > 1) // we are 1-side { // always map as set, the wrapper has to translate/order the elements this.WriteObjects(" <set ", nameAttr, " batch-size=\"100\" cascade=\"none\" inverse=\"true\" "); if (prop.EagerLoading) { // TODO: re-think and re-test eagerloading //this.WriteObjects("lazy=\"false\" fetch=\"join\" "); } this.WriteLine(">"); string columnAttr = String.Format("column=\"`{0}`\"", Construct.ForeignKeyColumnName(relEnd, prefix)); this.WriteObjects(" <key ", columnAttr, " />"); this.WriteLine(); this.WriteObjects(" <one-to-many ", classAttr, " />"); this.WriteLine(); this.WriteLine(" </set>"); } else // we are n-side { string columnAttr = String.Format("column=\"`{0}`\"", Construct.ForeignKeyColumnName(otherEnd, prefix)); this.WriteObjects(" <many-to-one ", nameAttr, " ", columnAttr, " ", classAttr, " "); if (prop.EagerLoading) { // TODO: re-think and re-test eagerloading //this.WriteObjects("fetch=\"join\" "); } this.WriteLine("/>"); if (rel.NeedsPositionStorage(relEnd.GetRole())) { string posNameAttr = String.Format("name=\"{0}\"", Construct.ListPositionPropertyName(relEnd)); string posColumnAttr = String.Format("column=\"`{0}`\"", Construct.ListPositionColumnName(otherEnd, prefix)); this.WriteObjects(" <property ", posNameAttr, " ", posColumnAttr, " />"); this.WriteLine(); } } break; case RelationType.n_m: ApplyNMProperty(rel, relEnd, otherEnd, prop); break; default: throw new NotImplementedException(String.Format("Unknown RelationType {0} found", rel.GetRelationType())); } this.WriteLine(); }
public override void Generate() { #line 31 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects("<?xml version=\"1.0\"?>\r\n"); this.WriteObjects("<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" \r\n"); this.WriteObjects(" default-cascade=\"save-update\"\r\n"); this.WriteObjects(" assembly=\"Zetbox.Objects.NHibernateImpl\">\r\n"); this.WriteObjects("\r\n"); this.WriteObjects(" <!-- RelationCollectionEntries -->\r\n"); #line 38 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" foreach (var rel in ctx.GetQuery <Relation>() .Where(r => r.Storage == StorageType.Separate) .ToList() .OrderBy(r => r.GetRelationClassName())) { var collectionEntryNamespace = rel.Module.Namespace; var collectionEntryClassName = rel.GetRelationClassName() + ImplementationSuffix; var proxyClassName = rel.GetRelationClassName() + "Proxy"; var schemaName = rel.Module.SchemaName; var tableName = rel.GetRelationTableName(); #line 49 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <class name=\"", collectionEntryNamespace, ".", collectionEntryClassName, "+", proxyClassName, "\"\r\n"); this.WriteObjects(" proxy=\"", collectionEntryNamespace, ".", collectionEntryClassName, "+", proxyClassName, "\"\r\n"); this.WriteObjects(" table=\"`", tableName, "`\"\r\n"); this.WriteObjects(" schema=\"`", schemaName, "`\" >\r\n"); this.WriteObjects("\r\n"); #line 54 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" IdGeneratorHbm.Call(Host, "id", schemaName, tableName); #line 55 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects("\r\n"); this.WriteObjects(" <many-to-one name=\"A\"\r\n"); this.WriteObjects(" column=\"`", rel.GetRelationFkColumnName(RelationEndRole.A), "`\" />\r\n"); this.WriteObjects(" <many-to-one name=\"B\"\r\n"); this.WriteObjects(" column=\"`", rel.GetRelationFkColumnName(RelationEndRole.B), "`\" />\r\n"); #line 60 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (rel.NeedsPositionStorage(RelationEndRole.A)) { #line 61 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <property name=\"A", Zetbox.API.Helper.PositionSuffix, "\"\r\n"); this.WriteObjects(" column=\"`", Construct.ListPositionColumnName(rel.B), "`\" />\r\n"); #line 63 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 64 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (rel.NeedsPositionStorage(RelationEndRole.B)) { #line 65 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <property name=\"B", Zetbox.API.Helper.PositionSuffix, "\"\r\n"); this.WriteObjects(" column=\"`", Construct.ListPositionColumnName(rel.A), "`\" />\r\n"); #line 67 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 68 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (rel.A.Type.ImplementsIExportable() && rel.B.Type.ImplementsIExportable()) { #line 69 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <property name=\"ExportGuid\" column=\"`ExportGuid`\" type=\"Guid\" />\r\n"); #line 70 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 71 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" </class>\r\n"); #line 72 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 73 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects("\r\n"); this.WriteObjects(" <!-- ValueCollectionEntries are defined directly on use -->\r\n"); #line 76 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" foreach (var prop in ctx.GetQuery <ValueTypeProperty>() .Where(p => p.IsList && !p.IsCalculated) .Where(p => p.ObjectClass is ObjectClass) .ToList() .OrderBy(p => p.ObjectClass.Name) .ThenBy(p => p.Name)) { var collectionEntryNamespace = prop.GetCollectionEntryNamespace(); var collectionEntryClassName = prop.GetCollectionEntryClassName() + ImplementationSuffix; var proxyClassName = prop.GetCollectionEntryClassName() + "Proxy"; var schemaName = prop.Module.SchemaName; var tableName = prop.GetCollectionEntryTable(); var ceReverseKeyColumnName = prop.GetCollectionEntryReverseKeyColumnName(); #line 90 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <class name=\"", collectionEntryNamespace, ".", collectionEntryClassName, "+", proxyClassName, "\"\r\n"); this.WriteObjects(" proxy=\"", collectionEntryNamespace, ".", collectionEntryClassName, "+", proxyClassName, "\"\r\n"); this.WriteObjects(" table=\"`", tableName, "`\"\r\n"); this.WriteObjects(" schema=\"`", schemaName, "`\" >\r\n"); this.WriteObjects("\r\n"); #line 95 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" IdGeneratorHbm.Call(Host, "id", schemaName, tableName); #line 96 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects("\r\n"); this.WriteObjects(" <many-to-one name=\"Parent\"\r\n"); this.WriteObjects(" column=\"`", ceReverseKeyColumnName, "`\" />\r\n"); #line 99 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" ValueTypePropertyHbm.Call(Host, String.Empty, prop, "Value", prop.Name, true, ImplementationSuffix, false); #line 100 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (prop.HasPersistentOrder) { #line 101 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <property name=\"Value_pos\"\r\n"); this.WriteObjects(" column=\"`Index`\" />\r\n"); #line 103 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 104 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (((ObjectClass)prop.ObjectClass).ImplementsIExportable()) { #line 105 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <!-- export guid is not needed since serialization is always \"in-place\"\r\n"); this.WriteObjects(" <property name=\"ExportGuid\" column=\"`ExportGuid`\" type=\"Guid\" />\r\n"); this.WriteObjects(" -->\r\n"); #line 108 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 109 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" </class>\r\n"); this.WriteObjects("\r\n"); #line 111 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 112 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <!-- CompoundObjectCollectionEntries -->\r\n"); #line 114 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" foreach (var prop in ctx.GetQuery <CompoundObjectProperty>() .Where(p => p.IsList /* && !p.IsCalculated */) .Where(p => p.ObjectClass is ObjectClass) .ToList() .OrderBy(p => p.ObjectClass.Name) .ThenBy(p => p.Name)) { var collectionEntryNamespace = prop.GetCollectionEntryNamespace(); var collectionEntryClassName = prop.GetCollectionEntryClassName() + ImplementationSuffix; var proxyClassName = prop.GetCollectionEntryClassName() + "Proxy"; var schemaName = prop.Module.SchemaName; var tableName = prop.GetCollectionEntryTable(); var ceReverseKeyColumnName = prop.GetCollectionEntryReverseKeyColumnName(); #line 128 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <class name=\"", collectionEntryNamespace, ".", collectionEntryClassName, "+", proxyClassName, "\"\r\n"); this.WriteObjects(" proxy=\"", collectionEntryNamespace, ".", collectionEntryClassName, "+", proxyClassName, "\"\r\n"); this.WriteObjects(" table=\"`", tableName, "`\"\r\n"); this.WriteObjects(" schema=\"`", schemaName, "`\" >\r\n"); this.WriteObjects("\r\n"); #line 133 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" IdGeneratorHbm.Call(Host, "id", schemaName, tableName); #line 134 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects("\r\n"); this.WriteObjects(" <many-to-one name=\"Parent\"\r\n"); this.WriteObjects(" column=\"`", ceReverseKeyColumnName, "`\" />\r\n"); #line 137 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" CompoundObjectPropertyHbm.Call(Host, ctx, String.Empty, prop, "Value", prop.Name, true, ImplementationSuffix); #line 138 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (prop.HasPersistentOrder) { #line 139 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <property name=\"Value_pos\"\r\n"); this.WriteObjects(" column=\"`Index`\" />\r\n"); #line 141 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 142 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" if (((ObjectClass)prop.ObjectClass).ImplementsIExportable()) { #line 143 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" <!-- export guid is not needed since serialization is always \"in-place\"\r\n"); this.WriteObjects(" <property name=\"ExportGuid\" column=\"`ExportGuid`\" type=\"Guid\" />\r\n"); this.WriteObjects(" -->\r\n"); #line 146 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 147 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects(" </class>\r\n"); this.WriteObjects("\r\n"); #line 149 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" } #line 150 "P:\zetbox\Zetbox.DalProvider.NHibernate.Generator\Templates\Mappings\CollectionEntriesHbm.cst" this.WriteObjects("</hibernate-mapping>\r\n"); }
private void Check_1_N_RelationColumns(Relation rel) { string assocName = rel.GetAssociationName(); RelationEnd relEnd, otherEnd; switch (rel.Storage) { case StorageType.MergeIntoA: relEnd = rel.A; otherEnd = rel.B; break; case StorageType.MergeIntoB: otherEnd = rel.A; relEnd = rel.B; break; default: Log.ErrorFormat("Relation '{0}' has unsupported Storage set: {1}, skipped", assocName, rel.Storage); return; } var tblName = db.GetTableName(relEnd.Type.Module.SchemaName, relEnd.Type.TableName); var refTblName = db.GetTableName(otherEnd.Type.Module.SchemaName, otherEnd.Type.TableName); bool isIndexed = rel.NeedsPositionStorage(relEnd.GetRole()); string colName = Construct.ForeignKeyColumnName(otherEnd); string indexName = Construct.ListPositionColumnName(otherEnd); CheckColumn(tblName, colName, System.Data.DbType.Int32, 0, 0, otherEnd.IsNullable(), null); if (!db.CheckFKConstraintExists(tblName, assocName)) { Log.WarnFormat("FK Constraint '{0}' is missing", assocName); if (repair) { db.CreateFKConstraint(tblName, refTblName, colName, assocName, false); } } if (!db.CheckIndexExists(tblName, Construct.IndexName(tblName.Name, colName))) { Log.WarnFormat("Index '{0}' is missing", Construct.IndexName(tblName.Name, colName)); if (repair) { db.CreateIndex(tblName, Construct.IndexName(tblName.Name, colName), false, false, colName); } } if (isIndexed) { CheckOrderColumn(tblName, indexName); } if (!isIndexed && db.CheckColumnExists(tblName, indexName)) { Log.WarnFormat("Index Column exists but property is not indexed"); if (repair) { Case.Do_1_N_RelationChange_FromIndexed_To_NotIndexed(rel); } } }