Module: E2Model::DatasetMethods

Defined in:
lib/engine2/core.rb

Instance Method Summary collapse

Instance Method Details

#ensure_primary_keyObject

Raises:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/engine2/core.rb', line 286

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.to_s =~ /\w+__(\w+)/ ? $1.to_sym : 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| k.qualify(@model.table_name)}
            select_more(*sels)
        end
    else
        select(*pk.map{|k| k.qualify(@model.table_name)})
    end

end

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



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/engine2/core.rb', line 386

def extract_select sel, al = nil, &blk
    case sel
    when Symbol
        if sel.to_s =~ /^(\w+)__(\w+?)(?:___(\w+))?$/
            yield $1.to_sym, $2.to_sym, $3 ? $3.to_sym : nil
        else
            yield nil, sel, al
        end
    when Sequel::SQL::QualifiedIdentifier
        yield sel.table, sel.column, al
    when Sequel::SQL::AliasedExpression
        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



406
407
408
# File 'lib/engine2/core.rb', line 406

def get_opts
    @opts
end

#setup!(fields) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/engine2/core.rb', line 318

def setup! fields
    joins = {}
    type_info = model.type_info
    model_table_name = model.table_name

    @opts[:select].map! do |sel|
        extract_select sel do |table, name, aliaz|
            if table
                if table == model_table_name
                    m = model
                else
                    a = model.many_to_one_associations[table] # || model.one_to_one_associations[table]
                    raise Engine2::E2Error.new("Association #{table} not found for model #{model}") unless a
                    m = Object.const_get(a[:class_name])
                end
                # raise Engine2::E2Error.new("Model not found for table #{table} in model #{model}") unless m
                info = m.type_info
            else
                info = type_info
            end

            f_info = info[name]
            raise Engine2::E2Error.new("Column #{name} not found for table #{table || model_table_name}") unless f_info

            table ||= model_table_name

            if table == model_table_name
                fields << name
            else
                fields << :"#{table}__#{name}"
                assoc = model.many_to_one_associations[table]
                unless assoc
                    # fail
                end
                joins[table] = assoc
            end

            if f_info[:dummy]
                nil
            # elsif f_info[:type] == :blob_store
            #     # (~{name => nil}).as :name
            #     # Sequel.char_length(name).as name
            #     nil
            else
                if table != model_table_name
                    if Sequel.alias_tables_in_joins
                        name.qualify(table).as(:"#{table}__#{name}")
                    else
                        name.qualify(table)
                    end
                else
                    name.qualify(table)
                end
            end
        end
    end

    @opts[:select].compact!

    joins.reduce(self) do |joined, (table, assoc)|
        assoc = model.many_to_one_associations[table] # || model.one_to_one_associations[table]
        m = Object.const_get(assoc[:class_name])
        keys = assoc[:qualified_key]
        keys = [keys] unless keys.is_a?(Array)
        joined.left_join(table, m.primary_keys.zip(keys))
    end
end

#with_proc(&blk) ⇒ Object



410
411
412
413
414
# File 'lib/engine2/core.rb', line 410

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