--- Pg/Scheme.pm 2003/08/12 20:38:06 1.2 +++ Pg/Scheme.pm 2003/10/28 18:56:55 1.4 @@ -106,24 +106,31 @@ my $oid = $self->get_table_oid($table); + # get table description my $sql=" SELECT a.attname, - pg_catalog.format_type(a.atttypid, a.atttypmod), + pg_catalog.format_type(a.atttypid, a.atttypmod) as format_type, a.attnotnull, a.atthasdef, a.attnum FROM pg_catalog.pg_attribute a WHERE a.attrelid = ? AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum "; + # get default value + my $sql_def=" + SELECT adsrc as def FROM pg_catalog.pg_attrdef + WHERE adrelid = ? and adnum=? + "; + my @cols; # all colums (for insert) my @cols_null; # colums compared by a=b or a is null and b is null my @cols_notnull;# colums compared by a=b my $sth = $self->{'dbh'}->prepare($sql); + my $sth_def = $self->{'dbh'}->prepare($sql_def); $sth->execute($oid) || die; while(my $row = $sth->fetchrow_hashref()) { # attname | format_type | attnotnull | atthasdef | attnum - push @pg_attribute, $row; push @cols,$row->{attname}; if ($row->{attnotnull}) { @@ -132,6 +139,13 @@ push @cols_null,$row->{attname}; } + if ($row->{atthasdef}) { + $sth_def->execute($oid,$row->{attnum}) || die; + my $row_def = $sth_def->fetchrow_hashref() || die "can't get attribute '",$row->{attname},"' default value!"; + $row->{default} = $row_def->{def}; + } + + push @pg_attribute, $row; } @{$self->{'explained'}->{$table}->{'pg_attribute'}} = @pg_attribute; @@ -191,6 +205,26 @@ @{$self->{'explained'}->{$table}->{cols_pk}} = @cols_pk; @{$self->{'explained'}->{$table}->{cols_notpk}} = @cols_notpk; + # find triggers + + my @triggers; + + $sql =" +SELECT t.tgname +FROM pg_catalog.pg_trigger t +WHERE t.tgrelid = ? and (not tgisconstraint OR NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d JOIN + pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) WHERE d.classid = t.tableoid AND d.objid = + t.oid AND d.deptype = 'i' AND c.contype = 'f')) + "; + + $sth = $self->{'dbh'}->prepare($sql); + $sth->execute($oid) || die; + while(my $row = $sth->fetchrow_hashref()) { + push @triggers,$row; + } + + @{$self->{'explained'}->{$table}->{'triggers'}} = @triggers; + return @pg_attribute; } @@ -270,4 +304,40 @@ return $self->{'explained'}->{$table}->{cols_notpk}; } + +# get active triggers +sub get_activetriggers { + my $self = shift; + + # find table oid + my $sql = " + SELECT tgname FROM pg_trigger + WHERE tgname not like 'pg_%' and tgenabled IS TRUE + "; + + my $sth = $self->{'dbh'}->prepare($sql); + $sth->execute() || die; + + my @triggers; + + while (my ($tr) = $sth->fetchrow_array()) { + push @triggers,$tr; + } + $sth->finish(); + + return @triggers; +} + +# return triggers +sub triggers { + my $self = shift; + my $table = shift; + + if (! $self->{'explained'}->{$table}) { + $self->explain($table); + } + + return $self->{'explained'}->{$table}->{triggers}; +} + 1;