| 1 | package SQL::Translator::Parser::DBI::Oracle; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | SQL::Translator::Parser::DBI::Oracle - parser for DBD::Oracle | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||
| 8 | |||||||||||||
| 9 | See SQL::Translator::Parser::DBI. | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||
| 12 | |||||||||||||
| 13 | Uses DBI introspection methods to determine schema details. | ||||||||||||
| 14 | |||||||||||||
| 15 | =cut | ||||||||||||
| 16 | |||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | use DBI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use SQL::Translator::Schema::Constants; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 1 | ▶ | use SQL::Translator::Schema::Table; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | use SQL::Translator::Schema::Field; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | ▶ | use SQL::Translator::Schema::Constraint; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 1 | our $VERSION = '1.66'; | |||||||||||
| 26 | |||||||||||||
| 27 | 0 | ▶ | sub parse { | ||||||||||
|
Subroutine
parse: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||
| 28 | 0 | my ($tr, $dbh) = @_; | |||||||||||
| 29 | |||||||||||||
| 30 | 0 | my $schema = $tr->schema; | |||||||||||
| 31 | |||||||||||||
| 32 | 0 | my $db_user = uc $tr->parser_args()->{db_user}; | |||||||||||
| 33 | 0 | my $sth = $dbh->table_info(undef, $db_user, '%', 'TABLE'); | |||||||||||
| 34 | |||||||||||||
| 35 | 0 | ▶ | while (my $table_info = $sth->fetchrow_hashref('NAME_uc')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | ▶ | next if ($table_info->{TABLE_NAME} =~ /\$/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | |||||||||||||
| 38 | # create the table | ||||||||||||
| 39 | |||||||||||||
| 40 | my $table = $schema->add_table( | ||||||||||||
| 41 | name => $table_info->{TABLE_NAME}, | ||||||||||||
| 42 | type => $table_info->{TABLE_TYPE}, | ||||||||||||
| 43 | 0 | ); | |||||||||||
| 44 | |||||||||||||
| 45 | # add the fields (columns) for this table | ||||||||||||
| 46 | |||||||||||||
| 47 | 0 | my $sth; | |||||||||||
| 48 | |||||||||||||
| 49 | 0 | $sth = $dbh->column_info(undef, $table_info->{TABLE_SCHEM}, $table_info->{TABLE_NAME}, '%'); | |||||||||||
| 50 | |||||||||||||
| 51 | 0 | ▶ | while (my $column = $sth->fetchrow_hashref('NAME_uc')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | my $f = $table->add_field( | ||||||||||||
| 53 | name => $column->{COLUMN_NAME}, | ||||||||||||
| 54 | default_value => $column->{COLUMN_DEF}, | ||||||||||||
| 55 | data_type => $column->{TYPE_NAME}, | ||||||||||||
| 56 | order => $column->{ORDINAL_POSITION}, | ||||||||||||
| 57 | size => $column->{COLUMN_SIZE}, | ||||||||||||
| 58 | 0 | ▶ | ) || die $table->error; | ||||||||||
|
Condition0%0/2
$table->add_field('name', $column->{'COLUMN_NAME'}, 'default_value', $column->{'COLUMN_DEF'}, 'data_type', $column->{'TYPE_NAME'}, 'order', $column->{'ORDINAL_POSITION'}, 'size', $column->{'COLUMN_SIZE'}) || die($table->error)
MC/DC0%0/1
$table->add_field('name', $column->{'COLUMN_NAME'}, 'default_value', $column->{'COLUMN_DEF'}, 'data_type', $column->{'TYPE_NAME'}, 'order', $column->{'ORDINAL_POSITION'}, 'size', $column->{'COLUMN_SIZE'}) || die($table->error)
$table->add_field('name', $column->{'COLUMN_NAME'}, 'default_value', $column->{'COLUMN_DEF'}, 'data_type', $column->{'TYPE_NAME'}, 'order', $column->{'ORDINAL_POSITION'}, 'size', $column->{'COLUMN_SIZE'})
Truth table0%0/2
A
A= $table->add_field('name', $column->{'COLUMN_NAME'}, 'default_value', $column->{'COLUMN_DEF'}, 'data_type', $column->{'TYPE_NAME'}, 'order', $column->{'ORDINAL_POSITION'}, 'size', $column->{'COLUMN_SIZE'})
| |||||||||||||
| 59 | |||||||||||||
| 60 | 0 | $f->is_nullable($column->{NULLABLE} == 1); | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | # add the primary key info | ||||||||||||
| 64 | |||||||||||||
| 65 | 0 | $sth = $dbh->primary_key_info(undef, $table_info->{TABLE_SCHEM}, $table_info->{TABLE_NAME},); | |||||||||||
| 66 | |||||||||||||
| 67 | 0 | ▶ | while (my $primary_key = $sth->fetchrow_hashref('NAME_uc')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | my $f = $table->get_field($primary_key->{COLUMN_NAME}); | |||||||||||
| 69 | 0 | $f->is_primary_key(1); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | # add the foreign key info (constraints) | ||||||||||||
| 73 | |||||||||||||
| 74 | 0 | $sth = $dbh->foreign_key_info(undef, undef, undef, undef, $table_info->{TABLE_SCHEM}, $table_info->{TABLE_NAME},); | |||||||||||
| 75 | |||||||||||||
| 76 | 0 | my $cons = {}; | |||||||||||
| 77 | 0 | ▶ | while (my $foreign_key = $sth->fetchrow_hashref('NAME_uc')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 0 | my $name = $foreign_key->{FK_NAME}; | |||||||||||
| 79 | 0 | $cons->{$name}->{reference_table} = $foreign_key->{UK_TABLE_NAME}; | |||||||||||
| 80 | 0 | push @{ $cons->{$name}->{fields} }, $foreign_key->{FK_COLUMN_NAME}; | |||||||||||
| 81 | 0 | push @{ $cons->{$name}->{reference_fields} }, $foreign_key->{UK_COLUMN_NAME}; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 0 | for my $name (keys %$cons) { | |||||||||||
| 85 | my $c = $table->add_constraint( | ||||||||||||
| 86 | type => FOREIGN_KEY, | ||||||||||||
| 87 | name => $name, | ||||||||||||
| 88 | fields => $cons->{$name}->{fields}, | ||||||||||||
| 89 | reference_fields => $cons->{$name}->{reference_fields}, | ||||||||||||
| 90 | reference_table => $cons->{$name}->{reference_table}, | ||||||||||||
| 91 | 0 | ▶ | ) || die $table->error; | ||||||||||
|
Condition0%0/2
$table->add_constraint('type', 'FOREIGN KEY', 'name', $name, 'fields', $cons->{$name}{'fields'}, 'reference_fields', $cons->{$name}{'reference_fields'}, 'reference_table', $cons->{$name}{'reference_table'}) || die($table->error)
MC/DC0%0/1
$table->add_constraint('type', 'FOREIGN KEY', 'name', $name, 'fields', $cons->{$name}{'fields'}, 'reference_fields', $cons->{$name}{'reference_fields'}, 'reference_table', $cons->{$name}{'reference_table'}) || die($table->error)
$table->add_constraint('type', 'FOREIGN KEY', 'name', $name, 'fields', $cons->{$name}{'fields'}, 'reference_fields', $cons->{$name}{'reference_fields'}, 'reference_table', $cons->{$name}{'reference_table'})
Truth table0%0/2
A
A= $table->add_constraint('type', 'FOREIGN KEY', 'name', $name, 'fields', $cons->{$name}{'fields'}, 'reference_fields', $cons->{$name}{'reference_fields'}, 'reference_table', $cons->{$name}{'reference_table'})
| |||||||||||||
| 92 | } | ||||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 0 | return 1; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 1 | 1; | |||||||||||
| 99 | |||||||||||||
| 100 | =pod | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 AUTHOR | ||||||||||||
| 103 | |||||||||||||
| 104 | Earl Cahill E<lt>cpan@spack.netE<gt>. | ||||||||||||
| 105 | |||||||||||||
| 106 | =head1 ACKNOWLEDGEMENT | ||||||||||||
| 107 | |||||||||||||
| 108 | Initial revision of this module came almost entirely from work done by | ||||||||||||
| 109 | Todd Hepler E<lt>thepler@freeshell.orgE<gt>. My changes were | ||||||||||||
| 110 | quite minor (ensuring NAME_uc, changing a couple variable names, | ||||||||||||
| 111 | skipping tables with a $ in them). | ||||||||||||
| 112 | |||||||||||||
| 113 | Todd claimed his work to be an almost verbatim copy of | ||||||||||||
| 114 | SQL::Translator::Parser::DBI::PostgreSQL revision 1.7 | ||||||||||||
| 115 | |||||||||||||
| 116 | For me, the real work happens in DBD::Oracle and DBI, which, also | ||||||||||||
| 117 | for me, that is the beauty of having introspection methods in DBI. | ||||||||||||
| 118 | |||||||||||||
| 119 | =head1 SEE ALSO | ||||||||||||
| 120 | |||||||||||||
| 121 | SQL::Translator, DBD::Oracle. | ||||||||||||
| 122 | |||||||||||||
| 123 | =cut | ||||||||||||