コード例 #1
0
        public override string BeforeField(AttributeModel model)
        {
            var annotations    = new List <string>();
            var columnSettings = new List <string>();

            columnSettings.Add($"name = \"{model.Name.ToSnakeCase()}\"");
            if (_template.GetTypeName(model.TypeReference) == "String" && model.GetTextConstraints()?.MaxLength() != null)
            {
                columnSettings.Add($"length = {model.GetTextConstraints().MaxLength()}");
            }

            if (!model.TypeReference.IsNullable)
            {
                columnSettings.Add("nullable = false");
            }
            annotations.Add($@"@{_template.ImportType("javax.persistence.Column")}({string.Join(", ", columnSettings)})");

            return(string.Join(@"
    ", annotations));
        }
        private string GetAttributeMapping(AttributeModel attribute)
        {
            var statements = new List<string>();
            statements.Add($"builder.Property(x => x.{ attribute.Name.ToPascalCase() })");
            if (!attribute.Type.IsNullable)
            {
                statements.Add(".IsRequired()");
            }

            if (attribute.GetPrimaryKey()?.Identity() == true)
            {
                statements.Add(".UseSqlServerIdentityColumn()");
            }

            if (attribute.GetDefaultConstraint()?.Value() != null)
            {
                var defaultValue = attribute.GetDefaultConstraint().Value();
                statements.Add($".HasDefaultValueSql({(attribute.Type.Element.Name == "string" ? $"\"{ defaultValue }\"" : defaultValue)})");
            }

            var maxLength = attribute.GetTextConstraints()?.MaxLength();
            if (maxLength.HasValue && attribute.Type.Element.Name == "string")
            {
                statements.Add($".HasMaxLength({maxLength.Value})");
            }

            var decimalPrecision = attribute.GetDecimalConstraints()?.Precision();
            var decimalScale = attribute.GetDecimalConstraints()?.Scale();
            var columnType = attribute.GetColumn()?.Type();
            if (decimalPrecision.HasValue && decimalScale.HasValue)
            {
                statements.Add($".HasColumnType(\"decimal({ decimalPrecision }, { decimalScale })\")");
            }
            else if (!string.IsNullOrWhiteSpace(columnType))
            {
                statements.Add($".HasColumnType(\"{columnType}\")");
            }

            var columnName = attribute.GetColumn()?.Name();
            if (!string.IsNullOrWhiteSpace(columnName))
            {
                statements.Add($".HasColumnName(\"{columnName}\")");
            }

            var computedValueSql = attribute.GetComputedValue()?.SQL();
            if (!string.IsNullOrWhiteSpace(computedValueSql))
            {
                statements.Add($".HasComputedColumnSql(\"{computedValueSql}\"{(attribute.GetComputedValue().Stored() ? ", stored: true" : string.Empty)})");
            }

            return $@"
            {string.Join(@"
                ", statements)};";
        }
        public override string BeforeField(AttributeModel model)
        {
            var annotations = new List <string>();

            if (model.GetTextConstraints()?.MaxLength() != null)
            {
                annotations.Add($"@{("javax.validation.constraints.Size")}(max = {model.GetTextConstraints().MaxLength()})");
            }

            if (model.Name.ToLower().EndsWith("email"))
            {
                annotations.Add($"@{Use("javax.validation.constraints.Email")}");
            }

            if (!model.TypeReference.IsNullable)
            {
                annotations.Add($"@{Use("javax.validation.constraints.NotNull")}");
            }

            return(@"
    " + string.Join(@"
    ", annotations));
        }