line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
144
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package LittleORM::Model; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# Extend LittleORM::Model capabilities with clause support: |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub clause |
8
|
|
|
|
|
|
|
{ |
9
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
10
|
|
|
|
|
|
|
|
11
|
0
|
|
|
|
|
|
my @args = @_; |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
0
|
|
|
|
my $class = ( ref( $self ) or $self ); |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
my @clause_creation_args = ( model => $class, |
16
|
|
|
|
|
|
|
LittleORM::Clause -> __smart_clause_creation_args( @args ) ); |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
return LittleORM::Clause -> new( @clause_creation_args ); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
package LittleORM::Clause; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
5
|
use Moose; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'logic' => ( is => 'rw', isa => 'Str', default => 'AND' ); |
26
|
|
|
|
|
|
|
has 'model' => ( is => 'rw', isa => 'Str', required => 1 ); |
27
|
|
|
|
|
|
|
has 'table_alias' => ( is => 'rw', isa => 'Maybe[Str]' ); |
28
|
|
|
|
|
|
|
has 'cond' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); |
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
1
|
|
5831
|
use Carp::Assert 'assert'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
31
|
1
|
|
|
1
|
|
5764
|
use Data::Dumper 'Dumper'; |
|
1
|
|
|
|
|
6808
|
|
|
1
|
|
|
|
|
410
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub __smart_clause_creation_args |
34
|
|
|
|
|
|
|
{ |
35
|
0
|
|
|
0
|
|
|
my $self = shift; |
36
|
0
|
|
|
|
|
|
my @args = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my %my_attrs = map { $_ -> name() => 1 } $self -> meta() -> get_all_attributes(); |
|
0
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my @goes_into_cond = (); |
40
|
0
|
|
|
|
|
|
my @goes_as_is = (); |
41
|
0
|
|
|
|
|
|
my $seen_cond = 0; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
while( my $arg = shift @args ) |
44
|
|
|
|
|
|
|
{ |
45
|
0
|
|
|
|
|
|
my $value = shift @args; |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if( exists $my_attrs{ $arg } ) |
48
|
|
|
|
|
|
|
{ |
49
|
0
|
0
|
|
|
|
|
if( $arg eq 'cond' ) |
50
|
|
|
|
|
|
|
{ |
51
|
0
|
|
|
|
|
|
$seen_cond = 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
push @goes_as_is, ( $arg => $value ); |
55
|
|
|
|
|
|
|
} else |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
|
|
|
|
|
push @goes_into_cond, ( $arg => $value ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if( @goes_into_cond ) |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
|
|
|
|
assert( ( $seen_cond == 0 ), |
64
|
|
|
|
|
|
|
'ambiguous clause creation arguments: ' . Dumper( \@args ) ); |
65
|
0
|
|
|
|
|
|
push @goes_as_is, ( cond => \@goes_into_cond ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
return @goes_as_is; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub sql |
72
|
|
|
|
|
|
|
{ |
73
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my @rv = $self -> gen_clauses( &LittleORM::Model::__for_read(), # default, can be overwritten with following in @_ |
76
|
|
|
|
|
|
|
@_ ); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
return sprintf( ' ( %s ) ', join( ' '. $self -> logic() . ' ', @rv ) ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub gen_clauses |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
84
|
0
|
|
|
|
|
|
my @args = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my @rv = (); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my @c = @{ $self -> cond() }; |
|
0
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
while( @c ) |
91
|
|
|
|
|
|
|
{ |
92
|
0
|
|
|
|
|
|
my $item = shift @c; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if( ref( $item ) eq 'LittleORM::Clause' ) |
95
|
|
|
|
|
|
|
{ |
96
|
0
|
0
|
0
|
|
|
|
if( ( $item -> model() eq $self -> model() ) and ( my $ta = $self -> table_alias() ) and ( not $item -> table_alias() ) ) |
|
|
|
0
|
|
|
|
|
97
|
|
|
|
|
|
|
{ |
98
|
|
|
|
|
|
|
# copy obj ? |
99
|
0
|
|
|
|
|
|
my $copy = bless( { %{ $item } }, ref $item ); |
|
0
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
$item = $copy; |
101
|
0
|
|
|
|
|
|
$item -> table_alias( $ta ); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
push @rv, $item -> sql(); |
105
|
|
|
|
|
|
|
} else |
106
|
|
|
|
|
|
|
{ |
107
|
0
|
|
|
|
|
|
my $value = shift @c; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
push @rv, $self -> model() -> __form_where( @args, |
110
|
|
|
|
|
|
|
$item => $value, |
111
|
|
|
|
|
|
|
_table_alias => $self -> table_alias() ); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
unless( @rv ) |
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
|
|
|
@rv = ( '2=2' ); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return @rv; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
42; |