From a93b3b648ec14f94c87176bb8d174da005446d97 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 13 Jan 2011 12:10:56 +0100 Subject: [PATCH] Hilfsfunktionen zum Mappen von Tabellennamen auf Paketnamen und umgekehrt Conflicts: SL/DB/Helper/Mappings.pm --- SL/DB/Helper/Mappings.pm | 66 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/SL/DB/Helper/Mappings.pm b/SL/DB/Helper/Mappings.pm index 3ffd58561..2b79bfe36 100644 --- a/SL/DB/Helper/Mappings.pm +++ b/SL/DB/Helper/Mappings.pm @@ -3,6 +3,10 @@ package SL::DB::Helper::Mappings; use utf8; use strict; +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names); + # these will not be managed as Rose::DB models, because they are not normalized, # significant changes are needed to get them done, or they were done by CRM. my @lxoffice_blacklist_permanent = qw( @@ -90,6 +94,8 @@ my %lxoffice_package_names = ( vendortax => 'vendor_tax', ); +my (%lxoffice_tables_to_packages, %lxoffice_tables_to_manager_packages, %lxoffice_packages_to_tables); + sub get_blacklist { return LXOFFICE => \@lxoffice_blacklist; } @@ -98,6 +104,28 @@ sub get_package_names { return LXOFFICE => \%lxoffice_package_names; } +sub get_package_for_table { + %lxoffice_tables_to_packages = map { ($_ => "SL::DB::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names + unless %lxoffice_tables_to_packages; + + return $lxoffice_tables_to_packages{ $_[0] }; +} + +sub get_manager_package_for_table { + %lxoffice_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names + unless %lxoffice_tables_to_manager_packages; + + return $lxoffice_tables_to_manager_packages{ $_[0] }; +} + +sub get_table_for_package { + get_package_for_table('dummy') if !%lxoffice_tables_to_packages; + %lxoffice_packages_to_tables = reverse %lxoffice_tables_to_packages unless %lxoffice_packages_to_tables; + + my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0]; + return $lxoffice_packages_to_tables{ $package }; +} + sub db { my $string = $_[0]; my $lookup = $lxoffice_package_names{$_[0]} || @@ -145,6 +173,8 @@ sub singlify { __END__ +=encoding utf8 + =head1 NAME SL::DB::Helper::Mappings - Rose Table <-> Model mapping information @@ -159,9 +189,13 @@ This modul stores table <-> model mappings used by the L script. If you add a new table that has custom mappings, add it here. -=head2 db +=head1 FUNCTIONS -A special function provided here is E. Without it you'd have to write: +=over 4 + +=item C + +A special function provided here is C. Without it you'd have to write: my $part = SL::DB::Part->new(id => 1234); my @all_parts = SL::DB::Manager::Part->get_all; @@ -177,6 +211,31 @@ simple s at the end will get you the associated Manager class. db is written to try to make sense of what you give it, but if all fails, it will die with an error. +=item C + +Returns the package name for a table name: + + SL::DB::Helpers::Mappings::get_package_for_table('oe') + # SL::DB::Order + +=item C + +Returns the manager package name for a table name: + + SL::DB::Helpers::Mappings::get_manager_package_for_table('oe') + # SL::DB::Manager::Order + +=item C + +Returns the table name for a package name: + + SL::DB::Helpers::Mappings::get_table_for_package('SL::DB::Order') + # oe + SL::DB::Helpers::Mappings::get_table_for_package('Order') + # oe + +=back + =head1 BUGS nothing yet @@ -187,6 +246,7 @@ L =head1 AUTHOR -Sven Schöling +Sven Schöling Es.schoeling@linet-services.deE, +Moritz Bunkus Em.bunkus@linet-services.deE =cut -- 2.20.1