When we create a table like this:
CREATE TABLE TEST (
TestId INTEGER NOT NULL,
TestText TEXT NOT NULL COLLATE NOCASE,
SecondId INTEGER NOT NULL,
PRIMARY KEY (TestId)
);
and query like this:
TEST_QUERY:
SELECT *
FROM TEST
WHERE SecondId = ? AND TestText LIKE ? ESCAPE '\' COLLATE NOCASE;
library will generate code like this:
public SqlDelightStatement TEST_QUERY(long SecondId, long arg2) {
List<String> args = new ArrayList<String>();
int currentIndex = 1;
StringBuilder query = new StringBuilder();
query.append("SELECT *\n"
+ "FROM TEST\n"
+ "WHERE SecondId = ");
query.append(SecondId);
query.append(" AND TestText LIKE ");
query.append(arg2);
query.append(" ESCAPE '\\' COLLATE NOCASE");
return new SqlDelightStatement(query.toString(), args.toArray(new String[args.size()]), Collections.<String>singleton("TEST"));
}
And now the question is: Why second parameter is long instead of String?
I also noticed that if I add brackets around LIKE part problem will dissapear.
In that case query and code will look like this:
TEST_QUERY:
SELECT *
FROM TEST
WHERE SecondId = ? AND (TestText LIKE ? ESCAPE '\' COLLATE NOCASE);
public SqlDelightStatement TEST_QUERY(long SecondId, @NonNull String TestText) {
List<String> args = new ArrayList<String>();
int currentIndex = 1;
StringBuilder query = new StringBuilder();
query.append("SELECT *\n"
+ "FROM TEST\n"
+ "WHERE SecondId = ");
query.append(SecondId);
query.append(" AND (TestText LIKE ");
query.append('?').append(currentIndex++);
args.add(TestText);
query.append(" ESCAPE '\\' COLLATE NOCASE)");
return new SqlDelightStatement(query.toString(), args.toArray(new String[args.size()]), Collections.<String>singleton("TEST"));
}
Seems like this is not completely resolved yet. I'm using the latest version at the moment: 0.6.1. Having this query:
SELECT *
FROM User
WHERE type = ?
AND first_name LIKE ?
AND last_name LIKE ?;
results in the following method definition:
@Nullable String type, long arg2, @Nullable String last_name
while it should be:
@Nullable String type, @Nullable String first_name, @Nullable String last_name
If I change the last AND to OR (as my original query was) like this:
SELECT *
FROM User
WHERE type = ?
AND first_name LIKE ?
OR last_name LIKE ?
again the result is the wrong method definition:
@Nullable String type, long arg2, @Nullable String last_name
Removing the second LIKE generates the expected result.
why are grammars so hard
I am using this workaround:
``sql
SELECT *
FROM User
WHERE type = ?
AND (1<>1 OR first_name LIKE ?)
AND (1<>1 OR last_name LIKE ?);
Confirmed fixed in 802e67d72e97e8f54c716699978178a52adca88e
Most helpful comment
I am using this workaround:
``
sql SELECT * FROM User WHERE type = ? AND (1<>1 OR first_name LIKE ?) AND (1<>1 OR last_name LIKE ?);