Revision 23711649
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
scripts/rose_auto_create_model.pl | ||
---|---|---|
14 | 14 |
use English qw( -no_match_vars ); |
15 | 15 |
use Getopt::Long; |
16 | 16 |
use List::MoreUtils qw(none); |
17 |
use List::UtilsBy qw(partition_by); |
|
17 | 18 |
use Pod::Usage; |
18 | 19 |
use Rose::DB::Object 0.809; |
19 | 20 |
use Term::ANSIColor; |
... | ... | |
87 | 88 |
} |
88 | 89 |
|
89 | 90 |
sub process_table { |
90 |
my @spec = @_; |
|
91 |
my $table = $spec[0]; |
|
91 |
my ($domain, $table, $package) = @_; |
|
92 | 92 |
my $schema = ''; |
93 | 93 |
($schema, $table) = split(m/\./, $table) if $table =~ m/\./; |
94 |
my $package = ucfirst($spec[1] || $spec[0]);
|
|
94 |
$package = ucfirst($package || $table);
|
|
95 | 95 |
$package =~ s/_+(.)/uc($1)/ge; |
96 | 96 |
my $meta_file = "${meta_path}/${package}.pm"; |
97 | 97 |
my $mngr_file = "${manager_path}/${package}.pm"; |
... | ... | |
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
sub make_tables { |
289 |
my @tables;
|
|
289 |
my %tables_by_domain;
|
|
290 | 290 |
if ($config{all}) { |
291 |
my $db = SL::DB::create(undef, 'KIVITENDO'); |
|
292 |
@tables = grep { my $table = $_; none { $_ eq $table } @{ $blacklist{KIVITENDO} } } $db->list_tables; |
|
291 |
foreach my $domain (sort keys %package_names) { |
|
292 |
my $db = SL::DB::create(undef, $domain); |
|
293 |
$tables_by_domain{$domain} = [ grep { my $table = $_; none { $_ eq $table } @{ $blacklist{$domain} } } $db->list_tables ]; |
|
294 |
$db->disconnect; |
|
295 |
} |
|
293 | 296 |
|
294 | 297 |
} elsif (@ARGV) { |
295 |
@tables = @ARGV; |
|
298 |
%tables_by_domain = partition_by { |
|
299 |
my ($domain, $table) = split m{:}; |
|
300 |
$table ? uc($domain) : 'KIVITENDO'; |
|
301 |
} @ARGV; |
|
302 |
|
|
303 |
foreach my $tables (values %tables_by_domain) { |
|
304 |
s{.*:}{} for @{ $tables }; |
|
305 |
} |
|
306 |
|
|
296 | 307 |
} else { |
297 | 308 |
error("You specified neither --all nor any specific tables."); |
298 | 309 |
usage(); |
299 | 310 |
} |
300 | 311 |
|
301 |
@tables;
|
|
312 |
return %tables_by_domain;
|
|
302 | 313 |
} |
303 | 314 |
|
304 | 315 |
sub error { |
... | ... | |
311 | 322 |
|
312 | 323 |
parse_args(\%config); |
313 | 324 |
setup(); |
314 |
my @tables = make_tables(); |
|
315 | 325 |
|
316 |
my @unknown_tables = grep { !$package_names{KIVITENDO}->{$_} } @tables; |
|
317 |
if (@unknown_tables) { |
|
318 |
error("The following tables do not have entries in \%SL::DB::Helper::Mappings::kivitendo_package_names: " . join(' ', sort @unknown_tables)); |
|
319 |
exit 1; |
|
320 |
} |
|
326 |
my %tables_by_domain = make_tables(); |
|
321 | 327 |
|
322 |
process_table($_, $package_names{KIVITENDO}->{$_}) for @tables; |
|
328 |
foreach my $domain (keys %tables_by_domain) { |
|
329 |
my @tables = @{ $tables_by_domain{$domain} }; |
|
330 |
my @unknown_tables = grep { !$package_names{$domain}->{$_} } @tables; |
|
331 |
if (@unknown_tables) { |
|
332 |
error("The following tables do not have entries in \%SL::DB::Helper::Mappings::${domain}_package_names: " . join(' ', sort @unknown_tables)); |
|
333 |
exit 1; |
|
334 |
} |
|
335 |
|
|
336 |
process_table($domain, $_, $package_names{$domain}->{$_}) for @tables; |
|
337 |
} |
|
323 | 338 |
|
324 | 339 |
1; |
325 | 340 |
|
... | ... | |
333 | 348 |
|
334 | 349 |
=head1 SYNOPSIS |
335 | 350 |
|
336 |
scripts/rose_auto_create_model.pl --client name-or-id table1 [table2 ...]
|
|
351 |
scripts/rose_auto_create_model.pl --client name-or-id [db1:]table1 [[db2:]table2 ...]
|
|
337 | 352 |
scripts/rose_auto_create_model.pl --client name-or-id [--all|-a] |
338 | 353 |
|
339 | 354 |
# updates all models |
... | ... | |
389 | 404 |
file. The first one will be updated if the schema changes, the second |
390 | 405 |
and third ones will only be created if it they do not exist. |
391 | 406 |
|
407 |
=head1 DATABASE NAMES AND TABLES |
|
408 |
|
|
409 |
If you want to generate the data for specific tables only then you |
|
410 |
have to list them on the command line. The format is |
|
411 |
C<db-name:table-name>. The part C<db-name:> is optional and defaults |
|
412 |
to C<KIVITENDO:> – which means the tables in the default kivitendo |
|
413 |
database. |
|
414 |
|
|
415 |
Valid database names are keys in the hash returned by |
|
416 |
L<SL::DB::Helper::Mappings/get_package_names>. |
|
417 |
|
|
392 | 418 |
=head1 OPTIONS |
393 | 419 |
|
394 | 420 |
=over 4 |
Auch abrufbar als: Unified diff
Rose-Model-Generator: prinzipielle Unterstützung verschiedener Datenbanken
Um Models für andere Datenbanken zu erstellen, müssen dann noch SL::DB,
SL::DB::Helper::Mappings und SL::DB::Object entsprechend angepasst
werden, damit die Verbindung richtig aufgebaut werden.