Class: Hayfork::StatementBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/hayfork/statement_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(haystack, relation, attributes: {}) ⇒ StatementBuilder

Returns a new instance of StatementBuilder.



7
8
9
10
11
12
13
# File 'lib/hayfork/statement_builder.rb', line 7

def initialize(haystack, relation, attributes: {})
  @haystack = haystack
  @relation = relation
  @attributes = attributes
  @statements = []
  @row = model.arel_table
end

Instance Attribute Details

#rowObject (readonly)

Returns the value of attribute row.



5
6
7
# File 'lib/hayfork/statement_builder.rb', line 5

def row
  @row
end

Instance Method Details

#insert(field, options = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/hayfork/statement_builder.rb', line 29

def insert(field, options={})
  Statement.new(haystack, relation, field, options).tap do |statement|
    statement.merge(attributes) if attributes.any?
    statements << statement
  end
end

#joins(join_value, &block) ⇒ Object

reject SQL literals?



20
21
22
# File 'lib/hayfork/statement_builder.rb', line 20

def joins(join_value, &block) # reject SQL literals?
  apply? Hayfork.join(relation, join_value), &block
end

#set(key, value) ⇒ Object



16
17
18
# File 'lib/hayfork/statement_builder.rb', line 16

def set(key, value)
  @attributes.merge!(key => value)
end

#to_aObject



52
53
54
# File 'lib/hayfork/statement_builder.rb', line 52

def to_a
  statements
end

#to_delete_sqlObject



47
48
49
# File 'lib/hayfork/statement_builder.rb', line 47

def to_delete_sql
  statements.map { |statement| "    " << statement.delete.to_sql }.join.strip
end

#to_insert_sql(**args) ⇒ Object



37
38
39
# File 'lib/hayfork/statement_builder.rb', line 37

def to_insert_sql(**args)
  statements.map { |statement| "    " << statement.insert.to_sql(**args) }.join.strip
end

#to_update_sqlObject



41
42
43
44
45
# File 'lib/hayfork/statement_builder.rb', line 41

def to_update_sql
  updates = statements.select(&:may_change_on_update?)
  return "-- nothing to update" if updates.empty?
  updates.map { |statement| "    " << statement.update.to_sql.lstrip }.join.strip
end

#where(*args, &block) ⇒ Object

reject SQL literals?



24
25
26
# File 'lib/hayfork/statement_builder.rb', line 24

def where(*args, &block) # reject SQL literals?
  apply? relation.where(*args), &block
end