#!/usr/bin/perl use Getopt::Std; my %opt; getopts('s:', \%opt) or usage(); my $DB2_HOME = "/home/db2inst"; my $db2lib = "$DB2_HOME/sqllib/java"; my $diagram = lc(shift); my %tableset = load_tablesets(); unless (exists $tableset{$diagram}) { warn "Unknown diagram name '$diagram'\n"; warn "Use one of:\n", join("\n", (map "\t$_", sort keys %tableset)), "\n"; exit 1; } my ($user, $pass, $dsn) = get_credentials(); defined($user) or die "Couldn't get username from credentials file\n"; defined($pass) or die "Couldn't get pass from credentials file\n"; defined($dsn) or die "Couldn't get dsn from credentials file\n"; use URI; my ($host, $port, $db); { # The db2 DSN has the form # jdbc:db2://hostname:port/database # which is the same format as an HTTP URI. Here we replace # "jdbc:db2:" with "http:" to fool the URI module into parsing the # URI as if it were an HTTP URI. $dsn =~ s/^jdbc:db2:/http:/ or die "Malformed dsn '$dsn'; aborting"; my $dsn_url = URI->new($dsn); $host = $dsn_url->host; $port = $dsn_url->port; ( $db = $dsn_url->path ) =~ s{^/}{}; } defined($host) or die "Couldn't get host name from credentials file\n"; defined($port) or die "Couldn't get port from credentials file\n"; defined($db) or die "Couldn't get db name from credentials file\n"; system("java", "-jar", "schemaSpy_4.1.1.jar", "-t", "udbt4", "-host", $host, "-port", $port, "-db", $db, "-s", $opt{s} || $db, "-u", $user, "-p", $pass, "-o", "schema_$diagram", "-dp", "$db2lib/db2jcc.jar:$db2lib/db2jcc_license_cu.jar", "-i", $tableset{$diagram}, ) == 0 or die "schemaSpy failed with exit code " . ($? >> 8); sub get_credentials { my %prop; my $file = get_jdbc_properties_file(); open my($C), "<", $file or die "Couldn't read $file: $!"; while (<$C>) { chomp; next if /^#/; next unless /\A([\w\.]+)=(.*)\z/; $prop{$1} = $2; } return @prop{'jdbc.username', 'jdbc.password', 'jdbc.url'}; } sub get_jdbc_properties_file { return "$ENV{JDBC_HOME}/jdbc.properties"; } sub load_tablesets { my %ts; while () { chomp; my ($k, $v) = split /\s+/, $_, 2; $ts{lc $k} = $v; } return %ts; } __DATA__ all .* study STUDY.*|SUBMISSION|PERSON|USER.*|ANALYSIS.*|ANALYZ.*|CITATION.*|SUB_.*|ALGORITHM|SOFTWARE|GAPMODE|POLYTCOUNT matrix MATRIX|MATRIXDATATYPE|MATRIXKIND|TAXONLABELSET|MATRIXROW.*|TAXONLABEL.*|SUB_MATRIX|GENETICCODESET|ROWSEGMENT.* character CHAR.*|COLUMNRANGE|COD.*|USERTYPE.*|TYPESET|PHYLOCHAR|STATE.*|DISCRETECHARSTATE|ANC.*|.*_CHARSTATE|CSTREENODE|GENETICCODE.* tree PHYLOTREE|PHYLOTREENODE|TREE.*|NODEATTRIBUTE|SUB_TREEBLOCK taxon TAXON.*|SUB_TAXONLABEL