File Coverage

blib/lib/Neo4j/Bolt/Cxn.pm
Criterion Covered Total %
statement 8 37 21.6
branch 0 18 0.0
condition 0 16 0.0
subroutine 2 11 18.1
pod 8 9 88.8
total 18 91 19.7


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