blib/lib/Neo4j/Cypher/Abstract.pm

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