Method: WidgetList::List#get_total_records

Defined in:
lib/widget_list.rb

#get_total_recordsObject



4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
# File 'lib/widget_list.rb', line 4356

def get_total_records()

  filter = ''
  fields = {}
  sql    = ''
  hashed = false

  if !get_view().empty?
    sql = WidgetList::Utils::fill({'<!--VIEW-->' => get_view(),'<!--GROUPBY-->' => !@items['groupBy'].empty? ? ' GROUP BY ' + @items['groupBy'] : '' }, @items['statement']['count']['view'])
  end

  if $is_mongo

    @active_record_model = @active_record_model.skip(0) #turn object into Mongoid::Criteria which has all the column definitions needed in _select

    #
    # List filters
    #
    @mongo_match = []
    @items['filter'].each_with_index { |value, key|
      field = value.to_sym
      predicate = ''
      if @items.key?('predicate') && !@items['predicate'][key].nil? && !@items['predicate'][key].empty? && value.to_sym.respond_to?(@items['predicate'][key])
        field = field.send(@items['predicate'][key])
        predicate = '$' + @items['predicate'][key]
      end

      @active_record_model = @active_record_model.where('$and' => [{
                                                                       field => WidgetList::List.parse_inputs_for_mongo_predicates(@active_record_model, value, predicate, @items['bindVars'][key])
                                                                   }]) if @items['groupBy'].empty?
      @mongo_match << { value =>  WidgetList::List.mysql_to_mongo_predicate(@items['predicate'][key],@items['bindVars'][key],true) } if @items['fields'].key?(value)
    } if @items.key?('filter') && !@items['filter'].empty?

    if !@items['groupBy'].empty?

      if @items['groupBy'].include?(',')
        criteriaTmp = @items['groupBy'].split_it(',')
      else
        criteriaTmp = [@items['groupBy']]
      end

      map_groups = {}
      criteriaTmp.each { |value|
        map_groups[value] =  "$#{value}"
      }
      #
      @group_match =
          {
              "group" =>  {"$group" => { "_id" => map_groups , "cnt" => { "$sum" => 1 } } },
          }
    end

  else

    if ! @filter.empty?
      filter = ' WHERE ' + @filter
    end
    sql = WidgetList::Utils::fill({'<!--WHERE-->' => filter}, sql)
  end

  if ! sql.empty? || $is_mongo
    if @items['showPagination']

      tmp_match = @group_match

      if $is_mongo
        if !tmp_match.nil?
          tmp_match = @group_match.dup
          tmp_match = tmp_match.merge({"match" =>   @mongo_match })
        end
      end

      cnt = get_database._select( $is_mongo ? 'count' : sql, @items['bindVars'], @items['bindVarsLegacy'], @active_record_model, tmp_match)
      if cnt > 0
        if cnt > get_database.final_results['TOTAL'][0].to_i
          #sometimes databases and queries run do not count(1) and group properly and instead
          rows = cnt
        else
          rows = get_database.final_results['TOTAL'][0].to_i
        end
      else
        rows = 0
      end
      if rows > 0
        @totalRows = rows.to_i
      end
    else
      rows = 1
    end
  else
    rows = 0
  end

  if @totalRows > 0
    @totalPages = (@totalRows.to_f / @items['rowLimit'].to_f).ceil()
  end

  rows
end