6 unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
7 push @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
10 use CGI qw( -no_xhtml);
13 use English qw( -no_match_vars );
14 use List::MoreUtils qw(any);
22 use SL::DB::Helper::ALL;
23 use SL::DB::Helper::Mappings;
30 our $script = __FILE__;
33 $OUTPUT_AUTOFLUSH = 1;
34 $Data::Dumper::Sortkeys = 1;
36 our $meta_path = "SL/DB/MetaSetup";
40 print "Usage: $PROGRAM_NAME login table1[=package1] [table2[=package2] ...]\n";
41 print " or $PROGRAM_NAME login [--all|-a] [--sugar|-s]\n";
45 read_config "config/lx_office.conf" => %lx_office_conf;
47 my $login = shift @ARGV;
49 $::lxdebug = LXDebug->new();
52 $::locale = Locale->new("de");
55 $::auth = SL::Auth->new();
57 $::user = User->new($login);
59 %::myconfig = $auth->read_user($login);
60 $form->{script} = 'rose_meta_data.pl';
61 $form->{login} = $login;
63 map { $form->{$_} = $::myconfig{$_} } qw(stylesheet charset);
65 mkdir $meta_path unless -d $meta_path;
69 my @spec = split(/=/, shift, 2);
72 ($schema, $table) = split(m/\./, $table) if $table =~ m/\./;
73 my $package = ucfirst($spec[1] || $spec[0]);
74 $package =~ s/_+(.)/uc($1)/ge;
75 my $meta_file = "${meta_path}/${package}.pm";
76 my $file = "SL/DB/${package}.pm";
78 $schema = <<CODE if $schema;
79 __PACKAGE__->meta->schema('$schema');
82 my $definition = eval <<CODE;
83 package SL::DB::AUTO::$package;
85 use base qw(SL::DB::Object);
87 __PACKAGE__->meta->table('$table');
89 __PACKAGE__->meta->auto_initialize;
91 __PACKAGE__->meta->perl_class_definition(indent => 2); # , braces => 'bsd'
95 print STDERR "Error in execution for table '$table': $EVAL_ERROR";
99 $definition =~ s/::AUTO::/::/g;
101 my $file_exists = -f $meta_file;
103 open(OUT, ">$meta_file") || die;
105 # This file has been auto-generated. Do not modify it; it will be overwritten
106 # by $::script automatically.
111 print "File '$meta_file' " . ($file_exists ? 'updated' : 'created') . " for table '$table'\n";
114 open(OUT, ">$file") || die;
116 # This file has been auto-generated only because it didn't exist.
117 # Feel free to modify it at will; it will not be overwritten automatically.
119 package SL::DB::${package};
123 use SL::DB::MetaSetup::${package};
125 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
126 __PACKAGE__->meta->make_manager_class;
132 print "File '$file' created as well.\n";
138 my %blacklist = SL::DB::Helper::Mappings->get_blacklist;
139 my %package_names = SL::DB::Helper::Mappings->get_package_names;
142 if (($ARGV[0] eq '--all') || ($ARGV[0] eq '-a') || ($ARGV[0] eq '--sugar') || ($ARGV[0] eq '-s')) {
143 my ($type, $prefix) = ($ARGV[0] eq '--sugar') || ($ARGV[0] eq '-s') ? ('SUGAR', 'sugar_') : ('LXOFFICE', '');
144 my $db = SL::DB::create(undef, $type);
145 @tables = map { $package_names{$type}->{$_} ? "${_}=" . $package_names{$type}->{$_} : $prefix ? "${_}=${prefix}${_}" : $_ }
146 grep { my $table = $_; !any { $_ eq $table } @{ $blacklist{$type} } }
153 foreach my $table (@tables) {
154 # add default model name unless model name is given or no defaults exists
155 $table .= '=' . $package_names{LXOFFICE}->{lc $table} if $table !~ /=/ && $package_names{LXOFFICE}->{lc $table};
157 process_table($table);