- 修复 DbFirst string not null 未生成特性 IsNullable = false 的问题;#678

This commit is contained in:
2881099 2021-02-07 17:23:30 +08:00
parent d7cc7cebe9
commit 6cba1dedaf

View File

@ -9,8 +9,10 @@ using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class RazorModel {
public RazorModel(IFreeSql fsql, string nameSpace, bool[] NameOptions, List<DbTableInfo> tables, DbTableInfo table) {
public class RazorModel
{
public RazorModel(IFreeSql fsql, string nameSpace, bool[] NameOptions, List<DbTableInfo> tables, DbTableInfo table)
{
this.fsql = fsql;
this.NameSpace = nameSpace;
this.NameOptions = NameOptions;
@ -26,7 +28,8 @@ public class RazorModel {
public string NameSpace { get; set; }
public string FullTableName => $"{(new[] { "public", "dbo" }.Contains(table.Schema) ? "" : table.Schema)}.{table.Name}".TrimStart('.');
public string GetCsName(string name) {
public string GetCsName(string name)
{
name = Regex.Replace(name.TrimStart('@', '.'), @"[^\w]", "_");
name = char.IsLetter(name, 0) ? name : string.Concat("_", name);
if (NameOptions[0]) name = UFString(name);
@ -35,18 +38,21 @@ public class RazorModel {
if (NameOptions[3]) name = string.Join("", name.Split('_').Select(a => UFString(a)));
return name;
}
public string UFString(string text) {
public string UFString(string text)
{
text = Regex.Replace(text, @"[^\w]", "_");
if (text.Length <= 1) return text.ToUpper();
else return text.Substring(0, 1).ToUpper() + text.Substring(1, text.Length - 1);
}
public string LFString(string text) {
public string LFString(string text)
{
text = Regex.Replace(text, @"[^\w]", "_");
if (text.Length <= 1) return text.ToLower();
else return text.Substring(0, 1).ToLower() + text.Substring(1, text.Length - 1);
}
public string GetCsType(DbColumnInfo col) {
public string GetCsType(DbColumnInfo col)
{
if (fsql.Ado.DataType == FreeSql.DataType.MySql)
if (col.DbType == (int)MySqlDbType.Enum || col.DbType == (int)MySqlDbType.Set)
return $"{this.GetCsName(this.FullTableName)}{this.GetCsName(col.Name).ToUpper()}{(col.IsNullable ? "?" : "")}";
@ -181,9 +187,10 @@ public class RazorModel {
if (dbinfo != null && dbinfo.isnullable != col.IsNullable)
{
if (col.IsNullable && fsql.DbFirst.GetCsType(col).Contains("?") == false && col.CsType.IsValueType)
var cstype = fsql.DbFirst.GetCsType(col);
if (col.IsNullable && cstype.Contains("?") == false && col.CsType.IsValueType)
sb.Add("IsNullable = true");
if (col.IsNullable == false && fsql.DbFirst.GetCsType(col).Contains("?") == true)
if (col.IsNullable == false && (cstype.Contains("?") == true || cstype == "string"))
sb.Add("IsNullable = false");
}
@ -260,8 +267,10 @@ public class RazorModel {
{
if (fsql.Ado.DataType != FreeSql.DataType.MySql && fsql.Ado.DataType != FreeSql.DataType.OdbcMySql) return null;
var sb = new StringBuilder();
foreach (var col in table.Columns) {
if (col.DbType == (int)MySqlDbType.Enum || col.DbType == (int)MySqlDbType.Set) {
foreach (var col in table.Columns)
{
if (col.DbType == (int)MySqlDbType.Enum || col.DbType == (int)MySqlDbType.Set)
{
if (col.DbType == (int)MySqlDbType.Set) sb.Append("\r\n\t[Flags]");
sb.Append($"\r\n\tpublic enum {this.GetCsName(this.FullTableName)}{this.GetCsName(col.Name).ToUpper()}");
if (col.DbType == (int)MySqlDbType.Set) sb.Append(" : long");
@ -272,10 +281,12 @@ public class RazorModel {
int unknow_idx = 0;
string exp2 = string.Concat(col.DbTypeTextFull);
int quote_pos = -1;
while (true) {
while (true)
{
int first_pos = quote_pos = exp2.IndexOf('\'', quote_pos + 1);
if (quote_pos == -1) break;
while (true) {
while (true)
{
quote_pos = exp2.IndexOf('\'', quote_pos + 1);
if (quote_pos == -1) break;
int r_cout = 0;
@ -284,7 +295,8 @@ public class RazorModel {
// else break;
//}
while (exp2[++quote_pos] == '\'') r_cout++;
if (r_cout % 2 == 0/* && quote_pos - first_pos > 2*/) {
if (r_cout % 2 == 0/* && quote_pos - first_pos > 2*/)
{
string str2 = exp2.Substring(first_pos + 1, quote_pos - first_pos - 2).Replace("''", "'");
if (Regex.IsMatch(str2, @"^[\u0391-\uFFE5a-zA-Z_\$][\u0391-\uFFE5a-zA-Z_\$\d]*$"))
slkdgjlksdjg += ", " + str2;