Class: ForestAdminDatasourceSnowflake::Utils::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_datasource_snowflake/utils/query.rb

Constant Summary collapse

Operators =
ForestAdminDatasourceToolkit::Components::Query::ConditionTree::Operators
ConditionTreeBranch =
ForestAdminDatasourceToolkit::Components::Query::ConditionTree::Nodes::ConditionTreeBranch
ConditionTreeLeaf =
ForestAdminDatasourceToolkit::Components::Query::ConditionTree::Nodes::ConditionTreeLeaf

Instance Method Summary collapse

Constructor Details

#initialize(collection, projection: nil, filter: nil, aggregation: nil, limit: nil) ⇒ Query

Returns a new instance of Query.



8
9
10
11
12
13
14
15
# File 'lib/forest_admin_datasource_snowflake/utils/query.rb', line 8

def initialize(collection, projection: nil, filter: nil, aggregation: nil, limit: nil)
  @collection  = collection
  @projection  = projection
  @filter      = filter
  @aggregation = aggregation
  @limit       = limit
  @binds       = []
end

Instance Method Details

#to_aggregate_sqlObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/forest_admin_datasource_snowflake/utils/query.rb', line 39

def to_aggregate_sql
  @binds = []
  op_expr = build_aggregation_expression(@aggregation)

  group_cols = (@aggregation.groups || []).map { |g| g[:field] }
  select_cols = [op_expr, *group_cols.map { |c| q(c) }]
  sql = "SELECT #{select_cols.join(", ")} FROM #{q(@collection.table_name)}"

  where = build_where_clause(@filter&.condition_tree)
  sql << " WHERE #{where}" if where

  sql << " GROUP BY #{group_cols.map { |c| q(c) }.join(", ")}" if group_cols.any?
  sql << " LIMIT #{Integer(@limit)}" if @limit

  [sql, @binds, group_cols]
end

#to_sqlObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/forest_admin_datasource_snowflake/utils/query.rb', line 17

def to_sql
  @binds = []
  cols = @projection&.columns&.map { |c| q(c) }
  cols = ['*'] if cols.nil? || cols.empty?
  sql  = "SELECT #{cols.join(", ")} FROM #{q(@collection.table_name)}"

  where = build_where_clause(@filter&.condition_tree)
  sql << " WHERE #{where}" if where

  order_by = build_order_by(@filter&.sort)
  sql << " ORDER BY #{order_by}" if order_by

  if @filter&.page
    limit  = @filter.page.respond_to?(:limit)  ? @filter.page.limit  : nil
    offset = @filter.page.respond_to?(:offset) ? @filter.page.offset : nil
    sql << " LIMIT #{Integer(limit)}" if limit
    sql << " OFFSET #{Integer(offset)}" if offset
  end

  [sql, @binds]
end