Class: Rooq::Dialect::PostgreSQL

Inherits:
Base
  • Object
show all
Defined in:
lib/rooq/dialect/postgresql.rb

Instance Method Summary collapse

Instance Method Details

#render_condition(condition, params) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rooq/dialect/postgresql.rb', line 171

def render_condition(condition, params)
  case condition
  when Condition
    render_simple_condition(condition, params)
  when CombinedCondition
    render_combined_condition(condition, params)
  when ExistsCondition
    render_exists_condition(condition, params)
  else
    raise ArgumentError, "Unknown condition type: #{condition.class}"
  end
end

#render_delete(query) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rooq/dialect/postgresql.rb', line 127

def render_delete(query)
  params = []
  sql_parts = []

  sql_parts << "DELETE FROM #{render_table_name(query.table)}"

  # WHERE clause
  if query.conditions
    condition_sql = render_condition(query.conditions, params)
    sql_parts << "WHERE #{condition_sql}"
  end

  # RETURNING clause
  unless query.returning_fields.empty?
    fields = render_select_fields(query.returning_fields, params)
    sql_parts << "RETURNING #{fields}"
  end

  RenderedQuery.new(sql_parts.join(" "), params)
end

#render_insert(query) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rooq/dialect/postgresql.rb', line 70

def render_insert(query)
  params = []
  sql_parts = []

  sql_parts << "INSERT INTO #{render_table_name(query.table)}"

  # Columns
  columns = query.column_list.map { |col| render_field_name(col) }
  sql_parts << "(#{columns.join(', ')})"

  # Values
  value_groups = query.insert_values.map do |values|
    placeholders = values.map do |value|
      params << value
      "$#{params.length}"
    end
    "(#{placeholders.join(', ')})"
  end
  sql_parts << "VALUES #{value_groups.join(', ')}"

  # RETURNING clause
  unless query.returning_fields.empty?
    fields = render_select_fields(query.returning_fields, params)
    sql_parts << "RETURNING #{fields}"
  end

  RenderedQuery.new(sql_parts.join(" "), params)
end

#render_ordered_set_operation(op) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rooq/dialect/postgresql.rb', line 154

def render_ordered_set_operation(op)
  params = []
  sql_parts = []

  sql_parts << "(#{render_set_operation_sql(op.set_operation, params)})"

  unless op.order_specs.empty?
    order_parts = op.order_specs.map { |spec| render_order_spec(spec, params) }
    sql_parts << "ORDER BY #{order_parts.join(', ')}"
  end

  sql_parts << "LIMIT #{op.limit_value}" if op.limit_value
  sql_parts << "OFFSET #{op.offset_value}" if op.offset_value

  RenderedQuery.new(sql_parts.join(" "), params)
end

#render_select(query) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rooq/dialect/postgresql.rb', line 6

def render_select(query)
  params = []
  sql_parts = []

  # CTEs (WITH clause)
  unless query.ctes.empty?
    cte_parts = query.ctes.map { |cte| render_cte(cte, params) }
    recursive = query.ctes.any?(&:recursive) ? "RECURSIVE " : ""
    sql_parts << "WITH #{recursive}#{cte_parts.join(', ')}"
  end

  # SELECT clause
  distinct = query.distinct_flag ? "DISTINCT " : ""
  fields = render_select_fields(query.selected_fields, params)
  sql_parts << "SELECT #{distinct}#{fields}"

  # FROM clause
  if query.from_table
    from_sql = render_from_source(query.from_table, params)
    from_sql = "#{from_sql} AS #{query.table_alias}" if query.table_alias
    sql_parts << "FROM #{from_sql}"
  end

  # JOIN clauses
  query.joins.each do |join|
    sql_parts << render_join(join, params)
  end

  # WHERE clause
  if query.conditions
    condition_sql = render_condition(query.conditions, params)
    sql_parts << "WHERE #{condition_sql}"
  end

  # GROUP BY clause
  unless query.group_by_fields.empty?
    group_parts = query.group_by_fields.map { |f| render_group_by_item(f, params) }
    sql_parts << "GROUP BY #{group_parts.join(', ')}"
  end

  # HAVING clause
  if query.having_condition
    having_sql = render_condition(query.having_condition, params)
    sql_parts << "HAVING #{having_sql}"
  end

  # ORDER BY clause
  unless query.order_specs.empty?
    order_parts = query.order_specs.map { |spec| render_order_spec(spec, params) }
    sql_parts << "ORDER BY #{order_parts.join(', ')}"
  end

  # LIMIT clause
  sql_parts << "LIMIT #{query.limit_value}" if query.limit_value

  # OFFSET clause
  sql_parts << "OFFSET #{query.offset_value}" if query.offset_value

  # FOR UPDATE
  sql_parts << "FOR UPDATE" if query.for_update_flag

  RenderedQuery.new(sql_parts.join(" "), params)
end

#render_set_operation(op) ⇒ Object



148
149
150
151
152
# File 'lib/rooq/dialect/postgresql.rb', line 148

def render_set_operation(op)
  params = []
  sql = render_set_operation_sql(op, params)
  RenderedQuery.new(sql, params)
end

#render_update(query) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rooq/dialect/postgresql.rb', line 99

def render_update(query)
  params = []
  sql_parts = []

  sql_parts << "UPDATE #{render_table_name(query.table)}"

  # SET clause
  set_parts = query.set_values.map do |field, value|
    params << value
    "#{render_field_name(field)} = $#{params.length}"
  end
  sql_parts << "SET #{set_parts.join(', ')}"

  # WHERE clause
  if query.conditions
    condition_sql = render_condition(query.conditions, params)
    sql_parts << "WHERE #{condition_sql}"
  end

  # RETURNING clause
  unless query.returning_fields.empty?
    fields = render_select_fields(query.returning_fields, params)
    sql_parts << "RETURNING #{fields}"
  end

  RenderedQuery.new(sql_parts.join(" "), params)
end