Giter Club home page Giter Club logo

Comments (8)

hiddentao avatar hiddentao commented on July 26, 2024

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.

fdeandao avatar fdeandao commented on July 26, 2024

Let me check thanks

from squel.

drewish avatar drewish commented on July 26, 2024

+1 for exists.

from squel.

drewish avatar drewish commented on July 26, 2024

@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.

hiddentao avatar hiddentao commented on July 26, 2024

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.

hiddentao avatar hiddentao commented on July 26, 2024

Am closing this due to lack of interest. I think the present solution (as outlined in the last comment) suffices.

from squel.

raddevon avatar raddevon commented on July 26, 2024

@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.

hiddentao avatar hiddentao commented on July 26, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.