line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Karas::Loader; |
2
|
4
|
|
|
4
|
|
118073
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
152
|
|
3
|
4
|
|
|
4
|
|
22
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
115
|
|
4
|
4
|
|
|
4
|
|
20
|
use utf8; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
26
|
|
5
|
4
|
|
|
4
|
|
166
|
use 5.0100000; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
163
|
|
6
|
4
|
|
|
4
|
|
20
|
use Carp (); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
85
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
1155
|
use Karas; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
133
|
|
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
3648
|
use String::CamelCase; |
|
4
|
|
|
|
|
2365
|
|
|
4
|
|
|
|
|
229
|
|
11
|
4
|
|
|
4
|
|
3269
|
use DBIx::Inspector; |
|
4
|
|
|
|
|
34863
|
|
|
4
|
|
|
|
|
386
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub load { |
14
|
5
|
|
|
5
|
0
|
32482
|
my $class = shift; |
15
|
5
|
50
|
|
|
|
40
|
my %args = @_==1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
16
|
5
|
|
33
|
|
|
35
|
$args{namespace} //= do { |
17
|
5
|
|
|
|
|
13
|
state $i=0; |
18
|
5
|
|
|
|
|
21
|
my $klass = "Karas::Loader::Anon" . $i++; |
19
|
|
|
|
|
|
|
{ |
20
|
4
|
|
|
4
|
|
34
|
no strict 'refs'; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
1239
|
|
|
5
|
|
|
|
|
9
|
|
21
|
5
|
|
|
|
|
18
|
push @{"${klass}::ISA"}, 'Karas'; |
|
5
|
|
|
|
|
91
|
|
22
|
|
|
|
|
|
|
} |
23
|
5
|
|
|
|
|
26
|
$klass; |
24
|
|
|
|
|
|
|
}; |
25
|
5
|
|
|
|
|
29
|
my $row_class_map = $class->load_schema(%args); |
26
|
5
|
|
|
|
|
80
|
my $db = $args{namespace}->new( |
27
|
|
|
|
|
|
|
%args, |
28
|
|
|
|
|
|
|
row_class_map => $row_class_map, |
29
|
|
|
|
|
|
|
); |
30
|
5
|
|
|
|
|
30
|
return $db; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub load_schema { |
34
|
6
|
|
|
6
|
0
|
3371
|
my $class = shift; |
35
|
6
|
50
|
|
|
|
36
|
my %args = @_ == 1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
36
|
6
|
|
33
|
|
|
28
|
my $connect_info = $args{connect_info} // Carp::croak "Missing mandatory parameter: connect_info"; |
37
|
6
|
|
33
|
|
|
32
|
my $namespace = $args{namespace} // Carp::croak "Missing mandatory parameter: namespace"; |
38
|
6
|
|
50
|
|
|
42
|
my $decamelize_map = $args{decamelize_map} || +{}; |
39
|
|
|
|
|
|
|
|
40
|
6
|
50
|
|
|
|
31
|
my $dbh = DBI->connect(@$connect_info) or Carp::croak("Cannot connect to database: $DBI::errstr"); |
41
|
6
|
|
|
|
|
14641
|
my $inspector = DBIx::Inspector->new(dbh => $dbh); |
42
|
6
|
|
|
|
|
25727
|
require Karas::Row; |
43
|
6
|
|
|
|
|
16
|
my %class_map; |
44
|
6
|
|
|
|
|
49
|
for my $table ($inspector->tables) { |
45
|
4
|
|
|
4
|
|
25
|
no strict 'refs'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
1752
|
|
46
|
6
|
|
33
|
|
|
4517
|
my $klass = sprintf( "%s::Row::%s", |
47
|
|
|
|
|
|
|
$namespace, |
48
|
|
|
|
|
|
|
$decamelize_map->{ $table->name } || String::CamelCase::camelize( $table->name ) |
49
|
|
|
|
|
|
|
); |
50
|
6
|
|
|
|
|
270
|
$class_map{$table->name} = $klass; |
51
|
|
|
|
|
|
|
# setup inheritance |
52
|
6
|
|
|
|
|
37
|
unshift @{"${klass}::ISA"}, 'Karas::Row'; |
|
6
|
|
|
|
|
150
|
|
53
|
|
|
|
|
|
|
# make accessors |
54
|
6
|
|
|
|
|
35
|
my @column_names = map { $_->name } $table->columns(); |
|
14
|
|
|
|
|
18585
|
|
55
|
6
|
|
|
|
|
174
|
$klass->mk_column_accessors(@column_names); |
56
|
|
|
|
|
|
|
# define 'table_name' method |
57
|
|
|
|
|
|
|
{ |
58
|
6
|
|
|
|
|
14
|
my $table_name = $table->name; |
|
6
|
|
|
|
|
29
|
|
59
|
6
|
|
|
3
|
|
53
|
*{"${klass}::table_name"} = sub { $table_name }; |
|
6
|
|
|
|
|
38
|
|
|
3
|
|
|
|
|
18
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
# define 'primary_key' method |
62
|
|
|
|
|
|
|
{ |
63
|
6
|
|
|
|
|
15
|
my @pk = map { $_->name } $table->primary_key(); |
|
6
|
|
|
|
|
32
|
|
|
6
|
|
|
|
|
6055
|
|
64
|
6
|
|
|
17
|
|
1022
|
*{"${klass}::primary_key"} = sub { @pk }; |
|
6
|
|
|
|
|
53
|
|
|
17
|
|
|
|
|
1008
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
# define 'column_names' method |
67
|
|
|
|
|
|
|
{ |
68
|
6
|
|
|
1
|
|
13
|
*{"${klass}::column_names"} = sub { @column_names }; |
|
6
|
|
|
|
|
26
|
|
|
6
|
|
|
|
|
38
|
|
|
1
|
|
|
|
|
7
|
|
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
# define 'has_column' |
71
|
|
|
|
|
|
|
{ |
72
|
6
|
|
|
|
|
13
|
my %column_names = map { $_ => 1 } @column_names; |
|
6
|
|
|
|
|
16
|
|
|
14
|
|
|
|
|
43
|
|
73
|
6
|
|
|
0
|
|
27
|
*{"${klass}::has_column"} = sub { $column_names{$_[1]} }; |
|
6
|
|
|
|
|
52
|
|
|
0
|
|
|
|
|
0
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
6
|
|
|
|
|
200
|
return \%class_map; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|