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