| 1 | package Teng::Schema::Declare; | ||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use parent qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use Teng::Schema; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use Teng::Schema::Table; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 3 | our @EXPORT = qw( | |||||||||||||||||||
| 9 | schema | ||||||||||||||||||||
| 10 | name | ||||||||||||||||||||
| 11 | table | ||||||||||||||||||||
| 12 | pk | ||||||||||||||||||||
| 13 | columns | ||||||||||||||||||||
| 14 | row_class | ||||||||||||||||||||
| 15 | base_row_class | ||||||||||||||||||||
| 16 | inflate | ||||||||||||||||||||
| 17 | deflate | ||||||||||||||||||||
| 18 | default_row_class_prefix | ||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||
| 20 | 3 | our $CURRENT_SCHEMA_CLASS; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub schema (&;$) { | ||||||||||||||||||||
| 23 | 2 | ▶ | my ($code, $schema_class) = @_; | ||||||||||||||||||
|
Subroutine
schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 24 | 2 | local $CURRENT_SCHEMA_CLASS = $schema_class; | |||||||||||||||||||
| 25 | 2 | $code->(); | |||||||||||||||||||
| 26 | 2 | _current_schema(); | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | sub base_row_class($) { | ||||||||||||||||||||
| 30 | 1 | ▶ | my $current = _current_schema(); | ||||||||||||||||||
|
Subroutine
base_row_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 31 | 1 | $current->{__base_row_class} = $_[0]; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | sub default_row_class_prefix ($) { | ||||||||||||||||||||
| 35 | 0 | ▶ | _current_schema()->{__default_row_class_prefix} = $_[0]; | ||||||||||||||||||
|
Subroutine
default_row_class_prefix: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | sub row_namespace ($) { | ||||||||||||||||||||
| 39 | 5 | ▶ | my $table_name = shift; | ||||||||||||||||||
|
Subroutine
row_namespace: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 5 | ▶ | my $prefix = defined(_current_schema()->{__default_row_class_prefix}) ? _current_schema()->{__default_row_class_prefix} : do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 5 | (my $caller = caller(1)) =~ s/::Schema$//; | |||||||||||||||||||
| 43 | 5 | join '::', $caller, 'Row'; | |||||||||||||||||||
| 44 | }; | ||||||||||||||||||||
| 45 | 5 | join '::', $prefix, Teng::Schema::camelize($table_name); | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub _current_schema { | ||||||||||||||||||||
| 49 | 13 | ▶ | my $class = __PACKAGE__; | ||||||||||||||||||
|
Subroutine
_current_schema: called CC=6 SCAR=18.0
| |||||||||||||||||||||
| 50 | 13 | my $schema_class; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 13 | ▶ | if ( $CURRENT_SCHEMA_CLASS ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 3 | $schema_class = $CURRENT_SCHEMA_CLASS; | |||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||
| 55 | 10 | my $i = 1; | |||||||||||||||||||
| 56 | 10 | ▶ | while ( $schema_class = caller($i++) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 14 | ▶ | if ( ! $schema_class->isa( $class ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 10 | last; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 13 | ▶ | if (! $schema_class) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | Carp::confess( "PANIC: cannot find a package name that is not ISA $class" ); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 3 | ▶ | no warnings 'once'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 68 | 13 | ▶ | if (! $schema_class->isa( 'Teng::Schema' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 3 | push @{ "$schema_class\::ISA" }, 'Teng::Schema'; | |||||||||||||||||||
| 71 | 3 | my $schema = $schema_class->new(); | |||||||||||||||||||
| 72 | 3 | $schema_class->set_default_instance( $schema ); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 13 | $schema_class->instance(); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub pk(@); | ||||||||||||||||||||
| 79 | sub columns(@); | ||||||||||||||||||||
| 80 | sub name ($); | ||||||||||||||||||||
| 81 | sub row_class ($); | ||||||||||||||||||||
| 82 | sub inflate_rule ($@); | ||||||||||||||||||||
| 83 | sub table(&) { | ||||||||||||||||||||
| 84 | 5 | ▶ | my $code = shift; | ||||||||||||||||||
|
Subroutine
table: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 85 | 5 | my $current = _current_schema(); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | my ( | ||||||||||||||||||||
| 88 | 5 | $table_name, | |||||||||||||||||||
| 89 | @table_pk, | ||||||||||||||||||||
| 90 | @table_columns, | ||||||||||||||||||||
| 91 | @inflate, | ||||||||||||||||||||
| 92 | @deflate, | ||||||||||||||||||||
| 93 | $row_class, | ||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||
| 95 | 3 | ▶ | no warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 5 | my $dest_class = caller(); | |||||||||||||||||||
| 98 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 99 | 3 | ▶ | no warnings 'once'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | 5 | local *{"$dest_class\::name"} = sub ($) { | |||||||||||||||||||
| 101 | 5 | ▶ | $table_name = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 102 | 5 | ▶ | $row_class ||= row_namespace($table_name); | ||||||||||||||||||
|
Condition33%1/3
$row_class ||= &row_namespace($table_name)
MC/DC0%0/2
$row_class ||= &row_namespace($table_name)
$row_class
&row_namespace($table_name)
Truth table33%1/3
A ||= B
A= $row_classB= &row_namespace($table_name)
| |||||||||||||||||||||
| 103 | 5 | }; | |||||||||||||||||||
| 104 | 5 | ▶ | local *{"$dest_class\::pk"} = sub (@) { @table_pk = @_ }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | 5 | ▶ | local *{"$dest_class\::columns"} = sub (@) { @table_columns = @_ }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | 5 | ▶ | local *{"$dest_class\::row_class"} = sub (@) { $row_class = shift }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||
| 107 | 5 | local *{"$dest_class\::inflate"} = sub ($&) { | |||||||||||||||||||
| 108 | 1 | ▶ | my ($rule, $code) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 109 | 1 | ▶ | if (ref $rule ne 'Regexp') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 1 | $rule = qr/^\Q$rule\E$/; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | 1 | push @inflate, ($rule, $code); | |||||||||||||||||||
| 113 | 5 | }; | |||||||||||||||||||
| 114 | 5 | local *{"$dest_class\::deflate"} = sub ($&) { | |||||||||||||||||||
| 115 | 1 | ▶ | my ($rule, $code) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 116 | 1 | ▶ | if (ref $rule ne 'Regexp') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 1 | $rule = qr/^\Q$rule\E$/; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | 1 | push @deflate, ($rule, $code); | |||||||||||||||||||
| 120 | 5 | }; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 5 | $code->(); | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 5 | my @col_names; | |||||||||||||||||||
| 125 | my %sql_types; | ||||||||||||||||||||
| 126 | 5 | ▶ | while ( @table_columns ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 14 | my $col_name = shift @table_columns; | |||||||||||||||||||
| 128 | 14 | ▶ | if (ref $col_name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | my $sql_type = $col_name->{type}; | |||||||||||||||||||
| 130 | 0 | $col_name = $col_name->{name}; | |||||||||||||||||||
| 131 | 0 | $sql_types{$col_name} = $sql_type; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 14 | push @col_names, $col_name; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | $current->add_table( | ||||||||||||||||||||
| 137 | Teng::Schema::Table->new( | ||||||||||||||||||||
| 138 | columns => \@col_names, | ||||||||||||||||||||
| 139 | name => $table_name, | ||||||||||||||||||||
| 140 | primary_keys => \@table_pk, | ||||||||||||||||||||
| 141 | sql_types => \%sql_types, | ||||||||||||||||||||
| 142 | inflators => \@inflate, | ||||||||||||||||||||
| 143 | deflators => \@deflate, | ||||||||||||||||||||
| 144 | row_class => $row_class, | ||||||||||||||||||||
| 145 | 5 | ▶ | ($current->{__base_row_class} ? (base_row_class => $current->{__base_row_class}) : ()), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | ) | ||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 3 | 1; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head1 NAME | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Teng::Schema::Declare - DSL For Declaring Teng Schema | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head1 NORMAL USE | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | package MyDB::Schema; | ||||||||||||||||||||
| 161 | use strict; | ||||||||||||||||||||
| 162 | use warnings; | ||||||||||||||||||||
| 163 | use Teng::Schema::Declare; | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | table { | ||||||||||||||||||||
| 166 | name "your_table_name"; | ||||||||||||||||||||
| 167 | pk "primary_key"; | ||||||||||||||||||||
| 168 | columns qw( col1 col2 col3 ); | ||||||||||||||||||||
| 169 | inflate 'col1' => sub { | ||||||||||||||||||||
| 170 | my ($col_value) = @_; | ||||||||||||||||||||
| 171 | return MyDB::Class->new(name => $col_value); | ||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||
| 173 | deflate 'col1' => sub { | ||||||||||||||||||||
| 174 | my ($col_value) = @_; | ||||||||||||||||||||
| 175 | return ref $col_value ? $col_value->name : $col_value; | ||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||
| 177 | row_class 'MyDB::Row'; # optional | ||||||||||||||||||||
| 178 | }; | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head1 INLINE DECLARATION | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | use Teng::Schema::Declare; | ||||||||||||||||||||
| 183 | my $schema = schema { | ||||||||||||||||||||
| 184 | table { | ||||||||||||||||||||
| 185 | name "your_table_name"; | ||||||||||||||||||||
| 186 | columns qw( col1 col2 col3 ); | ||||||||||||||||||||
| 187 | }; | ||||||||||||||||||||
| 188 | } "MyDB::Schema"; | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 METHODS | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =over 4 | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =item C<schema> | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | schema data creation wrapper. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =item C<table> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | set table name | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =item C<pk> | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | set primary key | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =item C<columns> | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | set columns | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | =item C<inflate_rule> | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | set inflate rule | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =item C<row_namespace> | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | create Row class namespace | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =item C<base_row_class> | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | Specify the default base row class with Teng::Schema::Declare. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | Default value is L<Teng::Row>. | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | This option is useful when you adds features for My::DB::Row class. | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =item C<default_row_class_prefix> | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | Specify the default prefix of row class. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | C<row_class> of each table definition has priority over C<default_row_class_prefix>. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | e.g.: | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | use Teng::Schema::Declare; | ||||||||||||||||||||
| 235 | my $schema = schema { | ||||||||||||||||||||
| 236 | default_row_class_prefix 'My::Entity'; | ||||||||||||||||||||
| 237 | table { | ||||||||||||||||||||
| 238 | name 'user'; | ||||||||||||||||||||
| 239 | column qw(name); | ||||||||||||||||||||
| 240 | }; | ||||||||||||||||||||
| 241 | }; | ||||||||||||||||||||
| 242 | $schema->get_row_class('user'); # => My::Entity::User | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | Default value is determined by the schema class. | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | e.g.: | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | package My::DB::Schema; | ||||||||||||||||||||
| 249 | use Teng::Schema::Declare; | ||||||||||||||||||||
| 250 | table { | ||||||||||||||||||||
| 251 | name 'user'; | ||||||||||||||||||||
| 252 | column qw(name); | ||||||||||||||||||||
| 253 | }; | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | __PACKAGE__->instance->get_row_class('user'); # => My::DB::Row::User | ||||||||||||||||||||
| 256 | 1; | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | =back | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | =cut | ||||||||||||||||||||
| 261 | |||||||||||||||||||||