private void WriteIds(List <Operation> ids) { if (ids.Count > 1) { WriteLine("ComposedId(map => { "); IndentLevel++; foreach (var id in ids) { if (!string.IsNullOrEmpty(id.ManyToOne)) { WriteLine( string.Format( "map.ManyToOne(x => x.{0}, m => {{ m.Class(typeof({1})); m.Column(\"`{2}`\"); }}); ", id.Name, id.Type, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName )); } else { WriteLine( string.Format( "map.Property(x => x.{0}, m => {{ m.Column(\"`{1}`\"); }}); ", id.Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName, id.IsNotNull.ToString().ToLower() )); } } IndentLevel--; WriteLine("});"); } else if (ids.Count == 1) { ClassType _class = (ClassType)Type; string generatorParams = null; if (_class.IdGenerator == "HiLo") { HiLoIdentityGeneratorParameters hiLo = GeneratorParametersDeSerializer.Deserialize <HiLoIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParams = string.Format( ", gmap => gmap.Params(new {{ table = \"{0}\", column = \"{1}\", max_lo = \"{2}\", where = \"{3}\" }})", hiLo.Table, hiLo.Column, hiLo.MaxLo, hiLo.Where ); } else if (_class.IdGenerator == "SeqHiLo") { SeqHiLoIdentityGeneratorParameters seqHiLo = GeneratorParametersDeSerializer.Deserialize <SeqHiLoIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParams = string.Format( ", gmap => gmap.Params(new {{ sequence = \"{0}\", max_lo = \"{1}\" }})", seqHiLo.Sequence, seqHiLo.MaxLo ); } else if (_class.IdGenerator == "Sequence") { SequenceIdentityGeneratorParameters sequence = GeneratorParametersDeSerializer.Deserialize <SequenceIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParams = string.Format( ", gmap => gmap.Params(new {{ sequence = \"{0}\" }})", sequence.Sequence ); } else if (_class.IdGenerator == "UuidHex") { UuidHexIdentityGeneratorParameters uuidHex = GeneratorParametersDeSerializer.Deserialize <UuidHexIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParams = string.Format( ", gmap => gmap.Params(new {{ format_value = \"{0}\", separator_value = \"{1}\" }})", uuidHex.Format, uuidHex.Separator ); } else if (_class.IdGenerator == "Foreign") { ForeignIdentityGeneratorParameters foreign = GeneratorParametersDeSerializer.Deserialize <ForeignIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParams = string.Format( ", gmap => gmap.Params(new {{ property = \"{0}\" }})", foreign.Property ); } else if (_class.IdGenerator == "Custom") { CustomIdentityGeneratorParameters custom = GeneratorParametersDeSerializer.Deserialize <CustomIdentityGeneratorParameters>(_class.GeneratorParameters); idGeneratorType = custom.Class; generatorParams = ", gmap => gmap.Params(new { "; for (int i = 0; i <= (custom.Parameters.Count - 1); i++) { generatorParams += custom.Parameters[i].Name + " = \"" + custom.Parameters[i].Value + "\""; if (i < (custom.Parameters.Count - 1)) { generatorParams += ", "; } } generatorParams += " })"; } WriteLine( string.Format( "Id(x => x.{0}, map => {{ map.Column(\"`{1}`\"); map.Generator(Generators.{2}{3}); }});", ids[0].Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(ids[0].Name) : string.IsNullOrEmpty(ids[0].NHMColumnName) ? ids[0].Name : ids[0].NHMColumnName, idGeneratorType, generatorParams )); } }
protected override void WriteFileContent() { useLazyLoading = Settings.Default.DefaultLazyFetching; useLowercaseUnderscored = Settings.Default.UseUnderscoreAndLowercaseInDB; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.Encoding = System.Text.Encoding.Unicode; ClassType _class = (ClassType)Type; if (_class.IdGenerator == null) { idGeneratorType = EnumExtensions.GetDescription(Settings.Default.DefaultIdentityGenerator); } else { idGeneratorType = EnumExtensions.GetDescription((IdentityGeneratorType)Enum.Parse(typeof(IdentityGeneratorType), _class.IdGenerator)); } using (XmlWriter xml = XmlWriter.Create(CodeBuilder, settings)) { xml.WriteStartDocument(); xml.WriteComment( string.Format( " This code was generated by {0} ", GetVersionString() )); xml.WriteStartElement("hibernate-mapping", "urn:nhibernate-mapping-2.2"); xml.WriteAttributeString("assembly", ProjectName); xml.WriteAttributeString("namespace", RootNamespace); xml.WriteStartElement("class"); xml.WriteAttributeString("name", _class.Name); xml.WriteAttributeString("table", string.Format("`{0}`", PrefixedText( useLowercaseUnderscored ? LowercaseAndUnderscoredWord(_class.Name) : string.IsNullOrEmpty(_class.NHMTableName) ? _class.Name : _class.NHMTableName ))); xml.WriteAttributeString("lazy", useLazyLoading.ToString().ToLower()); List <Operation> ids = _class.Operations.Where(o => o is Property && o.IsIdentity).ToList <Operation>(); if (ids.Count > 1) { xml.WriteStartElement("composite-id"); foreach (var id in ids) { if (!string.IsNullOrEmpty(id.ManyToOne)) { xml.WriteStartElement("key-many-to-one"); xml.WriteAttributeString("name", id.Name); xml.WriteAttributeString("column", string.Format("`{0}`", useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName )); xml.WriteAttributeString("class", id.Type); xml.WriteEndElement(); } else { xml.WriteStartElement("key-property"); xml.WriteAttributeString("name", id.Name); xml.WriteAttributeString("column", string.Format("`{0}`", useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName )); xml.WriteAttributeString("type", id.Type); xml.WriteEndElement(); } } xml.WriteEndElement(); } else if (ids.Count == 1) { xml.WriteStartElement("id"); xml.WriteAttributeString("name", ids[0].Name); xml.WriteAttributeString("column", string.Format("`{0}`", useLowercaseUnderscored ? LowercaseAndUnderscoredWord(ids[0].Name) : string.IsNullOrEmpty(ids[0].NHMColumnName) ? ids[0].Name : ids[0].NHMColumnName )); xml.WriteAttributeString("type", ids[0].Type); xml.WriteStartElement("generator"); if (_class.IdGenerator != "Custom") { xml.WriteAttributeString("class", idGeneratorType); } if (_class.IdGenerator == "HiLo") { HiLoIdentityGeneratorParameters hiLo = GeneratorParametersDeSerializer.Deserialize <HiLoIdentityGeneratorParameters>(_class.GeneratorParameters); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "table"); xml.WriteValue(hiLo.Table); xml.WriteEndElement(); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "column"); xml.WriteValue(hiLo.Column); xml.WriteEndElement(); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "max_lo"); xml.WriteValue(hiLo.MaxLo); xml.WriteEndElement(); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "where"); xml.WriteValue(hiLo.Where); xml.WriteEndElement(); } else if (_class.IdGenerator == "SeqHiLo") { SeqHiLoIdentityGeneratorParameters seqHiLo = GeneratorParametersDeSerializer.Deserialize <SeqHiLoIdentityGeneratorParameters>(_class.GeneratorParameters); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "sequence"); xml.WriteValue(seqHiLo.Sequence); xml.WriteEndElement(); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "max_lo"); xml.WriteValue(seqHiLo.MaxLo); xml.WriteEndElement(); } else if (_class.IdGenerator == "Sequence") { SequenceIdentityGeneratorParameters sequence = GeneratorParametersDeSerializer.Deserialize <SequenceIdentityGeneratorParameters>(_class.GeneratorParameters); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "sequence"); xml.WriteValue(sequence.Sequence); xml.WriteEndElement(); } else if (_class.IdGenerator == "UuidHex") { UuidHexIdentityGeneratorParameters uuidHex = GeneratorParametersDeSerializer.Deserialize <UuidHexIdentityGeneratorParameters>(_class.GeneratorParameters); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "format_value"); xml.WriteValue(uuidHex.Format); xml.WriteEndElement(); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "separator_value"); xml.WriteValue(uuidHex.Separator); xml.WriteEndElement(); } else if (_class.IdGenerator == "Foreign") { ForeignIdentityGeneratorParameters foreign = GeneratorParametersDeSerializer.Deserialize <ForeignIdentityGeneratorParameters>(_class.GeneratorParameters); xml.WriteStartElement("param"); xml.WriteAttributeString("name", "property"); xml.WriteValue(foreign.Property); xml.WriteEndElement(); } else if (_class.IdGenerator == "Custom") { CustomIdentityGeneratorParameters custom = GeneratorParametersDeSerializer.Deserialize <CustomIdentityGeneratorParameters>(_class.GeneratorParameters); xml.WriteAttributeString("class", custom.Class); foreach (var param in custom.Parameters) { xml.WriteStartElement("param"); xml.WriteAttributeString("name", param.Name); xml.WriteValue(param.Value); xml.WriteEndElement(); } } xml.WriteEndElement(); xml.WriteEndElement(); } foreach (var property in _class.Operations.Where(o => o is Property && !o.IsIdentity).ToList <Operation>()) { if (!string.IsNullOrEmpty(property.ManyToOne)) { xml.WriteStartElement("many-to-one"); xml.WriteAttributeString("name", property.Name); xml.WriteAttributeString("class", property.Type); xml.WriteAttributeString("column", string.Format("`{0}`", useLowercaseUnderscored ? LowercaseAndUnderscoredWord(property.Name) : string.IsNullOrEmpty(property.NHMColumnName) ? property.Name : property.NHMColumnName )); if (property.IsUnique) { xml.WriteAttributeString("unique", "true"); } xml.WriteAttributeString("not-null", property.IsNotNull.ToString().ToLower()); xml.WriteEndElement(); } else { xml.WriteStartElement("property"); xml.WriteAttributeString("name", property.Name); xml.WriteAttributeString("column", string.Format("`{0}`", useLowercaseUnderscored ? LowercaseAndUnderscoredWord(property.Name) : string.IsNullOrEmpty(property.NHMColumnName) ? property.Name : property.NHMColumnName )); xml.WriteAttributeString("type", property.Type); if (property.IsUnique) { xml.WriteAttributeString("unique", "true"); } xml.WriteAttributeString("not-null", property.IsNotNull.ToString().ToLower()); xml.WriteEndElement(); } } xml.WriteEndElement(); xml.WriteEndElement(); xml.WriteEndDocument(); } }
private void WriteNHibernateAttributesIds(List <Operation> ids) { if (ids.Count > 1) { WriteLine("[CompositeId(0)]"); int position = 1; foreach (var id in ids) { if (!string.IsNullOrEmpty(id.ManyToOne)) { WriteLine( string.Format( "[KeyManyToOne({0}, Name = \"{1}\", Column = \"`{2}`\", Class = \"{3}\", ClassType = typeof({4}))]", position, id.Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName, id.Type, id.Type )); } else { WriteLine( string.Format( "[KeyProperty({0}, Name = \"{1}\", Column = \"`{2}`\")]", position, id.Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName )); } position++; } foreach (var id in ids) { if (Settings.Default.UseAutomaticProperties) { WriteLine(string.Format("{0} {{ get; set; }}", id.GetDeclaration())); } else { WriteLine(id.GetDeclaration()); WriteProperty((Property)id); } AddBlankLine(); } } else if (ids.Count == 1) { ClassType _class = (ClassType)Type; WriteLine( string.Format( "[Id(0, Name = \"{0}\", Column = \"`{1}`\")]", ids[0].Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(ids[0].Name) : string.IsNullOrEmpty(ids[0].NHMColumnName) ? ids[0].Name : ids[0].NHMColumnName )); if (_class.IdGenerator != "Custom") { WriteLine( string.Format( "[Generator(1, Class = \"{0}\")]", idGeneratorType )); } if (_class.IdGenerator == "HiLo") { HiLoIdentityGeneratorParameters hiLo = GeneratorParametersDeSerializer.Deserialize <HiLoIdentityGeneratorParameters>(_class.GeneratorParameters); WriteLine( string.Format( "[Param(2, Name = \"table\", Content = \"{0}\")]", hiLo.Table )); WriteLine( string.Format( "[Param(3, Name = \"column\", Content = \"{0}\")]", hiLo.Column )); WriteLine( string.Format( "[Param(4, Name = \"max_lo\", Content = \"{0}\")]", hiLo.MaxLo )); WriteLine( string.Format( "[Param(5, Name = \"where\", Content = \"{0}\")]", hiLo.Where )); } else if (_class.IdGenerator == "SeqHiLo") { SeqHiLoIdentityGeneratorParameters seqHiLo = GeneratorParametersDeSerializer.Deserialize <SeqHiLoIdentityGeneratorParameters>(_class.GeneratorParameters); WriteLine( string.Format( "[Param(2, Name = \"sequence\", Content = \"{0}\")]", seqHiLo.Sequence )); WriteLine( string.Format( "[Param(3, Name = \"max_lo\", Content = \"{0}\")]", seqHiLo.MaxLo )); } else if (_class.IdGenerator == "Sequence") { SequenceIdentityGeneratorParameters sequence = GeneratorParametersDeSerializer.Deserialize <SequenceIdentityGeneratorParameters>(_class.GeneratorParameters); WriteLine( string.Format( "[Param(2, Name = \"sequence\", Content = \"{0}\")]", sequence.Sequence )); } else if (_class.IdGenerator == "UuidHex") { UuidHexIdentityGeneratorParameters uuidHex = GeneratorParametersDeSerializer.Deserialize <UuidHexIdentityGeneratorParameters>(_class.GeneratorParameters); WriteLine( string.Format( "[Param(2, Name = \"format_value\", Content = \"{0}\")]", uuidHex.Format )); WriteLine( string.Format( "[Param(3, Name = \"separator_value\", Content = \"{0}\")]", uuidHex.Format )); } else if (_class.IdGenerator == "Foreign") { ForeignIdentityGeneratorParameters foreign = GeneratorParametersDeSerializer.Deserialize <ForeignIdentityGeneratorParameters>(_class.GeneratorParameters); WriteLine( string.Format( "[Param(2, Name = \"property\", Content = \"{0}\")]", foreign.Property )); } else if (_class.IdGenerator == "Custom") { CustomIdentityGeneratorParameters custom = GeneratorParametersDeSerializer.Deserialize <CustomIdentityGeneratorParameters>(_class.GeneratorParameters); idGeneratorType = custom.Class; WriteLine( string.Format( "[Generator(1, Class = \"{0}\")]", idGeneratorType )); int position = 2; for (int i = 0; i <= (custom.Parameters.Count - 1); i++) { WriteLine( string.Format( "[Param({0}, Name = \"{1}\", Content = \"{2}\")]", position.ToString(), custom.Parameters[i].Name, custom.Parameters[i].Value )); position++; } } if (Settings.Default.UseAutomaticProperties) { WriteLine(string.Format("{0} {{ get; set; }}", ids[0].GetDeclaration())); } else { WriteLine(ids[0].GetDeclaration()); WriteProperty((Property)ids[0]); } AddBlankLine(); } }
private void WriteIds(List <Operation> ids) { if (ids.Count > 1) { WriteLine("CompositeId()"); IndentLevel++; foreach (var id in ids) { if (!string.IsNullOrEmpty(id.ManyToOne)) { Write( string.Format( ".KeyReference(x => x.{0}, \"`{1}`\")", id.Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName )); } else { Write( string.Format( ".KeyProperty(x => x.{0}, \"`{1}`\")", id.Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(id.Name) : string.IsNullOrEmpty(id.NHMColumnName) ? id.Name : id.NHMColumnName )); } if (id != ids.Last()) { WriteLine("", false); } else { WriteLine(";", false); } } IndentLevel--; } else if (ids.Count == 1) { ClassType _class = (ClassType)Type; string generatorParameters = null; if (_class.IdGenerator == "HiLo") { HiLoIdentityGeneratorParameters hiLo = GeneratorParametersDeSerializer.Deserialize <HiLoIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParameters = string.Format( "\"{0}\", \"{1}\", \"{2}\", \"{3}\"", hiLo.Table, hiLo.Column, hiLo.MaxLo, hiLo.Where ); } else if (_class.IdGenerator == "SeqHiLo") { SeqHiLoIdentityGeneratorParameters seqHiLo = GeneratorParametersDeSerializer.Deserialize <SeqHiLoIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParameters = string.Format( "\"{0}\", \"{1}\"", seqHiLo.Sequence, seqHiLo.MaxLo ); } else if (_class.IdGenerator == "Sequence") { SequenceIdentityGeneratorParameters sequence = GeneratorParametersDeSerializer.Deserialize <SequenceIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParameters = string.Format( "\"{0}\"", sequence.Sequence ); } else if (_class.IdGenerator == "UuidHex") { UuidHexIdentityGeneratorParameters uuidHex = GeneratorParametersDeSerializer.Deserialize <UuidHexIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParameters = string.Format( "\"{0}\", \"{1}\"", uuidHex.Format, uuidHex.Separator ); } else if (_class.IdGenerator == "Foreign") { ForeignIdentityGeneratorParameters foreign = GeneratorParametersDeSerializer.Deserialize <ForeignIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParameters = string.Format( "\"{0}\"", foreign.Property ); } else if (_class.IdGenerator == "Custom") { CustomIdentityGeneratorParameters custom = GeneratorParametersDeSerializer.Deserialize <CustomIdentityGeneratorParameters>(_class.GeneratorParameters); generatorParameters = string.Format( "\"{0}\", p => {{ ", custom.Class ); for (int i = 0; i <= (custom.Parameters.Count - 1); i++) { generatorParameters += "p.AddParam(\"" + custom.Parameters[i].Name + "\", \"" + custom.Parameters[i].Value + "\"); "; } generatorParameters += "}"; } WriteLine( string.Format( "Id(x => x.{0}).Column(\"`{1}`\").GeneratedBy.{2}({3});", ids[0].Name, useLowercaseUnderscored ? LowercaseAndUnderscoredWord(ids[0].Name) : string.IsNullOrEmpty(ids[0].NHMColumnName) ? ids[0].Name : ids[0].NHMColumnName, idGeneratorType, generatorParameters )); } }