blib/lib/Neo4j/Cypher/Abstract.pm

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