Class: QueryGenerator::Table
- Inherits:
-
Object
- Object
- QueryGenerator::Table
- Defined in:
- lib/query_generator/table.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#select_used ⇒ Object
readonly
Returns the value of attribute select_used.
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#where_used ⇒ Object
readonly
Returns the value of attribute where_used.
Instance Method Summary collapse
- #group_by(*columns) ⇒ Object
-
#initialize(table, strict: true) ⇒ Table
constructor
A new instance of Table.
- #left_outer_join(join_table, base, to) ⇒ Object
- #or(*conds) ⇒ Object
- #order_by(by, order_type = :ASC) ⇒ Object
- #right_outer_join(join_table, base, to) ⇒ Object
- #run ⇒ Object
- #select(*columns) ⇒ Object
- #to_s ⇒ Object
- #where(*conds) ⇒ Object
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
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/query_generator/table.rb', line 3 def query @query end |
#select_used ⇒ Object (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 |
#strict ⇒ Object (readonly)
Returns the value of attribute strict.
3 4 5 |
# File 'lib/query_generator/table.rb', line 3 def strict @strict end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
3 4 5 |
# File 'lib/query_generator/table.rb', line 3 def table @table end |
#where_used ⇒ Object (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
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 #{(conds, 'OR')}" self end |
#order_by(by, order_type = :ASC) ⇒ Object
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 |
#run ⇒ Object
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
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_s ⇒ Object
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 #{(conds)}" else " WHERE #{(conds)}" end @where_used = true self end |