| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | DBIx::DBO2::Schema - Group of tables and associated record classes | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use DBIx::DBO2::Schema; | ||||||||||
| 8 | my $ts = DBIx::DBO2::Schema->new(); | ||||||||||
| 9 | $ts->connect_datasource( $dsn, $user, $pass ); | ||||||||||
| 10 | $ts->packages( 'MyClassName' => 'mytablename' ); | ||||||||||
| 11 | $ts->require_packages; | ||||||||||
| 12 | $ts->declare_tables; | ||||||||||
| 13 | |||||||||||
| 14 | =head1 | ||||||||||
| 15 | |||||||||||
| 16 | This is an example use of the DBIx::DBO2 framework used for testing purposes. | ||||||||||
| 17 | |||||||||||
| 18 | =cut | ||||||||||
| 19 | |||||||||||
| 20 | package DBIx::DBO2::Schema; | ||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | ▶ | use Class::MakeMethods; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | use DBIx::SQLEngine::Schema::Table; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | ######################################################################## | ||||||||||
| 29 | |||||||||||
| 30 | 1 | ▶ | use Class::MakeMethods::Standard::Hash ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | 'new' => 'new', | |||||||||
| 32 | 'object' => { name=>'datasource', class=>'DBIx::SQLEngine::Driver::Default'}, | ||||||||||
| 33 | 'hash' => 'packages', | ||||||||||
| 34 | 1 | ); | |||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub connect_datasource { | ||||||||
|
Subroutine
connect_datasource: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 37 | 0 | my $self = shift; | |||||||||
| 38 | 0 | $self->datasource( DBIx::SQLEngine->new( @_ ) ); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | ######################################################################## | ||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | sub require_packages { | ||||||||
|
Subroutine
require_packages: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 44 | 0 | my $self = shift; | |||||||||
| 45 | 0 | my %tables = $self->packages; | |||||||||
| 46 | 0 | ▶ | while ( my( $package, $tablename ) = each %tables ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | $package =~ s{::}{/}g; | |||||||||
| 48 | 0 | $package .= '.pm'; | |||||||||
| 49 | # warn "Loading $package...\n"; | ||||||||||
| 50 | 0 | require $package; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | ######################################################################## | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub declare_tables { | ||||||||
|
Subroutine
declare_tables: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 57 | 0 | my $self = shift; | |||||||||
| 58 | 0 | my $datasource = $self->datasource; | |||||||||
| 59 | 0 | my %tables = $self->packages; | |||||||||
| 60 | 0 | ▶ | while ( my( $package, $tablename ) = each %tables ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | $package->table( $datasource->table( $tablename ) ) | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | ######################################################################## | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub create_tables { | ||||||||
|
Subroutine
create_tables: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 68 | 0 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 0 | my %tables = $self->packages; | |||||||||
| 71 | 0 | ▶ | while ( my( $package, $tablename ) = each %tables ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | my $table = $package->table; | |||||||||
| 73 | 0 | $table->columnset( DBIx::SQLEngine::Schema::ColumnSet->new() ); | |||||||||
| 74 | 0 | my $cols = $package->field_columns; | |||||||||
| 75 | 0 | $table->columnset( DBIx::SQLEngine::Schema::ColumnSet->new( @$cols ) ); | |||||||||
| 76 | 0 | $table->create_table; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub ensure_tables_exist { | ||||||||
|
Subroutine
ensure_tables_exist: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 81 | 0 | my $self = shift; | |||||||||
| 82 | |||||||||||
| 83 | 0 | my %tables = $self->packages; | |||||||||
| 84 | 0 | ▶ | while ( my( $package, $tablename ) = each %tables ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | my $table = $package->table; | |||||||||
| 86 | 0 | ▶ | next if $table->table_exists; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | my $cols = $package->field_columns; | |||||||||
| 88 | 0 | $table->columnset( DBIx::DBO2::ColumnSet->new( @$cols ) ); | |||||||||
| 89 | 0 | $table->create_table; | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub refresh_tables_schema { | ||||||||
|
Subroutine
refresh_tables_schema: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 94 | 0 | my $self = shift; | |||||||||
| 95 | |||||||||||
| 96 | 0 | my %tables = $self->packages; | |||||||||
| 97 | 0 | ▶ | while ( my( $package, $tablename ) = each %tables ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | my $table = $package->table; | |||||||||
| 99 | 0 | ▶ | next if $table->table_exists; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | my $cols = $package->field_columns; | |||||||||
| 101 | 0 | $table->columnset( DBIx::DBO2::ColumnSet->new( @$cols ) ); | |||||||||
| 102 | 0 | $table->recreate_table_with_rows; | |||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | sub drop_tables { | ||||||||
|
Subroutine
drop_tables: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 107 | 0 | my $self = shift; | |||||||||
| 108 | |||||||||||
| 109 | 0 | my %tables = $self->packages; | |||||||||
| 110 | 0 | ▶ | while ( my( $package, $tablename ) = each %tables ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | ▶ | my $table = $package->table or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | $table->drop_table; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | ######################################################################## | ||||||||||
| 117 | |||||||||||
| 118 | 1 | 1; | |||||||||