1 |
# -*- perl -*- |
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 Pg; |
10 |
use Getopt::Long; |
11 |
|
12 |
$| = 1; |
13 |
|
14 |
$result = GetOptions("debug!", "verbose!", "help", |
15 |
"host=s", "user=s", "password=s"); |
16 |
|
17 |
my $debug = $opt_debug || 0; |
18 |
my $verbose = $opt_verbose || 0; |
19 |
|
20 |
if (defined($opt_help) || (scalar(@ARGV) < 3)) { |
21 |
print "Usage: $0 --host=name --user=name --password=string masterdb table column\n"; |
22 |
exit ((scalar(@ARGV) < 3)? 1: 0); |
23 |
} |
24 |
|
25 |
my $dbname = $ARGV[0]; |
26 |
my $table = $ARGV[1]; |
27 |
my $keyname = $ARGV[2]; |
28 |
|
29 |
my $minfo = "dbname=$dbname"; |
30 |
$minfo = "$minfo host=$opt_host" if (defined($opt_host)); |
31 |
$minfo = "$minfo user=$opt_user" if (defined($opt_user)); |
32 |
$minfo = "$minfo password=$opt_password" if (defined($opt_password)); |
33 |
|
34 |
my $conn = Pg::connectdb($minfo); |
35 |
|
36 |
my $result = $conn->exec("BEGIN"); |
37 |
die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK; |
38 |
|
39 |
$result = $conn->exec("select pgc.oid, pga.attnum from pg_class pgc" . |
40 |
", pg_attribute pga where pgc.relname = '$table'" . |
41 |
" and pgc.oid = pga.attrelid" . |
42 |
" and pga.attname = '$keyname'"); |
43 |
die $conn->errorMessage if $result->resultStatus ne PGRES_TUPLES_OK; |
44 |
|
45 |
my @row = $result->fetchrow; |
46 |
die "Can't find table/key\n" if ! defined $row[0] || ! defined $row[1]; |
47 |
|
48 |
$result = $conn->exec("create trigger _RSERV_TRIGGER_T_ after" . |
49 |
" insert or update or delete on $table" . |
50 |
" for each row execute procedure" . |
51 |
" _rserv_log_('$row[1]')"); |
52 |
die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK; |
53 |
|
54 |
$result = $conn->exec("insert into _RSERV_TABLES_ (tname, cname, reloid, key)" . |
55 |
" values ('$table', '$keyname', $row[0], $row[1])"); |
56 |
die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK; |
57 |
|
58 |
$result = $conn->exec("COMMIT"); |
59 |
die $conn->errorMessage if $result->resultStatus ne PGRES_COMMAND_OK; |
60 |
|
61 |
exit(0); |