Class: Rooq::DSL::SelectQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/rooq/dsl/select_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ SelectQuery

Returns a new instance of SelectQuery.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rooq/dsl/select_query.rb', line 11

def initialize(fields)
  @selected_fields = fields.flatten.freeze
  @from_table = nil
  @table_alias = nil
  @conditions = nil
  @order_specs = []
  @limit_value = nil
  @offset_value = nil
  @joins = []
  @distinct_flag = false
  @group_by_fields = []
  @having_condition = nil
  @ctes = []
  @for_update_flag = false
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def conditions
  @conditions
end

#ctesObject (readonly)

Returns the value of attribute ctes.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def ctes
  @ctes
end

#distinct_flagObject (readonly)

Returns the value of attribute distinct_flag.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def distinct_flag
  @distinct_flag
end

#for_update_flagObject (readonly)

Returns the value of attribute for_update_flag.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def for_update_flag
  @for_update_flag
end

#from_tableObject (readonly)

Returns the value of attribute from_table.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def from_table
  @from_table
end

#group_by_fieldsObject (readonly)

Returns the value of attribute group_by_fields.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def group_by_fields
  @group_by_fields
end

#having_conditionObject (readonly)

Returns the value of attribute having_condition.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def having_condition
  @having_condition
end

#joinsObject (readonly)

Returns the value of attribute joins.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def joins
  @joins
end

#limit_valueObject (readonly)

Returns the value of attribute limit_value.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def limit_value
  @limit_value
end

#offset_valueObject (readonly)

Returns the value of attribute offset_value.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def offset_value
  @offset_value
end

#order_specsObject (readonly)

Returns the value of attribute order_specs.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def order_specs
  @order_specs
end

#selected_fieldsObject (readonly)

Returns the value of attribute selected_fields.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def selected_fields
  @selected_fields
end

#table_aliasObject (readonly)

Returns the value of attribute table_alias.



6
7
8
# File 'lib/rooq/dsl/select_query.rb', line 6

def table_alias
  @table_alias
end

Instance Method Details

#add_join(join) ⇒ Object



105
106
107
# File 'lib/rooq/dsl/select_query.rb', line 105

def add_join(join)
  dup_with(joins: @joins + [join])
end

#and_where(condition) ⇒ Object



43
44
45
# File 'lib/rooq/dsl/select_query.rb', line 43

def and_where(condition)
  where(condition)
end

#as_subquery(alias_name) ⇒ Object

Convert to subquery for use in FROM or conditions



129
130
131
# File 'lib/rooq/dsl/select_query.rb', line 129

def as_subquery(alias_name)
  Subquery.new(self, alias_name)
end

#cross_join(table, as: nil) ⇒ Object



100
101
102
103
# File 'lib/rooq/dsl/select_query.rb', line 100

def cross_join(table, as: nil)
  join = Join.new(:cross, table, nil, as)
  dup_with(joins: @joins + [join])
end

#distinctObject



27
28
29
# File 'lib/rooq/dsl/select_query.rb', line 27

def distinct
  dup_with(distinct_flag: true)
end

#except(other, all: false) ⇒ Object



124
125
126
# File 'lib/rooq/dsl/select_query.rb', line 124

def except(other, all: false)
  SetOperation.new(:except, self, other, all: all)
end

#for_updateObject



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

def for_update
  dup_with(for_update_flag: true)
end

#from(table, as: nil) ⇒ Object



31
32
33
# File 'lib/rooq/dsl/select_query.rb', line 31

def from(table, as: nil)
  dup_with(from_table: table, table_alias: as)
end

#full_join(table, as: nil) ⇒ Object



96
97
98
# File 'lib/rooq/dsl/select_query.rb', line 96

def full_join(table, as: nil)
  JoinBuilder.new(self, :full, table, as)
end

#group_by(*fields) ⇒ Object



55
56
57
# File 'lib/rooq/dsl/select_query.rb', line 55

def group_by(*fields)
  dup_with(group_by_fields: @group_by_fields + fields.flatten)
end

#having(condition) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rooq/dsl/select_query.rb', line 59

def having(condition)
  if @having_condition
    dup_with(having_condition: @having_condition.and(condition))
  else
    dup_with(having_condition: condition)
  end
end

#inner_join(table, as: nil) ⇒ Object

JOIN methods



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

def inner_join(table, as: nil)
  JoinBuilder.new(self, :inner, table, as)
end

#intersect(other, all: false) ⇒ Object



120
121
122
# File 'lib/rooq/dsl/select_query.rb', line 120

def intersect(other, all: false)
  SetOperation.new(:intersect, self, other, all: all)
end

#left_join(table, as: nil) ⇒ Object



88
89
90
# File 'lib/rooq/dsl/select_query.rb', line 88

def left_join(table, as: nil)
  JoinBuilder.new(self, :left, table, as)
end

#limit(value) ⇒ Object



71
72
73
# File 'lib/rooq/dsl/select_query.rb', line 71

def limit(value)
  dup_with(limit_value: value)
end

#offset(value) ⇒ Object



75
76
77
# File 'lib/rooq/dsl/select_query.rb', line 75

def offset(value)
  dup_with(offset_value: value)
end

#or_where(condition) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rooq/dsl/select_query.rb', line 47

def or_where(condition)
  if @conditions
    dup_with(conditions: @conditions.or(condition))
  else
    dup_with(conditions: condition)
  end
end

#order_by(*specs) ⇒ Object



67
68
69
# File 'lib/rooq/dsl/select_query.rb', line 67

def order_by(*specs)
  dup_with(order_specs: @order_specs + specs.flatten)
end

#right_join(table, as: nil) ⇒ Object



92
93
94
# File 'lib/rooq/dsl/select_query.rb', line 92

def right_join(table, as: nil)
  JoinBuilder.new(self, :right, table, as)
end

#to_sql(dialect = Rooq::Dialect::PostgreSQL.new) ⇒ Object



133
134
135
# File 'lib/rooq/dsl/select_query.rb', line 133

def to_sql(dialect = Rooq::Dialect::PostgreSQL.new)
  dialect.render_select(self)
end

#union(other, all: false) ⇒ Object

Set operations - return a new combined query



116
117
118
# File 'lib/rooq/dsl/select_query.rb', line 116

def union(other, all: false)
  SetOperation.new(:union, self, other, all: all)
end

#where(condition) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rooq/dsl/select_query.rb', line 35

def where(condition)
  if @conditions
    dup_with(conditions: @conditions.and(condition))
  else
    dup_with(conditions: condition)
  end
end

#with(name, query, recursive: false) ⇒ Object

CTE support



110
111
112
113
# File 'lib/rooq/dsl/select_query.rb', line 110

def with(name, query, recursive: false)
  cte = CTE.new(name, query, recursive: recursive)
  dup_with(ctes: @ctes + [cte])
end