/[rserv]/bin/MasterAddTable
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/MasterAddTable

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Tue Aug 5 21:10:28 2003 UTC (20 years, 8 months ago) by dpavlin
Branch: MAIN
Changes since 1.2: +22 -13 lines
command line options cleanup and cosistency fix, modification to work
under "use strict;"

1 #!/usr/bin/perl -w
2 # MasterAddTable
3 # Vadim Mikheev, (c) 2000, PostgreSQL Inc.
4
5 eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
6 & eval 'exec perl -S $0 $argv:q'
7 if 0;
8
9 use strict;
10 use Pg;
11 use Getopt::Long;
12
13 $| = 1;
14
15 my ($debug,$verbose) = (0,0);
16 my ($help,$masterhost,$masterport,$masteruser,$masterpassword);
17
18 my $result = GetOptions(
19 "debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help,
20 "masterhost=s" => \$masterhost, "masterport=i" => \$masterport,
21 "masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword,
22 );
23
24 if (defined($help) || (scalar(@ARGV) < 3)) {
25 print "Usage: $0 [options] masterdb table column
26 Options:
27 --masterhost=hostname --masterport=port
28 --masteruser=username --masterpassword=string
29 ";
30 exit ((scalar(@ARGV) < 3)? 1:0);
31 }
32
33 my $dbname = $ARGV[0];
34 my $table = $ARGV[1];
35 my $keyname = $ARGV[2];
36
37 my $minfo = "dbname=$dbname";
38 $minfo = "$minfo host=$masterhost" if (defined($masterhost));
39 $minfo = "$minfo port=$masterport" if (defined($masterport));
40 $minfo = "$minfo user=$masteruser" if (defined($masteruser));
41 $minfo = "$minfo password=$masterpassword" if (defined($masterpassword));
42
43 my $conn = Pg::connectdb($minfo);
44 if ($conn->status != PGRES_CONNECTION_OK) {
45 print STDERR "Failed opening $minfo\n";
46 exit 1;
47 }
48
49 $result = $conn->exec("BEGIN");
50 die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK;
51
52 $result = $conn->exec("select pgc.oid, pga.attnum from pg_class pgc" .
53 ", pg_attribute pga where pgc.relname = '$table'" .
54 " and pgc.oid = pga.attrelid" .
55 " and pga.attname = '$keyname'");
56 die $conn->errorMessage if $result->resultStatus ne PGRES_TUPLES_OK;
57
58 my @row = $result->fetchrow;
59 die "Can't find table/key\n" if ! defined $row[0] || ! defined $row[1];
60
61 $result = $conn->exec("create trigger _RSERV_TRIGGER_T_ after" .
62 " insert or update or delete on \"$table\"" .
63 " for each row execute procedure" .
64 " _rserv_log_('$row[1]')");
65 die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK;
66
67 $result = $conn->exec("insert into _RSERV_TABLES_ (tname, cname, reloid, key)" .
68 " values ('$table', '$keyname', $row[0], $row[1])");
69 die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK;
70
71 $result = $conn->exec("COMMIT");
72 die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK;
73
74 exit(0);

  ViewVC Help
Powered by ViewVC 1.1.26