Skip to content

Instantly share code, notes, and snippets.

@jvans1
Last active August 29, 2015 14:03
Show Gist options
  • Save jvans1/5b1f1673d3508ba1e03e to your computer and use it in GitHub Desktop.
Save jvans1/5b1f1673d3508ba1e03e to your computer and use it in GitHub Desktop.
SqlCondition.new(operator, left_side, right_side).to_query(scope)
input = "(attendees.id = 1 OR id < 2)"
sql_condition1 = SqlCondition.new('=' , "attendees.id", 1).to_query(Ticket)
#leverage SqlAttribute in this class
sql_condition1.to_query #=> "select * from tickets WHERE tickets.id < 1"
sql_condition2 = SqlCondition.new('=' , "attendees.id", 1).to_query(Ticket)
sql_condition2.to_query(Ticket) #=> "select * from tickets JOIN companies on companies.id = tickets.company_id JOIN attendees on attendees.company_id = companies.id WHERE attendees.id = 1"
sql_condition3 = SqlCondition.new('OR' , sql_condition1 , sql_condition2)
sql_condition3.to_query(Ticket) #=> "select * from tickets JOIN companies on companies.id = tickets.company_id JOIN attendees on attendees.company_id = companies.id WHERE (attendees.id = 1 OR tickets.id < 2)"
@binarylogic
Copy link

I like. Instead of a string for the column I would pass an actual column object, which we already have. This way we can know what type the column is, we can build in all kinds of information into the object, etc. Again, I'd like to use our already existing Column class. With the operator, I'd do the same. Make an operator class and have each type of Operator extend a base class. The condition can be plain ruby objects like you, and I think it should be, we shouldn't be doing anything crazy with conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment