| 1 | package Egg::Helper::Model::DBIC; | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt> | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # $Id: DBIC.pm 352 2008-07-14 13:26:41Z lushe $ | ||||||||||||
| 6 | # | ||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use UNIVERSAL::require; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use DBIx::Class::Schema::Loader qw/ make_schema_at /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 2 | our $VERSION= '3.00'; | |||||||||||
| 13 | |||||||||||||
| 14 | 0 | ▶ | sub _start_helper { | ||||||||||
|
Subroutine
_start_helper: not called CC=15 SCAR=54.8
| |||||||||||||
| 15 | 0 | my($self)= @_; | |||||||||||
| 16 | 0 | my $c= $self->config; | |||||||||||
| 17 | 0 | my $project= $self->project_name; | |||||||||||
| 18 | 0 | ▶ | $c->{helper_option}{project_root} || return $self->_helper_help | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | ('I want you to start from helper of the project.'); | ||||||||||||
| 20 | 0 | ▶ | my $schema_name= ucfirst( shift( @ARGV ) ) | ||||||||||
|
Condition0%0/2
ucfirst shift @ARGV || (return $self->_helper_help('I want component name.'))
MC/DC0%0/1
ucfirst shift @ARGV || (return $self->_helper_help('I want component name.'))
ucfirst shift @ARGV
Truth table0%0/2
A
A= ucfirst shift @ARGV
| |||||||||||||
| 21 | || return $self->_helper_help('I want component name.'); | ||||||||||||
| 22 | 0 | ▶ | $schema_name=~m{^^[A-Z][A-Za-z0-9\_]+$} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | || return $self->_helper_help("Bad schema name '$schema_name'"); | ||||||||||||
| 24 | 0 | my $o= $self->_helper_get_options; | |||||||||||
| 25 | 0 | ▶ | my $version= $self->helper_valid_version_number($o->{version}) || return 0; | ||||||||||
|
Condition0%0/2
$self->helper_valid_version_number($o->{'version'}) || (return 0)
MC/DC0%0/1
$self->helper_valid_version_number($o->{'version'}) || (return 0)
$self->helper_valid_version_number($o->{'version'})
Truth table0%0/2
A
A= $self->helper_valid_version_number($o->{'version'})
| |||||||||||||
| 26 | $o->{dsn} | ||||||||||||
| 27 | 0 | ▶ | || return $self->_helper_help(q{ '-d' Please give DSN by the option. }); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | ▶ | $o->{user} ||= ""; | ||||||||||
|
Condition0%0/2
$o->{"user"} ||= ""
MC/DC0%0/1
$o->{"user"} ||= ""
$o->{"user"}
Truth table0%0/2
A
A= $o->{"user"}
| |||||||||||||
| 29 | 0 | ▶ | $o->{password} ||= ""; | ||||||||||
|
Condition0%0/2
$o->{"password"} ||= ""
MC/DC0%0/1
$o->{"password"} ||= ""
$o->{"password"}
Truth table0%0/2
A
A= $o->{"password"}
| |||||||||||||
| 30 | 0 | ▶ | $o->{dsn}.= ";host=$o->{host}" if $o->{host}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | ▶ | $o->{dsn}.= ";port=$o->{inet_port}" if $o->{inet_port}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | my $param= $self->helper_prepare_param({ | |||||||||||
| 33 | module_version=> $version, dbi=> $o, | ||||||||||||
| 34 | created=> __PACKAGE__. " v$VERSION", | ||||||||||||
| 35 | }); | ||||||||||||
| 36 | 0 | $self->helper_prepare_param_module | |||||||||||
| 37 | ($param, $self->project_name, qw/ Model DBIC /, $schema_name); | ||||||||||||
| 38 | 0 | my $lib_path = "$param->{output_path}/lib/$param->{module_basedir}"; | |||||||||||
| 39 | 0 | my $schema_path= "$param->{output_path}/lib/$param->{module_filepath}"; | |||||||||||
| 40 | 0 | ▶ | unless (-e $lib_path) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 0 | $self->helper_create_dir($lib_path); | |||||||||||
| 42 | } | ||||||||||||
| 43 | 0 | ▶ | -e $schema_path | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | and return $self->_helper_help("'$schema_path' already exists."); | ||||||||||||
| 45 | |||||||||||||
| 46 | 0 | my $origin_code= <<END_CODE; | |||||||||||
| 47 | use base qw/ Egg::Model::DBIC::Schema /; | ||||||||||||
| 48 | |||||||||||||
| 49 | __PACKAGE__->config( | ||||||||||||
| 50 | |||||||||||||
| 51 | # label_name => 'mylabel', | ||||||||||||
| 52 | # label_source => { | ||||||||||||
| 53 | # moniker => 'mymoniker', | ||||||||||||
| 54 | # }, | ||||||||||||
| 55 | |||||||||||||
| 56 | dsn => '$o->{dsn}', | ||||||||||||
| 57 | user => '$o->{user}', | ||||||||||||
| 58 | password => '$o->{password}', | ||||||||||||
| 59 | options => { | ||||||||||||
| 60 | AutoCommit => 1, | ||||||||||||
| 61 | RaiseError => 1, | ||||||||||||
| 62 | PrintError => 0, | ||||||||||||
| 63 | }, | ||||||||||||
| 64 | |||||||||||||
| 65 | ); | ||||||||||||
| 66 | END_CODE | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | ▶ | $self->helper_chdir($c->{root}) || return | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | $self->_helper_help('The root directory of the project cannot be changed.'); | ||||||||||||
| 70 | 0 | eval { | |||||||||||
| 71 | make_schema_at("${project}::Model::DBIC::$schema_name", { | ||||||||||||
| 72 | debug => ($o->{debug} || 0), | ||||||||||||
| 73 | relationships => 1, | ||||||||||||
| 74 | dump_directory => './lib', | ||||||||||||
| 75 | }, | ||||||||||||
| 76 | 0 | ▶ | [@{$o}{qw/ dsn user password /}], | ||||||||||
|
Condition0%0/2
$o->{'debug'} || 0
MC/DC0%0/1
$o->{'debug'} || 0
$o->{'debug'}
Truth table0%0/2
A
A= $o->{'debug'}
| |||||||||||||
| 77 | ); | ||||||||||||
| 78 | 0 | my $value= $self->helper_fread($schema_path); | |||||||||||
| 79 | 0 | $value=~s{\n+(use\s+base\s+[^\;]+\;)\s*} [\n# $1\n\n${origin_code}\n]s; | |||||||||||
| 80 | 0 | $self->helper_save_file( $schema_path => \$value ); | |||||||||||
| 81 | }; | ||||||||||||
| 82 | 0 | ▶ | if (my $err= $@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 0 | $self->helper_remove_file($schema_path); | |||||||||||
| 84 | 0 | $schema_path=~s{\.pm$} []; | |||||||||||
| 85 | 0 | $self->helper_remove_dir($schema_path); | |||||||||||
| 86 | 0 | $self->_helper_help($err); | |||||||||||
| 87 | } else { | ||||||||||||
| 88 | 0 | print <<END_INFO; | |||||||||||
| 89 | ... done. | ||||||||||||
| 90 | |||||||||||||
| 91 | output of Schema: $schema_path | ||||||||||||
| 92 | |||||||||||||
| 93 | END_INFO | ||||||||||||
| 94 | } | ||||||||||||
| 95 | } | ||||||||||||
| 96 | 0 | ▶ | sub _helper_get_options { | ||||||||||
|
Subroutine
_helper_get_options: not called CC=1 SCAR=6.9
| |||||||||||||
| 97 | 0 | $_[0]->next::method | |||||||||||
| 98 | ('d-dsn= u-user= p-password= s-host= i-inet_port='); | ||||||||||||
| 99 | } | ||||||||||||
| 100 | 0 | ▶ | sub _helper_help { | ||||||||||
|
Subroutine
_helper_help: not called CC=3 SCAR=24.8
| |||||||||||||
| 101 | 0 | my $self= shift; | |||||||||||
| 102 | 0 | ▶ | my $msg = shift || ""; | ||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 103 | 0 | ▶ | $msg= "\nERROR: ${msg}\n\n" if $msg; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 0 | print <<END_HELP; | |||||||||||
| 105 | ${msg}% perl egg_helper.pl Model::DBIC [SCHEMA_NAME] [OPTIONS] | ||||||||||||
| 106 | |||||||||||||
| 107 | OPTIONS: -d ... DSN. | ||||||||||||
| 108 | -u ... DB_USER. | ||||||||||||
| 109 | -p ... DB_PASSWORD. | ||||||||||||
| 110 | -s ... DB_HOST. | ||||||||||||
| 111 | -i ... DB_PORT. | ||||||||||||
| 112 | |||||||||||||
| 113 | Usage: | ||||||||||||
| 114 | 0 | % perl @{[ lc $self->project_name ]}\_helper.pl Model:DBIC MySchema \\ | |||||||||||
| 115 | > -d dbi:Pg:dbname=mydb \\ | ||||||||||||
| 116 | > -u dbuser \\ | ||||||||||||
| 117 | > -p dbpassword | ||||||||||||
| 118 | |||||||||||||
| 119 | END_HELP | ||||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 2 | 1; | |||||||||||
| 123 | |||||||||||||
| 124 | __END__ | ||||||||||||