| 1 | package Mojo::CouchDB::DB; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | has ua => sub { Mojo::UserAgent->new }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | has 'url'; | |||||||||||||||||||||||||||
| 17 | 2 | has 'auth'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub all_docs { | ||||||||||||||||||||||||||
|
Subroutine
all_docs: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 2 | my $query = $self->_to_query(shift); | |||||||||||||||||||||||||||
| 22 | 0 | return $self->_call('_all_docs' . $query, 'get')->result->json; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub all_docs_p { | ||||||||||||||||||||||||||
|
Subroutine
all_docs_p: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 27 | 2 | my $query = $self->_to_query(shift); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | return $self->_call_p('_all_docs' . $query, 'get_p') | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | ->then(sub { return shift->res->json }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub create_db { | ||||||||||||||||||||||||||
|
Subroutine
create_db: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 2 | return shift->_call('', 'put'); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 2 | my $sc = shift; | |||||||||||||||||||||||||||
| 40 | 2 | return $self->_find($sc)->_call('_find', 'post', $sc); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub find_p { | ||||||||||||||||||||||||||
|
Subroutine
find_p: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | 2 | my $sc = shift; | |||||||||||||||||||||||||||
| 46 | return $self->_find($sc)->_call_p('_find', 'post_p', $sc) | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | ->then(sub { return shift->res->json }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 5 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | 5 | my $id = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | $id = $$id while (reftype($id)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 4 | return $self->_get($id)->_call("/$id", 'get'); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub get_p { | ||||||||||||||||||||||||||
|
Subroutine
get_p: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 0 | my $id = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $id = $$id while (reftype($id)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | return $self->_get($id)->_call_p("/$id", 'get'); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub index { | ||||||||||||||||||||||||||
|
Subroutine
index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 2 | my $idx = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | return $self->_index($idx)->_call('_index', 'post', $idx); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub index_p { | ||||||||||||||||||||||||||
|
Subroutine
index_p: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 2 | my $idx = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | return $self->_index($idx)->_call_p('_index', 'post_p', $idx); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 2 | my $self = shift->SUPER::new; | |||||||||||||||||||||||||||
| 82 | 2 | my $url = shift; | |||||||||||||||||||||||||||
| 83 | 2 | my $auth = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | carp qq{No auth provided for $url. This may have been a mistake.} unless $auth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | croak qq{No path part found for database.} unless exists $url->path->parts->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | $self->{auth} = $auth; | |||||||||||||||||||||||||||
| 89 | 2 | $self->{url} = $url; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | return $self; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 4 | my $doc = shift; | |||||||||||||||||||||||||||
| 97 | 4 | my $res = $self->_save($doc)->_call('', 'post', $doc); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | my $dc = dclone $doc; | |||||||||||||||||||||||||||
| 100 | 2 | $dc->{_id} = $res->{_id}; | |||||||||||||||||||||||||||
| 101 | 2 | $dc->{_rev} = $res->{_rev}; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | return $dc; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | sub save_p { | ||||||||||||||||||||||||||
|
Subroutine
save_p: called CC=1 SCAR=2.2
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 2 | my $doc = shift; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | return $self->_save($doc)->_call_p('', 'post_p', $doc)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 110 | 0 | my $res = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $dc = dclone $doc; | |||||||||||||||||||||||||||
| 113 | 0 | $dc->{_id} = $res->{_id}; | |||||||||||||||||||||||||||
| 114 | 0 | $dc->{_rev} = $res->{_rev}; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return $doc; | |||||||||||||||||||||||||||
| 117 | 2 | }); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | sub save_many { | ||||||||||||||||||||||||||
|
Subroutine
save_many: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | 2 | my $docs = shift; | |||||||||||||||||||||||||||
| 123 | 2 | return $self->_save_many($docs)->_call('/bulk_docs', 'post', {docs => $docs}); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | ▶ | sub save_many_p { | ||||||||||||||||||||||||||
|
Subroutine
save_many_p: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 128 | 2 | my $docs = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | return $self->_save_many($docs)->_call_p('/bulk_docs', 'post_p', {docs => $docs}); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 7 | ▶ | sub _call { | ||||||||||||||||||||||||||
|
Subroutine
_call: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 134 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | 7 | my $loc = shift; | |||||||||||||||||||||||||||
| 136 | 7 | my $method = shift; | |||||||||||||||||||||||||||
| 137 | 7 | my $body = shift; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 7 | ▶ | my $url = $loc && $loc ne '' ? $self->url->to_string . "$loc" : $self->url->to_string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$loc and $loc ne ""
MC/DC50%1/2
$loc and $loc ne ""
$loc
$loc ne ""
Truth table66%2/3
A and B
A= $locB= $loc ne ""
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 7 | my $headers = {Authorization => $self->auth}; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 7 | ▶ | my $r | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | = ($body | ||||||||||||||||||||||||||||
| 145 | ? $self->ua->$method($url, $headers, 'json', $body) | ||||||||||||||||||||||||||||
| 146 | : $self->ua->$method($url, $headers))->result; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | croak 'CouchDB encountered an error: ' . $r->json->{error} | ||||||||||||||||||||||||||||
| 149 | 7 | ▶ | if $r->json and exists($r->json->{error}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->json and exists $r->json->{"error"}
MC/DC0%0/2
$r->json and exists $r->json->{"error"}
$r->json
exists $r->json->{"error"}
Truth table66%2/3
A and B
A= $r->jsonB= exists $r->json->{"error"}
| |||||||||||||||||||||||||||||
| 150 | 7 | ▶ | croak 'CouchDB encountered an error: ' . $r->code . ' ' . encode_json($r->json) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | unless $r->is_success; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 6 | ▶ | return $r->json || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$r->json || {}
MC/DC100%1/1
$r->json || {}
$r->json
Truth table100%2/2
A
A= $r->json
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub _call_p { | ||||||||||||||||||||||||||
|
Subroutine
_call_p: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 157 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 158 | 0 | my $loc = shift; | |||||||||||||||||||||||||||
| 159 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 160 | 0 | my $body = shift; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $url = $loc && $loc ne '' ? $self->url->to_string . "$loc" : $self->url->to_string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$loc and $loc ne ""
MC/DC0%0/2
$loc and $loc ne ""
$loc
$loc ne ""
Truth table0%0/3
A and B
A= $locB= $loc ne ""
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my $headers = {Authorization => $self->auth}; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($body) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return $self->ua->$method($url, $headers, 'json', $body)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 168 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | croak 'CouchDB encountered an error: ' . $r->res->json->{error} | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if (exists $r->res->json->{error}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | return $r->res->json; | |||||||||||||||||||||||||||
| 174 | 0 | }); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return $self->ua->$method($url, $headers)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 178 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | croak 'CouchDB encountered an error: ' . $r->res->json->{error} | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if (exists $r->res->json->{error}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | croak 'CouchDB encountered an error: ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | . $r->res->code . ' ' | ||||||||||||||||||||||||||||
| 184 | . encode_json($r->res->json) | ||||||||||||||||||||||||||||
| 185 | if (!$r->is_success); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | return $r->res->json; | |||||||||||||||||||||||||||
| 188 | 0 | }); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | ▶ | sub _find { | ||||||||||||||||||||||||||
|
Subroutine
_find: called CC=4 SCAR=16.4
| |||||||||||||||||||||||||||||
| 192 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 193 | 4 | my $sc = shift; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 4 | ▶ | croak qq{Invalid type supplied for search criteria, expected hashref got: undef } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | unless $sc; | ||||||||||||||||||||||||||||
| 197 | 2 | ▶ | croak qq{Invalid type supplied for search criteria, expected hashref got: scalar } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | unless reftype $sc; | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | croak qq{Invalid type supplied for search criteria, expected hashref got: } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | . reftype $sc | ||||||||||||||||||||||||||||
| 201 | unless reftype($sc) eq 'HASH'; | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | return $self; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 4 | ▶ | sub _get { | ||||||||||||||||||||||||||
|
Subroutine
_get: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 207 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 208 | 4 | my $id = shift; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 4 | ▶ | croak qq{Invalid type supplied for id, expected scalar got: undef} unless $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 3 | return $self; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 4 | ▶ | sub _index { | ||||||||||||||||||||||||||
|
Subroutine
_index: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 216 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 217 | 4 | my $idx = shift; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 4 | ▶ | croak qq{Invalid type supplied for index, expected hashref got: undef } unless $idx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 2 | ▶ | croak qq{Invalid type supplied for index, expected hashref got: scalar } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | unless reftype $idx; | ||||||||||||||||||||||||||||
| 222 | 1 | ▶ | croak qq{Invalid type supplied for index, expected hashref got: } . reftype $idx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | unless reftype($idx) eq 'HASH'; | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | return $self; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 6 | ▶ | sub _save { | ||||||||||||||||||||||||||
|
Subroutine
_save: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 229 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 230 | 6 | my $doc = shift; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 6 | ▶ | croak qq{No save argument specified, expected hashref got: undef } unless $doc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 4 | ▶ | croak qq{Invalid type supplied for document, expected hashref got: scalar } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | unless reftype $doc; | ||||||||||||||||||||||||||||
| 235 | 3 | ▶ | croak qq{Invalid type supplied for document, expected hashref got: } . (reftype $doc) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | unless reftype($doc) eq 'HASH'; | ||||||||||||||||||||||||||||
| 237 | 2 | ▶ | croak qq{Cannot call save without a document} unless (defined $doc); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 2 | return $self; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 4 | ▶ | sub _save_many { | ||||||||||||||||||||||||||
|
Subroutine
_save_many: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 243 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | 4 | my $docs = shift; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 4 | ▶ | croak qq{Cannot save many without a documents} unless defined $docs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 2 | ▶ | croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | qq{Invalid type supplied for documents, expected arrayref of hashref's got: scalar } | ||||||||||||||||||||||||||||
| 249 | unless reftype $docs; | ||||||||||||||||||||||||||||
| 250 | 1 | ▶ | croak qq{Invalid type supplied for documents, expected arrayref of hashref's got: } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | . (reftype $docs) | ||||||||||||||||||||||||||||
| 252 | unless (reftype($docs) eq 'ARRAY'); | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | return $self; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 4 | ▶ | sub _to_query { | ||||||||||||||||||||||||||
|
Subroutine
_to_query: called CC=5 SCAR=23.1
| |||||||||||||||||||||||||||||
| 258 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 259 | 4 | my $query = shift; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 4 | ▶ | croak qq{Invalid type supplied for query, expected hashref got undef} unless $query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 2 | ▶ | croak qq{Invalid type supplied for query, expected hashref got scalar} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | unless reftype $query; | ||||||||||||||||||||||||||||
| 264 | 0 | ▶ | croak qq{Invalid type supplied for query, expected hashref got: } . reftype($query) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$query and builtin::reftype($query) eq "HASH"
MC/DC0%0/2
$query and builtin::reftype($query) eq "HASH"
$query
builtin::reftype($query) eq "HASH"
Truth table0%0/3
A and B
A= $queryB= builtin::reftype($query) eq "HASH"
| |||||||||||||||||||||||||||||
| 265 | unless $query && reftype($query) eq 'HASH'; | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | my $t_uri = URI->new('', 'http'); | |||||||||||||||||||||||||||
| 268 | 0 | $t_uri->query_form(%$query); | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | return $t_uri->query; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 2 | 1; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =encoding utf8 | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head1 NAME | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Mojo::CouchDB::DB | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | # Create a Mojo::CouchDB::DB instance via Mojo::CouchDB | ||||||||||||||||||||||||||||
| 285 | my $couch = Mojo::CouchDB->new('http://localhost:5984', 'username', 'password'); | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | my $books_db = $couch->db('books'); # Mojo::CouchDB::DB; | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # Do database stuff | ||||||||||||||||||||||||||||
| 290 | my $dune_books = $books_db->find({ selector => { name => "Dune" } })->{docs}; | ||||||||||||||||||||||||||||
| 291 | # ... | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =head2 all_docs | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | $db->all_docs({ limit => 10, skip => 5}); | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Retrieves a list of all of the documents in the database. This is packaged as a hashref with | ||||||||||||||||||||||||||||
| 298 | C<offset>: the offset of the query, C<rows>: the documents in the page, and C<total_rows>: the number of rows returned in the dataset. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | Optionally, can take a hashref of query parameters that correspond with the CouchDB L<view query specification|https://docs.couchdb.org/en/stable/api/ddoc/views.html#db-design-design-doc-view-view-name>. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =head2 all_docs_p | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | $db->all_docs_p({ limit => 10, skip => 5 }); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | See L<"all_docs">, except returns the result in a L<Mojo::Promise>. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head2 create_db | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | $db->create_db | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | Create the database, returns C<1> if succeeds or if it already exsits, else returns C<undef>. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =head2 find | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | $db->find($search_criteria); | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | Searches for documents based on the search criteria specified. The search criteria hashref provided for searching must follow the CouchDB L<_find specification|https://docs.couchdb.org/en/stable/api/database/find.html#selector-syntax>. | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Returns a hashref with two fields: C<docs> and C<execution_stats>. C<docs> contains an arrayref of found documents, while | ||||||||||||||||||||||||||||
| 321 | C<execution_stats> contains a hashref of various statistics about the query you ran to find the documents. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head2 find_p | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | $db->find_p($search_criteria); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | See L<"find">, except returns the result asynchronously in a L<Mojo::Promise>. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =head2 get | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | $db->get($id); | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | Finds a document by a given id. Dies if it can't find the document. Returns the document in hashref form. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =head2 get_p | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | $db->get_p($id); | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | See L<"get">, except returns the result asynchronously in a L<Mojo::Promise>. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head2 index | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | $db->index($idx); | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Creates an index, where C<$idx> is a hashref following the CouchDB L<mango specification|https://docs.couchdb.org/en/stable/api/database/find.html#db-index>. Returns the result of the index creation. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head2 index_p | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | $db->index_p($idx); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | See L<"index">, except returns the result asynchronously in a L<Mojo::Promise>. | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =head2 save | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | $db->save($document); | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | Saves a document (hashref) to the database. If the C<_id> field is provided, it will update if it already exists. If you provide both the C<_id> and C<_rev> field, that specific revision will be updated. Returns a hashref that corresponds to the CouchDB C<POST /{db}> specification. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 save_p | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | $couch->save_p($document); | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Does the same as L<"save"> but instead returns the result asynchronously in a L<Mojo::Promise>. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head2 save_many | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | $couch->save_many($documents); | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | Saves an arrayref of documents (hashrefs) to the database. Each document follows the same rules as L<\"save">. Returns an arrayref of the documents you saved with the C<_id> and C<_rev> fields filled. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =head2 save_many_p | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | $couch->save_many_p($documents); | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | See L<"save_many">, except returns the result asynchronously in a L<Mojo::Promise>. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =head1 API | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =over 2 | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =item * L<Mojo::CouchDB> | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =back | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Rawley Fowler, C<rawleyfow@cpan.org>. | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =head1 CREDITS | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =over 2 | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =back | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Copyright (C) 2023, Rawley Fowler and contributors. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =cut | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||