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 next if ($control->{ignore});
52 $control->{charset} ||= Common::DEFAULT_CHARSET;
54 if (!$control->{"tag"}) {
55 _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ;
58 if ($control->{"tag"} =~ /[^a-zA-Z0-9_\(\)\-]/) {
59 _control_error($form, $file_name, $locale->text("The 'tag' field must only consist of alphanumeric characters or the carachters - _ ( )"))
62 if (defined($all_controls{$control->{"tag"}})) {
63 _control_error($form, $file_name, sprintf($locale->text("More than one control file with the tag '%s' exist."), $control->{"tag"}))
66 if (!$control->{"description"}) {
67 _control_error($form, $file_name, sprintf($locale->text("Missing 'description' field."))) ;
70 $control->{"priority"} *= 1;
71 $control->{"priority"} ||= 1000;
72 $control->{"file"} = $file;
74 delete @{$control}{qw(depth applied)};
76 $all_controls{$control->{"tag"}} = $control;
81 foreach my $control (values(%all_controls)) {
82 foreach my $dependency (@{$control->{"depends"}}) {
83 _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency}));
86 map({ $_->{"loop"} = 0; } values(%all_controls));
87 _check_for_loops($form, $control->{"file"}, \%all_controls, $control->{"tag"});
90 map({ _dbupdate2_calculate_depth(\%all_controls, $_->{"tag"}) }
91 values(%all_controls));
93 $main::lxdebug->leave_sub();
95 return \%all_controls;
98 sub _check_for_loops {
99 my ($form, $file_name, $controls, $tag, @path) = @_;
103 my $ctrl = $controls->{$tag};
105 if ($ctrl->{"loop"} == 1) {
107 _control_error($form, $file_name, $main::locale->text("Dependency loop detected:") . " " . join(" -> ", @path))
109 } elsif ($ctrl->{"loop"} == 0) {
112 map({ _check_for_loops($form, $file_name, $controls, $_, @path); } @{ $ctrl->{"depends"} });
118 my ($form, $file_name, $message) = @_;
121 my $locale = $main::locale;
123 $form->error(sprintf($locale->text("Error in database control file '%s': %s"), $file_name, $message));
126 sub _dbupdate2_calculate_depth {
127 $main::lxdebug->enter_sub();
129 my ($tree, $tag) = @_;
131 my $node = $tree->{$tag};
133 return $main::lxdebug->leave_sub() if (defined($node->{"depth"}));
137 foreach $tag (@{$node->{"depends"}}) {
138 _dbupdate2_calculate_depth($tree, $tag);
139 my $value = $tree->{$tag}->{"depth"};
140 $max_depth = $value if ($value > $max_depth);
143 $node->{"depth"} = $max_depth + 1;
145 $main::lxdebug->leave_sub();
148 sub sort_dbupdate_controls {
149 return sort({ $a->{"depth"} != $b->{"depth"} ? $a->{"depth"} <=> $b->{"depth"}
150 : $a->{"priority"} != $b->{"priority"} ? $a->{"priority"} <=> $b->{"priority"}
151 : $a->{"tag"} cmp $b->{"tag"} } values(%{$_[0]}));