| 1 | package Mojolicious::Command::generate::DBIxCustomModel; | ||
| 2 | 0 | ▶ | use Mojo::Base 'Mojolicious::Command'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use Mojo::Util qw(class_to_file class_to_path getopt encode decode decamelize); | ||
| 4 | use Mojo::File 'path'; | ||
| 5 | use Data::Dumper; | ||
| 6 | |||
| 7 | has description => 'generate DBIx::Custom model directory structure'; | ||
| 8 | has usage => sub{shift->extract_usage}; | ||
| 9 | has package => "MyApp"; | ||
| 10 | has lib => "lib"; | ||
| 11 | has config => sub{ | ||
| 12 | { | ||
| 13 | base => { | ||
| 14 | sdel => "is_deleted", | ||
| 15 | ctime => "create_time", | ||
| 16 | mtime => "update_time" | ||
| 17 | }, | ||
| 18 | models => { | ||
| 19 | |||
| 20 | } | ||
| 21 | } | ||
| 22 | }; | ||
| 23 | |||
| 24 | sub run{ | ||
| 25 | my ($self, @args) = @_; | ||
| 26 | my $app = $self->app; | ||
| 27 | my $home = $app->home; | ||
| 28 | getopt \@args, 'p|package=s' => \my $package, 'l|lib=s' => \my $lib, 'c|config=s' => \my $config; | ||
| 29 | $self->package($package) if($package); | ||
| 30 | $self->lib($lib) if($lib); | ||
| 31 | print "package : " . $self->package . $/; | ||
| 32 | print "lib : " . $self->lib . $/; | ||
| 33 | print "home : " . $home . $/; | ||
| 34 | if($config){ | ||
| 35 | print "========== start load config ==========" . $/; | ||
| 36 | my $file = $home->child($config); | ||
| 37 | print "config file : " . $file . $/; | ||
| 38 | $self->config($self->load($file, $app)) if(-e $file); | ||
| 39 | print "========== end load config ==========" . $/; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $base_model = { | ||
| 43 | package => $self->package . "::Model", | ||
| 44 | base_package => "Mojolicious::DBIxCustom::Model" | ||
| 45 | }; | ||
| 46 | |||
| 47 | $base_model = { %{$base_model}, %{$self->config->{base}} } if($self->config->{base}); | ||
| 48 | |||
| 49 | $self->render_to_file("baseModel", $home->child($self->lib, class_to_path($base_model->{package})), $base_model); | ||
| 50 | |||
| 51 | my $dbi = $app->dbi; | ||
| 52 | my $tables = $dbi->execute("SHOW TABLE STATUS;")->all; | ||
| 53 | |||
| 54 | foreach my $table (@{$tables}){ | ||
| 55 | $table = { %{$self->config->{models}->{$table->{Name}}}, %{$table} } if($self->config->{models} && $self->config->{models}->{$table->{Name}}); | ||
| 56 | my $ctime = $table->{ctime} || ""; | ||
| 57 | my $base_ctime = $base_model->{ctime} || ""; | ||
| 58 | my $mtime = $table->{mtime} || ""; | ||
| 59 | my $base_mtime = $base_model->{mtime} || ""; | ||
| 60 | my $sdel = $table->{sdel} || ""; | ||
| 61 | my $base_sdel = $base_model->{sdel} || ""; | ||
| 62 | $table->{Comment} = encode("utf8", $table->{Comment}) if($table->{Comment}); | ||
| 63 | $table->{columns} = $dbi->execute("show full columns from $table->{Name};")->all; | ||
| 64 | foreach my $col (@{$table->{columns}}){ | ||
| 65 | $col->{Comment} = encode("utf8", $col->{Comment}) if($col->{Comment}); | ||
| 66 | $col->{Default} = encode("utf8", $col->{Default}) if($col->{Default}); | ||
| 67 | if($col->{Key} eq "PRI"){ | ||
| 68 | $table->{primary_key} = [] unless($table->{primary_key}); | ||
| 69 | push(@{$table->{primary_key}}, $col); | ||
| 70 | } | ||
| 71 | if($ctime eq $col->{Field}){ | ||
| 72 | $ctime = ""; | ||
| 73 | } | ||
| 74 | if($mtime eq $col->{Field}){ | ||
| 75 | $mtime = ""; | ||
| 76 | } | ||
| 77 | if($sdel eq $col->{Field}){ | ||
| 78 | $sdel = ""; | ||
| 79 | } | ||
| 80 | |||
| 81 | if($base_ctime eq $col->{Field}){ | ||
| 82 | $base_ctime = ""; | ||
| 83 | } | ||
| 84 | if($base_mtime eq $col->{Field}){ | ||
| 85 | $base_mtime = ""; | ||
| 86 | } | ||
| 87 | if($base_sdel eq $col->{Field}){ | ||
| 88 | $base_sdel = ""; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | if($table->{ctime} && $ctime eq $table->{ctime}){ | ||
| 93 | $table->{ctime} = undef; | ||
| 94 | } | ||
| 95 | if($table->{mtime} && $mtime eq $table->{mtime}){ | ||
| 96 | $table->{mtime} = undef; | ||
| 97 | } | ||
| 98 | if($table->{sdel} && $sdel eq $table->{sdel}){ | ||
| 99 | $table->{sdel} = undef; | ||
| 100 | } | ||
| 101 | |||
| 102 | $table->{overwrite_fields} = []; | ||
| 103 | if(!$table->{ctime} && $base_model->{ctime} && $base_ctime eq $base_model->{ctime}){ | ||
| 104 | push(@{$table->{overwrite_fields}}, "ctime"); | ||
| 105 | } | ||
| 106 | if(!$table->{mtime} && $base_model->{mtime} && $base_mtime eq $base_model->{mtime}){ | ||
| 107 | push(@{$table->{overwrite_fields}}, "mtime"); | ||
| 108 | } | ||
| 109 | if(!$table->{sdel} && $base_model->{sdel} && $base_sdel eq $base_model->{sdel}){ | ||
| 110 | push(@{$table->{overwrite_fields}}, "sdel"); | ||
| 111 | } | ||
| 112 | |||
| 113 | $table->{package} = $base_model->{package} . "::$table->{Name}"; | ||
| 114 | $table->{base_package} = $base_model->{package}; | ||
| 115 | $self->render_to_file("model", $home->child($self->lib, class_to_path($table->{package})), $table); | ||
| 116 | } | ||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | sub load{$_[0]->parse(decode('UTF-8', path($_[1])->slurp), @_[1, 2, 3])} | ||
| 123 | |||
| 124 | sub parse{ | ||
| 125 | my ($self, $content, $file, $app) = @_; | ||
| 126 | |||
| 127 | # Run Perl code in sandbox | ||
| 128 | my $config = eval 'package Mojolicious::Command::generate::DBIxCustomModel::Sandbox; no warnings;' | ||
| 129 | . "sub app; local *app = sub { \$app }; use Mojo::Base -strict; $content"; | ||
| 130 | die qq{Can't load StatusCode configuration from file "$file": $@} if($@); | ||
| 131 | die qq{StatusCode Configuration file "$file" did not return a hash reference.\n} | ||
| 132 | unless(ref $config eq 'HASH'); | ||
| 133 | |||
| 134 | return $config; | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | =encoding utf8 | ||
| 139 | |||
| 140 | |||
| 141 | =head1 NAME | ||
| 142 | |||
| 143 | Mojolicious::Command::generate::DBIxCustomModel - generate DBIx::Custom model directory structure | ||
| 144 | |||
| 145 | =head1 VERSION | ||
| 146 | |||
| 147 | Version 1.0.2 | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | our $VERSION = '1.0.2'; | ||
| 152 | |||
| 153 | |||
| 154 | =head1 SYNOPSIS | ||
| 155 | |||
| 156 | Usage: APPLICATION generate DBIxCustomModel [OPTIONS] | ||
| 157 | |||
| 158 | mojo generate DBIxCustomModel -p MyApp -l lib -c generate_model.conf | ||
| 159 | mojo generate DBIxCustomModel --package MyApp --lib lib --conf generate_model.conf | ||
| 160 | |||
| 161 | Options: | ||
| 162 | -h, --help Show this summary of available options | ||
| 163 | -p, --package the package of generate model on ,defaults "MyApp" | ||
| 164 | -l, --lib lib path ,defaults "lib" | ||
| 165 | -c, --config config file | ||
| 166 | |||
| 167 | Config: content of config fiel | ||
| 168 | { | ||
| 169 | base => { # base mode config | ||
| 170 | sdel => "is_deleted", # sremove field | ||
| 171 | ctime => "create_time",# create time field | ||
| 172 | mtime => "update_time",# update time field | ||
| 173 | code=> ' # user defiend code | ||
| 174 | sub abc{ | ||
| 175 | } | ||
| 176 | ' | ||
| 177 | }, | ||
| 178 | models => { # the key is table name | ||
| 179 | table_name=>{ # table model config | ||
| 180 | sdel => "is_deleted", # sremove field | ||
| 181 | ctime => "create_time",# create time field | ||
| 182 | mtime => "update_time",# update time field | ||
| 183 | code=> ' # user defiend code | ||
| 184 | sub abc{ | ||
| 185 | } | ||
| 186 | ' | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | =head1 Config | ||
| 193 | |||
| 194 | 可以在配置文件中进行以下配置: | ||
| 195 | |||
| 196 | { | ||
| 197 | base => { # 对 base_package 的配置 | ||
| 198 | sdel => "is_deleted", # 软删除字段 | ||
| 199 | ctime => "create_time",# 创建时间字段 | ||
| 200 | mtime => "update_time",# 更新时间字段 | ||
| 201 | code=> ' # 用户自定义扩展代码 | ||
| 202 | sub abc{ | ||
| 203 | } | ||
| 204 | ' | ||
| 205 | }, | ||
| 206 | models => { # 以表名为key,对各个表的配置 | ||
| 207 | table_name=>{ # 对 base_package 的配置 | ||
| 208 | sdel => "is_deleted", # 软删除字段 | ||
| 209 | ctime => "create_time",# 创建时间字段 | ||
| 210 | mtime => "update_time",# 更新时间字段 | ||
| 211 | code=> ' # 用户自定义扩展代码 | ||
| 212 | sub abc{ | ||
| 213 | } | ||
| 214 | ' | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | =head1 AUTHOR | ||
| 221 | |||
| 222 | WFSO, C<< <461663376@qq.com> >> | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | 1; # End of Mojolicious::Command::generate::DBIxCustomModel | ||
| 227 | |||
| 228 | |||
| 229 | __DATA__ | ||