MINI MINI MANI MO

Path : /opt/oracle/product/18c/dbhomeXE/R/migration/exp/
File Upload :
Current File : //opt/oracle/product/18c/dbhomeXE/R/migration/exp/oremigcommon.pm

# 
# $Header: oracler/migration/exp/oremigcommon.pm /st_oracler_1.5.1.0.1/1 2017/06/20 18:00:39 qinwan Exp $
#
# oremigcommon.pl
# 
# Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      oremigcommon.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    qinwan      06/19/17 - remove shebang line
#    ffeli       02/18/16 - ORE 1.5.1 upgrade
#    lbzhang     12/15/14 - ORE 1.5
#    kpsyed      06/16/14 - Fixing ORE Version Verification
#    gayyappa    01/17/14 - Creation
# 
#
package oracle::oremigcommon;

use strict;
use warnings;
use Exporter;
use File::Spec;

our @EXPORT = qw( sanityChecks checkcmd checkcmdAndDie findoraerr getScriptSuffix convertShToBat);
 
#returns $SQLPLUS, $MIGPWD variables
sub sanityChecks
{
  my ($SQLPLUS, $MIGPWD, $ORAHOME, $MIGSRCDIR );
  my ($miguser, $connect_str, $migdir) = @_ ;
  my ($count, $sqlplus);
  my $connect=0;
  if (defined $ENV{'ORACLE_HOME'})
  {
     $ORAHOME= $ENV{'ORACLE_HOME'};
     if ( ($^O eq 'MSWin32') || ($^O eq 'dos'))
     { 
       $ORAHOME=~ s/\\/\//g;
     }
  }
  else
  {
    die("ORACLE_HOME is not set");
  }
#  $MIGSRCDIR="$ORAHOME/R/migration/";
  $MIGSRCDIR=File::Spec->catdir($ORAHOME, qw( R migration) );
  if( ! -d "$migdir" )
  {
    print("Error => Directory $migdir does not exist.Please create this directory and try again \n");
    die("$!");
  }
 
  print( "Checking connection to $connect_str.........\n");
  my $tmpexit_in= File::Spec->catfile($migdir, 'tmpexit.sql');
  open FILE , ">", $tmpexit_in or die $!;
  print FILE "exit;\n" ;
  close FILE;  

  for( $count=0; $connect == 0  && $count < 3; $count++)
  {
    my $sqlplus_str= File::Spec->catdir($ORAHOME, qw( bin sqlplus));
    my $migsql = File::Spec->catfile($migdir, 'tmpexit.sql'); 
    my $miglog = File::Spec->catfile($migdir, 'tmpexit.log'); 
    print " Enter db user $miguser password:";
    $MIGPWD = <STDIN>;
    chomp $MIGPWD;   
    $SQLPLUS="$sqlplus_str -L -S $miguser/$MIGPWD\@\"$connect_str\"";
    $sqlplus="$SQLPLUS \@$migsql > $miglog";
    if( system('$sqlplus') == 0 )
    {
      $connect = 1;
    } 
  }
  if( $count == 3 )
  {
    die("Connect to db failed $sqlplus\n");
  }
  print "Connect to db connect_str .....   Pass\n";
  &versionCheck($SQLPLUS, $migdir);

  return($SQLPLUS, $MIGPWD, $ORAHOME, $MIGSRCDIR );
}

sub checkcmd
{
  my($cmd, $mesg)=@_;
  if ( system($cmd) != 0 )
  {
    print("$mesg :".$cmd."\n");
  }
}

sub checkcmdAndDie
{
  my($cmd, $mesg)=@_; 
  if ( system($cmd) != 0 )
  {
    die("$mesg :".$cmd."\n");
  }
}

#####check version of ORE
#called by sanityCheck
#pass in $SQLPLUS as argument
sub versionCheck
{
  my ($SQLPLUS, $migdir) = @_;
  my $tmpver_in=File::Spec->catfile($migdir, 'tmpver.sql');
  my $tmpver_log=File::Spec->catfile($migdir, 'tmpver.log');
  my $sqlplus;
  my $ver=0;
  
  print( "Checking ORE version .........");
  open FILE , ">", $tmpver_in or die $!;
  print FILE "set echo off\n ";
  print FILE "set heads off\n ";
  print FILE "set heading off\n ";
  print FILE "set feedback off\n ";
  print FILE "set serveroutput on\n ";
  print FILE "set timing off\n ";
  print FILE "declare\n ";
  print FILE "  res number;\n ";
  print FILE "  ver varchar2(100);\n ";
  print FILE "  ore boolean;\n ";
  print FILE "begin\n ";
  print FILE "  ver := '1.0';\n ";
  print FILE "  begin\n ";
  print FILE "    select 1 into res\n ";
  print FILE "    from   dba_users\n ";
  print FILE "    where  username = 'RQSYS';\n ";
  print FILE "\n ";
  print FILE "    ore := TRUE;\n ";
  print FILE "  exception\n ";
  print FILE "    when no_data_found then\n ";
  print FILE "      ore := FALSE;\n ";
  print FILE "  end;\n ";
  print FILE "\n ";
  print FILE "  if ore then\n ";
  print FILE "      execute immediate\n ";
  print FILE "        'select value '||\n ";
  print FILE "        'from   sys.rq_config '||\n ";
  print FILE "        'where  name = :ver'\n ";
  print FILE "      into ver using 'VERSION';\n ";
  print FILE "  end if;\n ";
  print FILE "  dbms_output.put_line(ver);\n ";
  print FILE "end;\n ";
  print FILE "/\n ";
  print FILE "exit;\n ";
  close FILE;  
  
  $sqlplus= $SQLPLUS." \@$tmpver_in > $tmpver_log";
  checkcmdAndDie($sqlplus, "ORE version check failed");
  open FILE, "<", $tmpver_log or die $!; 
  while ( <FILE> )
  {
    $ver = $_;
    chomp($ver);
  }
  close FILE;
  if( ($ver eq '1.4') || ( $ver eq '1.4.1') || ( $ver eq '1.5') || ( $ver eq '1.5.1'))
  {
    print "Pass\n";
  }
  else
  {
    die("Failed $ver is older than 1.4");
  }
}

sub findoraerr
{
  my ($filel) = @_;
  my( $output, $findcmd);
  if ( ($^O eq 'MSWin32') || ($^O eq 'dos') )
  {
     $findcmd = "findstr /S \"ORA\" $filel";
  }
  else
  {
     $findcmd = "grep ORA $filel";
  }
  $output = `$findcmd`;
  return length($output); 
}

sub getScriptSuffix
{
  my $suff;
  if ( ($^O eq 'MSWin32') || ($^O eq 'dos'))
  { 
    $suff='bat';
  }
  else
  {
    $suff='sh';
  }
  return $suff; 
}

sub convertShToBat
{
  my ($filename) = @_;
  my $batfile = $filename;
  my ($mfile, $line);
  $batfile =~ s/\.sh/\.bat/g;
  open FILE, "<", $filename or die $!;
  open $mfile,">",$batfile or die $!;
  while(<FILE>)
  {
     $line = $_;
     $line =~ s/\$1/%1/g;
     $line =~ s/\$2/%2/g;
     $line =~ s/\$3/%3/g;
     $line =~ s/\\\$/\$/g;
     print $mfile "$line";
  }
  close FILE;
  close $mfile;
  return $batfile;
}


OHA YOOOO