blib/lib/DBD/Neo4p.pm untested

filter: Statementstmt 0.0% 0 / 321 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 38 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 529 CC 54 SCAR 35.7
CC
54
Cov
0%
connect
90.0
execute
72.0
commit
56.0
CRAP 35.4
1 use v5.10.1;
2 package DBD::Neo4p;
3 use strict;
4 use warnings;
5 use REST::Neo4p 0.3030;
6 use JSON;
7 require DBI;
8 no warnings qw/once/;
9  
10 BEGIN {
11 $DBD::Neo4p::VERSION = '0.2000';
12 }
13  
14 our $err = 0; # holds error code for DBI::err
15 our $errstr = ''; # holds error string for DBI::errstr
16 our $drh = undef; # holds driver handle once initialised
17 our $prefix = 'neo';
18  
19 sub driver($$){
20 return $drh if $drh;
21 my($sClass, $rhAttr) = @_;
22 $sClass .= '::dr';
23  
24 # install methods if nec.
25  
26 DBD::Neo4p::db->install_method('neo_neo4j_version');
27  
28 $drh = DBI::_new_drh($sClass,
29 {
30 Name => $sClass,
31 Version => $DBD::Neo4p::VERSION,
32 Err => \$DBD::Neo4p::err,
33 Errstr => \$DBD::Neo4p::errstr,
34 State => \$DBD::Neo4p::sqlstate,
35 Attribution => 'DBD::Neo4p by Mark A. Jensen'
36 }
37 );
38 return $drh;
39 }
40  
41 package # hide from PAUSE
42 DBD::Neo4p::dr;
43 $DBD::Neo4p::dr::imp_data_size = 0;
44  
45 sub connect($$;$$$) {
46 my($drh, $sDbName, $sUsr, $sAuth, $rhAttr)= @_;
47  
48 #1. create database-handle
49 my ($outer, $dbh) = DBI::_new_dbh($drh, {
50 Name => $sDbName,
51 USER => $sUsr,
52 CURRENT_USER => $sUsr,
53 });
54 local $REST::Neo4p::HANDLE;
55 $dbh->STORE("${prefix}_Handle", REST::Neo4p->create_and_set_handle);
56 # default attributes
57 $dbh->STORE("${prefix}_ResponseAsObjects",0);
58  
59 #2. Parse extra strings in DSN(key1=val1;key2=val2;...)
60 foreach my $sItem (split(/;/, $sDbName)) {
61 my ($key, $value) = $sItem =~ /(.*?)=(.*)/;
62 return $drh->set_err($DBI::stderr, "Can't parse DSN part '$sItem'")
63 unless defined $value;
64 $key = "${prefix}_$key" unless $key =~ /^${prefix}_/;
65 $dbh->STORE($key, $value);
66 }
67 $DB::single=1;
68 my $db = delete $rhAttr->{"${prefix}_database"} || delete $rhAttr->{"${prefix}_db"} || $dbh->{neo_db};
69 my $host = $dbh->FETCH("${prefix}_host") || 'localhost';
70 my $port = $dbh->FETCH("${prefix}_port") || 7474;
71 my $protocol = $dbh->FETCH("${prefix}_protocol") || 'http';
72 my $user = delete $rhAttr->{Username} || $dbh->FETCH("${prefix}_user") || $sUsr;
73 my $pass = delete $rhAttr->{Password} || $dbh->FETCH("${prefix}_pass") || $sAuth;
74 if (my $ssl_opts = delete $rhAttr->{SSL_OPTS}) {
75 if (REST::Neo4p->agent->isa('LWP::UserAgent')) {
76 while (my ($k,$v) = each %$ssl_opts) {
77 REST::Neo4p->agent->ssl_opts($k => $v);
78 }
79 }
80 }
81 # use db=<protocol>://<host>:<port> or host=<host>;port=<port>
82 # db attribute trumps
83  
84 if ($db) {
85 ($protocol, $host, $port) = $db =~ m|^(https?)?(?:://)?([^:]+):?([0-9]*)$|;
86 $protocol //= 'http';
87 return $drh->set_err($DBI::stderr, "DB host and/or port not specified correctly") unless ($host && $port);
88 }
89  
90 # real connect...
91  
92 $db = "$protocol://$host:$port";
93 eval {
94 REST::Neo4p->connect($db,$user,$pass);
95 };
96 if (my $e = Exception::Class->caught()) {
97 return
98 ref $e ? $drh->set_err($DBI::stderr, "Can't connect to $sDbName: ".ref($e)." : ".$e->message.' ('.$e->code.')') :
99 $drh->set_err($DBI::stderr, $e);
100 };
101  
102 foreach my $sKey (keys %$rhAttr) {
103 $dbh->STORE($sKey, $rhAttr->{$sKey});
104 }
105 $dbh->STORE(Active => 1);
106 $dbh->STORE(AutoCommit => 1);
107 $dbh->{"${prefix}_agent"} = REST::Neo4p->agent;
108  
109 return $outer;
110 }
111  
112 sub data_sources ($;$) {
113 my($drh, $rhAttr) = @_;
114 return;
115 }
116  
117 sub disconnect_all($) { }
118  
119 package #hide from PAUSE
120 DBD::Neo4p::db;
121 $DBD::Neo4p::db::imp_data_size = 0;
122  
123 sub prepare {
124 my($dbh, $sStmt, $rhAttr) = @_;
125 #1. Create blank sth
126 my ($outer, $sth) = DBI::_new_sth($dbh, { Statement => $sStmt, });
127 return $sth unless($sth);
128  
129 # cypher query parameters are given as tokens surrounded by curly braces:
130 # crude count:
131 my @parms = $sStmt =~ /\{\s*([^}[:space:]]*)\s*\}/g;
132 $sth->STORE('NUM_OF_PARAMS', scalar @parms);
133 $sth->{"${prefix}_param_names"} = \@parms;
134 $sth->{"${prefix}_param_values"} = [];
135 return $outer;
136 }
137  
138 sub begin_work {
139 my ($dbh) = @_;
140 local $REST::Neo4p::HANDLE;
141 REST::Neo4p->set_handle($dbh->{"${prefix}_Handle"});
142 unless ($dbh->{AutoCommit}) {
143 $drh->set_err($DBI::stderr, "begin_work not effective, AutoCommit already off");
144 return;
145 }
146 eval {
147 REST::Neo4p->begin_work;
148 };
149 if ( my $e = REST::Neo4p::VersionMismatchException->caught()) {
150 warn("Your neo4j server does not support transactions via REST API") if $dbh->FETCH('Warn');
151 return;
152 }
153 elsif ($e = Exception::Class->caught()) {
154 return
155 ref $e ? $drh->set_err($DBI::stderr, "Can't begin transaction: ".ref($e)." : ".$e->message.' ('.$e->code.')') :
156 $drh->set_err($DBI::stderr, $e);
157 };
158 $dbh->STORE('AutoCommit',0);
159 return 1;
160 }
161  
162 sub commit ($) {
163 my($dbh) = @_;
164 if ($dbh->FETCH('AutoCommit')) {
165 warn("Commit ineffective while AutoCommit is on") if $dbh->FETCH('Warn');
166 return;
167 }
168 else {
169 local $REST::Neo4p::HANDLE;
170 REST::Neo4p->set_handle($dbh->{"${prefix}_Handle"});
171 eval {
172 REST::Neo4p->commit;
173 };
174 if ( my $e = REST::Neo4p::VersionMismatchException->caught()) {
175 warn("Your neo4j server does not support REST transactions") if $dbh->FETCH('Warn');
176 return;
177 }
178 elsif ($e = Exception::Class->caught()) {
179 return
180 ref $e ? $drh->set_err($DBI::stderr, "Can't commit: ".ref($e)." : ".$e->message.' ('.$e->code.')') :
181 $drh->set_err($DBI::stderr, $e);
182 };
183 return 1;
184 }
185 }
186  
187 sub rollback ($) {
188 my($dbh) = @_;
189 if ($dbh->FETCH('AutoCommit')) {
190 warn("Rollback ineffective while AutoCommit is on") if $dbh->FETCH('Warn');
191 return;
192 }
193 else {
194 local $REST::Neo4p::HANDLE;
195 REST::Neo4p->set_handle($dbh->{"${prefix}_Handle"});
196 eval {
197 REST::Neo4p->rollback;
198 };
199 if ( my $e = REST::Neo4p::VersionMismatchException->caught()) {
200 warn("Your neo4j server does not support REST transactions") if $dbh->FETCH('Warn');
201 return;
202 }
203 elsif ($e = Exception::Class->caught()) {
204 return
205 ref $e ? $drh->set_err($DBI::stderr, "Can't rollback: ".ref($e)." : ".$e->message.' ('.$e->code.')') :
206 $drh->set_err($DBI::stderr, $e);
207 };
208 return 1;
209 }
210 }
211  
212 sub ping {
213 my $dbh = shift;
214 my $s = ($dbh->neo_neo4j_version =~ /^[3-9]\.0/ ? 'match (a) return a limit 1' :
215 'return 1');
216 my $sth = $dbh->prepare($s) or return 0;
217 $sth->execute or return 0;
218 $sth->finish;
219 return 1;
220 }
221  
222 # neo4j metadata -- needs thinking
223 # v2.0 : http://docs.neo4j.org/chunked/2.0.0-M06/rest-api-cypher.html#rest-api-retrieve-query-metadata
224  
225 sub neo_neo4j_version {
226 my $dbh = shift;
227 return $dbh->{"${prefix}_agent"}->{_actions}{neo4j_version};
228 }
229  
230  
231 # table_info is a nop
232  
233 sub table_info ($) {
234 my($dbh) = @_;
235 # -->> Change
236 my ($raTables, $raName) = (undef, undef);
237 # <<-- Change
238 return undef unless $raTables;
239 # 2. create DBD::Sponge driver
240 my $dbh2 = $dbh->{'_sponge_driver'};
241 if (!$dbh2) {
242 $dbh2 = $dbh->{'_sponge_driver'} = DBI->connect("DBI:Sponge:");
243 if (!$dbh2) {
244 $dbh->DBI::set_err( 1, $DBI::errstr);
245 return undef;
246 $DBI::errstr .= ''; #Just for IGNORE warning
247 }
248 }
249 #3. assign table info to the DBD::Sponge driver
250 my $sth = $dbh2->prepare("TABLE_INFO",
251 { 'rows' => $raTables, 'NAMES' => $raName });
252 if (!$sth) {
253 $dbh->DBI::set_err(1, $dbh2->errstr());
254 }
255 return $sth;
256 }
257  
258 sub type_info_all ($) {
259 my ($dbh) = @_;
260 return [];
261 }
262  
263 sub disconnect ($) {
264 my ($dbh) = @_;
265 eval {
266 REST::Neo4p->disconnect_handle($dbh->{"${prefix}_Handle"});
267 };
268 $dbh->STORE(Active => 0);
269 1;
270 }
271  
272 sub FETCH ($$) {
273 my ($dbh, $sAttr) = @_;
274 use experimental qw/smartmatch/;
275 given ($sAttr) {
276 when ('AutoCommit') { return $dbh->{$sAttr} }
277 when (/^${prefix}_/) { return $dbh->{$sAttr} }
278 default { return $dbh->SUPER::FETCH($sAttr) }
279 }
280 }
281  
282 sub STORE ($$$) {
283 my ($dbh, $sAttr, $sValue) = @_;
284 use experimental qw/smartmatch/;
285 given ($sAttr) {
286 when ('AutoCommit') {
287 local $REST::Neo4p::HANDLE = $dbh->{"${prefix}_Handle"};
288 if (!!$sValue) {
289 REST::Neo4p->_set_autocommit;
290 $dbh->{$sAttr} = 1;
291 }
292 else {
293 $dbh->{$sAttr} = 0 if REST::Neo4p->_clear_autocommit;
294 }
295 return 1;
296 }
297 # private attributes (neo_)
298 when (/^${prefix}_/) {
299 $dbh->{$sAttr} = $sValue;
300 return 1;
301 }
302 default {
303 return $dbh->SUPER::STORE($sAttr => $sValue);
304 }
305 }
306 }
307  
308 sub DESTROY($) {
309 my($dbh) = @_;
310 $dbh->disconnect;
311 # deal with the REST::Neo4p object
312 }
313  
314 package #hide from PAUSE
315 DBD::Neo4p::st;
316 $DBD::Neo4p::st::imp_data_size = 0;
317  
318 sub bind_param ($$$;$) {
319 my($sth, $param, $value, $attribs) = @_;
320 return $sth->DBI::set_err(2, "Can't bind_param $param, too big")
321 if ($param > $sth->FETCH('NUM_OF_PARAMS'));
322 $sth->{"${prefix}_param_values"}->[$param-1] = $value;
323 return 1;
324 }
325  
326 sub execute($@) {
327 my ($sth, @bind_values) = @_;
328  
329 $sth->finish if $sth->{Active}; # DBI::DBD example, follow up...
330  
331 my $params = @bind_values ? \@bind_values : $sth->{"${prefix}_param_values"};
332 unless (@$params == $sth->FETCH('NUM_OF_PARAMS')) {
333 return $sth->set_err($DBI::stderr, "Wrong number of parameters");
334 }
335 # Execute
336 # by this time, I know all my parameters
337 # so create the Query obj here
338 local $REST::Neo4p::HANDLE = $sth->{Database}->{"${prefix}_Handle"};
339  
340 # per DBI spec, begin work under the hood if AutoCommit is FALSE:
341 unless ($sth->{Database}->FETCH('AutoCommit')) {
342 unless (REST::Neo4p->_transaction) {
343 REST::Neo4p->begin_work;
344 }
345 }
346  
347 my %params;
348 @params{@{$sth->{"${prefix}_param_names"}}} = @$params;
349 my $q = $sth->{"${prefix}_query_obj"} = REST::Neo4p::Query->new(
350 $sth->{Statement}, \%params
351 );
352 $q->{ResponseAsObjects} = $sth->{Database}->{"${prefix}_ResponseAsObjects"};
353  
354 my $numrows = $q->execute;
355 if ($q->err) {
356 return $sth->set_err($DBI::stderr,$q->errstr.' ('.$q->err.')');
357 }
358  
359 $sth->{"${prefix}_rows"} = $numrows;
360 # don't know why I have to do the following, when the FETCH
361 # method delegates this to the query object and $sth->{NUM_OF_FIELDS}
362 # thereby returns the correct number, but $sth->_set_bav($row) segfaults
363 # if I don't:
364 $sth->STORE(NAME => $q->{NAME});
365 $sth->STORE(NUM_OF_FIELDS => 0);
366 $sth->STORE(NUM_OF_FIELDS => $q ? $q->{NUM_OF_FIELDS} : undef);
367  
368 $sth->{Active} = 1;
369 return $numrows || '0E0';
370 }
371  
372 sub fetch ($) {
373 my ($sth) = @_;
374 my $q =$sth->{"${prefix}_query_obj"};
375 unless ($q) {
376 return $sth->set_err($DBI::stderr, "Query not yet executed");
377 }
378 my $row;
379 eval {
380 $row = $q->fetch;
381 };
382 if (my $e = Exception::Class->caught) {
383 $sth->finish;
384 return $sth->set_err($DBI::stderr, ref $e ? ref($e)." : ".$e->message : $e);
385 }
386 if ($q->err) {
387 $sth->finish;
388 return $sth->set_err($DBI::stderr,$q->errstr.' ('.$q->err.')');
389 }
390
391 unless ($row) {
392 $sth->STORE(Active => 0);
393 return undef;
394 }
395 $sth->STORE(NAME => $q->{NAME});
396 $sth->STORE(NUM_OF_FIELDS => $q->{NUM_OF_FIELDS});
397 $sth->_set_fbav($row);
398 }
399  
400 *fetchrow_arrayref = \&fetch;
401  
402 # override fetchall_hashref - create a sensible hash key from node,
403 # relationship structures
404 sub fetchall_hashref {
405 my ($sth, $key_field) = @_;
406 my @keys;
407 push @keys, ref $key_field ? @{$key_field} : $key_field;
408 my @names = @{$sth->FETCH($sth->{Database}->{FetchHashKeyName})};
409 for my $key (@keys) {
410 my $qkey = quotemeta $key;
411 unless (grep(/^$qkey$/, @names)) {
412 return $sth->set_err($DBI::stderr, "'$key_field' not a column name");
413 }
414  
415 }
416 my $rows = $sth->fetchall_arrayref;
417 my $ret = {};
418 return unless $rows;
419 use experimental qw/smartmatch/;
420 for my $row (@$rows) {
421 my %data;
422 @data{@names} = @$row;
423 my $h = $ret;
424 for my $k (@keys) {
425 my $key_from_data;
426 given (ref $data{$k}) {
427 when (!$_) {
428 $key_from_data = $data{$k};
429 }
430 when (/REST::Neo4p/) {
431 $key_from_data = ${$data{$k}}; # id
432 }
433 when (/HASH|ARRAY/) {
434 $key_from_data = $data{$k}{_node} || $data{$k}{_relationship};
435 $key_from_data = JSON->new->utf8->encode($data{$k}) unless $key_from_data;
436 }
437 default {
438 die "whaaa? (fetchall_hashref)";
439 }
440 }
441 $h->{$key_from_data} = {};
442 $h = $h->{$key_from_data};
443 }
444 for my $n (@names) {
445 my $qn = quotemeta $n;
446 next if grep /^$qn$/,@keys;
447 $h->{$n} = $data{$n};
448 }
449 }
450  
451 return $ret;
452 }
453 sub rows ($) {
454 my($sth) = @_;
455 return $sth->{"${prefix}_rows"};
456 }
457  
458 sub finish ($) {
459 my ($sth) = @_;
460 $sth->{"${prefix}_query_obj"}->finish()
461 if (defined($sth->{"${prefix}_query_obj"}));
462 $sth->{"${prefix}_query_obj"} = undef;
463 $sth->STORE(Active => 0);
464 $sth->SUPER::finish();
465 return 1;
466 }
467  
468 sub FETCH ($$) {
469 my ($sth, $attrib) = @_;
470 my $q =$sth->{"${prefix}_query_obj"};
471 use experimental qw/smartmatch/;
472 given ($attrib) {
473 when ('TYPE') {
474 return;
475 }
476 when ('PRECISION') {
477 return;
478 }
479 when ('SCALE') {
480 return;
481 }
482 when ('NULLABLE') {
483 return;
484 }
485 when ('RowInCache') {
486 return;
487 }
488 when ('CursorName') {
489 return;
490 }
491 # Private driver attributes have neo_ prefix
492 when (/^${prefix}_/) {
493 return $sth->{$attrib}
494 }
495 default {
496 return $sth->SUPER::FETCH($attrib)
497 }
498 }
499 }
500  
501 sub STORE ($$$) {
502 my ($sth, $attrib, $value) = @_;
503 use experimental qw/smartmatch/;
504 #1. Private driver attributes have neo_ prefix
505 given ($attrib) {
506 when (/^${prefix}_|(?:NAME$)/) {
507 $sth->{$attrib} = $value;
508 return 1;
509 }
510 default {
511 return $sth->SUPER::STORE($attrib, $value);
512 }
513 }
514 }
515  
516 sub DESTROY {
517 my ($sth) = @_;
518 undef $sth->{"${prefix}_query_obj"};
519 }
520  
521 #>> Just for no warning-----------------------------------------------
522 $DBD::Neo4p::dr::imp_data_size = 0;
523 $DBD::Neo4p::db::imp_data_size = 0;
524 $DBD::Neo4p::st::imp_data_size = 0;
525 *DBD::Neo4p::st::fetchrow_arrayref = \&DBD::Neo4p::st::fetch;
526 #<< Just for no warning------------------------------------------------
527 1;
528 __END__