Class: Seaquel::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/seaquel/statement.rb,
lib/seaquel/statement/join.rb

Overview

Root node for an sql statement.

Defined Under Namespace

Classes: Join

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression_convertor) ⇒ Statement

Returns a new instance of Statement.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/seaquel/statement.rb', line 34

def initialize expression_convertor
  @expression_convertor = expression_convertor

  @from = list()
  @project = list()
  @order_by = list()
  @where = AST::JoinOp.new(:and, [])
  @set = list()
  @values = list()
  @fields = column_list()
  @joins = []
  @having = AST::JoinOp.new(:and, [])
  @group_by = list()
end

Instance Attribute Details

#expression_convertorObject (readonly)

Returns the value of attribute expression_convertor.



16
17
18
# File 'lib/seaquel/statement.rb', line 16

def expression_convertor
  @expression_convertor
end

#fieldsObject (readonly)

Returns the value of attribute fields.



23
24
25
# File 'lib/seaquel/statement.rb', line 23

def fields
  @fields
end

#fromObject (readonly)

Returns the value of attribute from.



18
19
20
# File 'lib/seaquel/statement.rb', line 18

def from
  @from
end

#group_byObject (readonly)

Returns the value of attribute group_by.



32
33
34
# File 'lib/seaquel/statement.rb', line 32

def group_by
  @group_by
end

#havingObject (readonly)

Returns the value of attribute having.



27
28
29
# File 'lib/seaquel/statement.rb', line 27

def having
  @having
end

#joinsObject (readonly)

Returns the value of attribute joins.



24
25
26
# File 'lib/seaquel/statement.rb', line 24

def joins
  @joins
end

#limitObject (readonly)

Returns the value of attribute limit.



25
26
27
# File 'lib/seaquel/statement.rb', line 25

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



26
27
28
# File 'lib/seaquel/statement.rb', line 26

def offset
  @offset
end

#order_byObject (readonly)

Returns the value of attribute order_by.



31
32
33
# File 'lib/seaquel/statement.rb', line 31

def order_by
  @order_by
end

#projectObject (readonly)

These are overwritten, not appended to.



30
31
32
# File 'lib/seaquel/statement.rb', line 30

def project
  @project
end

#setObject (readonly)

Returns the value of attribute set.



20
21
22
# File 'lib/seaquel/statement.rb', line 20

def set
  @set
end

#targetObject (readonly)

Returns the value of attribute target.



21
22
23
# File 'lib/seaquel/statement.rb', line 21

def target
  @target
end

#valuesObject (readonly)

Returns the value of attribute values.



22
23
24
# File 'lib/seaquel/statement.rb', line 22

def values
  @values
end

#whereObject (readonly)

Returns the value of attribute where.



19
20
21
# File 'lib/seaquel/statement.rb', line 19

def where
  @where
end

Instance Method Details

#convert(exp) ⇒ Object

Turns an expression into SQL



95
96
97
# File 'lib/seaquel/statement.rb', line 95

def convert exp
  expression_convertor.sql(exp).toplevel
end

#join(tables) ⇒ Join

Produces a join clause and adds it to the joins list.

Parameters:

  • tables (Array<Object>)

    a list of tables to join to

Returns:

  • (Join)

    Join subobject.



73
74
75
76
77
78
# File 'lib/seaquel/statement.rb', line 73

def join tables
  join = Join.new(tables)
  joins << join

  join
end

#set_limit(n) ⇒ Object



49
50
51
# File 'lib/seaquel/statement.rb', line 49

def set_limit n
  @limit = n
end

#set_offset(n) ⇒ Object



52
53
54
# File 'lib/seaquel/statement.rb', line 52

def set_offset n
  @offset = n
end

#set_target(table) ⇒ Object



64
65
66
# File 'lib/seaquel/statement.rb', line 64

def set_target table
  @target = table
end

#set_type(type) ⇒ Object

Allows to determine the type of statement generated.

Parameters:

  • type ({:update, :select, :delete})


60
61
62
# File 'lib/seaquel/statement.rb', line 60

def set_type type
  @type = type
end

#to_s(variant = :compact) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/seaquel/statement.rb', line 80

def to_s variant=:compact
  case @type 
    when :update
      generate_update(variant)
    when :insert
      generate_insert(variant)
    when :delete
      generate_delete(variant)
  else
    generate_select(variant)
  end
end