Skip to content

Commit 0c0b1d1

Browse files
committed
MySQL: Fix Pad operations
LPAD/RPAD functions of MySQL requre actual character, so explicitly define whitespace as character.
1 parent bf44a10 commit 0c0b1d1

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Compiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public override void Visit(SqlFunctionCall node)
160160
return;
161161
case SqlFunctionType.PadLeft:
162162
case SqlFunctionType.PadRight:
163-
SqlHelper.GenericPad(node).AcceptVisitor(this);
163+
SqlHelper.GenericPad(node, true).AcceptVisitor(this);
164164
return;
165165
case SqlFunctionType.Rand:
166166
SqlDml.FunctionCall(translator.TranslateToString(SqlFunctionType.Rand)).AcceptVisitor(this);

Orm/Xtensive.Orm/Sql/SqlHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public static string TimeSpanToString(TimeSpan value, string format)
331331
days, hours, minutes, seconds, milliseconds);
332332
}
333333

334-
public static SqlExpression GenericPad(SqlFunctionCall node)
334+
public static SqlExpression GenericPad(SqlFunctionCall node, bool padStringRequired = false)
335335
{
336336
string paddingFunction;
337337
switch (node.FunctionType) {
@@ -346,9 +346,12 @@ public static SqlExpression GenericPad(SqlFunctionCall node)
346346
}
347347
var operand = node.Arguments[0];
348348
var result = SqlDml.Case();
349-
result.Add(
350-
SqlDml.CharLength(operand) < node.Arguments[1],
351-
SqlDml.FunctionCall(paddingFunction, node.Arguments));
349+
var lenghtCheck = SqlDml.CharLength(operand) < node.Arguments[1];
350+
var paddingItself = (padStringRequired && node.Arguments.Count < 3)
351+
? SqlDml.FunctionCall(paddingFunction, operand, node.Arguments[1], SqlDml.Literal(" "))
352+
: SqlDml.FunctionCall(paddingFunction, node.Arguments);
353+
354+
_ = result.Add(lenghtCheck, paddingItself);
352355
result.Else = operand;
353356
return result;
354357
}

0 commit comments

Comments
 (0)