106 |
|
|
107 |
my $oid = $self->get_table_oid($table); |
my $oid = $self->get_table_oid($table); |
108 |
|
|
109 |
|
# get table description |
110 |
my $sql=" |
my $sql=" |
111 |
SELECT a.attname, |
SELECT a.attname, |
112 |
pg_catalog.format_type(a.atttypid, a.atttypmod), |
pg_catalog.format_type(a.atttypid, a.atttypmod) as format_type, |
113 |
a.attnotnull, a.atthasdef, a.attnum |
a.attnotnull, a.atthasdef, a.attnum |
114 |
FROM pg_catalog.pg_attribute a |
FROM pg_catalog.pg_attribute a |
115 |
WHERE a.attrelid = ? AND a.attnum > 0 AND NOT a.attisdropped |
WHERE a.attrelid = ? AND a.attnum > 0 AND NOT a.attisdropped |
116 |
ORDER BY a.attnum |
ORDER BY a.attnum |
117 |
"; |
"; |
118 |
|
|
119 |
|
# get default value |
120 |
|
my $sql_def=" |
121 |
|
SELECT adsrc as def FROM pg_catalog.pg_attrdef |
122 |
|
WHERE adrelid = ? and adnum=? |
123 |
|
"; |
124 |
|
|
125 |
my @cols; # all colums (for insert) |
my @cols; # all colums (for insert) |
126 |
my @cols_null; # colums compared by a=b or a is null and b is null |
my @cols_null; # colums compared by a=b or a is null and b is null |
127 |
my @cols_notnull;# colums compared by a=b |
my @cols_notnull;# colums compared by a=b |
128 |
|
|
129 |
my $sth = $self->{'dbh'}->prepare($sql); |
my $sth = $self->{'dbh'}->prepare($sql); |
130 |
|
my $sth_def = $self->{'dbh'}->prepare($sql_def); |
131 |
$sth->execute($oid) || die; |
$sth->execute($oid) || die; |
132 |
while(my $row = $sth->fetchrow_hashref()) { |
while(my $row = $sth->fetchrow_hashref()) { |
133 |
# attname | format_type | attnotnull | atthasdef | attnum |
# attname | format_type | attnotnull | atthasdef | attnum |
|
push @pg_attribute, $row; |
|
134 |
push @cols,$row->{attname}; |
push @cols,$row->{attname}; |
135 |
|
|
136 |
if ($row->{attnotnull}) { |
if ($row->{attnotnull}) { |
139 |
push @cols_null,$row->{attname}; |
push @cols_null,$row->{attname}; |
140 |
} |
} |
141 |
|
|
142 |
|
if ($row->{atthasdef}) { |
143 |
|
$sth_def->execute($oid,$row->{attnum}) || die; |
144 |
|
my $row_def = $sth_def->fetchrow_hashref() || die "can't get attribute '",$row->{attname},"' default value!"; |
145 |
|
$row->{default} = $row_def->{def}; |
146 |
|
} |
147 |
|
|
148 |
|
push @pg_attribute, $row; |
149 |
} |
} |
150 |
|
|
151 |
@{$self->{'explained'}->{$table}->{'pg_attribute'}} = @pg_attribute; |
@{$self->{'explained'}->{$table}->{'pg_attribute'}} = @pg_attribute; |
205 |
@{$self->{'explained'}->{$table}->{cols_pk}} = @cols_pk; |
@{$self->{'explained'}->{$table}->{cols_pk}} = @cols_pk; |
206 |
@{$self->{'explained'}->{$table}->{cols_notpk}} = @cols_notpk; |
@{$self->{'explained'}->{$table}->{cols_notpk}} = @cols_notpk; |
207 |
|
|
208 |
|
# find triggers |
209 |
|
|
210 |
|
my @triggers; |
211 |
|
|
212 |
|
$sql =" |
213 |
|
SELECT t.tgname |
214 |
|
FROM pg_catalog.pg_trigger t |
215 |
|
WHERE t.tgrelid = ? and (not tgisconstraint OR NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d JOIN |
216 |
|
pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) WHERE d.classid = t.tableoid AND d.objid = |
217 |
|
t.oid AND d.deptype = 'i' AND c.contype = 'f')) |
218 |
|
"; |
219 |
|
|
220 |
|
$sth = $self->{'dbh'}->prepare($sql); |
221 |
|
$sth->execute($oid) || die; |
222 |
|
while(my $row = $sth->fetchrow_hashref()) { |
223 |
|
push @triggers,$row; |
224 |
|
} |
225 |
|
|
226 |
|
@{$self->{'explained'}->{$table}->{'triggers'}} = @triggers; |
227 |
|
|
228 |
return @pg_attribute; |
return @pg_attribute; |
229 |
} |
} |
230 |
|
|
306 |
} |
} |
307 |
|
|
308 |
# get active triggers |
# get active triggers |
309 |
sub get_triggers { |
sub get_activetriggers { |
310 |
my $self = shift; |
my $self = shift; |
311 |
|
|
312 |
# find table oid |
# find table oid |
328 |
return @triggers; |
return @triggers; |
329 |
} |
} |
330 |
|
|
331 |
|
# return triggers |
332 |
|
sub triggers { |
333 |
|
my $self = shift; |
334 |
|
my $table = shift; |
335 |
|
|
336 |
|
if (! $self->{'explained'}->{$table}) { |
337 |
|
$self->explain($table); |
338 |
|
} |
339 |
|
|
340 |
|
return $self->{'explained'}->{$table}->{triggers}; |
341 |
|
} |
342 |
|
|
343 |
1; |
1; |