Comments (8)
I imagine the query within the exists can be constructed using squel.select()
builder. Then you could just use .where('exists(' + existsQuery + ')')
in the main query.
As for implementing the EXISTS
keyword as a first-class item the way I would do is to implement a WhereExists
block which exposed a .where_exists
method. This block would be very similar to the Where
block but would be specifically for the syntax you've described. So it would take a single parameter (a QueryBuilder
instance). If .where_exists
got called more than once then it would combine the multiple EXISTS
invocations using the AND
keyword as you've shown in your example query above.
What do you think?
from squel.
Let me check thanks
from squel.
+1 for exists.
from squel.
@hiddentao does it really make sense to use a separate WhereExists block? It seems like you'd want the option to mix field criteria with subqueries, e.g. WHERE (a.deleted_at IS NULL) AND (EXISTS (SELECT sub.id FROM ...))
. I suppose they could share a @wheres
but otherwise it seems like you'd have to worry about getting a single WHERE
and would loose the ability to do an OR (EXISTS (...))
from squel.
Good point. And yet in that case the Where
block already allows you to supply an arbitrary string so you could do something like this:
...
.where('a.deleted is NULL')
.where('EXISTS(' + squel.select().from('blah') + ')')
.toString()
Is this good enough?
from squel.
Am closing this due to lack of interest. I think the present solution (as outlined in the last comment) suffices.
from squel.
@hiddentao Sorry to resurrect after so long. If I want to use data from the user inside my EXISTS
subquery, is there a way I can parameterize that and join it with the rest of my parameterized query to help me avoid injection attacks? (My EXISTS
will be in a .field()
.)
from squel.
@raddevon Squel 5 has a more flexible architecture which allows for unlimited parameterized query nesting, so with the latest squel (5.4.2) you should be able to do something like:
squel.select()
.from('table')
.where('b = ?', 4)
.where(squel.str('EXISTS(?)', squel.select().from('blah').where('a = ?', 5)))
.toParam()
/*
{
text: "SELECT * FROM table WHERE (b = ?) AND (EXISTS((SELECT * FROM blah WHERE (a = ?))))",
values: [4,5]
}
*/
from squel.
Related Issues (20)
- Add option to add column/table name as a parameter HOT 1
- Add semantic-comparison logic for queries/expressions
- Feature request: Placeholders in the squel.case(...) expression?
- MSSQL Boolean type
- .group().having() is not a function HOT 1
- string escape is incorrect, especially when string contains \n or ', which may cause SQL injection HOT 1
- How to select into array? HOT 1
- Scalar values aliasing
- squel.select().from is undefined after minimizing HOT 6
- Numbered parameters in postgres flavour are substituted inside a string literal
- Incorrect syntax near '`'.
- SQL Server Update Fails with Trigger ? HOT 3
- Use a normal SQL string HOT 2
- Can you get the result value from an object like the result map of mybtis?
- SQL Server Select HOT 1
- Interested in Becoming an Active co-maintainer HOT 1
- How to use IGNORE in INSERT in mysql?
- Mark as deprecated HOT 1
- Grouping conditions together in expression HOT 1
- Lateral Joins
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from squel.