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

Diff of /Pg/Scheme.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by dpavlin, Tue Aug 12 20:38:06 2003 UTC revision 1.4 by dpavlin, Tue Oct 28 18:56:55 2003 UTC
# Line 106  sub explain { Line 106  sub explain {
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}) {
# Line 132  sub explain { Line 139  sub explain {
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;
# Line 191  ORDER BY Line 205  ORDER BY
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    
# Line 270  sub cols_notpk { Line 304  sub cols_notpk {
304    
305          return $self->{'explained'}->{$table}->{cols_notpk};          return $self->{'explained'}->{$table}->{cols_notpk};
306  }  }
307    
308    # get active triggers
309    sub get_activetriggers {
310            my $self = shift;
311    
312            # find table oid
313            my $sql = "
314            SELECT tgname FROM pg_trigger
315                    WHERE tgname not like 'pg_%' and tgenabled IS TRUE
316            ";
317    
318            my $sth = $self->{'dbh'}->prepare($sql);
319            $sth->execute() || die;
320    
321            my @triggers;
322    
323            while (my ($tr) = $sth->fetchrow_array()) {
324                    push @triggers,$tr;
325            }
326            $sth->finish();
327    
328            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;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.26