-
Notifications
You must be signed in to change notification settings - Fork 25
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
OR from array #34
Comments
@EmileSpecs: There are two ways to do this (currently) with SQLBricks: (1) switch to an select().from('table').where({ this: 'test' }).and($in('that', ['test1', 'test2']));
// SELECT * FROM "table" WHERE this = 'test' AND that IN ('test1', 'test2')
select().from('table').where({ this: 'test' }).and(or(['test1', 'test2'].map(function(val) { return eq('that', val); })));
// SELECT * FROM "table" WHERE this = 'test' AND (that = 'test1' OR that = 'test2') Before 0.9.0, The idea of |
@EmileSpecs: Ah, @RandyPearson just reminded me that the select().from('table').where({ this: 'test', that: ['test1', 'test2']});
// SELECT * FROM "table" WHERE this = 'test' AND that = {'test1', 'test2'} |
@prust, thanks for the help, I'll try and see what I can figure out. Since I'm just using a single handler per statement type (SELECT, INSERT...) for all my basic CRUD operations and throwing an object at it, it's dynamic and does not have predetermined structure. So to use any of the initial suggestions isn't quite trivial. I'm using MYSQL so this syntax:
...unfortunately doesn't work. That would have been nice! It would still be a really nice feature to OR array values together or automatically construct the IN part. Like you said it's not a bad idea, and I think logically implicitly creating OR or IN for array values makes sense. An array as a value for a field can't really mean anything else... Is this a non-trivial feature to implement? |
No, it's trivial -- I'm just not sure it's unambiguous or that it wouldn't conflict with any legitimate uses of SQL arrays. For starters, I would encourage layering it on top of sql-bricks like this: function arraysToOrs(criteria) {
var where = and();
for (var col in criteria) {
var val = criteria[col];
var expr;
if (_.isArray(val))
expr = or(val.map(function(val) { return eq(col, val); }));
else
expr = eq(col, val);
where.expressions.push(expr);
}
return where;
} Then you can call select().from('table').where(arraysToOrs({ this: 'test', that: ['test1', 'test2']}));
// SELECT * FROM "table" WHERE this = 'test' AND (that = 'test1' OR that = 'test2') Or you can make this the default var stmts = ['select', 'update', 'insert', 'delete'];
stmts.forEach(function(stmt) {
var proto = sql[stmt].prototype;
var orig = proto.where;
proto.where = function(criteria) {
return orig.call(this, arraysToOrs(criteria));
};
}); I went ahead and added a couple of tests to the suite to ensure that this customization works and so that we'll know if it ever breaks: 89ff9d9. Note that Also note that in order to dynamically add to Let me know if this works for you! |
@EmileSpecs: Just realized that |
@prust: Fantastic! Thanks for your help, that function is working perfectly on the few tests I've written for my code. I did have to add:
Added that as first line of function otherwise if there were no WHERE criteria it would produce:
Also wondering while we're at it, the double quotes around the table name breaks the queries, MYSQL doesn't like double quotes. I fixed that with:
Don't know if that's correct or intended use but it took the quotes away... Thanks again for the help! |
Hi,
Works great, just one little addition that would make life much easier... Would it be difficult, given:
to generate:
I don't see how it's possible to use "or" when using dynamic data such as that.
If there is a way that I could do that without any changes please help!
Thanks!
The text was updated successfully, but these errors were encountered: