Created
November 15, 2012 07:36
-
-
Save nobuhito/4077227 to your computer and use it in GitHub Desktop.
Oracleのビューをテキストファイルに出力
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use DBI; | |
use FindBin; | |
use Digest::MD5; | |
# use Data::Dumper; | |
my $host = 'host'; # ホスト名 | |
my $user = 'user'; # ユーザー名 | |
my $pass = 'pass'; # パスワード | |
my $sid = 'sid'; # SID | |
my @shemas = qw/SCOTT/; # スキーマ | |
my $dbh = DBI->connect( | |
"dbi:Oracle:host=$host;sid=$sid", | |
$user, $pass, { | |
LongReadLen => 1024 * 1024 * 10}) | |
or die "Unable to connect: $DBI::errstr"; | |
foreach my $schema (@schemas) { | |
my $sql =<<EOF; | |
-- SQL -- | |
SELECT VIEW_NAME | |
,TEXT | |
FROM SYS.ALL_VIEWS | |
WHERE OWNER = '$schema' | |
EOF | |
my $sth = $dbh->prepare($sql); | |
$sth->execute(); | |
while (my $result = $sth->fetchrow_hashref) { | |
if ($result) { | |
my $view_name = $result->{VIEW_NAME}; | |
my $filename = $schema . "." . $view_name . ".sql"; | |
my $file_exist = 0; | |
my $file_md5 = Digest::MD5->new; | |
if (-e $filename) { | |
open FILE, $filename; | |
$file_md5->addfile(*FILE); | |
close FILE; | |
$file_exist = 1 | |
} | |
my $text = "CREATE OR REPLACE VIEW $schema.$view_name AS\n"; | |
$text .= $result->{TEXT}; | |
my $text_md5 = Digest::MD5->new; | |
$text_md5->add($text); | |
if ($file_exist = 0 || $file_md5->digest ne $text_md5->digest) { | |
open OUT, "> $filename" or die "Can't open file $filename"; | |
print OUT $text; | |
close OUT; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment