class Ruby::Reports::Services::QueryBuilder

Public Instance Methods

request_batch(offset) click to toggle source
# File lib/ruby/reports/services/query_builder.rb, line 12
def request_batch(offset)
  execute take_batch(config.batch_size, offset)
end
request_count() click to toggle source
# File lib/ruby/reports/services/query_builder.rb, line 8
def request_count
  execute(count)[0]['count'].to_i
end

Private Instance Methods

base_query() click to toggle source

Internal: Основной запрос отчета (Arel)

Returns Arel::SelectManager

# File lib/ruby/reports/services/query_builder.rb, line 66
def base_query
  fail NotImplementedError
end
batch_size() click to toggle source

Internal: Размер пачки отчета

Returns Fixnum

# File lib/ruby/reports/services/query_builder.rb, line 52
def batch_size
  BATCH_SIZE
end
connection() click to toggle source
# File lib/ruby/reports/services/query_builder.rb, line 20
def connection
  ActiveRecord::Base.connection
end
count() click to toggle source

Internal: Запрос количества строк в отчете

Returns String (SQL)

# File lib/ruby/reports/services/query_builder.rb, line 94
def count
  query.project(Arel.sql('COUNT(*) as count')).to_sql
end
filter(query) click to toggle source

Internal: Фильтры отчета

Returns Arel::SelectManager

# File lib/ruby/reports/services/query_builder.rb, line 87
def filter(query)
  query
end
join_tables(source_table, *joins) click to toggle source

Internal: Полезный метод для join’а необходимых таблиц через Arel

Returns Arel

# File lib/ruby/reports/services/query_builder.rb, line 45
def join_tables(source_table, *joins)
  joins.inject(source_table) { |query, joined| query.join(joined[:table]).on(joined[:on]) }
end
models() click to toggle source

Internal: Модели используемые в отчете

Returns Array of Arel::Table

# File lib/ruby/reports/services/query_builder.rb, line 59
def models
  fail NotImplementedError
end
order_by() click to toggle source

Internal: Порядок строк отчета

Returns String (SQL)

# File lib/ruby/reports/services/query_builder.rb, line 80
def order_by
  nil
end
query() click to toggle source

Internal: Возвращает отфильтрованный запрос отчета

Returns Arel::SelectManager

# File lib/ruby/reports/services/query_builder.rb, line 27
def query
  filter base_query
end
select() click to toggle source

Internal: Поля запрашиваемые отчетом

Returns String (SQL)

# File lib/ruby/reports/services/query_builder.rb, line 73
def select
  fail NotImplementedError
end
tables() click to toggle source

Internal: Полезный метод для хранения Arel::Table объектов для запроса отчета

Returns Hash, {:table_name => #<Arel::Table @name=“table_name”>, …}

# File lib/ruby/reports/services/query_builder.rb, line 34
def tables
  return @tables if defined? @tables

  tables = models.map(&:arel_table)

  @tables = tables.reduce({}) { |a, e| a.store(e.name, e) && a }.with_indifferent_access
end
take_batch(limit, offset) click to toggle source

Internal: Запрос пачки строк отчета

offset - Numeric, число строк на которое сдвигается запрос

Returns String (SQL)

# File lib/ruby/reports/services/query_builder.rb, line 103
def take_batch(limit, offset)
  query.project(Arel.sql(select))
       .take(limit)
       .skip(offset)
       .order(order_by)
       .to_sql
end