blib/lib/MYDan/Node.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 121 CC 16 SCAR 29.1
CC
16
Cov
0%
query_cond
42.0
complex
20.0
cluster
20.0
CRAP 18.4
1 package MYDan::Node;
2  
3 =head1 NAME
4  
5 MYDan::Node - Cluster information interpreter. Extends MYDan::Node::Range
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Node;
10  
11 my $a = MYDan::Node->new( cache => '/database/file' );
12  
13 $a->load( 'foo{2~9}{??==*=={foo,bar}!!baz}.bar' );
14  
15 ## ... see base class for other methods ...
16  
17 =cut
18 use strict;
19 use warnings;
20  
21 use base qw( MYDan::Node::Range );
22  
23 =head1 QUERY
24  
25 =head3 cluster
26  
27 A I<cluster> query is expressed as a tuple of elements, corresponding
28 to columns of the cache table. See MYDan::Node::DBI::Cache.
29  
30 Such a query is made when an element is indicated by the I<select> symbol,
31 and the other three are given as query conditions, expressed by a I<condition>
32 symbol followed a range expression. e.g. "{??==*=={foo,bar}!!baz}" may be
33 translated into the following SQL statement,
34  
35 "SELECT col1 FROM $TABLE WHERE col3 IN ('foo','bar') AND col4!='baz'"
36  
37 =cut
38 use MYDan::Node::DBI::Cache;
39  
40 =head3 callabck
41  
42 A I<callback> query is expressed as a callback symbol followed by the
43 callback name, and optionally, followed by a query condition.
44 See MYDan::Node::Call.
45  
46 e.g. "{%%foo!!bar,baz}" means to get all elements returned by callback
47 I<foo>, except those indexed by I<bar> and I<baz>.
48  
49 Naturally, '*' in a query condition means 'any', i.e. I<no condition>.
50 And a query expression may be I<recursive>.
51  
52 =cut
53 use MYDan::Node::Call;
54  
55 =head1 SYMBOLS
56  
57 ( in addition to those in the base class )
58  
59 =head3 QUERY
60  
61 '??' : select
62 '==' : in
63 '!!' : not
64  
65 =cut
66 $MYDan::Node::Range::SYMBOL{QUERY} =
67 {
68 select => '??', in => '==', 'not' => '!!', call => '%%'
69 };
70  
71 sub new
72 {
73 my ( $class, %path ) = splice @_;
74 my ( $cache, $callback ) = @path{ qw( cache callback ) };
75 my $self = bless MYDan::Node::Range->new(), ref $class || $class;
76 $self->{db} = MYDan::Node::DBI::Cache->new( $cache ) if $cache;
77 $self->{cb} = MYDan::Node::Call->new( $callback ) if $callback;
78 return $self;
79 }
80  
81 =head1 METHODS
82  
83 =head3 db()
84  
85 Returns cache db object.
86  
87 =cut
88 sub db
89 {
90 my $self = shift;
91 return $self->{db};
92 }
93  
94 =head3 cb()
95  
96 Returns callback object.
97  
98 =cut
99 sub cb
100 {
101 my $self = shift;
102 return $self->{cb};
103 }
104  
105 =head1 GRAMMAR
106  
107 ( BNF rules additional to those in the base class )
108  
109 =head3 complex
110  
111 '{' [ <expr> | <call> | <cluster> ] '}'
112  
113 =cut
114 sub complex
115 {
116 my $self = shift;
117 my $token = $self->incr->token( '' );
118 my $stage = ! $self->op( QUERY => 0 )
119 ? 'expr' : $token eq 'call' ? $token : 'cluster';
120  
121 my $result = $self->$stage;
122  
123 die unless $self->token( 'close' );
124 $self->incr;
125 return $result;
126 }
127  
128 =head3 call
129  
130 <call_sym> <string> <query_cond>
131  
132 =cut
133 sub call
134 {
135 my $self = shift;
136 my $result = MYDan::Node::KeySet->new();
137 my $name = $self->incr->token;
138 my $cond = $self->incr->token( 'close' ) ? [] : $self->query_cond;
139  
140 return $result unless my $cb = $self->{cb};
141 return $result->load( [ map { @$_ } $cb->select( $name => $cond || [] ) ] );
142 }
143  
144 =head3 cluster
145
146 [ <select_symbol> <query_cond> ** 3 ] |
147 [ <query_cond> <select_symbol> <query_cond> ** 2 ] |
148 [ <query_cond> ** 2 <select_symbol> <query_cond> ] |
149 [ <query_cond> ** 3 <select_symbol> ]
150  
151 =cut
152 sub cluster
153 {
154 my $self = shift;
155 my ( $result, $select, %cond ) = MYDan::Node::KeySet->new();
156  
157 for my $col ( $self->{db}->column() )
158 {
159 next unless my $cond = $self->query_cond( $col );
160 if ( ref $cond ) { $cond{$col} = $cond } else { $select = $col }
161 }
162  
163 return $result unless my $db = $self->{db};
164 return $result->load( [ map { @$_ } $db->select( $select, %cond ) ] );
165 }
166  
167 =head3 query_cond
168  
169 <condition_symbol> <expr> |
170 <condition_symbol> <regex>
171  
172 =cut
173 sub query_cond
174 {
175 my ( $self, $col ) = splice @_;
176  
177 die unless my $op = $self->op( QUERY => 1 );
178 return $op if $op eq 'select';
179  
180 my $match = $op eq 'in';
181  
182 if ( $self->token( 'regex' ) )
183 {
184 my $regex = $self->match();
185 return
186 [
187 $match, defined $col ? grep { $_ =~ $regex }
188 map { @$_ } $self->{db}->select( $col ) : $regex
189 ];
190 }
191  
192 my $range = $self->expr;
193 return $range->has( '*' ) ? undef : [ $match, $range->list ];
194 }
195  
196 1;