blib/lib/Neo4j/Cypher/Pattern.pm

filter: Statementstmt 93.0% 121 / 130 Branchbran 93.7% 45 / 48 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 84.0% 21 / 25 Podpod 72.2% 13 / 18 totaltotal 88.6% 211 / 238 SCAR 33.8
CC
29
Cov
92%
related_to
16.0
node
7.0
_quote
5.0
CRAP 29.4
« blib/lib/Neo4j/Cypher/Abstract.pm ↑ Summary Build »
1 package Neo4j::Cypher::Pattern;
2 3 ▶use base Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use overload '""' => 'as_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.1001';
9 3 our $VERSION = '0.1001';
10 3 our @EXPORT_OK = qw/pattern ptn/;
11  
12 sub puke(@);
13 sub belch(@);
14  
15 sub new {
16 50 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17 50 my $self = {};
18 50 $self->{stmt}=[];
19 50 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 48 50
20 2 my $nq = join('|',@_);
21 2 $self->{no_quote} = qr/$nq/;
22 }
23 50 bless $self, $class;
24 }
25  
26 sub pattern {
27 0 ▶ Neo4j::Cypher::Pattern->new(@_);
Subroutine pattern: not called CC=1 SCAR=6.9
Pod documented
28 }
29  
30 49 ▶sub ptn { Neo4j::Cypher::Pattern->new(@_); }
Subroutine ptn: called CC=1 SCAR=0.0
Pod documented
31  
32 sub path {
33 2 ▶ my $self = shift;
Subroutine path: called CC=3 SCAR=12.5
Pod documented
34 2 ▶ puke("Need arg1 => identifier") if (!defined $_[0] || ref($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 2 2
Condition33%1/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table33%1/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
35 2 return "$_[0] = $self";
36 }
37  
38 # alias for path
39 2 ▶sub as { shift->path(@_) }
Subroutine as: called CC=1 SCAR=0.0
Pod documented
40  
41 sub node {
42 # args:
43 # scalar string = varname
44 # array ref - array of labels
45 # hash ref - hash of props/values
46 93 ▶ my $self = shift;
Subroutine node: called CC=7 SCAR=19.5
Pod documented
47 93 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 6 87 93
48 6 push @{$self->{stmt}}, '()';
49 6 return $self;
50 }
51 87 my ($varname) = grep { !ref } @_;
52 87 my ($lbls) = grep { ref eq 'ARRAY' } @_;
53 87 my ($props) = grep { ref eq 'HASH' } @_;
54 # look for labels
55 87 my @l;
56 87 ($varname, @l) = split /:/, $varname;
57 87 ▶ if (@l) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 19 68 87
58 19 ▶ $lbls //= [];
Condition50%1/2
$lbls //= []
l!l
019
MC/DC0%0/1
$lbls //= []
$lbls
Truth table50%1/2
A
A= $lbls
Aresult
11
00
59 19 push @$lbls, @l;
60 }
61 87 ▶ my $str = $lbls ? join(':',$varname, @$lbls) : $varname;
Branch: 1 branch
BranchTrueFalseTotal
$lbls ? : 23 64 87
62 87 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 27 60 87
63 27 my $p;
64 27 ▶ while (my($k,$v) = each %$props) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$props) 31 27 58
65 31 push @$p, "$k:".$self->_quote($v);
66 }
67 27 $p = join(',',@$p);
68 27 $str .= " {$p}";
69 }
70 87 push @{$self->{stmt}}, "($str)";
71 87 return $self;
72 }
73  
74 85 ▶sub N {shift->node(@_);}
Subroutine N: called CC=1 SCAR=0.0
Pod documented
75  
76 sub related_to {
77 32 ▶ my $self = shift;
Subroutine related_to: called CC=16 SCAR=27.7
Pod documented
78 32 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 3 29 32
79 3 push @{$self->{stmt}}, '--';
80 3 return $self;
81 }
82 29 my ($hops) = grep { ref eq 'ARRAY' } @_;
83 29 my ($props) = grep { ref eq 'HASH' } @_;
84 29 my ($varname,$type) = grep { !ref } @_;
85 29 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 3 26 29
86 3 ($varname) = split /:/,$varname;
87 } else {
88 26 ($varname, $type) = $varname =~ /^([^:]*):?(.*)/;
89 }
90 29 my $dir;
91 29 ▶ if ($varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varname) 27 2 29
92 27 $varname =~ s/^(<|>)//;
93 27 $dir = $1;
94 27 $varname =~ s/(<|>)$//;
95 27 $dir = $1;
96 }
97 29 ▶ unless ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 15 14 29
98 15 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 11 4 15
99 11 $type =~ s/^(<|>)//;
100 11 $dir = $1;
101 11 $type =~ s/(<|>)$//;
102 11 $dir = $1;
103 }
104 }
105 29 ▶ my $str = $varname.($type ? ":$type" : "");
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 15 14 29
106  
107 29 ▶ if ($hops) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hops) 9 20 29
108 9 ▶ if (@$hops == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$hops == 0) { } 2 7 9
elsif (@$hops == 1) { } 0 7 7
109 2 $str.="*";
110 }
111 elsif (@$hops==1) {
112 0 $str .= "*$$hops[0]";
113 }
114 else {
115 7 $str .= "*$$hops[0]..$$hops[1]"
116 }
117 }
118 29 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 3 26 29
119 3 my $p;
120 3 ▶ while (my($k,$v) = each %$props) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$props) 3 3 6
121 3 push @$p, "$k:".$self->_quote($v);
122 }
123 3 $p = join(',',@$p);
124 3 $str .= " {$p}";
125 }
126 29 ▶ $str = ($str ? "-[$str]-" : '--');
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 25 4 29
127 29 $str =~ s/\[ \{/[{/;
128 29 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 22 7 29
129 22 ▶ if ($dir eq "<") {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir eq '<') { } 7 15 22
elsif ($dir eq '>') { } 15 0 15
130 7 $str = "<$str";
131 }
132 elsif ($dir eq ">") {
133 15 $str = "$str>";
134 }
135 else {
136 0 1; # huh?
137 }
138 }
139 29 push @{$self->{stmt}}, $str;
140 29 return $self;
141 }
142  
143 29 ▶sub R {shift->related_to(@_)}
Subroutine R: called CC=1 SCAR=0.0
Pod documented
144  
145 # N('a')->toN('b') -> (a)-->(b)
146 # N('a')->fromN('b') -> (a)<--(b)
147 1 ▶sub _N {shift->related_to->node(@_)}
Subroutine _N: called CC=1 SCAR=0.0
148 1 ▶sub to_N {shift->related_to('>')->node(@_)}
Subroutine to_N: called CC=1 SCAR=0.0
Pod documented
149 1 ▶sub from_N {shift->related_to('<')->node(@_)}
Subroutine from_N: called CC=1 SCAR=0.0
Pod documented
150  
151 # 'class' method
152 # do pattern->C($pat1, $pat2)
153 sub compound {
154 5 ▶ my $self = shift;
Subroutine compound: called CC=1 SCAR=0.0
Pod documented
155 5 return join(',',@_);
156 }
157  
158 5 ▶sub C {shift->compound(@_)}
Subroutine C: called CC=1 SCAR=0.0
Pod documented
159  
160 17 ▶sub clear { shift->{stmt}=[],1; }
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
161  
162 sub as_string {
163 160 ▶ my $self = shift;
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
164 160 return join('',@{$self->{stmt}});
165 }
166  
167 sub _quote {
168 return $_[1] if (
169 34 ▶ ($_[0]->{no_quote} and $_[1] =~ $_[0]->{no_quote}) or
Subroutine _quote: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/ or $_[1] =~ /(?:^|\s)\$/ 4 30 34
Condition100%6/6
$_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/ or $_[1] =~ /(?:^|\s)\$/
l!l&&r!l&&!r
2230
$_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/
!ll&&!rl&&r
3112
MC/DC100%3/3
$_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/ or $_[1] =~ /(?:^|\s)\$/
$_[0]{"no_quote"} $_[1] =~ /$_[0]{"no_quote"}/ $_[1] =~ /(?:^|\s)\$/
Truth table80%4/5
A and B or C
A= $_[0]{"no_quote"}B= $_[1] =~ /$_[0]{"no_quote"}/C= $_[1] =~ /(?:^|\s)\$/
ABCresult
11X1
0X11
1011
0X00
1000
170 $_[1] =~ /(?:^|\s)\$/ # no quote parameters
171 );
172 30 ▶ return ${$_[1]} if (ref $_[1] eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "SCALAR" 1 29 30
173 # escape single quotes
174 29 my $v = $_[1];
175 29 $v =~ s/'/\\'/g;
176 29 return "'$v'";
177 }
178 0 ▶sub pop { pop @{shift->{stmt}}; }
Subroutine pop: not called CC=1 SCAR=6.9
Pod undocumented
179  
180 sub belch (@) {
181 0 ▶ my($func) = (caller(1))[3];
Subroutine belch: not called CC=1 SCAR=6.9
Pod undocumented
182 0 Carp::carp "[$func] Warning: ", @_;
183 }
184  
185 sub puke (@) {
186 0 ▶ my($func) = (caller(1))[3];
Subroutine puke: not called CC=1 SCAR=6.9
Pod undocumented
187 0 Carp::croak "[$func] Fatal: ", @_;
188 }
189  
190 =head1 NAME
191  
192 Neo4j::Cypher::Pattern - Generate Cypher pattern strings
193  
194 =head1 SYNOPSIS
195  
196 # express a cypher pattern
197 use Neo4j::Cypher::Pattern qw/ptn/;
198  
199 ptn->node();
200 ptn->N(); #alias
201 ptn->N("varname");
202 ptn->N("varname",["label"],{prop => "value"});
203 ptn->N("varname:label");
204 ptn->N(["label"],{prop => "value"});
205  
206 ptn->node('a')->related_to()->node('b'); # (a)--(b)
207 ptn->N('a')->R()->N('b'); # alias
208 # additional forms
209 ptn->N('a')->R("varname","typename",[$minhops,$maxhops],{prop => "value"})
210 ->N('b'); # (a)-[varname:typename*minhops..maxhops { prop:"value }]-(b)
211 ptn->N('a')->R("varname:typename")->N('b'); # (a)-[varname:typename]-(b)
212 ptn->N('a')->R(":typename")->N('b'); # (a)-[:typename]-(b)
213 ptn->N('a')->R("", "typename")->N('b'); # (a)-[:typename]-(b)
214 # directed relns
215 ptn->N('a')->R("<:typename")->N('b'); # (a)<-[:typename]-(b)
216 ptn->N('a')->R("varname:typename>")->N('b'); # (a)-[varname:typename]->(b)
217  
218 # these return strings
219 $pattern->path('varname'); # path variable assigned to a pattern
220 $pattern->as('varname'); # alias
221 ptn->compound($pattern1, $pattern2); # comma separated patterns
222 ptn->C($pattern1, $pattern2); # alias
223  
224 =head1 DESCRIPTION
225  
226 The L<Cypher|https://neo4j.com/docs/developer-manual/current/cypher/>
227 query language of the graph database L<Neo4j|https://neo4j.com> uses
228 L<patterns|https://neo4j.com/docs/developer-manual/current/cypher/syntax/patterns>
229 to represent graph nodes and their relationships, for selecting and
230 matching in queries. C<Neo4j::Cypher::Pattern> can be used to create
231 Cypher pattern productions in Perl in an intuitive way. It is part of
232 the L<Neo4j::Cypher::Abstract> distribution.
233  
234 =head2 Basic idea : produce patterns by chaining method calls
235  
236 C<Neo4j::Cypher::Pattern> objects possess methods to represent nodes
237 and relationships. Each method adds its portion of the pattern, with
238 arguments, to the object's internal queue. Every method returns the
239 object itself. When an object is rendered as a string, it concatenates
240 nodes and relationship productions to yield the entire query statement
241 as a string.
242  
243 These features add up to the following idiom. Suppose we want to
244 render the Cypher pattern
245  
246 (b {name:"Slate"})<-[:WORKS_FOR]-(a {name:"Fred"})-[:KNOWS]->(c {name:"Barney"})
247  
248 In C<Neo4j::Cypher::Pattern>, we do
249  
250 $p = Neo4j::Cypher::Pattern->new()->N('b',{name=>'Slate')
251 ->R('<:WORKS_FOR')->N('a',{name => 'Fred'})
252 ->R(':KNOWS>')->N('c',{name=>'Barney'});
253 print "$p\n"; # "" is overloaded by $p->as_string()
254  
255 Because you may create many patterns in a program, a short
256 alias for the constructor can be imported, and extra variable
257 assignments can be avoided.
258  
259 print ptn->N('b',{name=>'Slate'})
260 ->R('<:WORKS_FOR')->N('a',{name => 'Fred'})
261 ->R(':KNOWS>')->N('c',{name=>'Barney'}), "\n";
262  
263 =head2 Quoting
264  
265 In pattern productions, values for properties will be quoted by
266 default with single quotes (single quotes that are present will be
267 escaped) unless the values are numeric.
268  
269 To prevent quoting Cypher statement list variable names (for example), make the name an argument to the pattern I<constructor>:
270  
271 ptn('event')->N('y')->R("<:IN")->N('e:Event'=> { id => 'event.id' });
272  
273 # renders (y)<-[:IN]-(e:Event {id:event.id})
274 # rather than (y)<-[:IN]-(e:Event {id:"event.id"})
275  
276 =head1 METHODS
277  
278 =over
279  
280 =item Constructor new()
281  
282 =item pattern(), ptn()
283  
284 Exportable aliases for the constructor. Arguments are variable names
285 that should not be quoted in rendering values of properties.
286  
287 =item node(), N()
288  
289 Render a node. Arguments in any order:
290  
291 scalar string: variable name or variable:label
292 array ref: array of node labels
293 hash ref: hash of property => value
294  
295 =item related_to(), R()
296  
297 Render a relationship. Arguments in any order:
298  
299 scalar string: variable name or variable:type
300 array ref: variable-length pattern:
301 [$minhops, $maxhops]
302 [] (empty array)- any number of hops
303 [$hops] - exactly $hops
304 hash ref : hash of property => value
305  
306 =item path(), as()
307  
308 Render the pattern set equal to a path variable:
309  
310 $p = ptn->N('a')->_N('b');
311 print $p->as('pth'); # gives 'pth = (a)--(b)'
312  
313 =item compound(), C()
314  
315 Render multiple patterns separated by commas
316  
317 ptn->compound( ptn->N('a')->to_N('b'), ptn->N('a')->from_N('c'));
318 # (a)-->(b), (a)<--(c)
319  
320 =item Shortcuts _N, to_N, from_N
321  
322 ptn->N('a')->_N('b'); # (a)--(b)
323 ptn->N('a')->to_N('b'); # (a)-->(b)
324 pth->N('a')->from_N('b'); # (a)<--(b)
325  
326 =back
327  
328 =head1 SEE ALSO
329  
330 L<Neo4j::Cypher::Abstract>
331  
332 =head1 AUTHOR
333  
334 Mark A. Jensen
335 CPAN: MAJENSEN
336 majensen -at- cpan -dot- org
337  
338 =head1 COPYRIGHT
339  
340 (c) 2017 Mark A. Jensen
341  
342 =cut
343 3 1;
« blib/lib/Neo4j/Cypher/Abstract.pm ↑ Summary Build »