File Coverage

lib/AutoSQL/AdaptorFactory.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package AutoSQL::AdaptorFactory;
2 1     1   8930 use strict;
  1         3  
  1         40  
3 1     1   366 use AutoSQL::DBModuleFactory;
  0            
  0            
4             our @ISA=qw(AutoSQL::DBModuleFactory);
5             use AutoSQL::DBSQL::DBAdaptor;
6             use AutoSQL::DBSQL::ObjectAdaptor;
7             use AutoSQL::DBObject;
8              
9             sub _initialize {
10             my ($self, @args)=@_;
11             $self->SUPER::_initialize(@args);
12             $self->throw("AdaptorFactory accepts AutoSQL::Schema only, so far")
13             unless $self->schema->isa('AutoSQL::Schema');
14             }
15              
16             sub get_adaptor_instance {
17             my ($self, @args)=@_;
18             push @args, -factory => $self;
19             my $dba = AutoSQL::DBSQL::DBAdaptor->new(@args);
20             foreach my $type ($self->schema->get_all_types){
21             $dba->add_object_adaptor($type,
22             $self->get_object_adaptor_instance($type, $dba));
23             }
24             return $dba;
25             }
26              
27             sub get_object_adaptor_instance {
28             my ($self, $type, $dba)=@_;
29             my $vp = $self->make_object_adaptor($type);
30             my $object_adaptor = $vp->new(
31             -dba => $dba,
32             -factory => $self, # I do not use it yet
33             -model => $self->schema->get_table_model($type)
34             );
35             return $object_adaptor;
36             }
37              
38             our %OBJECT_ADAPTORS;
39             sub make_object_adaptor {
40             my ($self, $type) =@_;
41             return $OBJECT_ADAPTORS{$type} if exists $OBJECT_ADAPTORS{$type};
42             my $schema=$self->schema;
43             my $model=$schema->get_table_model($type);
44            
45             my $vp = $self->_get_object_adaptor_package($type);
46             no strict 'refs';
47             push @{"$vp\::ISA"}, 'AutoSQL::DBSQL::ObjectAdaptor'
48             unless grep /^AutoSQL::DBSQL::ObjectAdaptor$/, @{"$vp\::ISA"};
49             # $self->_make_table_name_method($vp, $model->table_name);
50             # $self->_make_slots_method($vp, $model->slots);
51             print __PACKAGE__ ." : no customized table_name method made\n";
52             $self->_make_only_fetch_methods($model);
53             $OBJECT_ADAPTORS{$type} = $vp;
54             # print "$vp\n";
55             return $vp;
56             }
57              
58             sub _get_object_adaptor_package {
59             my ($self, $type)=@_;
60             my $package_prefix=$self->package_prefix;
61             "$package_prefix\::Virtual::DBSQL::${type}Adaptor";
62             }
63              
64             sub _make_table_name_method {
65             my ($self, $vp, $table_name)=@_;
66             no strict 'refs';
67             *{"$vp\::_table_name"} = sub { return $table_name; };
68             }
69              
70             sub _make_slots_method {
71             my ($self, $vp, @args)=@_;
72             my @slots;
73             push @slots, ((ref($args[0])eq'ARRAY')?@{$args[0]}:@args);
74             no strict 'refs';
75             *{"$vp\::_slots"} = sub { return @slots; };
76             }
77              
78             sub _make_only_fetch_methods {
79             my ($self, $model)=@_;
80             my $type = $model->type;
81             my $vp = $self->_get_object_adaptor_package($type);
82             no strict 'refs';
83             foreach my $scalar_slot ($model->get_scalar_slots){
84             *{"$vp\::only_fetch_$scalar_slot\_by_dbID"}=sub {
85             my ($self, $dbid)=@_;
86             my $where = $self->_primary_key_name .' = ?';
87             ($self->_only_fetch_by_where($scalar_slot, $where, [$dbid]))[0];
88             };
89             }
90             foreach my $slot ($model->get_array_slots){
91             *{"$vp\::only_fetch_$slot\_by_dbID"}=sub{
92             undef;
93             };
94             }
95             }
96              
97             1;