Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql IN Statemenr #70

Open
brescia123 opened this issue Apr 12, 2014 · 4 comments
Open

Sql IN Statemenr #70

brescia123 opened this issue Apr 12, 2014 · 4 comments

Comments

@brescia123
Copy link

How can I make a query with the IN statement?

@jdreesen
Copy link
Contributor

Afaik there is no helper which auto-expands the params for you.
But you could simply do this:

Query.many(YourModel.class, "SELECT * FROM YourModel WHERE column IN (?, ?)", "value1", "value2");

@brescia123
Copy link
Author

Ok, but if I don't know the number of parameters I can't use this. I have to manually construct the sql query, right?

@jdreesen
Copy link
Contributor

Yeah, something like this should do:

String[] params = new String[] {"value1", "value2", "value3", ...};

StringBuilder placeholder = new StringBuilder();
for (int i = 1; i <= params.length; i++) {
    placeholder.append("?");
    if (i < params.length) {
        placeholder.append(",");
    }
}

Query.many(YourModel.class, "SELECT * FROM YourModel WHERE column IN (" + placeholder.toString() + ")", params);

But maybe @emilsjolander comes with a better solution.

@emilsjolander
Copy link
Owner

@jdreesen is correct, this is the best solution currently. We should maybe look into adding a helper method for this. Of the top of my head i am thinking something a long the lines of

ArrayParam param = new ArrayParam(array)

param.commas() -> "?,?,?"
param.args() -> [obj1, obj2, obj3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants