Class: QueryGenerator::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/query_generator/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, strict: true) ⇒ Table

Returns a new instance of Table.



4
5
6
7
8
9
10
# File 'lib/query_generator/table.rb', line 4

def initialize(table, strict: true)
  @table = table
  @query = ''
  @select_used = false
  @where_used = false
  @strict = strict
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/query_generator/table.rb', line 3

def query
  @query
end

#select_usedObject (readonly)

Returns the value of attribute select_used.



3
4
5
# File 'lib/query_generator/table.rb', line 3

def select_used
  @select_used
end

#strictObject (readonly)

Returns the value of attribute strict.



3
4
5
# File 'lib/query_generator/table.rb', line 3

def strict
  @strict
end

#tableObject (readonly)

Returns the value of attribute table.



3
4
5
# File 'lib/query_generator/table.rb', line 3

def table
  @table
end

#where_usedObject (readonly)

Returns the value of attribute where_used.



3
4
5
# File 'lib/query_generator/table.rb', line 3

def where_used
  @where_used
end

Instance Method Details

#group_by(*columns) ⇒ Object



38
39
40
41
# File 'lib/query_generator/table.rb', line 38

def group_by(*columns)
  @query += " GROUP BY #{columns.join(',')}"
  self
end

#left_outer_join(join_table, base, to) ⇒ Object



61
62
63
# File 'lib/query_generator/table.rb', line 61

def left_outer_join(join_table, base, to)
  "LEFT #{outer_join(join_table, base, to)}"
end

#or(*conds) ⇒ Object

Raises:



30
31
32
33
34
35
36
# File 'lib/query_generator/table.rb', line 30

def or(*conds)
  # NOTE: OR句はWHERE句のあとに使うので単独では使わせない
  raise WhereDoesNotUsed unless where_used

  @query += " OR #{expand_conds(conds, 'OR')}"
  self
end

#order_by(by, order_type = :ASC) ⇒ Object

Raises:



43
44
45
46
47
48
# File 'lib/query_generator/table.rb', line 43

def order_by(by, order_type = :ASC)
  raise NotIncludeType unless [:ASC, :DESC].include?(order_type.upcase)

  @query += " ORDER BY #{by} #{order_type.upcase}"
  self
end

#right_outer_join(join_table, base, to) ⇒ Object



65
66
67
# File 'lib/query_generator/table.rb', line 65

def right_outer_join(join_table, base, to)
  "RIGHT #{outer_join(join_table, base, to)}"
end

#runObject

Raises:



50
51
52
53
# File 'lib/query_generator/table.rb', line 50

def run
  raise StrictModeError if !where_used && strict
  # FIXME: 各DBへSQLを実行しその結果を取得する
end

#select(*columns) ⇒ Object

Raises:



12
13
14
15
16
17
18
# File 'lib/query_generator/table.rb', line 12

def select(*columns)
  raise SelectAlreadyUsed if select_used

  @query = "SELECT #{result_columns(columns)} FROM #{table}"
  @select_used = true
  self
end

#to_sObject

Raises:



55
56
57
58
59
# File 'lib/query_generator/table.rb', line 55

def to_s
  raise StrictModeError if !where_used && strict

  puts query
end

#where(*conds) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/query_generator/table.rb', line 20

def where(*conds)
  @query += if where_used
              " AND #{expand_conds(conds)}"
            else
              " WHERE #{expand_conds(conds)}"
            end
  @where_used = true
  self
end