Skip to content

Custom function cuts characters #468

@S4TNEW

Description

@S4TNEW

I am using custom functions for simple string manipulations but receive some weird results.

Background: I learned that SQLite doesn't handle lower/upper for characters outside the ASCII. So I wrote custom functions for those cases that look like this:

static let s4tToLower: (Expression<String>) -> Expression<String?> = (
    try! db.createFunction(function: "s4tToLower")
    { value in
        return value.lowercaseString
    }
)

Then I use it in a query:

var vehicleQuery = Table(DatabaseTables.Vehicle)
let condition = FLFDbFunctions.s4tToLower(FLFVehicle.kLicense) == licenseNo.lowercaseString
vehicleQuery = vehicleQuery.filter(condition)

Problem: In some cases the searched string contains unicode characters (eg the German umlauts "äöü").
The problem in those cases is that the custom function returns the correct lowercaseString (verified by debugging in Xcode), but SQLite gives a right-side trimmed string.
Please see the following example code:

let testString = "TÖL-AA 12"
let test1: String = db.scalar("select ? ", testString.lowercaseString) as! String
print(test1)
//töl-aa 12
let test2: String = db.scalar("select s4tToLower(?) ", testString) as! String
print(test2)
//töl-aa 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions