We have a piece of code in which we are trying to match a template with data in a database.
We use ServiceStack.OrmLite for our SQLite database.
So, for example, taking into account the following entries:
ColA ColB
----- ---------
ABC ABC_Long
GHI GHI_Long
GHIP GHIP_Long
We use the predicate as:
var result = db.Select(x => x.ColA.StartsWith("GHI_"));
Everything works fine until we have a search pattern that includes a special SQL character, such as "%" or "_", for example, given the search pattern "GHI _":
The expected line should be:
GHI GHI_Long
However, we get:
GHI GHI_Long
GHIP GHIP_Long
Due to the fact that ORMLite does not escape from the special character and generates below SQL:
SELECT * FROM myTable WHERE UPPER(colA) LIKE 'GHI_%' OR UPPER(colB) LIKE 'GHI_%';
Instead of a properly shielded version, which should be:
SELECT * FROM myTable WHERE UPPER(colA) LIKE 'GHI\_%' OR UPPER(colB) LIKE 'GHI\_%' ESCAPE '\';
Can you come up with a way to solve this problem?