| 1 | package DBIx::DBSchema::DBD::Oracle; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION @ISA %typemap); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use DBIx::DBSchema::DBD; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | $VERSION = '0.01'; | |||||||||
| 8 | 1 | @ISA = qw(DBIx::DBSchema::DBD); | |||||||||
| 9 | |||||||||||
| 10 | 1 | %typemap = ( | |||||||||
| 11 | 'VARCHAR' => 'VARCHAR2', | ||||||||||
| 12 | 'SERIAL' => 'INTEGER', | ||||||||||
| 13 | 'LONG VARBINARY' => 'BLOB', | ||||||||||
| 14 | 'TIMESTAMP' => 'DATE', | ||||||||||
| 15 | 'BOOL' => 'INTEGER' | ||||||||||
| 16 | ); | ||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | DBIx::DBSchema::DBD::Oracle - Oracle native driver for DBIx::DBSchema | ||||||||||
| 21 | |||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||
| 23 | |||||||||||
| 24 | use DBI; | ||||||||||
| 25 | use DBIx::DBSchema; | ||||||||||
| 26 | |||||||||||
| 27 | $dbh = DBI->connect('dbi:Oracle:tns_service_name', 'user','pass'); | ||||||||||
| 28 | $schema = new_native DBIx::DBSchema $dbh; | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | This module implements a Oracle-native driver for DBIx::DBSchema. | ||||||||||
| 33 | |||||||||||
| 34 | =head1 AUTHOR | ||||||||||
| 35 | |||||||||||
| 36 | Daniel Hanks <hanksdc@about-inc.com> | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | ### Return column name, column type, nullability, column length, column default, | ||||||||||
| 41 | ### and a field reserved for driver-specific use | ||||||||||
| 42 | sub columns { | ||||||||||
| 43 | 0 | ▶ | my ($proto, $dbh, $table) = @_; | ||||||||
|
Subroutine
columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 44 | 0 | return $proto->_column_info($dbh, $table); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | sub column { | ||||||||||
| 48 | 0 | ▶ | my ($proto, $dbh, $table, $column) = @_; | ||||||||
|
Subroutine
column: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 49 | 0 | return $proto->_column_info($dbh, $table, $column); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub _column_info { | ||||||||||
| 53 | 0 | ▶ | my ($proto, $dbh, $table, $column) = @_; | ||||||||
|
Subroutine
_column_info: not called CC=3 SCAR=24.8
| |||||||||||
| 54 | 0 | my $sql = "SELECT column_name, data_type, | |||||||||
| 55 | CASE WHEN nullable = 'Y' THEN 1 | ||||||||||
| 56 | WHEN nullable = 'N' THEN 0 | ||||||||||
| 57 | ELSE 1 | ||||||||||
| 58 | END AS nullable, | ||||||||||
| 59 | data_length, data_default, NULL AS reserved | ||||||||||
| 60 | FROM user_tab_columns | ||||||||||
| 61 | WHERE table_name = ?"; | ||||||||||
| 62 | 0 | ▶ | $sql .= " AND column_name = ?" if defined($column); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | ▶ | if(defined($column)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | return $dbh->selectrow_arrayref($sql, undef, $table, $column); | |||||||||
| 65 | } else { ### Assume columns | ||||||||||
| 66 | 0 | return $dbh->selectall_arrayref($sql, undef, $table); | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | ### This is broken. Primary keys can be comprised of any subset of a tables | ||||||||||
| 71 | ### fields, not just one field, as this module assumes. | ||||||||||
| 72 | sub primary_key { | ||||||||||
| 73 | 0 | ▶ | my ($proto, $dbh, $table) = @_; | ||||||||
|
Subroutine
primary_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | my $sql = "SELECT column_name | |||||||||
| 75 | FROM user_constraints uc, user_cons_columns ucc | ||||||||||
| 76 | WHERE uc.constraint_name = ucc.constraint_name | ||||||||||
| 77 | AND uc.constraint_type = 'P' | ||||||||||
| 78 | AND uc.table_name = ?"; | ||||||||||
| 79 | 0 | my ($key) = $dbh->selectrow_array($sql, undef, $table); | |||||||||
| 80 | 0 | return $key; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | ### Wraoper around _index_info | ||||||||||
| 84 | sub unique { | ||||||||||
| 85 | 0 | ▶ | my ($proto, $dbh, $table) = @_; | ||||||||
|
Subroutine
unique: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 0 | return $proto->_index_info($dbh, $table, 'UNIQUE'); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | ### Wrapper around _index_info | ||||||||||
| 90 | sub index { | ||||||||||
| 91 | 0 | ▶ | my ($proto, $dbh, $table) = @_; | ||||||||
|
Subroutine
index: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | 0 | return $proto->_index_info($dbh, $table, 'NONUNIQUE'); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | ### Collect info about unique or non-unique indexes | ||||||||||
| 96 | ### $type must be 'UNIQUE' or 'NONUNIQUE' | ||||||||||
| 97 | sub _index_info { | ||||||||||
| 98 | 0 | ▶ | my ($proto, $dbh, $table, $type) = @_; | ||||||||
|
Subroutine
_index_info: not called CC=4 SCAR=30.0
| |||||||||||
| 99 | |||||||||||
| 100 | ### Sanity-check | ||||||||||
| 101 | 0 | ▶ | die "\$type must be 'UNIQUE' or 'NONUNIQUE'" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | unless $type =~ /^(NON)?UNIQUE$/; | ||||||||||
| 103 | |||||||||||
| 104 | ### Set up the query | ||||||||||
| 105 | 0 | my $sql = "SELECT ui.index_name, uic.column_name | |||||||||
| 106 | FROM user_indexes ui, user_ind_columns uic | ||||||||||
| 107 | WHERE ui.index_name = uic.index_name | ||||||||||
| 108 | AND ui.uniqueness = ? | ||||||||||
| 109 | AND table_name = ?"; | ||||||||||
| 110 | 0 | my $sth = $dbh->prepare($sql); | |||||||||
| 111 | 0 | $sth->execute($table, $type); | |||||||||
| 112 | |||||||||||
| 113 | ### Now collect the results | ||||||||||
| 114 | 0 | my $results = {}; | |||||||||
| 115 | 0 | ▶ | while(my ($idx, $col) = $sth->fetchrow_array()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | ▶ | if(!exists($results->{$idx})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | $results->{$idx} = []; | |||||||||
| 118 | } | ||||||||||
| 119 | 0 | push @{$results->{$idx}}, $col; | |||||||||
| 120 | } | ||||||||||
| 121 | 0 | return $results; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||