| 1 | package Teng::Schema::Dumper; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use DBIx::Inspector 0.03; | ||
| 5 | use Carp (); | ||
| 6 | |||
| 7 | sub dump { | ||
| 8 | my $class = shift; | ||
| 9 | my %args = @_==1 ? %{$_[0]} : @_; | ||
| 10 | |||
| 11 | my $dbh = $args{dbh} or Carp::croak("missing mandatory parameter 'dbh'"); | ||
| 12 | my $namespace = $args{namespace} or Carp::croak("missing mandatory parameter 'namespace'"); | ||
| 13 | |||
| 14 | my $inspector = DBIx::Inspector->new(dbh => $dbh); | ||
| 15 | |||
| 16 | my $ret = ""; | ||
| 17 | |||
| 18 | if ( ref $args{tables} eq "ARRAY" ) { | ||
| 19 | for my $table_name (@{ $args{tables} }) { | ||
| 20 | $ret .= _render_table($inspector->table($table_name), \%args); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | elsif ( $args{tables} ) { | ||
| 24 | $ret .= _render_table($inspector->table($args{tables}), \%args); | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | $ret .= "package ${namespace}::Schema;\n"; | ||
| 28 | $ret .= "use Teng::Schema::Declare;\n"; | ||
| 29 | for my $table_info (sort { $a->name cmp $b->name } $inspector->tables) { | ||
| 30 | $ret .= _render_table($table_info, \%args); | ||
| 31 | } | ||
| 32 | $ret .= "1;\n"; | ||
| 33 | } | ||
| 34 | |||
| 35 | return $ret; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _render_table { | ||
| 39 | my ($table_info, $args) = @_; | ||
| 40 | |||
| 41 | my $ret = ""; | ||
| 42 | |||
| 43 | $ret .= "table {\n"; | ||
| 44 | $ret .= sprintf(" name '%s';\n", $table_info->name); | ||
| 45 | $ret .= sprintf(" pk %s;\n", join ',' , map { q{'}.$_->name.q{'} } $table_info->primary_key); | ||
| 46 | $ret .= " columns (\n"; | ||
| 47 | for my $col ($table_info->columns) { | ||
| 48 | if ($col->data_type) { | ||
| 49 | $ret .= sprintf(" {name => '%s', type => %s},\n", $col->name, $col->data_type); | ||
| 50 | } else { | ||
| 51 | $ret .= sprintf(" '%s',\n", $col->name); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | $ret .= " );\n"; | ||
| 55 | |||
| 56 | if (my $rule = $args->{inflate}->{$table_info->name}) { | ||
| 57 | $ret .= $rule; | ||
| 58 | } | ||
| 59 | |||
| 60 | $ret .= "};\n\n"; | ||
| 61 | |||
| 62 | return $ret; | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | __END__ | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | Teng::Schema::Dumper - Schema code generator | ||
| 71 | |||
| 72 | =head1 SYNOPSIS | ||
| 73 | |||
| 74 | use DBI; | ||
| 75 | use Teng::Schema::Dumper; | ||
| 76 | |||
| 77 | my $dbh = DBI->connect(@dsn) or die; | ||
| 78 | print Teng::Schema::Dumper->dump( | ||
| 79 | dbh => $dbh, | ||
| 80 | namespace => 'Mock::DB', | ||
| 81 | inflate => +{ | ||
| 82 | user => q| | ||
| 83 | use Mock::Inflate::Name; | ||
| 84 | inflate 'name' => sub { | ||
| 85 | my ($col_value) = @_; | ||
| 86 | return Mock::Inflate::Name->new(name => $col_value); | ||
| 87 | }; | ||
| 88 | deflate 'name' => sub { | ||
| 89 | my ($col_value) = @_; | ||
| 90 | return ref $col_value ? $col_value->name : $col_value . '_deflate'; | ||
| 91 | }; | ||
| 92 | inflate qr/.+oo/ => sub { | ||
| 93 | my ($col_value) = @_; | ||
| 94 | return Mock::Inflate::Name->new(name => $col_value); | ||
| 95 | }; | ||
| 96 | deflate qr/.+oo/ => sub { | ||
| 97 | my ($col_value) = @_; | ||
| 98 | return ref $col_value ? $col_value->name : $col_value . '_deflate'; | ||
| 99 | }; | ||
| 100 | |, | ||
| 101 | }, | ||
| 102 | ); | ||
| 103 | |||
| 104 | =head1 DESCRIPTION | ||
| 105 | |||
| 106 | This module generates the Perl code to generate L<Teng::Schema> instance. | ||
| 107 | |||
| 108 | You can use it by C<do "my/schema.pl"> or embed it to the package. | ||
| 109 | |||
| 110 | =head1 METHODS | ||
| 111 | |||
| 112 | =over 4 | ||
| 113 | |||
| 114 | =item Teng::Schema::Dumper->dump(dbh => $dbh, namespace => $namespace); | ||
| 115 | |||
| 116 | This is the method to generate code from DB. It returns the Perl5 code in string. | ||
| 117 | |||
| 118 | The arguments are: | ||
| 119 | |||
| 120 | =over 4 | ||
| 121 | |||
| 122 | =item dbh | ||
| 123 | |||
| 124 | Database handle from DBI. | ||
| 125 | |||
| 126 | =item namespace | ||
| 127 | |||
| 128 | your project teng namespace. | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =back | ||
| 133 |