Exemplo n.º 1
0
 public void ShouldMapDBColumn()
 {
     var mapper = new DBColumnMapper();
     var column = new Column
                      {
                          Name = "Age",
                          DataType = "Int",
                          IsNullable = true
                      };
     Assert.That(mapper.Map(column, "Age", new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Age).Column(\"Age\");"));
 }
        public string IdMap(Column column, ITextFormatter formatter)
        {
            var mapList = new List<string>();
            var propertyName = formatter.FormatText(column.Name);

            if (column.Name.ToLower() != propertyName.ToLower())
            {
                mapList.Add("map.Column(\"" + column.Name + "\")");
            }
            mapList.Add(column.IsIdentity ? "map.Generator(Generators.Identity)" : "map.Generator(Generators.Assigned)");

            // Outer property definition
            return FormatCode("Id", propertyName, mapList);
        }
Exemplo n.º 3
0
 public void ShouldMapDBColumnWithProperties()
 {
     var mapper = new DBColumnMapper();
     var column = new Column
                      {
                          Name = "Name",
                          DataLength = 16,
                          DataType = "varchar",
                          IsForeignKey = false,
                          IsNullable = false,
                          IsPrimaryKey = false,
                          MappedDataType = "string"
                      };
     Assert.That(mapper.Map(column, "Name", new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Name).Column(\"Name\").Not.Nullable().Length(16);"));
 }
        public string Map(Column column, string fieldName, ITextFormatter Formatter, bool includeLengthAndScale = true)
        {
            var mappedStrBuilder = new StringBuilder(string.Format("Map(x => x.{0})", fieldName));
            mappedStrBuilder.Append(Constants.Dot);
            mappedStrBuilder.Append("Column(\"" + column.Name + "\")");

            if (!column.IsNullable)
            {
                mappedStrBuilder.Append(Constants.Dot);
                mappedStrBuilder.Append("Not.Nullable()");
            }

            if (column.IsUnique)
            {
                mappedStrBuilder.Append(Constants.Dot);
                mappedStrBuilder.Append("Unique()");
            }

            if (column.DataLength.GetValueOrDefault() > 0 & includeLengthAndScale)
            {
                mappedStrBuilder.Append(Constants.Dot);
                mappedStrBuilder.Append("Length(" + column.DataLength + ")");
            }
            else
            {
                if (column.DataPrecision.GetValueOrDefault(0) > 0 & includeLengthAndScale)
                {
                    mappedStrBuilder.Append(Constants.Dot);
                    mappedStrBuilder.Append("Precision(" + column.DataPrecision + ")");
                }

                if (column.DataScale.GetValueOrDefault(0) > 0 & includeLengthAndScale)
                {
                    mappedStrBuilder.Append(Constants.Dot);
                    mappedStrBuilder.Append("Scale(" + column.DataScale + ")");
                }
            }

            mappedStrBuilder.Append(Constants.SemiColon);
            return mappedStrBuilder.ToString();
        }
Exemplo n.º 5
0
 private void AttachValidatorAttributes(ref CodeMemberProperty property, Column column)
 {
     switch (appPrefs.ValidatorStyle)
     {
         case ValidationStyle.Microsoft:
             if (!column.IsNullable)
             {
                 property.CustomAttributes.Add(new CodeAttributeDeclaration("Required"));
             }
             if (column.DataLength.HasValue & column.DataLength > 0 & column.MappedDataType == "System.String" & appPrefs.IncludeLengthAndScale)
             {
                 property.CustomAttributes.Add(new CodeAttributeDeclaration("StringLength", new CodeAttributeArgument(new CodePrimitiveExpression(column.DataLength))));
             }
             break;
         case ValidationStyle.Nhibernate:
             if (!column.IsNullable)
             {
                 property.CustomAttributes.Add(new CodeAttributeDeclaration("NotNullNotEmpty"));
             }
             if (column.DataLength.HasValue & column.DataLength > 0 & column.MappedDataType == "System.String" & appPrefs.IncludeLengthAndScale)
             {
                 property.CustomAttributes.Add(new CodeAttributeDeclaration("Length", new CodeAttributeArgument(new CodePrimitiveExpression(column.DataLength))));
             }
             break;
     }
 }
        //Id(x => x.Id, map =>
        //{
        //    map.Column("ID");
        //    map.Generator(Generators.Sequence, g => g.Params(new { sequence = "TABLE_SEQ" }));
        //});
        public string IdSequenceMap(Column column, string sequenceName, ITextFormatter formatter)
        {
            var builder = new StringBuilder();
            switch (_language)
            {
                case Language.CSharp:
                    builder.AppendFormat("Id(x => x.{0}, map => ", formatter.FormatText(column.Name));
                    builder.AppendLine();
                    builder.AppendLine("\t\t\t\t{");
                    builder.AppendLine("\t\t\t\t\tmap.Column(\"" + column.Name + "\");");
                    builder.AppendLine("\t\t\t\t\tmap.Generator(Generators.Sequence, g => g.Params(new { sequence = \"" + sequenceName + "\" }));");
                    builder.Append("\t\t\t\t});");
                    break;
                case Language.VB:
                    builder.AppendFormat("Id(Function(x) x.{0}, Sub(map)", formatter.FormatText(column.Name));
                    builder.AppendLine();
                    builder.AppendLine("\t\t\t\t\tmap.Column(\"" + column.Name + "\")");
                    builder.AppendLine("\t\t\t\t\tmap.Generator(Generators.Sequence, Function(g) g.Params(New { sequence = \"" + sequenceName + "\" }))");
                    builder.Append("\t\t\t\tEnd Sub)");
                    break;
            }

            return builder.ToString();
        }
        //Property(x => x.Name, map =>
        //                {
        //                    map.Column("NAME");
        //                    map.NotNullable(true);
        //                    map.Length(200);
        //                    map.Unique(true);
        //                });
        public string Map(Column column, ITextFormatter formatter, bool includeLengthAndScale = true)
        {
            var propertyName = formatter.FormatText(column.Name);
            var mapList = new List<string>();

            // Column
            if (column.Name.ToLower() != propertyName.ToLower())
            {
                mapList.Add("map.Column(\"" + column.Name + "\")");
            }
            // Not Null
            if (!column.IsNullable)
            {
                mapList.Add("map.NotNullable(true)");
            }
            // Unique
            if (column.IsUnique)
            {
                mapList.Add("map.Unique(true)");
            }
            // Length
            if (column.DataLength.GetValueOrDefault() > 0 & includeLengthAndScale)
            {
                mapList.Add("map.Length(" + column.DataLength + ")");
            }
            else
            {
                // Precision
                if (column.DataPrecision.GetValueOrDefault(0) > 0 & includeLengthAndScale)
                {
                    mapList.Add("map.Precision(" + column.DataPrecision + ")");
                }
                // Scale
                if (column.DataScale.GetValueOrDefault(0) > 0 & includeLengthAndScale)
                {
                    mapList.Add("map.Scale(" + column.DataScale + ")");
                }
            }

            // m.Access(Accessor.Field);
            if (_applicationPreferences.FieldGenerationConvention == FieldGenerationConvention.Field)
            {
                mapList.Add("map.Access(Accessor.Field)");
            }

            // Outer property definition
            return FormatCode("Property", propertyName, mapList);
        }