| 1 | package DBIx::SystemCatalog::Oracle; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use DBI; | ||
| 5 | use DBIx::SystemCatalog; | ||
| 6 | use vars qw/$VERSION @ISA/; | ||
| 7 | |||
| 8 | $VERSION = '0.04'; | ||
| 9 | @ISA = qw/DBIx::SystemCatalog/; | ||
| 10 | |||
| 11 | 1; | ||
| 12 | |||
| 13 | sub schemas { | ||
| 14 | my $obj = shift; | ||
| 15 | |||
| 16 | my $d = $obj->{dbi}->selectall_arrayref("SELECT DISTINCT owner FROM (SELECT owner FROM all_tables UNION SELECT owner FROM all_views)"); | ||
| 17 | return () unless defined $d and @$d; | ||
| 18 | return map { $_->[0] } @$d; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub tables { | ||
| 22 | my $obj = shift; | ||
| 23 | |||
| 24 | my $d = $obj->{dbi}->selectall_arrayref("SELECT table_name FROM all_tables WHERE owner = ? UNION SELECT view_name FROM all_views WHERE owner = ?",{},$obj->{schema},$obj->{schema}); | ||
| 25 | return () unless defined $d and @$d; | ||
| 26 | return map { $_->[0] } @$d; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub table_type { | ||
| 30 | my $obj = shift; | ||
| 31 | my $table = shift; | ||
| 32 | |||
| 33 | my $d = $obj->{dbi}->selectall_arrayref("SELECT 1 FROM all_tables WHERE table_name = ? AND owner = ?",{},$table,$obj->{schema}); | ||
| 34 | return SC_TYPE_TABLE if defined $d and @$d; | ||
| 35 | |||
| 36 | $d = $obj->{dbi}->selectall_arrayref("SELECT 1 FROM all_views WHERE view_name = ? AND owner = ?",{},$table,$obj->{schema}); | ||
| 37 | return SC_TYPE_VIEW if defined $d and @$d; | ||
| 38 | |||
| 39 | return SC_TYPE_UNKNOWN; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub tables_with_types { | ||
| 43 | my $obj = shift; | ||
| 44 | |||
| 45 | my $d = $obj->{dbi}->selectall_arrayref("SELECT table_name,".SC_TYPE_TABLE." FROM all_tables WHERE owner = ? UNION SELECT view_name,".SC_TYPE_VIEW." FROM all_views WHERE owner = ?",{},$obj->{schema},$obj->{schema}); | ||
| 46 | return () unless defined $d and @$d; | ||
| 47 | return map { { name => $_->[0], type => $_->[1] }; } @$d; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub relationships { | ||
| 51 | my $obj = shift; | ||
| 52 | |||
| 53 | my $d = $obj->{dbi}->selectall_arrayref(q!SELECT first.table_name,second.table_name,first.constraint_name,second.constraint_name FROM all_constraints first, all_constraints second WHERE first.owner = :p1 AND first.constraint_type = 'R' AND first.r_constraint_name = second.constraint_name AND second.owner = :p1!,{},$obj->{schema}); | ||
| 54 | my $e = $obj->{dbi}->selectall_arrayref(q!SELECT constraint_name,table_name,column_name FROM all_cons_columns WHERE owner = ? ORDER BY constraint_name,position!,{},$obj->{schema}); | ||
| 55 | my %columns = (); | ||
| 56 | if (defined $e and @$e) { | ||
| 57 | for (@$e) { | ||
| 58 | push @{$columns{$_->[0]}}, | ||
| 59 | { table => $_->[1], column => $_->[2] }; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | return map { { from_table => $_->[0], to_table => $_->[1], | ||
| 63 | name => $_->[2], from_columns => $columns{$_->[2]}, | ||
| 64 | to_columns => $columns{$_->[3]} } } @$d if defined $d and @$d; | ||
| 65 | return (); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub primary_keys { | ||
| 69 | my $obj = shift; | ||
| 70 | my $table = shift; | ||
| 71 | |||
| 72 | return () unless $table; | ||
| 73 | |||
| 74 | my $d = $obj->{dbi}->selectall_arrayref(q!SELECT all_cons_columns.column_name FROM all_constraints,all_cons_columns WHERE all_constraints.owner = ? AND all_constraints.constraint_type = 'P' AND all_constraints.table_name = ? AND all_constraints.constraint_name = all_cons_columns.constraint_name!,{},$obj->{schema},$table); | ||
| 75 | |||
| 76 | return map { $_->[0] } @$d if defined $d and @$d; | ||
| 77 | |||
| 78 | return (); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub unique_indexes { | ||
| 82 | my $obj = shift; | ||
| 83 | my $table = shift; | ||
| 84 | |||
| 85 | return () unless $table; | ||
| 86 | |||
| 87 | my $d = $obj->{dbi}->selectall_arrayref(q!SELECT all_constraints.constraint_name,all_cons_columns.column_name FROM all_constraints,all_cons_columns WHERE all_constraints.owner = ? AND all_constraints.constraint_type = 'U' AND all_constraints.table_name = ? AND all_constraints.constraint_name = all_cons_columns.constraint_name!,{},$obj->{schema},$table); | ||
| 88 | |||
| 89 | if (defined $d) { | ||
| 90 | my %res = (); | ||
| 91 | for (@$d) { | ||
| 92 | push @{$res{$_->[0]}},$_->[1]; | ||
| 93 | } | ||
| 94 | my @res = (); | ||
| 95 | for (keys %res) { | ||
| 96 | push @res,$res{$_}; | ||
| 97 | } | ||
| 98 | return @res; | ||
| 99 | } | ||
| 100 | |||
| 101 | return (); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub indexes { | ||
| 105 | my $obj = shift; | ||
| 106 | my $table = shift; | ||
| 107 | |||
| 108 | return () unless $table; | ||
| 109 | |||
| 110 | my $d = $obj->{dbi}->selectall_arrayref(q!SELECT all_indexes.index_name,all_ind_columns.column_name FROM all_indexes,all_ind_columns WHERE all_indexes.owner = ? AND all_indexes.table_name = ? AND all_indexes.index_name = all_ind_columns.index_name!,{},$obj->{schema},$table); | ||
| 111 | |||
| 112 | if (defined $d) { | ||
| 113 | my %res = (); | ||
| 114 | for (@$d) { | ||
| 115 | push @{$res{$_->[0]}},$_->[1]; | ||
| 116 | } | ||
| 117 | my @res = (); | ||
| 118 | for (keys %res) { | ||
| 119 | push @res,$res{$_}; | ||
| 120 | } | ||
| 121 | return @res; | ||
| 122 | } | ||
| 123 | return (); | ||
| 124 | } | ||
| 125 |