Module: E2Model::DatasetMethods

Defined in:
lib/engine2/core.rb

Instance Method Summary collapse

Instance Method Details

#apply_after_load_processors(model, entries) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'lib/engine2/core.rb', line 366

def apply_after_load_processors model, entries
    model.after_load_processors.each do |name, proc|
        type_info = model.find_type_info(name)
        name_sym = name.to_sym
        entries.each do |entry|
            proc.(entry, name_sym, type_info) if entry.key?(name_sym)
        end
    end
end

#ensure_primary_keyObject

Raises:



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/engine2/core.rb', line 376

def ensure_primary_key
    pk = model.primary_keys
    raise Engine2::E2Error.new("No primary key defined for model #{model}") unless pk && pk.all?

    if opts_select = @opts[:select]
        sel_pk = []
        opts_select.each do |sel|
            name = case sel
                when Symbol
                    sel
                when Sequel::SQL::QualifiedIdentifier
                    sel.column
                when Sequel::SQL::AliasedExpression
                    sel
                    # nil #sel.aliaz # ?
                    # sel.expression
                end
            sel_pk << name if name && pk.include?(name)
        end

        if pk.length == sel_pk.length
            self
        else
            sels = (pk - sel_pk).map{|k| model.table_name.q(k)}
            select_more(*sels)
        end
    else
        select(*pk.map{|k| model.table_name.q(k)})
    end
end

#extract_select(sel, al = nil, &blk) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/engine2/core.rb', line 407

def extract_select sel, al = nil, &blk
    case sel
    when Symbol
        yield nil, sel, nil
    when Sequel::SQL::QualifiedIdentifier
        yield sel.table, sel.column, al
    when Sequel::SQL::AliasedExpression, Sequel::SQL::Function
        sel
        # extract_select sel.expression, sel.aliaz, &blk
        # expr = sel.expression
        # yield  expr.table, expr.column
    else
        raise Engine2::E2Error.new("Unknown selection #{sel}")
    end
end

#get_optsObject



478
479
480
# File 'lib/engine2/core.rb', line 478

def get_opts
    @opts
end

#load(*args) ⇒ Object



349
350
351
352
353
354
355
356
357
358
# File 'lib/engine2/core.rb', line 349

def load *args
    if entry = self[*args]
        model.after_load_processors.each do |name, proc|
            type_info = model.find_type_info(name)
            name_sym = name.to_sym
            proc.(entry, name_sym, type_info) if entry.key?(name_sym)
        end if model.after_load_processors
        entry
    end
end

#load_allObject



360
361
362
363
364
# File 'lib/engine2/core.rb', line 360

def load_all
    entries = self.all
    apply_after_load_processors(model, entries) if model.after_load_processors
    entries
end

#setup_query(fields) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/engine2/core.rb', line 423

def setup_query fields
    joins = {}
    model_table_name = model.table_name

    select = @opts[:select].map do |sel|
        extract_select sel do |table, name, aliaz|
            mdl = if table
                if table == model_table_name
                    model
                else
                    assoc = model.many_to_one_associations[table] || model.many_to_many_associations[table]
                    raise Engine2::E2Error.new("Association #{table} not found for model #{model}") unless assoc
                    assoc.associated_class
                end
            else
                model
            end

            mdl_table_name = mdl.table_name
            table ||= mdl_table_name
            if mdl_table_name == model_table_name
                fields << name
            else
                fields << table.q(name)
                joins[mdl_table_name] ||= model.many_to_one_associations[table] || model.many_to_many_associations[table]
            end

            f_info = mdl.type_info[name]
            raise Engine2::E2Error.new("Column #{name} not found for table #{table}") unless f_info
            if f_info[:dummy]
                nil
            else
                qname = mdl_table_name.q(name)
                if table == model_table_name
                    qname
                else
                    Sequel.alias_columns_in_joins ? qname.as(:"#{table}__#{name}") : qname
                end
            end
        end
    end.compact

    joins.reduce(clone(select: select)) do |joined, (table, assoc)|
        m = assoc.associated_class
        case assoc[:type]
        when :many_to_one
            keys = assoc[:qualified_key]
            joined.left_join(table, m.primary_keys.zip(keys.is_a?(Array) ? keys : [keys]))
        when :many_to_many
            joined.left_join(assoc[:join_table], assoc[:left_keys].zip(model.primary_keys)).left_join(m.table_name, m.primary_keys.zip(assoc[:right_keys]))
        else unsupported_association
        end
    end
end

#with_proc(&blk) ⇒ Object



482
483
484
485
486
# File 'lib/engine2/core.rb', line 482

def with_proc &blk
    ds = clone
    ds.row_proc = blk
    ds
end