| 1 | package DBIx::SystemCatalog::Pg; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use DBI; | ||
| 5 | use DBIx::SystemCatalog; | ||
| 6 | use vars qw/$VERSION @ISA/; | ||
| 7 | |||
| 8 | $VERSION = '0.03'; | ||
| 9 | @ISA = qw/DBIx::SystemCatalog/; | ||
| 10 | |||
| 11 | 1; | ||
| 12 | |||
| 13 | sub _relkind2type { | ||
| 14 | my ($obj,$relkind) = @_; | ||
| 15 | |||
| 16 | if (defined $relkind) { | ||
| 17 | return SC_TYPE_TABLE if $relkind eq 'r'; | ||
| 18 | return SC_TYPE_VIEW if $relkind eq 'v'; | ||
| 19 | } | ||
| 20 | return SC_TYPE_UNKNOWN | ||
| 21 | } | ||
| 22 | |||
| 23 | sub schemas { | ||
| 24 | my $obj = shift; | ||
| 25 | |||
| 26 | # return $obj->{dbi}->{Name}; | ||
| 27 | return (); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub tables { | ||
| 31 | my $obj = shift; | ||
| 32 | |||
| 33 | my $d = $obj->{dbi}->selectall_arrayref("SELECT relname FROM pg_class WHERE relkind IN ('r','v')"); | ||
| 34 | return () unless defined $d and @$d; | ||
| 35 | return map { $_->[0] } @$d; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub table_type { | ||
| 39 | my $obj = shift; | ||
| 40 | my $table = shift; | ||
| 41 | |||
| 42 | my $d = $obj->{dbi}->selectall_arrayref("SELECT relkind FROM pg_class WHERE relname = ?",{},$table); | ||
| 43 | |||
| 44 | return $obj->_relkind2type($d->[0]->[0]) if defined $d and @$d; | ||
| 45 | return SC_TYPE_UNKNOWN; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub tables_with_types { | ||
| 49 | my $obj = shift; | ||
| 50 | my $d = $obj->{dbi}->selectall_arrayref("SELECT relname,relkind FROM pg_class WHERE relkind IN ('r','v')"); | ||
| 51 | return () unless defined $d and @$d; | ||
| 52 | return map { { name => $_->[0], type => $obj->_relkind2type($_->[1]) }; } @$d; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub primary_keys { | ||
| 56 | my $obj = shift; | ||
| 57 | my $table = shift; | ||
| 58 | |||
| 59 | return () unless $table; | ||
| 60 | |||
| 61 | my $d = $obj->{dbi}->selectall_arrayref(q!SELECT pg_index.indkey FROM pg_class,pg_index WHERE pg_class.relname = ? AND pg_class.oid = pg_index.indrelid AND pg_index.indisprimary!,{},$table); | ||
| 62 | if (defined $d and @$d) { | ||
| 63 | my $sloupce = $d->[0]->[0]; | ||
| 64 | $sloupce =~ tr/ /,/; | ||
| 65 | $d = $obj->{dbi}->selectall_arrayref(qq!SELECT pg_attribute.attname FROM pg_class,pg_attribute WHERE pg_class.relname = ? AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum IN ($sloupce)!,{},$table); | ||
| 66 | |||
| 67 | return map { $_->[0] } @$d if defined $d and @$d; | ||
| 68 | } | ||
| 69 | return (); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub relationships { | ||
| 73 | my $obj = shift; | ||
| 74 | |||
| 75 | my $d = $obj->{dbi}->selectall_arrayref(q!SELECT st.relname,dt.relname,COALESCE(NULLIF(tgconstrname,'<unnamed>'),tgname),tgargs FROM pg_trigger,pg_class st,pg_class dt WHERE tgfoid = 1644 AND st.oid = tgrelid AND dt.oid = tgconstrrelid!); | ||
| 76 | |||
| 77 | return map { | ||
| 78 | my @args = split /\x0/,$_->[3]; | ||
| 79 | my $f = 1; my @cols1 = (); my @cols2 = (); | ||
| 80 | for (@args[4..$#args]) { | ||
| 81 | if ($f) { push @cols1,$_; $f = 0; } else { push @cols2,$_; $f = 1; } | ||
| 82 | }; | ||
| 83 | { from_table => $_->[0], to_table => $_->[1], name => $_->[2], | ||
| 84 | from_columns => [ map { { table => $args[1], column => $_ } } @cols1 ], | ||
| 85 | to_columns => [ map { { table => $args[2], column => $_ } } @cols2 ] } | ||
| 86 | } @$d if defined $d and @$d; | ||
| 87 | return (); | ||
| 88 | } | ||
| 89 | |||
| 90 | # For Ludek: | ||
| 91 | # What we will need to add to DBIx::SystemCatalog::Pg in near future: | ||
| 92 | # - schema support (now I see all tables together, but I want to | ||
| 93 | # separate tables from one schema (database in PgSQL terminology) | ||
| 94 | # and from another schema | ||
| 95 | # - I remade _rel2kind to OOP version - please respect OOP in all methods |