Abstract.pm

filter: Statementstmt 73.8% 130 / 176 Branchbran 39.2% 33 / 84 Conditioncond 29.4% 23 / 78 MC/DCmcdc 23.5% 12 / 51 Subroutinesub 65.0% 26 / 40 Podpod 73.0% 19 / 26 totaltotal 53.4% 243 / 455 CC 72 SCAR 28.0
CC
72
Cov
55%
using_index
56.0
create_constraint_exist
42.0
create_constraint_unique
42.0
CRAP 16.4
« Abstract/Peeler.pm ↑ Summary Pattern.pm »
1 package Neo4j::Cypher::Abstract;
2 2 ▶use lib '../../../lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use base Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Neo4j::Cypher::Pattern qw/pattern ptn/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Neo4j::Cypher::Abstract::Peeler;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use overload
9 2 ▶ '""' => as_string,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶ 'cmp' => sub { "$_[0]" cmp "$_[1]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 2 our @EXPORT_OK = qw/cypher pattern ptn/;
16 2 our $AUTOLOAD;
17  
18 sub puke(@);
19 sub belch(@);
20  
21 2 our $VERSION='0.1003';
22  
23 # let an Abstract object keep its own stacks of clauses
24 # rather than clearing an existing Abstract object, get
25 # new objects from a factory = cypher()
26  
27 # create, create_unique, match, merge - patterns for args
28 # where, set - SQL::A like expression for argument (only assignments make
29 # sense for set)
30 # for_each - third arg is a cypher write query
31  
32 # 'as' - include in the string arguments : "n.name as name"
33  
34 2 our %clause_table = (
35 read => [qw/match optional_match where start/],
36 write => [qw/create merge set delete remove foreach
37 detach_delete
38 on_create on_match
39 create_unique/],
40 general => [qw/return order_by limit skip with unwind union
41 return_distinct with_distinct
42 call yield/],
43 hint => [qw/using_index using_scan using_join/],
44 load => [qw/load_csv load_csv_with_headers
45 using_periodic_commit/],
46 schema => [qw/create_constraint drop_constraint
47 create_index drop_index/],
48 modifier => [qw/skip limit order_by/]
49 );
50 2 our @all_clauses = ( map { @{$clause_table{$_}} } keys %clause_table );
51  
52 64 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
53 64 my $class = shift;
54 64 my $self = {};
55 64 $self->{stack} = [];
56 64 bless $self, $class;
57 }
58  
59 64 ▶sub cypher {
Subroutine cypher: called CC=1 SCAR=0.0
Pod undocumented
60 64 Neo4j::Cypher::Abstract->new;
61 }
62 0 ▶sub available_clauses {no warnings qw/once/; @__PACKAGE__::all_clauses }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine available_clauses: not called CC=1 SCAR=0.2
Pod undocumented
63  
64 0 ▶sub bind_values { $_[0]->{bind_values} && @{$_[0]->{bind_values}} }
Subroutine bind_values: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"bind_values"} and @{$_[0]{"bind_values"};} 0 0 0
65 3 ▶sub parameters { $_[0]->{parameters} && @{$_[0]->{parameters}} }
Subroutine parameters: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"parameters"} and @{$_[0]{"parameters"};} 3 0 3
66  
67 # specials
68  
69 22 ▶sub where {
Subroutine where: called CC=2 SCAR=7.0
Pod documented
70 22 my $self = shift;
71 22 ▶ puke "Need arg1 => expression" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 22 22
72 22 my $arg = $_[0];
73 22 $self->_add_clause('where',$arg);
74 }
75  
76 1 ▶sub union { $_[0]->_add_clause('union') }
Subroutine union: called CC=1 SCAR=0.0
Pod documented
77 1 ▶sub union_all { $_[0]->_add_clause('union_all') }
Subroutine union_all: called CC=1 SCAR=0.0
Pod undocumented
78  
79 7 ▶sub order_by {
Subroutine order_by: called CC=5 SCAR=16.1
Pod documented
80 7 my $self = shift;
81 7 ▶ puke "Need arg1 => identifier" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 7 7
82 7 my @args;
83 7 ▶ while (my $a = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = shift()) 9 7 16
84 9 ▶ if ($_[0] and $_[0] =~ /^(?:de|a)sc$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $_[0] =~ /^(?:de|a)sc$/i) { } 2 7 9
Condition100%3/3
$_[0] and $_[0] =~ /^(?:de|a)sc$/i
!ll&&!rl&&r
612
MC/DC100%2/2
$_[0] and $_[0] =~ /^(?:de|a)sc$/i
$_[0] $_[0] =~ /^(?:de|a)sc$/i
Truth table100%3/3
A and B
A= $_[0]B= $_[0] =~ /^(?:de|a)sc$/i
ABresult
0X0
100
111
85 2 push @args, "$a ".uc(shift());
86 }
87 else {
88 7 push @args, $a;
89 }
90 }
91 7 $self->_add_clause('order_by',@args);
92 }
93  
94 2 ▶sub unwind {
Subroutine unwind: called CC=4 SCAR=15.6
Pod documented
95 2 my $self = shift;
96 2 ▶ puke "need arg1 => list expr" unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 2 2
97 2 ▶ puke "need arg2 => list variable" unless ($_[1] && !ref($_[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] and not ref $_[1] 0 2 2
Condition33%1/3
$_[1] and not ref $_[1]
!ll&&!rl&&r
002
MC/DC0%0/2
$_[1] and not ref $_[1]
$_[1] not ref $_[1]
Truth table33%1/3
A and not(B)
A= $_[1]B= not ref $_[1]
ABresult
0X0
100
111
98 2 $self->_add_clause('unwind',$_[0],'AS',$_[1]);
99 }
100  
101 49 ▶sub match {
Subroutine match: called CC=3 SCAR=11.0
Pod documented
102 49 my $self = shift;
103 # shortcut for a single node identifier, with labels
104 49 ▶ if (@_==1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i) { } 24 25 49
Condition66%2/3
@_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i
!ll&&!rl&&r
02524
MC/DC50%1/2
@_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i
@_ == 1 $_[0] =~ /^[a-z][a-z0-9_:]*$/i
Truth table66%2/3
A and B
A= @_ == 1B= $_[0] =~ /^[a-z][a-z0-9_:]*$/i
ABresult
0X0
100
111
105 24 $self->_add_clause('match',"($_[0])");
106 }
107 else {
108 25 $self->_add_clause('match',@_);
109 }
110 }
111  
112 8 ▶sub create {
Subroutine create: called CC=3 SCAR=11.0
Pod documented
113 8 my $self = shift;
114 # shortcut for a single node identifier, with labels
115 8 ▶ if (@_==1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i) { } 3 5 8
Condition66%2/3
@_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i
!ll&&!rl&&r
053
MC/DC50%1/2
@_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i
@_ == 1 $_[0] =~ /^[a-z][a-z0-9_:]*$/i
Truth table66%2/3
A and B
A= @_ == 1B= $_[0] =~ /^[a-z][a-z0-9_:]*$/i
ABresult
0X0
100
111
116 3 $self->_add_clause('create',"($_[0])");
117 }
118 else {
119 5 $self->_add_clause('create',@_);
120 }
121 }
122  
123 10 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
124 10 my $self = shift;
125 # interpret a hashref argument as a set of key = value pairs
126 10 ▶ if (ref $_[0] eq 'HASH' && @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH' and @_ == 1) { } 5 5 10
Condition100%3/3
ref $_[0] eq "HASH" and @_ == 1
!ll&&!rl&&r
325
MC/DC100%2/2
ref $_[0] eq "HASH" and @_ == 1
ref $_[0] eq "HASH" @_ == 1
Truth table100%3/3
A and B
A= ref $_[0] eq "HASH"B= @_ == 1
ABresult
0X0
100
111
127 5 $self->_add_clause('set', map { { $_ => $_[0]->{$_} } } sort keys %{$_[0]} )
128 }
129 else {
130 5 $self->_add_clause('set',@_);
131 }
132 }
133  
134 1 ▶sub foreach {
Subroutine foreach: called CC=5 SCAR=18.1
Pod documented
135 1 my $self = shift;
136 1 ▶ puke "need arg1 => list variable" unless ($_[0] && !ref($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] and not ref $_[0] 0 1 1
Condition33%1/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
137 1 ▶ puke "need arg2 => list expr" unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 1 1
138 1 ▶ puke "need arg3 => cypher update stmt" unless $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[2] 0 1 1
139 1 $self->_add_clause('foreach', $_[0],'IN',$_[1],'|',$_[2]);
140 }
141  
142 0 ▶sub load_csv {
Subroutine load_csv: not called CC=4 SCAR=30.0
Pod documented
143 0 my $self = shift;
144 0 ▶ puke "need arg1 => file location" unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
145 0 ▶ puke "need arg2 => identifier" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
146 0 $self->_add_clause('load_csv','FROM',$_[0],'AS',$_[1]);
147 }
148  
149 0 ▶sub load_csv_with_headers {
Subroutine load_csv_with_headers: not called CC=4 SCAR=30.0
Pod documented
150 0 my $self = shift;
151 0 ▶ puke "need arg1 => file location" unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
152 0 ▶ puke "need arg2 => identifier" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
153 0 $self->_add_clause('load_csv_with_headers','FROM',$_[0],'AS',$_[1]);
154 }
155  
156 #create_constraint_exist('node', 'label', 'property')
157  
158 0 ▶sub create_constraint_exist {
Subroutine create_constraint_exist: not called CC=6 SCAR=37.4
Pod documented
159 0 my $self = shift;
160 0 ▶ puke "need arg1 => node/reln pattern" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
161 0 ▶ puke "need arg2 => label" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
162 0 ▶ puke "need arg2 => property" if (!defined $_[2] || ref $_[2]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[2] or ref $_[2] 0 0 0
Condition0%0/3
not defined $_[2] or ref $_[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[2] or ref $_[2]
not defined $_[2] ref $_[2]
Truth table0%0/3
not(A) or B
A= not defined $_[2]B= ref $_[2]
ABresult
1X1
011
000
163 0 $self->_add_clause('create_constraint_on', "($_[0]:$_[1])", 'ASSERT',"exists($_[0].$_[2])");
164 }
165  
166 # create_constraint_unique('node', 'label', 'property')
167 0 ▶sub create_constraint_unique {
Subroutine create_constraint_unique: not called CC=6 SCAR=37.4
Pod documented
168 0 my $self = shift;
169 0 ▶ puke "need arg1 => node/reln pattern" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
170 0 ▶ puke "need arg2 => label" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
171 0 ▶ puke "need arg2 => property" if (!defined $_[2] || ref $_[2]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[2] or ref $_[2] 0 0 0
Condition0%0/3
not defined $_[2] or ref $_[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[2] or ref $_[2]
not defined $_[2] ref $_[2]
Truth table0%0/3
not(A) or B
A= not defined $_[2]B= ref $_[2]
ABresult
1X1
011
000
172 0 $self->_add_clause('create_constraint_on', "($_[0]:$_[1])", 'ASSERT',
173 "$_[0].$_[2]", 'IS UNIQUE');
174 }
175  
176 # create_index('label' => 'property')
177 0 ▶sub create_index {
Subroutine create_index: not called CC=5 SCAR=34.0
Pod documented
178 0 my $self = shift;
179 0 ▶ puke "need arg1 => node label" if (!defined $_[0] || ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
180 0 ▶ puke "need arg2 => node property" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
181 0 $self->_add_clause('create_index','ON',":$_[0]($_[1])");
182 }
183  
184 # drop_index('label'=>'property')
185 0 ▶sub drop_index {
Subroutine drop_index: not called CC=5 SCAR=34.0
Pod documented
186 0 my $self = shift;
187 0 ▶ puke "need arg1 => node label" if (!defined $_[0] || ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
188 0 ▶ puke "need arg2 => node property" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
189 0 $self->_add_clause('drop_index','ON',":$_[0]($_[1])");
190 }
191  
192 # using_index('identifier', 'label', 'property')
193 0 ▶sub using_index {
Subroutine using_index: not called CC=7 SCAR=40.3
Pod documented
194 0 my $self = shift;
195 0 ▶ puke "need arg1 => identifier" if (!defined $_[0] || ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
196 0 ▶ puke "need arg2 => node label" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
197 0 ▶ puke "need arg3 => node property" if (!defined $_[2] || ref $_[2]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[2] or ref $_[2] 0 0 0
Condition0%0/3
not defined $_[2] or ref $_[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[2] or ref $_[2]
not defined $_[2] ref $_[2]
Truth table0%0/3
not(A) or B
A= not defined $_[2]B= ref $_[2]
ABresult
1X1
011
000
198 0 $self->_add_clause('using_index',"$_[0]:$_[1]($_[2])");
199 }
200  
201 # using_scan('identifier' => 'label')
202 0 ▶sub using_scan {
Subroutine using_scan: not called CC=5 SCAR=34.0
Pod documented
203 0 my $self = shift;
204 0 ▶ puke "need arg1 => identifier" if (!defined $_[0] || ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
205 0 ▶ puke "need arg2 => node label" if (!defined $_[1] || ref $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[1] or ref $_[1] 0 0 0
Condition0%0/3
not defined $_[1] or ref $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[1] or ref $_[1]
not defined $_[1] ref $_[1]
Truth table0%0/3
not(A) or B
A= not defined $_[1]B= ref $_[1]
ABresult
1X1
011
000
206 0 $self->_add_clause('using_scan',"$_[0]:$_[1]");
207 }
208  
209 # using_join('identifier', ...)
210 0 ▶sub using_join {
Subroutine using_join: not called CC=3 SCAR=24.8
Pod documented
211 0 my $self = shift;
212 0 ▶ puke "need arg => identifier" if (!defined $_[0] || ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
213 0 $self->_add_clause('using_join', 'ON', join(',',@_));
214 }
215  
216 # everything winds up here
217 175 ▶sub _add_clause {
Subroutine _add_clause: called CC=9 SCAR=22.0
218 175 my $self = shift;
219 175 my $clause = shift;
220 175 $self->{dirty} = 1;
221 175 my @clause;
222 175 push @clause, $clause;
223 175 ▶ if ( $clause =~ /^match|create|merge/ and
Branch: 1 branch
BranchTrueFalseTotal
if ($clause =~ /^match|create|merge/ and @_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i) { } 2 173 175
Condition100%6/6
$clause =~ /^match|create|merge/ and @_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i
!ll&&!rl&&r
109642
$clause =~ /^match|create|merge/ and @_ == 1
!ll&&!rl&&r
108166
MC/DC100%3/3
$clause =~ /^match|create|merge/ and @_ == 1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i
$clause =~ /^match|create|merge/ @_ == 1 $_[0] =~ /^[a-z][a-z0-9_:]*$/i
Truth table100%4/4
A and B and C
A= $clause =~ /^match|create|merge/B= @_ == 1C= $_[0] =~ /^[a-z][a-z0-9_:]*$/i
ABCresult
0XX0
10X0
1100
1111
224 @_==1 and $_[0] =~ /^[a-z][a-z0-9_:]*$/i) {
225 2 push @clause, "($_[0])";
226 }
227 else {
228 173 for (@_) {
229 215 ▶ if (ref && !blessed($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and not builtin::blessed($_)) { } 32 183 215
Condition100%3/3
ref $_ and not builtin::blessed($_)
!ll&&!rl&&r
1483532
MC/DC100%2/2
ref $_ and not builtin::blessed($_)
ref $_ not builtin::blessed($_)
Truth table100%3/3
A and not(B)
A= ref $_B= not builtin::blessed($_)
ABresult
0X0
100
111
230 32 my $plr = Neo4j::Cypher::Abstract::Peeler->new();
231 32 push @clause, $plr->express($_);
232 # kludge
233 32 ▶ if ($clause =~ /^set/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause =~ /^set/) 10 22 32
234 # removing enclosing parens from peel
235 10 $clause[-1] =~ s/^\s*\(//;
236 10 $clause[-1] =~ s/\)\s*$//;
237 }
238 32 push @{$self->{bind_values}}, $plr->bind_values;
239 32 push @{$self->{parameters}}, $plr->parameters;
240 }
241 else {
242 183 push @clause, $_;
243 183 my @parms = m/(\$[a-z][a-z0-9]*)/ig;
244 183 push @{$self->{parameters}}, @parms;
245 }
246 }
247 }
248 175 ▶ if ($clause =~ /^return|with|order|set|remove/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause =~ /^return|with|order|set|remove/) 71 104 175
249 # group args in array so they are separated by commas
250 71 @clause = (shift @clause, [@clause]);
251 }
252 175 push @{$self->{stack}}, \@clause;
253 175 return $self;
254 }
255  
256 69 ▶sub as_string {
Subroutine as_string: called CC=7 SCAR=19.5
Pod documented
257 69 my $self = shift;
258 69 ▶ return $self->{string} if ($self->{string} && !$self->{dirty});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"string"} and not $self->{"dirty"} 6 63 69
Condition66%2/3
$self->{"string"} and not $self->{"dirty"}
!ll&&!rl&&r
6306
MC/DC50%1/2
$self->{"string"} and not $self->{"dirty"}
$self->{"string"} not $self->{"dirty"}
Truth table66%2/3
A and not(B)
A= $self->{"string"}B= not $self->{"dirty"}
ABresult
0X0
100
111
259 63 undef $self->{dirty};
260 63 my @c;
261 63 for (@{$self->{stack}}) {
262 175 my ($kws, @arg) = @$_;
263 175 $kws =~ s/_/ /g;
264 175 for (@arg) {
265 180 ▶ $_ = join(',',@$_) if ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "ARRAY" 71 109 180
266 }
267 175 ▶ if ($kws =~ /foreach/i) { #kludge for FOREACH
Branch: 1 branch
BranchTrueFalseTotal
if ($kws =~ /foreach/i) { } 1 174 175
268 1 push @c, uc($kws)." (".join(' ',@arg).")";
269 }
270 else {
271 174 push @c, join(' ',uc $kws, @arg);
272 }
273 }
274 63 $self->{string} = join(' ',@c);
275 63 $self->{string} =~ s/(\s)+/$1/g;
276 63 return $self->{string};
277 }
278  
279 74 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.4
280 74 my $self = shift;
281 74 my ($method) = $AUTOLOAD =~ /.*::(.*)/;
282 74 ▶ unless (grep /$method/, @all_clauses) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /$method/, @Neo4j::Cypher::Abstract::all_clauses) 0 74 74
283 0 puke "Unknown clause '$method'";
284 }
285 74 $self->_add_clause($method,@_);
286 }
287  
288 0 ▶sub belch (@) {
Subroutine belch: not called CC=1 SCAR=6.9
Pod undocumented
289 0 my($func) = (caller(1))[3];
290 0 Carp::carp "[$func] Warning: ", @_;
291 }
292  
293 0 ▶sub puke (@) {
Subroutine puke: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my($func) = (caller(1))[3];
295 0 Carp::croak "[$func] Fatal: ", @_;
296 }
297  
298 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
299  
300 =head1 NAME
301  
302 Neo4j::Cypher::Abstract - Generate Cypher query statements
303  
304 =head1 SYNOPSIS
305  
306 =head1 DESCRIPTION
307  
308 When writing code to automate database queries, sometimes it is
309 convenient to use a wrapper that generates desired query strings. Then
310 the user can think conceptually and avoid having to remember precise
311 syntax or write and debug string manipulations. A good wrapper can
312 also allow the user to produce query statements dynamically, hide
313 dialect details, and may include some simple syntax
314 checking. C<SQL::Abstract> is an example of a widely-used wrapper for
315 SQL.
316  
317 The graph database L<Neo4j|https://www.neo4j.com> allows SQL-like
318 declarative queries through its query language
319 L<Cypher|https://neo4j.com/docs/developer-manual/current/cypher/>. C<Neo4j::Cypher::Abstract>
320 is a Cypher wrapper in the spirit of C<SQL::Abstract> that creates
321 very general Cypher productions in an intuitive, Perly way.
322  
323 =head2 Basic idea : stringing clauses together with method calls
324  
325 A clause is a portion of a complete query statement that plays a
326 specific functional role in the statement and is set off by one or
327 more reserved words. L<Clauses in
328 Cypher|https://neo4j.com/docs/developer-manual/current/cypher/clauses/>
329 include reading (e.g., MATCH), writing (CREATE), importing (LOAD CSV), and
330 schema (CREATE CONSTRAINT) clauses, among others. They have
331 arguments that define the clause's scope of action.
332  
333 L<Cypher::Abstract|Neo4j::Cypher::Abstract> objects possess methods
334 for every Cypher clause. Each method adds its clause, with arguments,
335 to the object's internal queue. Every method returns the object
336 itself. When an object is rendered as a string, it concatenates its
337 clauses to yield the entire query statement.
338  
339 These features add up to the following idiom. Suppose we want to
340 render the Cypher statement
341  
342 MATCH (n:Users) WHERE n.name =~ 'Fred.*' RETURN n.manager
343  
344 In C<Cypher::Abstract>, we do
345  
346 $s = Neo4j::Cypher::Abstract->new()->match('n:Users')
347 ->where("n.name =~ 'Fred.*'")->return('n.manager');
348 print "$s;\n"; # "" is overloaded by $s->as_string()
349  
350 Because you may create many such statements in a program, a short
351 alias for the constructor can be imported, and extra variable
352 assignments can be avoided.
353  
354 use Neo4j::Cypher::Abstract qw/cypher/;
355 use DBI;
356  
357 my $dbh = DBI->connect("dbi:Neo4p:http://127.0.0.1:7474;user=foo;pass=bar");
358 my $sth = $dbh->prepare(
359 cypher->match('n:Users')->where("n.name =~ 'Fred.*'")->return('n.manager')
360 );
361 $sth->execute();
362 ...
363  
364 =head2 Patterns
365  
366 L<Patterns|https://neo4j.com/docs/developer-manual/current/cypher/syntax/patterns/>
367 are representations of subgraphs with constraints that are key
368 components of Cypher queries. They have their own syntax and are also
369 amenable to wrapping. In the example L<above|/"Basic idea : stringing
370 clauses together with method calls">, C<match()> uses a simple
371 built-in shortcut:
372  
373 $s->match('n:User') eq $s->match('(n:User)')
374  
375 where C<(n:User)> is the simple pattern for "all nodes with label
376 'User'". The module L<Neo4j::Cypher::Pattern> handles
377 complex and arbitrary patterns. It is loaded automatically on C<use
378 Neo4j::Cypher::Abstract>. Abstract patterns are written in a similar
379 idiom as Cypher statements. They can be used anywhere a string is
380 allowed. For example:
381  
382 use Neo4j::Cypher::Abstract qw/cypher ptn/;
383  
384 ptn->N(':Person',{name=>'Oliver Stone'})->R("r>")->N('movie') eq
385 '(:Person {name:'Oliver Stone'})-[r]->(movie)'
386 $sth = $dbh->prepare(
387 cypher->match(ptn->N(':Person',{name=>'Oliver Stone'})->R("r>")->N('movie'))
388 ->return('type(r)')
389 );
390  
391 See L<Neo4j::Cypher::Pattern> for a full description of how
392 to specify patterns.
393  
394 =head2 WHERE clauses
395  
396 As in SQL, Cypher has a WHERE clause that is used to filter returned
397 results. Rather than having to create custom strings for common WHERE
398 expressions, L<SQL::Abstract> provides an intuitive system for
399 constructing valid expressions from Perl data structures made up of
400 hash, array, and scalar references. L<Neo4j::Cypher::Abstract>
401 contains a new implementation of the L<SQL::Abstract> expression
402 "compiler". If the argument to the C<where()> method (or any other
403 method, in fact) is an array or hash reference, it is interpreted as
404 an expression in L<SQL::Abstract> style. (The parser is a complete
405 reimplementation, so some idioms in that style may not result in
406 exactly the same productions.)
407  
408 For details on writing expressions, see
409 L<Neo4j::Cypher::Abstract::Peeler/"Expressing Expressions">.
410  
411 =head2 Parameters
412  
413 Parameters in Cypher are named, and given as alphanumeric tokens
414 prefixed (sadly) with '$'. The C<Cypher::Abstract> object collects
415 these in the order they appear in the complete statement. The list of
416 parameters can be recovered with the C<parameters()> method.
417  
418 $c = cypher->match('n:Person')->return('n.name')
419 ->skip('$s')->limit('$l');
420 @p = $c->parameters; # @p is ('$s', '$l') /;
421  
422 =head1 METHODS
423  
424 =head2 Reading clauses
425  
426 =over
427  
428 =item match(@ptns)
429  
430 =item optional_match(@ptns)
431  
432 =item where($expr)
433  
434 =item start($ptn)
435  
436 =back
437  
438 =head2 Writing clauses
439  
440 =over
441  
442 =item create(@ptns), create_unique($ptn)
443  
444 =item merge(@ptns)
445  
446 =item foreach($running_var => $list, cypher-><update statement>)
447  
448 =item set()
449  
450 =item delete(), detach_delete()
451  
452 =item on_create(), on_match()
453  
454 =back
455  
456 =head2 Modifiers
457  
458 =over
459  
460 =item limit($num)
461  
462 =item skip($num)
463  
464 =item order_by($identifier)
465  
466 =back
467  
468 =head2 General clauses
469  
470 =over
471  
472 =item return(@items), return_distinct(@items)
473  
474 =item with(@identifiers), with_distinct(@identifiers)
475  
476 =item unwind($list => $identifier)
477  
478 =item union()
479  
480 =item call()
481  
482 =item yield()
483  
484 =back
485  
486 =head2 Hinting
487  
488 =over
489  
490 =item using_index($index)
491  
492 =item using_scan()
493  
494 =item using_join($identifier)
495  
496 =back
497  
498 =head2 Loading
499  
500 =over
501  
502 =item load_csv($file => $identifier), load_csv_with_headers(...)
503  
504 =back
505  
506 =head2 Schema
507  
508 =over
509  
510 =item create_constraint_exist($node => $label, $property),create_constraint_unique($node => $label, $property)
511  
512 =item drop_constraint(...)
513  
514 =item create_index($label => $property), drop_index($label => $property)
515  
516 =back
517  
518 =head2 Utility Methods
519  
520 =over
521  
522 =item parameters()
523  
524 Return a list of statement parameters.
525  
526 =item as_string()
527  
528 Render the Cypher statement as a string. Overloads C<"">.
529  
530 =back
531  
532 =head1 SEE ALSO
533  
534 L<Neo4j::Cypher::Pattern>, L<Neo4j::Cypher::Abstract::Peeler/"Expressing Expressions">, L<REST::Neo4p>, L<DBD::Neo4p>, L<SQL::Abstract>
535  
536 =head1 AUTHOR
537  
538 Mark A. Jensen
539 CPAN: MAJENSEN
540 majensen -at- cpan -dot- org
541  
542 =head1 LICENSE
543  
544 This software is provided for use under the terms of Perl itself.
545  
546 =head1 COPYRIGHT
547  
548 (c) 2017 Mark A. Jensen
549  
550 =cut
551  
552 2 1;
553  
« Abstract/Peeler.pm ↑ Summary Pattern.pm »