1 package SL::DBUpgrade2;
8 @EXPORT = qw(parse_dbupdate_controls sort_dbupdate_controls);
10 sub parse_dbupdate_controls {
11 $main::lxdebug->enter_sub();
13 my ($form, $dbdriver) = @_;
15 my $locale = $main::locale;
20 my $path = "sql/${dbdriver}-upgrade2";
22 foreach my $file_name (<$path/*.sql>, <$path/*.pl>) {
23 next unless (open(IN, $file_name));
25 my $file = $file_name;
35 next unless (/^(--|\#)\s*\@/);
40 my @fields = split(/\s*:\s*/, $_, 2);
41 next unless (scalar(@fields) == 2);
43 if ($fields[0] eq "depends") {
44 push(@{$control->{"depends"}}, split(/\s+/, $fields[1]));
46 $control->{$fields[0]} = $fields[1];
50 $control->{charset} ||= Common::DEFAULT_CHARSET;
52 _control_error($form, $file_name,
53 $locale->text("Missing 'tag' field."))
54 unless ($control->{"tag"});
56 _control_error($form, $file_name,
57 $locale->text("The 'tag' field must only consist of " .
58 "alphanumeric characters or the carachters " .
60 if ($control->{"tag"} =~ /[^a-zA-Z0-9_\(\)\-]/);
62 _control_error($form, $file_name,
63 sprintf($locale->text("More than one control file " .
64 "with the tag '%s' exist."),
66 if (defined($all_controls{$control->{"tag"}}));
68 _control_error($form, $file_name,
69 sprintf($locale->text("Missing 'description' field.")))
70 unless ($control->{"description"});
72 $control->{"priority"} *= 1;
73 $control->{"priority"} = 1000 unless ($control->{"priority"});
75 $control->{"file"} = $file;
77 map({ delete($control->{$_}); } qw(depth applied));
79 $all_controls{$control->{"tag"}} = $control;
84 foreach my $control (values(%all_controls)) {
85 foreach my $dependency (@{$control->{"depends"}}) {
86 _control_error($form, $control->{"file"},
87 sprintf($locale->text("Unknown dependency '%s'."),
89 if (!defined($all_controls{$dependency}));
92 map({ $_->{"loop"} = 0; } values(%all_controls));
93 _check_for_loops($form, $control->{"file"}, \%all_controls,
97 map({ _dbupdate2_calculate_depth(\%all_controls, $_->{"tag"}) }
98 values(%all_controls));
100 $main::lxdebug->leave_sub();
102 return \%all_controls;
105 sub _check_for_loops {
106 my ($form, $file_name, $controls, $tag, @path) = @_;
110 my $ctrl = $controls->{$tag};
112 if ($ctrl->{"loop"} == 1) {
114 _control_error($form, $file_name,
115 $main::locale->text("Dependency loop detected:") .
116 " " . join(" -> ", @path))
117 } elsif ($ctrl->{"loop"} == 0) {
120 map({ _check_for_loops($form, $file_name, $controls, $_, @path); }
121 @{ $ctrl->{"depends"} });
127 my ($form, $file_name, $message) = @_;
130 my $locale = $main::locale;
132 $form->error(sprintf($locale->text("Error in database control file '%s': %s"),
133 $file_name, $message));
136 sub _dbupdate2_calculate_depth {
137 $main::lxdebug->enter_sub();
139 my ($tree, $tag) = @_;
141 my $node = $tree->{$tag};
143 return $main::lxdebug->leave_sub() if (defined($node->{"depth"}));
147 foreach $tag (@{$node->{"depends"}}) {
148 _dbupdate2_calculate_depth($tree, $tag);
149 my $value = $tree->{$tag}->{"depth"};
150 $max_depth = $value if ($value > $max_depth);
153 $node->{"depth"} = $max_depth + 1;
155 $main::lxdebug->leave_sub();
158 sub sort_dbupdate_controls {
160 sort({ $a->{"depth"} != $b->{"depth"} ? $a->{"depth"} <=> $b->{"depth"} :
161 $a->{"priority"} != $b->{"priority"} ?
162 $a->{"priority"} <=> $b->{"priority"} :
163 $a->{"tag"} cmp $b->{"tag"} } values(%{$_[0]}));