Skip to content

Commit 7ede399

Browse files
committed
Better handling for IsUnicode.
1 parent 4c36da2 commit 7ede399

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/FirebirdSql.EntityFrameworkCore.Firebird/Query/Internal/FbQuerySqlGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ protected override Expression VisitSqlParameter(SqlParameterExpression sqlParame
114114
Sql.Append(" AS ");
115115
if (sqlParameterExpression.Type == typeof(string))
116116
{
117-
Sql.Append(((IFbSqlGenerationHelper)Dependencies.SqlGenerationHelper).StringParameterQueryType());
117+
var isUnicode = FbTypeMappingSource.IsUnicode(sqlParameterExpression.TypeMapping);
118+
Sql.Append(((IFbSqlGenerationHelper)Dependencies.SqlGenerationHelper).StringParameterQueryType(isUnicode));
118119
}
119120
else
120121
{

src/FirebirdSql.EntityFrameworkCore.Firebird/Storage/Internal/FbSqlGenerationHelper.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ public FbSqlGenerationHelper(RelationalSqlGenerationHelperDependencies dependenc
3030
public virtual string StringLiteralQueryType(string s)
3131
{
3232
var length = MinimumStringQueryTypeLength(s);
33-
EnsureStringQueryTypeLength(length);
33+
EnsureStringLiteralQueryTypeLength(length);
3434
return $"VARCHAR({length}) CHARACTER SET UTF8";
3535
}
3636

37-
public virtual string StringParameterQueryType()
37+
public virtual string StringParameterQueryType(bool isUnicode)
3838
{
39-
return $"VARCHAR({FbTypeMappingSource.VarcharMaxSize})";
39+
var size = isUnicode ? FbTypeMappingSource.UnicodeVarcharMaxSize : FbTypeMappingSource.VarcharMaxSize;
40+
return $"VARCHAR({size})";
4041
}
4142

4243
public virtual void GenerateBlockParameterName(StringBuilder builder, string name)
@@ -54,9 +55,9 @@ static int MinimumStringQueryTypeLength(string s)
5455
return length;
5556
}
5657

57-
static void EnsureStringQueryTypeLength(int length)
58+
static void EnsureStringLiteralQueryTypeLength(int length)
5859
{
59-
if (length > FbTypeMappingSource.VarcharMaxSize)
60+
if (length > FbTypeMappingSource.UnicodeVarcharMaxSize)
6061
throw new ArgumentOutOfRangeException(nameof(length));
6162
}
6263
}

src/FirebirdSql.EntityFrameworkCore.Firebird/Storage/Internal/FbTypeMappingSource.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace FirebirdSql.EntityFrameworkCore.Firebird.Storage.Internal;
2727
public class FbTypeMappingSource : RelationalTypeMappingSource
2828
{
2929
public const int BinaryMaxSize = Int32.MaxValue;
30-
public const int VarcharMaxSize = 32765 / 4;
30+
public const int UnicodeVarcharMaxSize = VarcharMaxSize / 4;
31+
public const int VarcharMaxSize = 32765;
3132
public const int DefaultDecimalPrecision = 18;
3233
public const int DefaultDecimalScale = 2;
3334

@@ -133,7 +134,7 @@ RelationalTypeMapping FindRawMapping(RelationalTypeMappingInfo mappingInfo)
133134
var clrType = mappingInfo.ClrType;
134135
var storeTypeName = mappingInfo.StoreTypeName;
135136
var storeTypeNameBase = mappingInfo.StoreTypeNameBase;
136-
var isUnicode = mappingInfo.IsUnicode ?? true;
137+
var isUnicode = IsUnicode(mappingInfo.IsUnicode);
137138

138139
if (storeTypeName != null)
139140
{
@@ -165,10 +166,11 @@ RelationalTypeMapping FindRawMapping(RelationalTypeMappingInfo mappingInfo)
165166
{
166167
var isFixedLength = mappingInfo.IsFixedLength == true;
167168
var size = mappingInfo.Size ?? (mappingInfo.IsKeyOrIndex ? 256 : (int?)null);
169+
var maxSize = isUnicode ? UnicodeVarcharMaxSize : VarcharMaxSize;
168170

169-
if (size > VarcharMaxSize)
171+
if (size > maxSize)
170172
{
171-
size = isFixedLength ? VarcharMaxSize : (int?)null;
173+
size = isFixedLength ? maxSize : (int?)null;
172174
}
173175

174176
if (size == null)
@@ -196,4 +198,8 @@ RelationalTypeMapping FindRawMapping(RelationalTypeMappingInfo mappingInfo)
196198

197199
return null;
198200
}
201+
202+
public static bool IsUnicode(RelationalTypeMapping mapping) => IsUnicode(mapping?.IsUnicode);
203+
public static bool IsUnicode(RelationalTypeMappingInfo mappingInfo) => IsUnicode(mappingInfo.IsUnicode);
204+
public static bool IsUnicode(bool? isUnicode) => isUnicode ?? true;
199205
}

src/FirebirdSql.EntityFrameworkCore.Firebird/Storage/Internal/IFbSqlGenerationHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace FirebirdSql.EntityFrameworkCore.Firebird.Storage.Internal;
2323
public interface IFbSqlGenerationHelper : ISqlGenerationHelper
2424
{
2525
string StringLiteralQueryType(string s);
26-
string StringParameterQueryType();
26+
string StringParameterQueryType(bool isUnicode);
2727
void GenerateBlockParameterName(StringBuilder builder, string name);
2828
string AlternativeStatementTerminator { get; }
2929
}

0 commit comments

Comments
 (0)