line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#ABSTRACT: ORMesque Database Schema Loader |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package ORMesque::SchemaLoader; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
73
|
|
6
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
69
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
10
|
use DBI; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1483
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 1.110422;# VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
0
|
|
my ($class, $dbh) = @_; |
15
|
0
|
|
|
|
|
|
bless { dbh => $dbh }, $class; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub mysql { |
19
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
20
|
0
|
|
|
|
|
|
my $this = {}; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
$this->{schema}->{escape_string} = '`'; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
push @{$this->{schema}->{tables}}, $_->[0] |
|
0
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
foreach @{$self->{dbh}->selectall_arrayref("SHOW TABLES")}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# load table columns |
28
|
0
|
|
|
|
|
|
foreach my $table (@{$this->{schema}->{tables}}) { |
|
0
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
for (@{$self->{dbh}->selectall_arrayref("SHOW COLUMNS FROM `$table`")}) { |
|
0
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
push @{$this->{schema}->{table}->{$table}->{columns}}, $_->[0]; |
|
0
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# find primary key |
33
|
0
|
0
|
|
|
|
|
$this->{schema}->{table}->{$table}->{primary_key} = $_->[0] |
34
|
|
|
|
|
|
|
if lc($_->[3]) eq 'pri'; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
return $this; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub sqlite { |
42
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
my $this = {}; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
$this->{schema}->{escape_string} = '"'; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# load tables |
48
|
0
|
|
|
|
|
|
push @{$this->{schema}->{tables}}, $_->[2] foreach @{ |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self->{dbh}->selectall_arrayref( |
50
|
|
|
|
|
|
|
"SELECT * FROM sqlite_master WHERE type='table'") |
51
|
|
|
|
|
|
|
}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# load table columns |
54
|
0
|
|
|
|
|
|
foreach my $table (@{$this->{schema}->{tables}}) { |
|
0
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
for (@{$self->{dbh}->selectall_arrayref("PRAGMA table_info('$table')")}) { |
|
0
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
push @{$this->{schema}->{table}->{$table}->{columns}}, $_->[1]; |
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# find primary key |
59
|
0
|
0
|
|
|
|
|
$this->{schema}->{table}->{$table}->{primary_key} = $_->[1] |
60
|
|
|
|
|
|
|
if lc($_->[5]) == 1; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $this; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub postgresql { |
68
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
69
|
0
|
|
|
|
|
|
my $this = {}; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$this->{schema}->{escape_string} = "'"; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# load tables |
74
|
0
|
|
|
|
|
|
push @{$this->{schema}->{tables}}, $_->[0] |
|
0
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
foreach @{$self->{dbh}->selectall_arrayref("SELECT table_name FROM |
76
|
|
|
|
|
|
|
information_schema.tables WHERE table_schema = 'public' ")}; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# load table columns |
79
|
0
|
|
|
|
|
|
foreach my $table (@{$this->{schema}->{tables}}) { |
|
0
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
for (@{$self->{dbh}->selectall_arrayref("SELECT column_name FROM |
|
0
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
information_schema.columns WHERE table_name ='$table'")}) { |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
push @{$this->{schema}->{table}->{$table}->{columns}}, $_->[0]; |
|
0
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# get primary key |
89
|
0
|
|
|
|
|
|
my $pkey_query = qq| |
90
|
|
|
|
|
|
|
SELECT |
91
|
|
|
|
|
|
|
pg_attribute.attname, |
92
|
|
|
|
|
|
|
format_type(pg_attribute.atttypid, pg_attribute.atttypmod) |
93
|
|
|
|
|
|
|
FROM pg_index, pg_class, pg_attribute |
94
|
|
|
|
|
|
|
WHERE |
95
|
|
|
|
|
|
|
pg_class.oid = '$table'::regclass AND |
96
|
|
|
|
|
|
|
indrelid = pg_class.oid AND |
97
|
|
|
|
|
|
|
pg_attribute.attrelid = pg_class.oid AND |
98
|
|
|
|
|
|
|
pg_attribute.attnum = any(pg_index.indkey) |
99
|
|
|
|
|
|
|
AND indisprimary|; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $key = $self->{dbh}->selectall_arrayref($pkey_query); |
102
|
0
|
|
|
|
|
|
$this->{schema}->{table}->{$table}->{primary_key} = $key->[0]->[0]; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
return $this; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |