Skip to content

Commit

Permalink
- 修复 Column DbType 与 StringLength 多次替换问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 1, 2023
1 parent a8c72b1 commit e69fc68
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
18 changes: 0 additions & 18 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 35 additions & 22 deletions FreeSql/Internal/UtilsExpressionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,26 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
if (colattr.MapType.NullableTypeOrThis() == typeof(string) && colattr.StringLength != 0)
{
int strlen = colattr.StringLength;
var charPatten = @"(CHARACTER|CHAR2|CHAR)\s*(\([^\)]*\))?";
var charPattern = @"(CHARACTER|CHAR2|CHAR)\s*(\([^\)]*\))?";
var replaceCounter = 0;
var strNotNull = colattr.IsNullable == false ? " NOT NULL" : "";
switch (common._orm.Ado.DataType)
{
case DataType.MySql:
case DataType.OdbcMySql:
case DataType.CustomMySql:
if (strlen == -2) colattr.DbType = $"LONGTEXT{strNotNull}";
else if (strlen < 0) colattr.DbType = $"TEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
if (strlen == -2) colattr.DbType = $"LONGTEXT{strNotNull}";
else if (strlen < 0) colattr.DbType = $"TEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.SqlServer:
case DataType.OdbcSqlServer:
case DataType.CustomSqlServer:
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1(MAX)");
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}(MAX)" : m.Groups[0].Value);
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.PostgreSQL:
case DataType.OdbcPostgreSQL:
Expand All @@ -371,39 +375,48 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
case DataType.OdbcKingbaseES:
case DataType.ShenTong:
if (strlen < 0) colattr.DbType = $"TEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.Oracle:
case DataType.CustomOracle:
if (strlen < 0) colattr.DbType = $"NCLOB{strNotNull}"; //v1.3.2+ https://github.com/dotnetcore/FreeSql/issues/259
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.Dameng:
if (strlen < 0) colattr.DbType = $"TEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.OdbcOracle:
case DataType.OdbcDameng:
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1(4000)"); //ODBC 不支持 NCLOB
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}(4000)" : m.Groups[0].Value); //ODBC 不支持 NCLOB
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.Sqlite:
if (strlen < 0) colattr.DbType = $"TEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.MsAccess:
charPatten = @"(CHAR|CHAR2|CHARACTER|TEXT)\s*(\([^\)]*\))?";
charPattern = @"(CHAR|CHAR2|CHARACTER|TEXT)\s*(\([^\)]*\))?";
if (strlen < 0) colattr.DbType = $"LONGTEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.Firebird:
charPatten = @"(CHAR|CHAR2|CHARACTER|TEXT)\s*(\([^\)]*\))?";
charPattern = @"(CHAR|CHAR2|CHARACTER|TEXT)\s*(\([^\)]*\))?";
if (strlen < 0) colattr.DbType = $"BLOB SUB_TYPE 1{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.GBase:
if (strlen < 0) colattr.DbType = $"TEXT{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
}
}
Expand All @@ -412,7 +425,7 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
if (colattr.MapType == typeof(byte[]) && colattr.StringLength != 0)
{
int strlen = colattr.StringLength;
var bytePatten = @"(VARBINARY|BINARY|BYTEA)\s*(\([^\)]*\))?";
var bytePattern = @"(VARBINARY|BINARY|BYTEA)\s*(\([^\)]*\))?";
var strNotNull = colattr.IsNullable == false ? " NOT NULL" : "";
switch (common._orm.Ado.DataType)
{
Expand All @@ -421,13 +434,13 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
case DataType.CustomMySql:
if (strlen == -2) colattr.DbType = $"LONGBLOB{strNotNull}";
else if (strlen < 0) colattr.DbType = $"BLOB{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePattern, $"$1({strlen})");
break;
case DataType.SqlServer:
case DataType.OdbcSqlServer:
case DataType.CustomSqlServer:
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1(MAX)");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1({strlen})");
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, bytePattern, $"$1(MAX)");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePattern, $"$1({strlen})");
break;
case DataType.PostgreSQL:
case DataType.OdbcPostgreSQL:
Expand All @@ -453,7 +466,7 @@ public static ColumnInfo ColumnAttributeToInfo(TableInfo trytb, object entityDef
break;
case DataType.MsAccess:
if (strlen < 0) colattr.DbType = $"BLOB{strNotNull}";
else colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePattern, $"$1({strlen})");
break;
case DataType.Firebird:
colattr.DbType = $"BLOB{strNotNull}";
Expand Down

0 comments on commit e69fc68

Please sign in to comment.