1 package SL::DBUpgrade2;
6 our @ISA = qw(Exporter);
8 our @EXPORT = qw(parse_dbupdate_controls sort_dbupdate_controls);
12 sub parse_dbupdate_controls {
13 $main::lxdebug->enter_sub();
15 my ($form, $dbdriver) = @_;
17 my $locale = $main::locale;
22 my $path = "sql/${dbdriver}-upgrade2";
24 foreach my $file_name (<$path/*.sql>, <$path/*.pl>) {
25 next unless (open(IN, $file_name));
27 my $file = $file_name;
37 next unless (/^(--|\#)\s*\@/);
42 my @fields = split(/\s*:\s*/, $_, 2);
43 next unless (scalar(@fields) == 2);
45 if ($fields[0] eq "depends") {
46 push(@{$control->{"depends"}}, split(/\s+/, $fields[1]));
48 $control->{$fields[0]} = $fields[1];
52 next if ($control->{ignore});
54 $control->{charset} ||= Common::DEFAULT_CHARSET;
56 if (!$control->{"tag"}) {
57 _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ;
60 if ($control->{"tag"} =~ /[^a-zA-Z0-9_\(\)\-]/) {
61 _control_error($form, $file_name, $locale->text("The 'tag' field must only consist of alphanumeric characters or the carachters - _ ( )"))
64 if (defined($all_controls{$control->{"tag"}})) {
65 _control_error($form, $file_name, sprintf($locale->text("More than one control file with the tag '%s' exist."), $control->{"tag"}))
68 if (!$control->{"description"}) {
69 _control_error($form, $file_name, sprintf($locale->text("Missing 'description' field."))) ;
72 $control->{"priority"} *= 1;
73 $control->{"priority"} ||= 1000;
74 $control->{"file"} = $file;
76 delete @{$control}{qw(depth applied)};
78 $all_controls{$control->{"tag"}} = $control;
83 foreach my $control (values(%all_controls)) {
84 foreach my $dependency (@{$control->{"depends"}}) {
85 _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency}));
88 map({ $_->{"loop"} = 0; } values(%all_controls));
89 _check_for_loops($form, $control->{"file"}, \%all_controls, $control->{"tag"});
92 map({ _dbupdate2_calculate_depth(\%all_controls, $_->{"tag"}) }
93 values(%all_controls));
95 $main::lxdebug->leave_sub();
97 return \%all_controls;
100 sub _check_for_loops {
101 my ($form, $file_name, $controls, $tag, @path) = @_;
105 my $ctrl = $controls->{$tag};
107 if ($ctrl->{"loop"} == 1) {
109 _control_error($form, $file_name, $main::locale->text("Dependency loop detected:") . " " . join(" -> ", @path))
111 } elsif ($ctrl->{"loop"} == 0) {
114 map({ _check_for_loops($form, $file_name, $controls, $_, @path); } @{ $ctrl->{"depends"} });
120 my ($form, $file_name, $message) = @_;
123 my $locale = $main::locale;
125 $form->error(sprintf($locale->text("Error in database control file '%s': %s"), $file_name, $message));
128 sub _dbupdate2_calculate_depth {
129 $main::lxdebug->enter_sub(2);
131 my ($tree, $tag) = @_;
133 my $node = $tree->{$tag};
135 return $main::lxdebug->leave_sub(2) if (defined($node->{"depth"}));
139 foreach $tag (@{$node->{"depends"}}) {
140 _dbupdate2_calculate_depth($tree, $tag);
141 my $value = $tree->{$tag}->{"depth"};
142 $max_depth = $value if ($value > $max_depth);
145 $node->{"depth"} = $max_depth + 1;
147 $main::lxdebug->leave_sub(2);
150 sub sort_dbupdate_controls {
151 return sort({ $a->{"depth"} != $b->{"depth"} ? $a->{"depth"} <=> $b->{"depth"}
152 : $a->{"priority"} != $b->{"priority"} ? $a->{"priority"} <=> $b->{"priority"}
153 : $a->{"tag"} cmp $b->{"tag"} } values(%{$_[0]}));