[#727] Simulate RPAD and LPAD in SQLite - Some improvements to support

padding by more than one character
This commit is contained in:
Lukas Eder 2012-07-22 13:01:42 +02:00
parent 55dd62d8cf
commit b60b575069
2 changed files with 26 additions and 4 deletions

View File

@ -79,9 +79,20 @@ class Lpad extends AbstractFunction<String> {
// This beautiful expression was contributed by "Ludo", here:
// http://stackoverflow.com/questions/6576343/how-to-simulate-lpad-rpad-with-sqlite
case SQLITE: {
return Factory.field("replace(replace(substr(quote(zeroblob(({0} + 1) / 2)), 3, ({1} - length({2}))), '''', ''), '0', {3}) || {4}",
return Factory.field(
"substr(" +
"replace(" +
"replace(" +
"substr(" +
"quote(" +
"zeroblob((({0} - length({1}) - 1 + length({2})) / length({3}) + 1) / 2)" +
"), 3" +
"), '''', ''" +
"), '0', {4}" +
"), 1, ({5} - length({6}))" +
") || {7}",
String.class,
length, length, field, character, field);
length, field, character, character, character, length, field, field);
}
default: {

View File

@ -79,9 +79,20 @@ class Rpad extends AbstractFunction<String> {
// This beautiful expression was contributed by "Ludo", here:
// http://stackoverflow.com/questions/6576343/how-to-simulate-lpad-rpad-with-sqlite
case SQLITE: {
return Factory.field("{0} || replace(replace(substr(quote(zeroblob(({1} + 1) / 2)), 3, ({2} - length({3}))), '''', ''), '0', {4})",
return Factory.field(
"{0} || substr(" +
"replace(" +
"replace(" +
"substr(" +
"quote(" +
"zeroblob((({1} - length({2}) - 1 + length({3})) / length({4}) + 1) / 2)" +
"), 3" +
"), '''', ''" +
"), '0', {5}" +
"), 1, ({6} - length({7}))" +
")",
String.class,
field, length, length, field, character);
field, length, field, character, character, character, length, field);
}