line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# the contents of this file are Copyright (c) 2009 Daniel Norman |
2
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
3
|
|
|
|
|
|
|
# modify it under the terms of the GNU General Public License as |
4
|
|
|
|
|
|
|
# published by the Free Software Foundation. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package DBR::Config::Schema; |
7
|
|
|
|
|
|
|
|
8
|
18
|
|
|
18
|
|
111
|
use strict; |
|
18
|
|
|
|
|
38
|
|
|
18
|
|
|
|
|
781
|
|
9
|
18
|
|
|
18
|
|
1839
|
use base 'DBR::Common'; |
|
18
|
|
|
|
|
43
|
|
|
18
|
|
|
|
|
1590
|
|
10
|
|
|
|
|
|
|
|
11
|
18
|
|
|
18
|
|
11777
|
use DBR::Config::Table; |
|
18
|
|
|
|
|
70
|
|
|
18
|
|
|
|
|
540
|
|
12
|
18
|
|
|
18
|
|
189
|
use Clone; |
|
18
|
|
|
|
|
42
|
|
|
18
|
|
|
|
|
39345
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my %TABLES_BY_NAME; |
15
|
|
|
|
|
|
|
my %INSTANCES_BY_CLASS; |
16
|
|
|
|
|
|
|
my %SCHEMAS_BY_ID; |
17
|
|
|
|
|
|
|
my %SCHEMAS_BY_HANDLE; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub load{ |
20
|
51
|
|
|
51
|
0
|
330
|
my( $package ) = shift; |
21
|
51
|
|
|
|
|
871
|
my %params = @_; |
22
|
|
|
|
|
|
|
|
23
|
51
|
|
|
|
|
243
|
my $self = { session => $params{session} }; |
24
|
51
|
|
|
|
|
312
|
bless( $self, $package ); # Dummy object |
25
|
|
|
|
|
|
|
|
26
|
51
|
|
50
|
|
|
6696
|
my $instance = $params{instance} || return $self->_error('instance is required'); |
27
|
|
|
|
|
|
|
|
28
|
51
|
|
50
|
|
|
331
|
my $dbrh = $instance->connect || return $self->_error("Failed to connect to ${\$instance->name}"); |
29
|
|
|
|
|
|
|
|
30
|
51
|
|
50
|
|
|
913
|
my $schema_ids = $params{schema_id} || return $self->_error('schema_id is required'); |
31
|
51
|
100
|
|
|
|
686
|
$schema_ids = [$schema_ids] unless ref($schema_ids) eq 'ARRAY'; |
32
|
|
|
|
|
|
|
|
33
|
51
|
50
|
|
|
|
200
|
return 1 unless @$schema_ids; |
34
|
|
|
|
|
|
|
|
35
|
51
|
|
|
|
|
1653
|
return $self->_error('Failed to select instances') unless |
36
|
|
|
|
|
|
|
my $schemas = $dbrh->select( |
37
|
|
|
|
|
|
|
-table => 'dbr_schemas', |
38
|
|
|
|
|
|
|
-fields => 'schema_id handle display_name', |
39
|
51
|
50
|
|
|
|
246
|
-where => { schema_id => ['d in', @{$schema_ids}] }, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
51
|
|
|
|
|
229
|
my @schema_ids; # track the schema ids from this request seperately from the global cache |
43
|
51
|
|
|
|
|
163
|
foreach my $schema (@$schemas){ |
44
|
51
|
|
|
|
|
204
|
$SCHEMAS_BY_ID{ $schema->{schema_id} } = $schema; |
45
|
51
|
|
|
|
|
241
|
$SCHEMAS_BY_HANDLE{ $schema->{handle} } = $schema->{schema_id}; |
46
|
|
|
|
|
|
|
|
47
|
51
|
|
|
|
|
201
|
push @schema_ids, $schema->{schema_id}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
DBR::Config::Table->load( |
51
|
51
|
50
|
|
|
|
678
|
session => $self->{session}, |
52
|
|
|
|
|
|
|
instance => $instance, |
53
|
|
|
|
|
|
|
schema_id => \@schema_ids, |
54
|
|
|
|
|
|
|
) or return $package->_error('failed to load tables'); |
55
|
|
|
|
|
|
|
|
56
|
51
|
|
|
|
|
516
|
return 1; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub list_schemas { |
60
|
0
|
|
0
|
0
|
0
|
0
|
return Clone::clone( [ sort { ($a->{display_name} || '') cmp ($b->{display_name} || '') } values %SCHEMAS_BY_ID ] ); |
|
0
|
|
0
|
|
|
0
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _register_table{ |
64
|
82
|
|
|
82
|
|
174
|
my $package = shift; # no dummy $self object here, for efficiency |
65
|
82
|
|
|
|
|
328
|
my %params = @_; |
66
|
|
|
|
|
|
|
|
67
|
82
|
50
|
|
|
|
447
|
my $schema_id = $params{schema_id} or return $package->_error('schema_id is required'); |
68
|
82
|
50
|
|
|
|
438
|
$SCHEMAS_BY_ID{ $schema_id } or return $package->_error('invalid schema_id'); |
69
|
|
|
|
|
|
|
|
70
|
82
|
50
|
|
|
|
267
|
my $name = $params{name} or return $package->_error('name is required'); |
71
|
82
|
50
|
|
|
|
337
|
my $table_id = $params{table_id} or return $package->_error('table_id is required'); |
72
|
|
|
|
|
|
|
|
73
|
82
|
|
|
|
|
252
|
$TABLES_BY_NAME{ $schema_id } -> { $name } = $table_id; |
74
|
|
|
|
|
|
|
|
75
|
82
|
|
|
|
|
474
|
return 1; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _register_instance{ |
79
|
17
|
|
|
17
|
|
47
|
my $package = shift; # no dummy $self object here, for efficiency |
80
|
17
|
|
|
|
|
89
|
my %params = @_; |
81
|
|
|
|
|
|
|
|
82
|
17
|
50
|
|
|
|
103
|
my $schema_id = $params{schema_id} or die 'schema_id is required'; |
83
|
17
|
50
|
|
|
|
91
|
my $class = $params{class} or die 'class is required'; |
84
|
17
|
50
|
|
|
|
222
|
my $guid = $params{guid} or die 'guid is required'; |
85
|
|
|
|
|
|
|
|
86
|
17
|
|
|
|
|
110
|
$INSTANCES_BY_CLASS{ $schema_id } -> { $class } = $guid; |
87
|
|
|
|
|
|
|
|
88
|
17
|
|
|
|
|
112
|
return 1; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
###################### BEGIN OBJECT ORIENTED CODE ###################### |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub new { |
94
|
259
|
|
|
259
|
0
|
785
|
my( $package ) = shift; |
95
|
259
|
|
|
|
|
1408
|
my %params = @_; |
96
|
259
|
|
|
|
|
949
|
my $self = { |
97
|
|
|
|
|
|
|
session => $params{session}, |
98
|
|
|
|
|
|
|
}; |
99
|
|
|
|
|
|
|
|
100
|
259
|
|
|
|
|
1240
|
bless( $self, $package ); |
101
|
|
|
|
|
|
|
|
102
|
259
|
50
|
|
|
|
1490
|
return $self->_error('session is required') unless $self->{session}; |
103
|
|
|
|
|
|
|
|
104
|
259
|
100
|
|
|
|
1561
|
if ($params{schema_id}){ |
|
|
50
|
|
|
|
|
|
105
|
82
|
|
|
|
|
295
|
$self->{schema_id} = $params{schema_id}; |
106
|
|
|
|
|
|
|
}elsif($params{handle}){ |
107
|
177
|
50
|
|
|
|
1123
|
$self->{schema_id} = $SCHEMAS_BY_HANDLE{ $params{handle} } or return $self->_error("handle $params{handle} is invalid"); |
108
|
|
|
|
|
|
|
}else{ |
109
|
0
|
|
|
|
|
0
|
return $self->_error('schema_id is required'); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
259
|
50
|
|
|
|
1254
|
return $self->_error("schema_id $self->{schema_id} is not defined") unless $SCHEMAS_BY_ID{ $self->{schema_id} }; |
113
|
|
|
|
|
|
|
|
114
|
259
|
|
|
|
|
2170
|
return( $self ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub get_table{ |
118
|
258
|
|
|
258
|
0
|
17868
|
my $self = shift; |
119
|
258
|
50
|
|
|
|
933
|
my $tname = shift or return $self->_error('name is required'); |
120
|
|
|
|
|
|
|
|
121
|
258
|
|
50
|
|
|
1338
|
my $table_id = $TABLES_BY_NAME{ $self->{schema_id} } -> { $tname } || return $self->_error("table $tname does not exist"); |
122
|
|
|
|
|
|
|
|
123
|
258
|
50
|
|
|
|
2614
|
my $table = DBR::Config::Table->new( |
124
|
|
|
|
|
|
|
session => $self->{session}, |
125
|
|
|
|
|
|
|
table_id => $table_id, |
126
|
|
|
|
|
|
|
) or return $self->_error('failed to create table object'); |
127
|
258
|
|
|
|
|
1464
|
return $table; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub tables{ |
131
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
0
|
my @tables; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
0
|
foreach my $table_id ( values %{$TABLES_BY_NAME{ $self->{schema_id}} } ) { |
|
0
|
|
|
|
|
0
|
|
136
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
0
|
my $table = DBR::Config::Table->new( |
138
|
|
|
|
|
|
|
session => $self->{session}, |
139
|
|
|
|
|
|
|
table_id => $table_id, |
140
|
|
|
|
|
|
|
) or return $self->_error('failed to create table object'); |
141
|
0
|
|
|
|
|
0
|
push @tables, $table; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
0
|
0
|
|
|
|
0
|
return wantarray ? @tables : \@tables; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub get_instance{ |
149
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
150
|
0
|
|
0
|
|
|
0
|
my $class = shift || 'master'; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
0
|
|
|
0
|
my $guid = $INSTANCES_BY_CLASS{ $self->{schema_id} } -> { $class } || return $self->_error("instance of class $class does not exist"); |
153
|
|
|
|
|
|
|
|
154
|
0
|
0
|
|
|
|
0
|
my $instance = DBR::Config::Instance->lookup( |
155
|
|
|
|
|
|
|
session => $self->{session}, |
156
|
|
|
|
|
|
|
guid => $guid, |
157
|
|
|
|
|
|
|
) or return $self->_error('failed to create table object'); |
158
|
0
|
|
|
|
|
0
|
return $instance; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub instances{ |
162
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
0
|
my @instances; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
0
|
foreach my $guid ( values %{$INSTANCES_BY_CLASS{ $self->{schema_id}} } ) { |
|
0
|
|
|
|
|
0
|
|
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
0
|
my $instance = DBR::Config::Instance->lookup( |
169
|
|
|
|
|
|
|
session => $self->{session}, |
170
|
|
|
|
|
|
|
guid => $guid, |
171
|
|
|
|
|
|
|
) or return $self->_error('failed to create instance object'); |
172
|
0
|
|
|
|
|
0
|
push @instances, $instance; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
0
|
0
|
|
|
|
0
|
return wantarray ? @instances : \@instances; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub schema_id { |
182
|
177
|
|
|
177
|
0
|
380
|
my $self = shift; |
183
|
177
|
|
|
|
|
1281
|
return $self->{schema_id}; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub handle { |
187
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
188
|
0
|
0
|
|
|
|
|
my $schema = $SCHEMAS_BY_ID{ $self->{schema_id} } or return $self->_error( 'lookup failed' ); |
189
|
0
|
|
|
|
|
|
return $schema->{handle}; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub display_name { |
193
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
194
|
0
|
0
|
|
|
|
|
my $schema = $SCHEMAS_BY_ID{ $self->{schema_id} } or return $self->_error( 'lookup failed' ); |
195
|
0
|
|
0
|
|
|
|
return $schema->{display_name} || ''; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |