| 1 | package Teng::Schema::Loader; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use DBIx::Inspector; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Teng::Schema; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Teng::Schema::Table; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Class::Load (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | sub load { | ||||||||||
| 11 | 4 | ▶ | my $class = shift; | ||||||||
|
Subroutine
load: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||
| 12 | 4 | ▶ | my %args = @_==1 ? %{$_[0]} : @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | |||||||||||
| 14 | 4 | ▶ | my $namespace = $args{namespace} or Carp::croak("missing mandatory parameter 'namespace'"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | ▶ | Class::Load::load_optional_class($namespace) or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | # make teng class automatically | ||||||||||
| 18 | 1 | require Teng; | |||||||||
| 19 | 1 | ▶ | no strict 'refs'; @{"$namespace\::ISA"} = ('Teng'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | }; | ||||||||||
| 21 | |||||||||||
| 22 | 4 | my $teng = $namespace->new(%args, loader => 1); | |||||||||
| 23 | 4 | my $dbh = $teng->dbh; | |||||||||
| 24 | 4 | ▶ | unless ($dbh) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | Carp::croak("missing mandatory parameter 'dbh' or 'connect_info'"); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 4 | my $schema = Teng::Schema->new(namespace => $args{namespace}); | |||||||||
| 29 | |||||||||||
| 30 | 4 | my $inspector = DBIx::Inspector->new(dbh => $dbh); | |||||||||
| 31 | 4 | for my $table_info ($inspector->tables) { | |||||||||
| 32 | |||||||||||
| 33 | 4 | my $table_name = $table_info->name; | |||||||||
| 34 | 4 | my @table_pk = map { $_->name } $table_info->primary_key; | |||||||||
| 35 | 4 | my @col_names; | |||||||||
| 36 | my %sql_types; | ||||||||||
| 37 | 4 | for my $col ($table_info->columns) { | |||||||||
| 38 | 16 | push @col_names, $col->name; | |||||||||
| 39 | 16 | $sql_types{$col->name} = $col->data_type; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | $schema->add_table( | ||||||||||
| 43 | 4 | Teng::Schema::Table->new( | |||||||||
| 44 | columns => \@col_names, | ||||||||||
| 45 | name => $table_name, | ||||||||||
| 46 | primary_keys => \@table_pk, | ||||||||||
| 47 | sql_types => \%sql_types, | ||||||||||
| 48 | inflators => [], | ||||||||||
| 49 | deflators => [], | ||||||||||
| 50 | row_class => join '::', $namespace, 'Row', Teng::Schema::camelize($table_name), | ||||||||||
| 51 | ) | ||||||||||
| 52 | ); | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 4 | $schema->prepare_from_dbh($dbh); | |||||||||
| 56 | 4 | $teng->schema($schema); | |||||||||
| 57 | 4 | return $teng; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | 1; | |||||||||
| 61 | __END__ | ||||||||||
| 62 | |||||||||||
| 63 | =head1 NAME | ||||||||||
| 64 | |||||||||||
| 65 | Teng::Schema::Loader - Dynamic Schema Loader | ||||||||||
| 66 | |||||||||||
| 67 | =head1 SYNOPSIS | ||||||||||
| 68 | |||||||||||
| 69 | use Teng; | ||||||||||
| 70 | use Teng::Schema::Loader; | ||||||||||
| 71 | |||||||||||
| 72 | my $teng = Teng::Schema::Loader->load( | ||||||||||
| 73 | dbh => $dbh, | ||||||||||
| 74 | namespace => 'MyAPP::DB' | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | =head1 DESCRIPTION | ||||||||||
| 78 | |||||||||||
| 79 | L<Teng::Schema::Loader> loads schema directly from DB. | ||||||||||
| 80 | |||||||||||
| 81 | =head1 CLASS METHODS | ||||||||||
| 82 | |||||||||||
| 83 | =over 4 | ||||||||||
| 84 | |||||||||||
| 85 | =item Teng::Schema::Loader->load(%attr) | ||||||||||
| 86 | |||||||||||
| 87 | This is the method to load schema from DB. It returns the instance of the given C<namespace> class which inherits L<Teng>. | ||||||||||
| 88 | |||||||||||
| 89 | The arguments are: | ||||||||||
| 90 | |||||||||||
| 91 | =over 4 | ||||||||||
| 92 | |||||||||||
| 93 | =item C<dbh> | ||||||||||
| 94 | |||||||||||
| 95 | Database handle from DBI. | ||||||||||
| 96 | |||||||||||
| 97 | =item namespace | ||||||||||
| 98 | |||||||||||
| 99 | your project name space. | ||||||||||
| 100 | |||||||||||
| 101 | =back | ||||||||||
| 102 | |||||||||||
| 103 | =back | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||