line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Teng::Schema::Loader; |
2
|
2
|
|
|
2
|
|
1571
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
58
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
49
|
|
4
|
2
|
|
|
2
|
|
926
|
use DBIx::Inspector; |
|
2
|
|
|
|
|
11621
|
|
|
2
|
|
|
|
|
52
|
|
5
|
2
|
|
|
2
|
|
13
|
use Teng::Schema; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
45
|
|
6
|
2
|
|
|
2
|
|
481
|
use Teng::Schema::Table; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
44
|
|
7
|
2
|
|
|
2
|
|
10
|
use Carp (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
35
|
|
8
|
2
|
|
|
2
|
|
12
|
use Class::Load (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
166
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub load { |
11
|
4
|
|
|
4
|
1
|
46563
|
my $class = shift; |
12
|
4
|
50
|
|
|
|
28
|
my %args = @_==1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
13
|
|
|
|
|
|
|
|
14
|
4
|
50
|
|
|
|
73
|
my $namespace = $args{namespace} or Carp::croak("missing mandatory parameter 'namespace'"); |
15
|
|
|
|
|
|
|
|
16
|
4
|
100
|
|
|
|
52
|
Class::Load::load_optional_class($namespace) or do { |
17
|
|
|
|
|
|
|
# make teng class automatically |
18
|
1
|
|
|
|
|
1043
|
require Teng; |
19
|
2
|
|
|
2
|
|
12
|
no strict 'refs'; @{"$namespace\::ISA"} = ('Teng'); |
|
2
|
|
|
|
|
35
|
|
|
2
|
|
|
|
|
650
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
52
|
|
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
|
|
699
|
my $teng = $namespace->new(%args, loader => 1); |
23
|
4
|
|
|
|
|
29
|
my $dbh = $teng->dbh; |
24
|
4
|
50
|
|
|
|
17
|
unless ($dbh) { |
25
|
0
|
|
|
|
|
0
|
Carp::croak("missing mandatory parameter 'dbh' or 'connect_info'"); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
48
|
my $schema = Teng::Schema->new(namespace => $args{namespace}); |
29
|
|
|
|
|
|
|
|
30
|
4
|
|
|
|
|
30
|
my $inspector = DBIx::Inspector->new(dbh => $dbh); |
31
|
4
|
|
|
|
|
5157
|
for my $table_info ($inspector->tables) { |
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
|
|
3936
|
my $table_name = $table_info->name; |
34
|
4
|
|
|
|
|
32
|
my @table_pk = map { $_->name } $table_info->primary_key; |
|
4
|
|
|
|
|
10401
|
|
35
|
4
|
|
|
|
|
38
|
my @col_names; |
36
|
|
|
|
|
|
|
my %sql_types; |
37
|
4
|
|
|
|
|
18
|
for my $col ($table_info->columns) { |
38
|
16
|
|
|
|
|
5881
|
push @col_names, $col->name; |
39
|
16
|
|
|
|
|
72
|
$sql_types{$col->name} = $col->data_type; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$schema->add_table( |
43
|
4
|
|
|
|
|
70
|
Teng::Schema::Table->new( |
44
|
|
|
|
|
|
|
columns => \@col_names, |
45
|
|
|
|
|
|
|
name => $table_name, |
46
|
|
|
|
|
|
|
primary_keys => \@table_pk, |
47
|
|
|
|
|
|
|
sql_types => \%sql_types, |
48
|
|
|
|
|
|
|
inflators => [], |
49
|
|
|
|
|
|
|
deflators => [], |
50
|
|
|
|
|
|
|
row_class => join '::', $namespace, 'Row', Teng::Schema::camelize($table_name), |
51
|
|
|
|
|
|
|
) |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
4
|
|
|
|
|
50
|
$schema->prepare_from_dbh($dbh); |
56
|
4
|
|
|
|
|
128
|
$teng->schema($schema); |
57
|
4
|
|
|
|
|
36
|
return $teng; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 NAME |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Teng::Schema::Loader - Dynamic Schema Loader |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 SYNOPSIS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
use Teng; |
70
|
|
|
|
|
|
|
use Teng::Schema::Loader; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $teng = Teng::Schema::Loader->load( |
73
|
|
|
|
|
|
|
dbh => $dbh, |
74
|
|
|
|
|
|
|
namespace => 'MyAPP::DB' |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 DESCRIPTION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
L<Teng::Schema::Loader> loads schema directly from DB. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 CLASS METHODS |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=over 4 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item Teng::Schema::Loader->load(%attr) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This is the method to load schema from DB. It returns the instance of the given C<namespace> class which inherits L<Teng>. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
The arguments are: |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=over 4 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item C<dbh> |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Database handle from DBI. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item namespace |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
your project name space. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=back |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=back |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |