Skip to content

Commit 04a6a75

Browse files
committed
String literals handling.
1 parent 53987fb commit 04a6a75

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

Provider/src/FirebirdSql.EntityFrameworkCore.Firebird.Tests/Migrations/MigrationsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public void CreateTable()
122122
""Id_Sequence"" INTEGER NOT NULL,
123123
""EmployerId"" INTEGER,
124124
""SSN"" char(11),
125-
""DEF_O"" VARCHAR(20) DEFAULT CAST(_UTF8'test' as VARCHAR(4) CHARACTER SET UTF8),
126-
""DEF_S"" VARCHAR(20) DEFAULT ('x'),
125+
""DEF_O"" VARCHAR(20) DEFAULT _UTF8'test',
126+
""DEF_S"" VARCHAR(20) DEFAULT 'x',
127127
PRIMARY KEY (""Id""),
128128
UNIQUE (""SSN""),
129129
FOREIGN KEY (""EmployerId"") REFERENCES ""Companies"" (""Id"") ON UPDATE NO ACTION ON DELETE NO ACTION

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* All Rights Reserved.
1414
*/
1515

16-
//$Authors = Jiri Cincura ([email protected]), Jean Ressouche, Rafael Almeida ([email protected])
16+
//$Authors = Jiri Cincura ([email protected])
1717

1818
using System.Linq;
1919
using System.Linq.Expressions;
@@ -170,6 +170,24 @@ protected override string GenerateOperator(Expression expression)
170170
return base.GenerateOperator(expression);
171171
}
172172

173+
protected override Expression VisitConstant(ConstantExpression constantExpression)
174+
{
175+
var svalue = constantExpression.Value as string;
176+
var isVarcharHack = constantExpression.Type == typeof(string) && svalue?.Length > 0;
177+
if (isVarcharHack)
178+
{
179+
Sql.Append("CAST(");
180+
}
181+
base.VisitConstant(constantExpression);
182+
if (isVarcharHack)
183+
{
184+
Sql.Append(" AS VARCHAR(");
185+
Sql.Append(svalue.Length);
186+
Sql.Append(") CHARACTER SET UTF8)");
187+
}
188+
return constantExpression;
189+
}
190+
173191
public virtual Expression VisitSubstring(FbSubstringExpression substringExpression)
174192
{
175193
Sql.Append("SUBSTRING(");

Provider/src/FirebirdSql.EntityFrameworkCore.Firebird/Storage/Internal/FbStringTypeMapping.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* All Rights Reserved.
1414
*/
1515

16-
//$Authors = Jiri Cincura ([email protected]), Jean Ressouche, Rafael Almeida ([email protected])
16+
//$Authors = Jiri Cincura ([email protected])
1717

1818
using System.Data.Common;
1919
using FirebirdSql.Data.FirebirdClient;
@@ -39,11 +39,9 @@ protected override void ConfigureParameter(DbParameter parameter)
3939
protected override string GenerateNonNullSqlLiteral(object value)
4040
{
4141
var svalue = (string)value;
42-
if (svalue == string.Empty)
43-
return "''";
4442
return IsUnicode
45-
? $"CAST(_UTF8'{EscapeSqlLiteral(svalue)}' as VARCHAR({svalue.Length}) CHARACTER SET UTF8)"
46-
: $"CAST('{EscapeSqlLiteral(svalue)}' as VARCHAR({svalue.Length}))";
43+
? $"_UTF8'{EscapeSqlLiteral(svalue)}'"
44+
: $"'{EscapeSqlLiteral(svalue)}'";
4745
}
4846
}
4947
}

0 commit comments

Comments
 (0)