Class: ClickHouse::SQL::SelectQuery
- Inherits:
-
Object
- Object
- ClickHouse::SQL::SelectQuery
- Includes:
- Part
- Defined in:
- lib/clickhouse/sql/select_query.rb
Instance Method Summary collapse
-
#distinct ⇒ ClickHouse::SQL::SelectQuery
Renders
SELECT DISTINCTinstead ofSELECT. -
#from(table, as: nil, final: false) ⇒ ClickHouse::SQL::SelectQuery
Sets the FROM table expression.
-
#from_subquery(query, as:) ⇒ ClickHouse::SQL::SelectQuery
Sets the FROM clause to a nested query.
-
#group_by(*expressions) ⇒ ClickHouse::SQL::SelectQuery
Appends GROUP BY expressions.
-
#having(condition = nil, *fills, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends a HAVING condition, or an array of HAVING conditions, later rendered with
AND. -
#initialize ⇒ ClickHouse::SQL::SelectQuery
constructor
Builds an empty mutable SELECT query.
-
#inner_join(table, as: nil, final: false, on:) ⇒ ClickHouse::SQL::SelectQuery
Appends an INNER JOIN clause.
-
#join(table, type: "INNER", as: nil, final: false, on:) ⇒ ClickHouse::SQL::SelectQuery
Appends a JOIN clause.
-
#joins(*joins) ⇒ ClickHouse::SQL::SelectQuery
Appends pre-rendered JOIN fragments.
-
#left_join(table, as: nil, final: false, on:) ⇒ ClickHouse::SQL::SelectQuery
Appends a LEFT JOIN clause.
-
#limit(expression = nil, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Sets the LIMIT expression.
-
#offset(expression = nil, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Sets the OFFSET expression.
-
#order_by(*expressions) ⇒ ClickHouse::SQL::SelectQuery
Appends ORDER BY expressions.
-
#placeholder_types ⇒ Hash{Symbol=>String}
Returns ClickHouse placeholder types used by the SELECT query.
-
#placeholders ⇒ Hash{Symbol=>Object}
Returns typed placeholder values used by the SELECT query.
-
#select(*columns, **aliases) ⇒ ClickHouse::SQL::SelectQuery
Appends SELECT expressions.
-
#setting(name, value) ⇒ ClickHouse::SQL::SelectQuery
Appends one validated SETTINGS assignment.
-
#settings(*settings, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends static SETTINGS fragments.
-
#to_query ⇒ ClickHouse::SQL::Query
Converts this builder into an immutable query object accepted by ClickHouse::HTTPClient.
-
#to_redacted_sql(bind_index_manager = ClickHouse::SQL::BindIndexManager.new) ⇒ String
Renders the SELECT query with typed placeholders replaced by bind markers.
-
#to_sql ⇒ String
Renders the SELECT query with ClickHouse typed placeholders intact.
-
#where(condition = nil, *fills, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends a WHERE condition, or an array of WHERE conditions, later rendered with
AND. -
#with(fragment = nil, *fills, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends a WITH/CTE fragment, or an array of WITH/CTE fragments.
-
#with_cte(handle, query) ⇒ ClickHouse::SQL::SelectQuery
Appends a CTE named by a table handle.
Constructor Details
#initialize ⇒ ClickHouse::SQL::SelectQuery
Builds an empty mutable SELECT query.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/clickhouse/sql/select_query.rb', line 11 def initialize @selects = [] @distinct = false @with = [] @joins = [] @wheres = [] @havings = [] @group_bys = [] @order_bys = [] @settings = [] end |
Instance Method Details
#distinct ⇒ ClickHouse::SQL::SelectQuery
Renders SELECT DISTINCT instead of SELECT.
26 27 28 29 |
# File 'lib/clickhouse/sql/select_query.rb', line 26 def distinct @distinct = true self end |
#from(table, as: nil, final: false) ⇒ ClickHouse::SQL::SelectQuery
Sets the FROM table expression.
A ClickHouse::SQL::Table handle owns its alias so column references
built from the handle always match the rendered clause; passing as:
alongside a handle raises.
88 89 90 91 92 93 |
# File 'lib/clickhouse/sql/select_query.rb', line 88 def from(table, as: nil, final: false) @from = ClickHouse::SQL.coerce_raw_expression(table) @from_alias = ClickHouse::SQL.resolve_table_alias(table, as) @from_final = final self end |
#from_subquery(query, as:) ⇒ ClickHouse::SQL::SelectQuery
Sets the FROM clause to a nested query.
102 103 104 105 106 107 |
# File 'lib/clickhouse/sql/select_query.rb', line 102 def from_subquery(query, as:) @from = ClickHouse::SQL.fragment("({query})", query: ClickHouse::SQL.coerce_expression(query)) @from_alias = as.is_a?(ClickHouse::SQL::Table) ? as.qualifier : as @from_final = false self end |
#group_by(*expressions) ⇒ ClickHouse::SQL::SelectQuery
Appends GROUP BY expressions.
185 186 187 |
# File 'lib/clickhouse/sql/select_query.rb', line 185 def group_by(*expressions) append_raw_expressions(@group_bys, expressions) end |
#having(condition = nil, *fills, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends a HAVING condition, or an array of HAVING conditions, later
rendered with AND.
177 178 179 |
# File 'lib/clickhouse/sql/select_query.rb', line 177 def having(condition = nil, *fills, **bindings) append_clause(@havings, condition, fills, bindings) end |
#inner_join(table, as: nil, final: false, on:) ⇒ ClickHouse::SQL::SelectQuery
Appends an INNER JOIN clause.
143 144 145 |
# File 'lib/clickhouse/sql/select_query.rb', line 143 def inner_join(table, as: nil, final: false, on:) join(table, type: "INNER", as:, final:, on:) end |
#join(table, type: "INNER", as: nil, final: false, on:) ⇒ ClickHouse::SQL::SelectQuery
Appends a JOIN clause.
118 119 120 121 |
# File 'lib/clickhouse/sql/select_query.rb', line 118 def join(table, type: "INNER", as: nil, final: false, on:) @joins << Join.new(table, type:, as:, final:, on:) self end |
#joins(*joins) ⇒ ClickHouse::SQL::SelectQuery
Appends pre-rendered JOIN fragments.
151 152 153 154 155 156 157 |
# File 'lib/clickhouse/sql/select_query.rb', line 151 def joins(*joins) joins.flatten.compact.each do |join| @joins << ClickHouse::SQL.coerce_expression(join) end self end |
#left_join(table, as: nil, final: false, on:) ⇒ ClickHouse::SQL::SelectQuery
Appends a LEFT JOIN clause.
131 132 133 |
# File 'lib/clickhouse/sql/select_query.rb', line 131 def left_join(table, as: nil, final: false, on:) join(table, type: "LEFT", as:, final:, on:) end |
#limit(expression = nil, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Sets the LIMIT expression.
202 203 204 205 |
# File 'lib/clickhouse/sql/select_query.rb', line 202 def limit(expression = nil, **bindings) @limit = one_fragment(expression, bindings) self end |
#offset(expression = nil, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Sets the OFFSET expression.
212 213 214 215 |
# File 'lib/clickhouse/sql/select_query.rb', line 212 def offset(expression = nil, **bindings) @offset = one_fragment(expression, bindings) self end |
#order_by(*expressions) ⇒ ClickHouse::SQL::SelectQuery
Appends ORDER BY expressions.
193 194 195 |
# File 'lib/clickhouse/sql/select_query.rb', line 193 def order_by(*expressions) append_raw_expressions(@order_bys, expressions) end |
#placeholder_types ⇒ Hash{Symbol=>String}
Returns ClickHouse placeholder types used by the SELECT query.
267 268 269 |
# File 'lib/clickhouse/sql/select_query.rb', line 267 def placeholder_types ClickHouse::SQL.collect_placeholder_types(query_parts) end |
#placeholders ⇒ Hash{Symbol=>Object}
Returns typed placeholder values used by the SELECT query.
260 261 262 |
# File 'lib/clickhouse/sql/select_query.rb', line 260 def placeholders ClickHouse::SQL.collect_placeholders(query_parts) end |
#select(*columns, **aliases) ⇒ ClickHouse::SQL::SelectQuery
Appends SELECT expressions.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/clickhouse/sql/select_query.rb', line 36 def select(*columns, **aliases) columns.flatten.compact.each do |column| @selects << SelectItem.new(expression: ClickHouse::SQL.coerce_raw_expression(column)) end aliases.each do |alias_name, expression| @selects << SelectItem.new(expression: ClickHouse::SQL.coerce_raw_expression(expression), alias_name:) end self end |
#setting(name, value) ⇒ ClickHouse::SQL::SelectQuery
Appends one validated SETTINGS assignment.
237 238 239 240 |
# File 'lib/clickhouse/sql/select_query.rb', line 237 def setting(name, value) @settings << Setting.new(name, value) self end |
#settings(*settings, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends static SETTINGS fragments.
222 223 224 225 226 227 228 229 230 |
# File 'lib/clickhouse/sql/select_query.rb', line 222 def settings(*settings, **bindings) if bindings.any? raise Error, "ClickHouse SETTINGS do not support query parameters; use setting(name, value) for dynamic setting values" end settings.flatten.compact.each { |setting| @settings << ClickHouse::SQL.coerce_expression(setting) } validate_settings! self end |
#to_query ⇒ ClickHouse::SQL::Query
Converts this builder into an immutable query object accepted by ClickHouse::HTTPClient.
274 275 276 277 278 279 280 281 |
# File 'lib/clickhouse/sql/select_query.rb', line 274 def to_query Query.new( sql: to_sql, redacted_sql: to_redacted_sql(Query.redacted_bind_index_manager), placeholders:, placeholder_types: ) end |
#to_redacted_sql(bind_index_manager = ClickHouse::SQL::BindIndexManager.new) ⇒ String
Renders the SELECT query with typed placeholders replaced by bind markers.
253 254 255 |
# File 'lib/clickhouse/sql/select_query.rb', line 253 def to_redacted_sql(bind_index_manager = ClickHouse::SQL::BindIndexManager.new) render(redacted: true, bind_index_manager:) end |
#to_sql ⇒ String
Renders the SELECT query with ClickHouse typed placeholders intact.
245 246 247 |
# File 'lib/clickhouse/sql/select_query.rb', line 245 def to_sql render(redacted: false) end |
#where(condition = nil, *fills, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends a WHERE condition, or an array of WHERE conditions, later
rendered with AND.
166 167 168 |
# File 'lib/clickhouse/sql/select_query.rb', line 166 def where(condition = nil, *fills, **bindings) append_clause(@wheres, condition, fills, bindings) end |
#with(fragment = nil, *fills, **bindings) ⇒ ClickHouse::SQL::SelectQuery
Appends a WITH/CTE fragment, or an array of WITH/CTE fragments.
54 55 56 |
# File 'lib/clickhouse/sql/select_query.rb', line 54 def with(fragment = nil, *fills, **bindings) append_clause(@with, fragment, fills, bindings) end |
#with_cte(handle, query) ⇒ ClickHouse::SQL::SelectQuery
Appends a CTE named by a table handle.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/clickhouse/sql/select_query.rb', line 63 def with_cte(handle, query) unless handle.is_a?(ClickHouse::SQL::Table) raise Error, "CTE handle must be a table handle, got #{handle.class}" end name = ClickHouse::SQL.validate_alias!(handle.name) @with << ClickHouse::SQL.fragment( "{} AS ({})", ClickHouse::SQL.raw_static(name), ClickHouse::SQL.coerce_expression(query) ) self end |