Skip to content

Commit ef9307e

Browse files
committed
ExplicitStringLiteralTypes
1 parent 04a6a75 commit ef9307e

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

Provider/src/FirebirdSql.EntityFrameworkCore.Firebird/Infrastructure/FbDbContextOptionsBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ public FbDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
2929

3030
public virtual FbDbContextOptionsBuilder WithExplicitParameterTypes(bool explicitParameterTypes = true)
3131
=> WithOption(e => e.WithExplicitParameterTypes(explicitParameterTypes));
32+
33+
public virtual FbDbContextOptionsBuilder WithExplicitStringLiteralTypes(bool explicitStringLiteralTypes = true)
34+
=> WithOption(e => e.WithExplicitStringLiteralTypes(explicitStringLiteralTypes));
3235
}
3336
}

Provider/src/FirebirdSql.EntityFrameworkCore.Firebird/Infrastructure/Internal/FbOptionsExtension.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class FbOptionsExtension : RelationalOptionsExtension
2525
{
2626
private long? _serviceProviderHash;
2727
private bool? _explicitParameterTypes;
28+
private bool? _explicitStringLiteralTypes;
2829

2930
public FbOptionsExtension()
3031
{ }
@@ -33,12 +34,14 @@ public FbOptionsExtension(FbOptionsExtension copyFrom)
3334
: base(copyFrom)
3435
{
3536
_explicitParameterTypes = copyFrom._explicitParameterTypes;
37+
_explicitStringLiteralTypes = copyFrom._explicitStringLiteralTypes;
3638
}
3739

3840
protected override RelationalOptionsExtension Clone()
3941
=> new FbOptionsExtension(this);
4042

4143
public virtual bool? ExplicitParameterTypes => _explicitParameterTypes;
44+
public virtual bool? ExplicitStringLiteralTypes => _explicitStringLiteralTypes;
4245

4346
public virtual FbOptionsExtension WithExplicitParameterTypes(bool explicitParameterTypes)
4447
{
@@ -47,12 +50,20 @@ public virtual FbOptionsExtension WithExplicitParameterTypes(bool explicitParame
4750
return clone;
4851
}
4952

53+
public virtual FbOptionsExtension WithExplicitStringLiteralTypes(bool explicitStringLiteralTypes)
54+
{
55+
var clone = (FbOptionsExtension)Clone();
56+
clone._explicitStringLiteralTypes = explicitStringLiteralTypes;
57+
return clone;
58+
}
59+
5060
public override long GetServiceProviderHashCode()
5161
{
5262
if (_serviceProviderHash == null)
5363
{
5464
_serviceProviderHash = (base.GetServiceProviderHashCode() * 397)
55-
^ (_explicitParameterTypes?.GetHashCode() ?? 0L);
65+
^ (_explicitParameterTypes?.GetHashCode() ?? 0L)
66+
^ (_explicitStringLiteralTypes?.GetHashCode() ?? 0L);
5667
}
5768
return _serviceProviderHash.Value;
5869
}

Provider/src/FirebirdSql.EntityFrameworkCore.Firebird/Infrastructure/Internal/IFbOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ namespace FirebirdSql.EntityFrameworkCore.Firebird.Infrastructure.Internal
2222
public interface IFbOptions : ISingletonOptions
2323
{
2424
bool ExplicitParameterTypes { get; }
25+
bool ExplicitStringLiteralTypes { get; }
2526
}
2627
}

Provider/src/FirebirdSql.EntityFrameworkCore.Firebird/Internal/FbOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public virtual void Initialize(IDbContextOptions options)
3030
var fbOptions = options.FindExtension<FbOptionsExtension>() ?? new FbOptionsExtension();
3131

3232
ExplicitParameterTypes = fbOptions.ExplicitParameterTypes ?? true;
33+
ExplicitStringLiteralTypes = fbOptions.ExplicitStringLiteralTypes ?? true;
3334
}
3435

3536
public virtual void Validate(IDbContextOptions options)
@@ -40,8 +41,13 @@ public virtual void Validate(IDbContextOptions options)
4041
{
4142
throw new InvalidOperationException($"A call was made to '{nameof(FbDbContextOptionsBuilder.WithExplicitParameterTypes)}' that changed an option that must be constant within a service provider, but Entity Framework is not building its own internal service provider. Either allow EF to build the service provider by removing the call to '{nameof(DbContextOptionsBuilder.UseInternalServiceProvider)}', or ensure that the configuration for '{nameof(FbDbContextOptionsBuilder.WithExplicitParameterTypes)}' does not change for all uses of a given service provider passed to '{nameof(DbContextOptionsBuilder.UseInternalServiceProvider)}'.");
4243
}
44+
if (ExplicitStringLiteralTypes != (fbOptions.ExplicitStringLiteralTypes ?? true))
45+
{
46+
throw new InvalidOperationException($"A call was made to '{nameof(FbDbContextOptionsBuilder.WithExplicitStringLiteralTypes)}' that changed an option that must be constant within a service provider, but Entity Framework is not building its own internal service provider. Either allow EF to build the service provider by removing the call to '{nameof(DbContextOptionsBuilder.UseInternalServiceProvider)}', or ensure that the configuration for '{nameof(FbDbContextOptionsBuilder.WithExplicitStringLiteralTypes)}' does not change for all uses of a given service provider passed to '{nameof(DbContextOptionsBuilder.UseInternalServiceProvider)}'.");
47+
}
4348
}
4449

4550
public virtual bool ExplicitParameterTypes { get; private set; }
51+
public virtual bool ExplicitStringLiteralTypes { get; private set; }
4652
}
4753
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ protected override string GenerateOperator(Expression expression)
173173
protected override Expression VisitConstant(ConstantExpression constantExpression)
174174
{
175175
var svalue = constantExpression.Value as string;
176-
var isVarcharHack = constantExpression.Type == typeof(string) && svalue?.Length > 0;
177-
if (isVarcharHack)
176+
var explicitVarcharPossible = constantExpression.Type == typeof(string) && svalue?.Length > 0;
177+
if (_fbOptions.ExplicitStringLiteralTypes && explicitVarcharPossible)
178178
{
179179
Sql.Append("CAST(");
180180
}
181181
base.VisitConstant(constantExpression);
182-
if (isVarcharHack)
182+
if (_fbOptions.ExplicitStringLiteralTypes && explicitVarcharPossible)
183183
{
184184
Sql.Append(" AS VARCHAR(");
185185
Sql.Append(svalue.Length);

0 commit comments

Comments
 (0)