home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / obo2jdbc.zip / orjdbcobjconverter.pl
Text File  |  2000-10-11  |  9KB  |  382 lines

  1. # Perl script to convert Oracle Business Objects to JDBC.
  2.  
  3. #!/usr/bin/perl
  4.  
  5. bag("Usage: $0 input_file_list(comma separated) output_file") unless @ARGV == 2;
  6.  
  7. my ($infile, $outfile) = @ARGV;
  8. my $inf="";
  9. my $filename="";
  10. my %file_obj_name_cache;
  11. my %hashcache=();
  12.  
  13. my @in_file_list=split(/\,/,$infile);
  14. my @in_file_list1=@in_file_list;
  15.  
  16.  
  17. # Copy the ReposVersion information to the output. It is assumed that 
  18. # ReposVersion.txt is present in the current directory.
  19.  
  20. open(REPOSVER,"ReposVersion.txt") || die "Could not open ReposVersion.txt.\n";
  21. open(OUTPUT,">$outfile");
  22.  
  23. while (<REPOSVER>)
  24. {
  25.     print OUTPUT $_;
  26. }
  27. close(REPOSVER);
  28.  
  29.  
  30. # Cache the objectname => tablename information from the input files.
  31. # Also, the objectname => filename information is cached in this subroutine.
  32.  
  33. cache_object_table_names();
  34.  
  35. # Convert the appspecificinfo from Oracle to JDBC.
  36.  
  37. convert_oracle_to_jdbc();
  38.  
  39.  
  40. sub convert_oracle_to_jdbc
  41. {
  42.     # Open each one of the input files, transform the same to JDBC and write
  43.     # to the output.
  44.  
  45.     while ($inf = shift(@in_file_list1))
  46.     {
  47.     open(INPUT,$inf);
  48.     my $objectname="";
  49.     my $attrname="";
  50.     my $tablename="";
  51.     my $objname="";
  52.  
  53.     while (<INPUT>)
  54.     {
  55.         $eachline=$_;
  56.         if ($eachline = m /BusinessObjectDefinition/)
  57.         {
  58.         $busobj=1;
  59.         $attr=0;
  60.         $verb=0;
  61.         }
  62.         if ($eachline = m /Attribute/)
  63.         {
  64.         $busobj=0;
  65.         $attr=1;
  66.         }
  67.         if ($eachline = m /Verb/)
  68.         {
  69.         $attr=0;
  70.         $verb=1;
  71.         }
  72.  
  73.         # AppSpecificInfo for business object and each attribute gets
  74.         # transformed. AppSpecificInfo for the verb remains as is.
  75.  
  76.         if (($eachline = m /Name/) && ($verb == 0))
  77.         {
  78.         @nameval=split(/=/,$_);
  79.         $val=@nameval[1];
  80.         $val=~s/^\s+//;
  81.         chop($val);
  82.         if ($busobj == 1)
  83.         {
  84.             $objectname=$val;
  85.             
  86.         } else
  87.         {
  88.             $attrname=$val;
  89.         }
  90.         }
  91.         if (($eachline= m /AppSpecificInfo/) && ($verb==0))
  92.         {
  93.         @appspecvals=split(/=/,$_);
  94.         chop(@appspecvals[1]);
  95.         @appspecvals[1]=~s/^\s+//;
  96.  
  97.         # Print to the output in case there is no value
  98.         # specified for appspecificinfo.
  99.  
  100.         if ((length(@appspecvals[1]) == 0) && ($attr == 1))
  101.         {
  102.             print OUTPUT $_;
  103.         } else
  104.         {
  105.             # Build the JDBC syntax for business object AppSpecificInfo.
  106.  
  107.             if ($busobj == 1)
  108.             {
  109.             while (($objname,$tblname) = each(%hashcache))
  110.             {
  111.                 if ($objname eq $objectname)
  112.                 {
  113.                 $tablename=$tblname;
  114.                 }
  115.             }
  116.             if (length(@appspecvals[1]) > 1)
  117.             {
  118.                 print OUTPUT "@appspecvals[0]= SCN=@appspecvals[1];TN=$tablename\n";
  119.             } else
  120.             {
  121.                 print OUTPUT "@appspecvals[0]= TN=$tablename\n";
  122.             }
  123.             
  124.             }
  125.         
  126.             # Transform the attribute AppSpecificInfo.
  127.  
  128.             if ($attr==1)
  129.             {
  130.             @fieldvals=split(/:/,@appspecvals[1]);
  131.  
  132.             # Handle the case for child attributes. Normally,
  133.             # when AppSpecificInfo = KEEP_RELATIONSHIP.
  134.  
  135.             if (@fieldvals[0] eq @appspecvals[1])
  136.             {
  137.                 if (@fieldvals[0] = m /KEEP_RELATIONSHIP/)
  138.                 {
  139.                 print OUTPUT "@appspecvals[0]= KEEP_RELATIONSHIP=true\n";
  140.                 } elsif ((@fieldvals[0] = m /OWNERSHIP/) || (@fieldvals[0] = m /NOOWNERSHIP/))
  141.                 {
  142.                 print OUTPUT "@appspecvals[0]= CONTAINMENT=@appspecvals[1]\n";
  143.                 } else
  144.                 {
  145.                 print OUTPUT $_;
  146.                 }
  147.             } else
  148.             {
  149.                 # Splitting on the delimiter : yields the 
  150.                 # values for each information part.
  151.  
  152.                 # Oracle ASI has the form tablename.columnname.
  153.                     # The same changes to CN=columnname for JDBC.
  154.  
  155.                 if(length(@fieldvals[0]) > 0)
  156.                 {
  157.                 @fieldvals[0]=~s/[a-zA-Z_0-9]*\./CN=/;
  158.                 }
  159.  
  160.                 # Oracle recognises the foreign key with the
  161.                 # foreignkeytablename.fkcolumnname syntax. The
  162.                 # same changes to fkobjectname.fkattributename
  163.                 # for JDBC.
  164.  
  165.                 if(length(@fieldvals[1]) > 0)
  166.                 {
  167.                 ($fktblname,$fkcolname) = split(/\./,@fieldvals[1]);
  168.                 # Look in the cache to obtain the object name
  169.                 # corresponding to fktblname.
  170.                 # If found, look in the objectname => filename
  171.                 # cache to open the corresponding file.
  172.  
  173.                 while (($objname,$tblname) = each(%hashcache))
  174.                 {
  175.                     if ($tblname eq $fktblname)
  176.                     {
  177.                     while (($oname,$fname) = each(%file_obj_name_cache))
  178.                     {
  179.                         if ($oname eq $objname)
  180.                         {
  181.                         $filename = $fname;
  182.                         }
  183.                         
  184.                     }
  185.                     open(FKINPUT,$filename) || die "Could not open foreign key table $filename.txt.\n";
  186.                     $attribute=0;
  187.                     $vb=0;
  188.                     $bo=0;
  189.                     $wrongbo=1;
  190.                     while (<FKINPUT>)
  191.                     {
  192.                         $line=$_;
  193.                         if ($line = m /BusinessObjectDefinition/)
  194.                         {
  195.                         $wrongbo=0;
  196.                         $bo=1;
  197.                         $attribute=0;
  198.                         }
  199.                         if (($bo == 1) && ($attribute == 0) && ($line = m /Name/))
  200.                         {
  201.                         @nameval=split(/=/,$_);
  202.                         chop(@nameval[1]);
  203.                         @nameval[1]=~s/^\s+//;
  204.                         
  205.                         # One file can have more than one
  206.                         # BOs. To process only the correct
  207.                         # object, wrongbo is used.
  208.  
  209.                         if (@nameval[1] ne $objname)
  210.                         {
  211.                             $wrongbo=1;
  212.                             next;
  213.                         }
  214.                         }
  215.                         if ($wrongbo == 0)
  216.                         {
  217.                         $objectname=$objname;
  218.                         if ($line = m /Attribute/)
  219.                         {
  220.                             $attribute=1;
  221.                         }
  222.                         if ($line = m /Verb/)
  223.                         {
  224.                             $vb=1;
  225.                         }
  226.                         # Store the attribute name.
  227.  
  228.                         if (($attribute == 1) && ($line = m /Name/))
  229.                         {
  230.                             @attrvals=split(/=/,$_);
  231.                             $aname=@attrvals[1];
  232.                             chop($aname);
  233.                             $aname=~s/^\s+//;
  234.                         }
  235.  
  236.                         # In case the ASI for the attribute
  237.                         # corresponds to the fkcolname,
  238.                         # the attribute name is set for    
  239.                         # JDBC.
  240.  
  241.                         if (($attribute == 1) && ($line = m /AppSpecificInfo/))
  242.                         {
  243.                             @appspec=split(/=/,$_);
  244.                             @appvals=split(/:/,$appspec[1]);
  245.                             if (@appvals[0] eq $appspec[1])
  246.                             {
  247.                             next;
  248.                             }
  249.                             ($tname,$cname)=split(/\./,@appvals[0]);
  250.                             if ($cname eq $fkcolname)
  251.                             {
  252.                             $attrname=$aname;
  253.                             
  254.                             }
  255.                         }
  256.                     
  257.                         }
  258.                     }
  259.                     }
  260.                 }
  261.                 @fieldvals[1]=~s/@fieldvals[1]/FK=$objectname.$attrname/;
  262.                 }
  263.  
  264.                 # Oracle sequence gets changed to UID=sequence for
  265.                 # JDBC.
  266.  
  267.                 if(length(@fieldvals[2]) > 0)
  268.                 {
  269.                 @fieldvals[2]=~s/@fieldvals[2]/UID=@fieldvals[2]/;
  270.                 }
  271.  
  272.                 # CA= gets added for JDBC for set_attr_name.
  273.  
  274.                 if(length(@fieldvals[3]) > 0)
  275.                 {
  276.                 @fieldvals[3]=~s/@fieldvals[3]/CA=@fieldvals[3]/;
  277.                 }
  278.  
  279.                 # ORDER_BY_DESC of Oracle changes to OB=DESC for JDBC.
  280.                 # ORDER_BY of Oracle changes to OB=ASC for JDBC.
  281.  
  282.                 if(length(@fieldvals[4]) > 0)
  283.                 {
  284.                 @fieldvals[4]=~s/ORDER_BY_DESC/OB=DESC/;
  285.                 @fieldvals[4]=~s/ORDER_BY/OB=ASC/;
  286.                 }
  287.                 
  288.                 # JDBC has an extra delimiter for UNVL specification.
  289.                 # Write the changed ASI to the output.
  290.  
  291.                 print OUTPUT "@appspecvals[0]= $fieldvals[0]:$fieldvals[1]:$fieldvals[2]:$fieldvals[3]:$fieldvals[4]:\n";
  292.             }
  293.             }
  294.         }
  295.         } else
  296.         {
  297.         print OUTPUT $_;
  298.         }
  299.     }
  300.     }
  301. }
  302.  
  303.  
  304.  
  305.  
  306. sub cache_object_table_names 
  307. {
  308.  
  309.     while ($inf = shift(@in_file_list))
  310.     {
  311.     open(INPUT,$inf);
  312.     $busobj=0;
  313.     $firstattr=1;
  314.     $attr=0;
  315.         $objectname="";
  316.         
  317.     while (<INPUT>)
  318.     {
  319.         $eachline=$_;
  320.         if ($eachline = m /BusinessObjectDefinition/)
  321.         {
  322.         $busobj=1;
  323.         $firstattr=1;
  324.         $attr=0;
  325.         }
  326.         if ($eachline = m /Attribute/)
  327.         {
  328.         $busobj=0;
  329.         $attr=1;
  330.         }
  331.         # Indicates the first attribute has been processed. There is no
  332.         # need to process the rest of the attributes as the table name 
  333.         # information has been obtained.
  334.  
  335.         if ($firstattr==0)
  336.         {
  337.         next;
  338.         }
  339.         if ($eachline = m /Attribute/)
  340.         {
  341.         $busobj=0;
  342.         $attr=1;
  343.         }
  344.         # Get the object name using the Name field under BusinessObjectDefinition.
  345.         if ($busobj==1)
  346.         {
  347.         if ($eachline= m/Name/)
  348.         {
  349.             @nameval=split(/=/,$_);
  350.             $objectname=@nameval[1];
  351.             $objectname=~s/^\s+//;
  352.             chop($objectname);
  353.         }
  354.         }
  355.         # Get the tablename information from appspecificinfo of the first
  356.         # attribute.
  357.         if ($attr == 1)
  358.         {
  359.         if ($eachline = m/AppSpecificInfo/)
  360.         {
  361.             @nameval=split(/=/,$_);
  362.             @appspecvals=split(/:/,@nameval[1]);
  363.             ($tablename,$colname)=split(/\./,@appspecvals[0]);
  364.             $tablename=~s/^\s+//;
  365.             $firstattr=0;
  366.             $hashcache{$objectname} = $tablename;
  367.             $file_obj_name_cache{$objectname}=$inf;
  368.             
  369.         }
  370.         }
  371.     }
  372.     }
  373.     close(INPUT);
  374. }
  375.  
  376. sub bag {
  377.   my $msg = shift;
  378.   $msg .= "\n";
  379.   warn $msg;
  380.   exit 2;
  381. }
  382.