class BabySqueel::DSL

Public Instance Methods

_(expr) click to toggle source

Create a Grouping node. This allows you to set balanced pairs of parentheses around your SQL.

Arguments

  • expr - The expression to group.

Example

Post.where.has{_([summary, description]).in(...)}
#=> SELECT "posts".* FROM "posts" WHERE ("posts"."summary", "posts"."description") IN (...)"
Post.select{[id, _(Comment.where.has{post_id == posts.id}.selecting{COUNT(id)})]}.as('comment_count')}
#=> SELECT "posts"."id", (SELECT COUNT("comments"."id") FROM "comments" WHERE "comments.post_id" = "posts"."id") AS "comment_count" FROM "posts"
# File lib/baby_squeel/dsl.rb, line 32
def _(expr)
  expr = Arel.sql(expr.to_sql) if expr.is_a? ::ActiveRecord::Relation
  Nodes.wrap Arel::Nodes::Grouping.new(expr)
end
exists(relation) click to toggle source

Generate an EXISTS subselect from an ActiveRecord::Relation

Arguments

  • relation - An ActiveRecord::Relation

Example

Post.where.has { exists Post.where(id: 1) }
# File lib/baby_squeel/dsl.rb, line 61
def exists(relation)
  func 'EXISTS', sql(relation.to_sql)
end
func(name, *args) click to toggle source

Create a SQL function. See Arel::Nodes::NamedFunction.

Arguments

  • name - The name of a SQL function (ex. coalesce).

  • args - The arguments to be passed to the SQL function.

Example

Post.selecting { func('coalesce', id, 1) }
#=> SELECT COALESCE("posts"."id", 1) FROM "posts"
# File lib/baby_squeel/dsl.rb, line 48
def func(name, *args)
  Nodes.wrap Arel::Nodes::NamedFunction.new(name.to_s, args)
end
not_exists(rel) click to toggle source

Generate a NOT EXISTS subselect from an ActiveRecord::Relation

Arguments

  • relation - An ActiveRecord::Relation

Example

Post.where.has { not_exists Post.where(id: 1) }
# File lib/baby_squeel/dsl.rb, line 74
def not_exists(rel)
  func 'NOT EXISTS', sql(rel.to_sql)
end
quoted(value) click to toggle source

Quotes a string and marks it as SQL

# File lib/baby_squeel/dsl.rb, line 84
def quoted(value)
  sql _scope.connection.quote(value)
end
sql(value) click to toggle source

See Arel::sql

# File lib/baby_squeel/dsl.rb, line 79
def sql(value)
  Nodes.wrap ::Arel.sql(value)
end

Private Instance Methods

resolver() click to toggle source
# File lib/baby_squeel/dsl.rb, line 90
def resolver
  @resolver ||= Resolver.new(self, [:function, :column, :association])
end