File Coverage

blib/lib/Neo4j/Bolt/Cxn.pm
Criterion Covered Total %
statement 8 39 20.5
branch 0 18 0.0
condition 0 16 0.0
subroutine 2 11 18.1
pod 8 9 88.8
total 18 93 19.3


line stmt bran cond sub pod time code
1             package Neo4j::Bolt::Cxn;
2 4     4   24 use Carp qw/croak/;
  4         7  
  4         270  
3              
4             BEGIN {
5 4     4   13 our $VERSION = "0.4203";
6 4         1439 require Neo4j::Bolt::CTypeHandlers;
7 4         1410 require Neo4j::Bolt::ResultStream;
8 4         26 require XSLoader;
9 4         3592 XSLoader::load();
10             }
11 0   0 0 0   sub default_db () { $Neo4j::Bolt::DEFAULT_DB // "" }
12              
13 0     0 1   sub errnum { shift->errnum_ }
14 0     0 1   sub errmsg { shift->errmsg_ }
15 0     0 1   sub reset_cxn { shift->reset_ }
16              
17 0     0 1   sub server_id { shift->server_id_ }
18 0     0 1   sub protocol_version { shift->protocol_version_ }
19              
20             sub run_query {
21 0     0 1   my $self = shift;
22 0           my ($query, $parms, $db) = @_;
23 0 0         unless ($query) {
24 0           croak "Arg 1 should be Cypher query string";
25             }
26 0 0 0       if ($parms && !(ref $parms == 'HASH')) {
27 0           croak "Arg 2 should be a hashref of { param => $value, ... }";
28             }
29 0 0         croak "No connection" unless $self->connected;
30 0           utf8::upgrade($query);
31 0   0       return $self->run_query_($query, $parms // {}, 0, $db // default_db);
      0        
32             }
33              
34             sub send_query {
35 0     0 1   my $self = shift;
36 0           my ($query, $parms) = @_;
37 0 0         unless ($query) {
38 0           croak "Arg 1 should be Cypher query string";
39             }
40 0 0 0       if ($parms && !(ref $parms == 'HASH')) {
41 0           croak "Arg 2 should be a hashref of { param => $value, ... }";
42             }
43 0 0         croak "No connection" unless $self->connected;
44 0           utf8::upgrade($query);
45 0 0 0       return $self->run_query_($query, $parms ? $parms : {}, 1, $db // default_db );
46             }
47              
48             sub do_query {
49 0     0 1   my $self = shift;
50 0           my $stream = $self->run_query(@_);
51 0           my @results;
52 0 0         if ($stream->success_) {
53 0           while (my @row = $stream->fetch_next_) {
54 0           push @results, [@row];
55             }
56             }
57 0 0         return wantarray ? ($stream, @results) : $stream;
58             }
59              
60             =head1 NAME
61              
62             Neo4j::Bolt::Cxn - Container for a Neo4j Bolt connection
63              
64             =head1 SYNOPSIS
65              
66             use Neo4j::Bolt;
67             $cxn = Neo4j::Bolt->connect("bolt://localhost:7687");
68             unless ($cxn->connected) {
69             die "Problem connecting: ".$cxn->errmsg;
70             }
71             $stream = $cxn->run_query(
72             "MATCH (a) RETURN head(labels(a)) as lbl, count(a) as ct",
73             );
74             if ($stream->failure) {
75             print STDERR "Problem with query run: ".
76             ($stream->client_errmsg || $stream->server_errmsg);
77             }
78              
79             =head1 DESCRIPTION
80              
81             L is a container for a Bolt connection, instantiated by
82             a call to C<< Neo4j::Bolt->connect() >>.
83              
84             =head1 METHODS
85              
86             =over
87              
88             =item connected()
89              
90             True if server connected successfully. If not, see L and
91             L.
92              
93             =item protocol_version()
94              
95             Returns a string representing the major and minor Bolt protocol version of the
96             server, as ".", or the empty string if not connected.
97              
98             =item run_query($cypher_query, [$param_hash])
99              
100             Run a L query on
101             the server. Returns a L which can be iterated
102             to retrieve query results as Perl types and structures. [$param_hash]
103             is an optional hashref of the form C<{ param =E $value, ... }>.
104              
105             =item send_query($cypher_query, [$param_hash])
106              
107             Send a L query to
108             the server. All results (except error info) are discarded.
109              
110             =item do_query($cypher_query, [$param_hash])
111              
112             ($stream, @rows) = do_query($cypher_query);
113             $stream = do_query($cypher_query, $param_hash);
114              
115             Run a L query on
116             the server, and iterate the stream to retrieve all result
117             rows. C is convenient for running write queries (e.g.,
118             C ), since it returns the $stream
119             with L ready for reading.
120              
121             =item reset_cxn()
122              
123             Send a RESET message to the Neo4j server. According to the L
124             protocol|https://boltprotocol.org/v1/>, this should force any currently
125             processing query to abort, forget any pending queries, clear any
126             failure state, dispose of outstanding result records, and roll back
127             the current transaction.
128              
129             =item errnum(), errmsg()
130              
131             Current error state of the connection. If
132              
133             $cxn->connected == $cxn->errnum == 0
134              
135             then you have a virgin Cxn object that came from someplace other than
136             C<< Neo4j::Bolt->connect() >>, which would be weird.
137              
138             =item server_id()
139              
140             print $cxn->server_id; # "Neo4j/3.3.9"
141              
142             Get the server ID string, including the version number. C if
143             connecting wasn't successful or the server didn't identify itself.
144              
145             =back
146              
147             =head1 SEE ALSO
148              
149             L, L.
150              
151             =head1 AUTHOR
152              
153             Mark A. Jensen
154             CPAN: MAJENSEN
155             majensen -at- cpan -dot- org
156              
157             =head1 LICENSE
158              
159             This software is Copyright (c) 2019-2021 by Mark A. Jensen.
160              
161             This is free software, licensed under:
162              
163             The Apache License, Version 2.0, January 2004
164              
165             =cut
166              
167             1;