Class: BabySqueel::DSL

Inherits:
Relation show all
Defined in:
lib/baby_squeel/dsl.rb

Instance Attribute Summary

Attributes inherited from Relation

#_scope

Attributes inherited from Table

#_join, #_on, #_table

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Relation

#association, #initialize, #sift

Methods inherited from Table

#[], #_arel, #alias, #alias!, #alias?, #as, #evaluate, #find_alias, #initialize, #inner, #inner!, #on, #on!, #outer, #outer!

Constructor Details

This class inherits a constructor from BabySqueel::Relation

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BabySqueel::Table

Class Method Details

.evaluate(scope, &block) ⇒ Object

:nodoc:



8
9
10
# File 'lib/baby_squeel/dsl.rb', line 8

def evaluate(scope, &block) # :nodoc:
  Nodes.unwrap new(scope).evaluate(&block)
end

.evaluate_sifter(scope, *args, &block) ⇒ Object

:nodoc:



12
13
14
15
16
# File 'lib/baby_squeel/dsl.rb', line 12

def evaluate_sifter(scope, *args, &block) # :nodoc:
  evaluate scope do |root|
    root.instance_exec(*args, &block)
  end
end

Instance Method Details

#_(expr) ⇒ Object

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"


32
33
34
35
# 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) ⇒ Object

Generate an EXISTS subselect from an ActiveRecord::Relation

Arguments

  • relation - An ActiveRecord::Relation

Example

Post.where.has { exists Post.where(id: 1) }


61
62
63
# File 'lib/baby_squeel/dsl.rb', line 61

def exists(relation)
  func 'EXISTS', sql(relation.to_sql)
end

#func(name, *args) ⇒ Object

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"


48
49
50
# 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) ⇒ Object

Generate a NOT EXISTS subselect from an ActiveRecord::Relation

Arguments

  • relation - An ActiveRecord::Relation

Example

Post.where.has { not_exists Post.where(id: 1) }


74
75
76
# File 'lib/baby_squeel/dsl.rb', line 74

def not_exists(rel)
  func 'NOT EXISTS', sql(rel.to_sql)
end

#quoted(value) ⇒ Object

Quotes a string and marks it as SQL



84
85
86
# File 'lib/baby_squeel/dsl.rb', line 84

def quoted(value)
  sql _scope.connection.quote(value)
end

#sql(value) ⇒ Object

See Arel::sql



79
80
81
# File 'lib/baby_squeel/dsl.rb', line 79

def sql(value)
  Nodes.wrap ::Arel.sql(value)
end