| 1 | package Teng::Schema::Loader; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use DBIx::Inspector; | ||
| 5 | use Teng::Schema; | ||
| 6 | use Teng::Schema::Table; | ||
| 7 | use Carp (); | ||
| 8 | use Class::Load (); | ||
| 9 | |||
| 10 | sub load { | ||
| 11 | my $class = shift; | ||
| 12 | my %args = @_==1 ? %{$_[0]} : @_; | ||
| 13 | |||
| 14 | my $namespace = $args{namespace} or Carp::croak("missing mandatory parameter 'namespace'"); | ||
| 15 | |||
| 16 | Class::Load::load_optional_class($namespace) or do { | ||
| 17 | # make teng class automatically | ||
| 18 | require Teng; | ||
| 19 | no strict 'refs'; @{"$namespace\::ISA"} = ('Teng'); | ||
| 20 | }; | ||
| 21 | |||
| 22 | my $teng = $namespace->new(%args); | ||
| 23 | my $dbh = $teng->dbh; | ||
| 24 | unless ($dbh) { | ||
| 25 | Carp::croak("missing mandatory parameter 'dbh' or 'connect_info'"); | ||
| 26 | } | ||
| 27 | |||
| 28 | my $schema = Teng::Schema->new(namespace => $args{namespace}); | ||
| 29 | |||
| 30 | my $inspector = DBIx::Inspector->new(dbh => $dbh); | ||
| 31 | for my $table_info ($inspector->tables) { | ||
| 32 | |||
| 33 | my $table_name = $table_info->name; | ||
| 34 | my @table_pk = map { $_->name } $table_info->primary_key; | ||
| 35 | my @col_names; | ||
| 36 | my %sql_types; | ||
| 37 | for my $col ($table_info->columns) { | ||
| 38 | push @col_names, $col->name; | ||
| 39 | $sql_types{$col->name} = $col->data_type; | ||
| 40 | } | ||
| 41 | |||
| 42 | $schema->add_table( | ||
| 43 | 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 | $schema->prepare_from_dbh($dbh); | ||
| 56 | $teng->schema($schema); | ||
| 57 | return $teng; | ||
| 58 | } | ||
| 59 | |||
| 60 | 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 instance of L<Teng::Scehema>. | ||
| 88 | |||
| 89 | The arguments are: | ||
| 90 | |||
| 91 | =item dbh | ||
| 92 | |||
| 93 | Database handle from DBI. | ||
| 94 | |||
| 95 | =item namespace | ||
| 96 | |||
| 97 | your project name space. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =cut |