Skip to content

Commit

Permalink
Add StrigLegth attribute if asked for (#2694)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ authored Dec 2, 2024
1 parent fe5baf1 commit 4592757
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClient;
using RevEng.Core.Abstractions;
using RevEng.Core.Abstractions.Metadata;
using RevEng.Core.Abstractions.Model;
Expand Down Expand Up @@ -50,7 +50,8 @@ protected override List<List<ModuleResultElement>> GetResultElementLists(SqlConn
c.name,
COALESCE(ts.name, tu.name) AS type_name,
c.column_id AS column_ordinal,
c.is_nullable
c.is_nullable,
c.max_length
FROM sys.columns c
inner join sys.types tu ON c.user_type_id = tu.user_type_id
inner join sys.objects AS o on o.object_id = c.object_id
Expand Down Expand Up @@ -79,6 +80,7 @@ FROM sys.columns c
StoreType = res["type_name"].ToString(),
Ordinal = int.Parse(res["column_ordinal"].ToString()!, CultureInfo.InvariantCulture),
Nullable = (bool)res["is_nullable"],
MaxLength = res["max_length"] == DBNull.Value ? (short)0 : short.Parse(res["max_length"].ToString()!, CultureInfo.InvariantCulture),
};

list.Add(parameter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClient;
using RevEng.Core.Abstractions;
using RevEng.Core.Abstractions.Metadata;
using RevEng.Core.Abstractions.Model;
Expand Down Expand Up @@ -126,6 +126,7 @@ private static List<List<ModuleResultElement>> GetAllResultSets(SqlConnection co
StoreType = storeType,
Precision = (short?)row["NumericPrecision"],
Scale = (short?)row["NumericScale"],
MaxLength = (short)row["ColumnSize"],
});
}
}
Expand Down Expand Up @@ -183,6 +184,7 @@ private static List<List<ModuleResultElement>> GetFirstResultSet(SqlConnection c
StoreType = string.IsNullOrEmpty(row["system_type_name"].ToString()) ? row["user_type_name"].ToString() : row["system_type_name"].ToString(),
Ordinal = int.Parse(row["column_ordinal"].ToString()!, CultureInfo.InvariantCulture),
Nullable = (bool)row["is_nullable"],
MaxLength = row["max_length"] == DBNull.Value ? (short)0 : short.Parse(row["max_length"].ToString()!, CultureInfo.InvariantCulture),
};

list.Add(parameter);
Expand Down
7 changes: 7 additions & 0 deletions src/Core/RevEng.Core.80/Routines/RoutineScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ private void GenerateProperties(List<ModuleResultElement> resultElements, bool n
}
}

if (useDecimalDataAnnotation
&& (property.StoreType == "varchar" || property.StoreType == "nvarchar")
&& property.MaxLength > 0)
{
Sb.AppendLine($"[StringLength({property.MaxLength})]");
}

var propertyType = typeMapper.GetClrType(property);
var nullableAnnotation = string.Empty;
var defaultAnnotation = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class ModuleResultElement
public bool Nullable { get; set; }
public short? Precision { get; set; }
public short? Scale { get; set; }
public short MaxLength { get; set; }
}
}

0 comments on commit 4592757

Please sign in to comment.