| 1 | package Data::CouchDB::Connection; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.14'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Data::CouchDB - CouchDB document management | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Data::CouchDB::Connection | ||
| 18 | |||
| 19 | =head1 SYNOPSYS | ||
| 20 | |||
| 21 | my $couch_connection = Data::CouchDB::Connection->new( | ||
| 22 | host => 'localhost', | ||
| 23 | port => 5432, | ||
| 24 | protocol => 'http://', | ||
| 25 | couch => 'testdb', | ||
| 26 | ); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This class represents a couchdb connection. | ||
| 31 | |||
| 32 | =head1 ATTRIBUTES | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | use Moose; | ||
| 37 | use MooseX::StrictConstructor; | ||
| 38 | use namespace::autoclean; | ||
| 39 | |||
| 40 | use CouchDB::Client; | ||
| 41 | use Net::SSL; | ||
| 42 | use LWP::UserAgent; | ||
| 43 | use HTTP::Request; | ||
| 44 | use HTTP::Headers; | ||
| 45 | use Try::Tiny; | ||
| 46 | use IO::Socket::SSL qw( SSL_VERIFY_NONE ); | ||
| 47 | use Data::CouchDB::Exceptions; | ||
| 48 | |||
| 49 | =head2 host | ||
| 50 | |||
| 51 | name of the host to connect to. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | has 'host' => ( | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'Str', | ||
| 58 | required => 1, | ||
| 59 | ); | ||
| 60 | |||
| 61 | =head2 port | ||
| 62 | |||
| 63 | name of the port to connect to. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | has 'port' => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => 'Str', | ||
| 70 | required => 1, | ||
| 71 | ); | ||
| 72 | |||
| 73 | =head2 db | ||
| 74 | |||
| 75 | db with which to operate. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | has db => ( | ||
| 80 | is => 'ro', | ||
| 81 | isa => 'Str', | ||
| 82 | default => 'db', | ||
| 83 | ); | ||
| 84 | |||
| 85 | =head2 protocol | ||
| 86 | |||
| 87 | protcol with which to connect. | ||
| 88 | |||
| 89 | Can be, | ||
| 90 | 'http://' or 'https://' | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | has 'protocol' => ( | ||
| 95 | is => 'ro', | ||
| 96 | isa => 'Str', | ||
| 97 | lazy_build => 1, | ||
| 98 | ); | ||
| 99 | |||
| 100 | =head2 couchdb | ||
| 101 | |||
| 102 | password for "couchdb" user | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | has 'couchdb' => ( | ||
| 107 | is => 'ro', | ||
| 108 | isa => 'Str', | ||
| 109 | default => 'TESTPASS' | ||
| 110 | ); | ||
| 111 | |||
| 112 | =head2 design_doc | ||
| 113 | |||
| 114 | Location inside the db where the design document is present. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | has 'design_doc' => ( | ||
| 119 | is => 'ro', | ||
| 120 | default => '_design/docs' | ||
| 121 | ); | ||
| 122 | |||
| 123 | =head2 uri | ||
| 124 | |||
| 125 | The uri with which we connect to. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | has 'uri' => ( | ||
| 130 | is => 'ro', | ||
| 131 | isa => 'Str', | ||
| 132 | lazy_build => 1, | ||
| 133 | ); | ||
| 134 | |||
| 135 | =head2 log_uri | ||
| 136 | |||
| 137 | The uri we use for throwing exceptions. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | has 'log_uri' => ( | ||
| 142 | is => 'ro', | ||
| 143 | isa => 'Str', | ||
| 144 | lazy_build => 1, | ||
| 145 | ); | ||
| 146 | |||
| 147 | =head2 ua | ||
| 148 | |||
| 149 | Optionally passed ua(user_agent). If not passed the couchdb's default ua is used. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | has 'ua' => ( | ||
| 154 | is => 'ro', | ||
| 155 | lazy_build => 1, | ||
| 156 | ); | ||
| 157 | |||
| 158 | =head1 METHODS | ||
| 159 | |||
| 160 | =head2 document | ||
| 161 | |||
| 162 | Get or set a couch document. | ||
| 163 | |||
| 164 | Usage, | ||
| 165 | To get a document | ||
| 166 | $couchdb->document($db, $doc_id); | ||
| 167 | |||
| 168 | To set a document | ||
| 169 | $couchdb->document($db, $doc_id, $data); | ||
| 170 | |||
| 171 | $data is a HashRef | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub document { | ||
| 176 | my $self = shift; | ||
| 177 | my $doc = shift; | ||
| 178 | my $data = shift; | ||
| 179 | my $ex_params = shift || {}; | ||
| 180 | |||
| 181 | return unless ($doc); | ||
| 182 | my $couch_doc = $self->_doc($doc); | ||
| 183 | try { $couch_doc->retrieve; } | ||
| 184 | catch { | ||
| 185 | Data::CouchDB::RetrieveFailed->throw( | ||
| 186 | db => $self->db, | ||
| 187 | uri => $self->log_uri, | ||
| 188 | document => $doc, | ||
| 189 | error => $_, | ||
| 190 | ); | ||
| 191 | }; | ||
| 192 | |||
| 193 | if ($data) { | ||
| 194 | if (exists $data->{_rev}) { | ||
| 195 | if ($data->{_rev} ne $couch_doc->rev) { | ||
| 196 | Data::CouchDB::RevisionNotMatched->throw( | ||
| 197 | uri => $self->log_uri, | ||
| 198 | db => $self->db, | ||
| 199 | document => $doc, | ||
| 200 | error => "CouchDB Revision mismatch " . $data->{_rev} . '!=' . $couch_doc->rev | ||
| 201 | ); | ||
| 202 | } | ||
| 203 | delete $data->{_rev}; | ||
| 204 | } | ||
| 205 | |||
| 206 | delete $data->{_id} if (exists $data->{_id}); | ||
| 207 | |||
| 208 | try { | ||
| 209 | $couch_doc->data($data); | ||
| 210 | $couch_doc->update; | ||
| 211 | } | ||
| 212 | catch { | ||
| 213 | Data::CouchDB::UpdateFailed->throw( | ||
| 214 | uri => $self->log_uri, | ||
| 215 | db => $self->db, | ||
| 216 | document => $doc, | ||
| 217 | error => $_, | ||
| 218 | ); | ||
| 219 | }; | ||
| 220 | |||
| 221 | } | ||
| 222 | |||
| 223 | $data = $couch_doc->data; | ||
| 224 | $data->{_rev} = $couch_doc->rev; | ||
| 225 | $data->{_id} = $couch_doc->id; | ||
| 226 | |||
| 227 | return $data; | ||
| 228 | } | ||
| 229 | |||
| 230 | =head2 view | ||
| 231 | |||
| 232 | Query a couchdb view | ||
| 233 | |||
| 234 | Usage, | ||
| 235 | Without Parameters | ||
| 236 | $couchdb->view($db, $viewname); | ||
| 237 | |||
| 238 | With Parameters | ||
| 239 | $couchdb->view($db, $viewname, $parameters); | ||
| 240 | |||
| 241 | $parameters is a HashRef | ||
| 242 | |||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub view { | ||
| 247 | my $self = shift; | ||
| 248 | my $view = shift; | ||
| 249 | my $params = shift; | ||
| 250 | my $ex_params = shift || {}; | ||
| 251 | |||
| 252 | my $design_doc = $ex_params->{design_doc} || $self->design_doc; | ||
| 253 | |||
| 254 | return unless ($view); | ||
| 255 | |||
| 256 | my $couch_design_doc = $self->_retrieved_design_doc; | ||
| 257 | my $result; | ||
| 258 | try { $result = $couch_design_doc->queryView($view, %$params); } | ||
| 259 | catch { | ||
| 260 | Data::CouchDB::QueryFailed->throw( | ||
| 261 | db => $self->db, | ||
| 262 | uri => $self->log_uri, | ||
| 263 | view => $view, | ||
| 264 | error => $_, | ||
| 265 | ); | ||
| 266 | }; | ||
| 267 | |||
| 268 | my $documents = []; | ||
| 269 | foreach my $row (@{$result->{rows}}) { | ||
| 270 | if ($row->{id}) { | ||
| 271 | push @{$documents}, $row->{id}; | ||
| 272 | } else { | ||
| 273 | push @{$documents}, $row; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | return $documents; | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 can_connect | ||
| 281 | |||
| 282 | Confirms that you can connect to this couchdb | ||
| 283 | |||
| 284 | Usage, | ||
| 285 | if($couchdb->can_connect) { | ||
| 286 | ... | ||
| 287 | } | ||
| 288 | |||
| 289 | Returns, | ||
| 290 | 1 - can read | ||
| 291 | undef - otherwise | ||
| 292 | =cut | ||
| 293 | |||
| 294 | sub can_connect { | ||
| 295 | my $self = shift; | ||
| 296 | |||
| 297 | my $client = CouchDB::Client->new(uri => $self->uri); | ||
| 298 | return $client->testConnection; | ||
| 299 | } | ||
| 300 | |||
| 301 | =head2 create_or_update_view | ||
| 302 | |||
| 303 | Creates a CouchDB view. | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | sub create_or_update_view { | ||
| 308 | my $self = shift; | ||
| 309 | my $new_view = shift; | ||
| 310 | |||
| 311 | my $doc = $self->_design_doc; | ||
| 312 | my $design_doc; | ||
| 313 | try { $doc->retrieve; $design_doc = $doc->data; }; | ||
| 314 | |||
| 315 | # possibly new view | ||
| 316 | unless ($design_doc) { | ||
| 317 | $doc->create; | ||
| 318 | $design_doc = {}; | ||
| 319 | $design_doc->{views} = {}; | ||
| 320 | } | ||
| 321 | |||
| 322 | $design_doc->{views} = {%{$design_doc->{views}}, %$new_view}; | ||
| 323 | try { | ||
| 324 | $doc->data($design_doc); | ||
| 325 | $doc->update; | ||
| 326 | } | ||
| 327 | catch { | ||
| 328 | Data::CouchDB::UpdateFailed->throw( | ||
| 329 | uri => $self->log_uri, | ||
| 330 | db => $self->db, | ||
| 331 | document => $self->design_doc, | ||
| 332 | error => $_, | ||
| 333 | ); | ||
| 334 | }; | ||
| 335 | |||
| 336 | # Recreate design doc since we updated it. | ||
| 337 | $self->_design_doc($self->_build__design_doc); | ||
| 338 | $self->_retrieved_design_doc($self->_build__retrieved_design_doc); | ||
| 339 | |||
| 340 | return 1; | ||
| 341 | } | ||
| 342 | |||
| 343 | =head2 create_document | ||
| 344 | |||
| 345 | Creates a couch document | ||
| 346 | |||
| 347 | Usage, | ||
| 348 | With known doc_id | ||
| 349 | my $doc_id = $couchdb->create_document($db, $doc_id); | ||
| 350 | |||
| 351 | Without known doc_id or with rev | ||
| 352 | my $doc_id = $couchdb->create_document($db); | ||
| 353 | |||
| 354 | =cut | ||
| 355 | |||
| 356 | sub create_document { | ||
| 357 | my $self = shift; | ||
| 358 | my $doc = shift; | ||
| 359 | my $ex_params = shift || {}; | ||
| 360 | |||
| 361 | my $couch_doc = $self->_doc($doc); | ||
| 362 | |||
| 363 | try { $couch_doc->create; } | ||
| 364 | catch { | ||
| 365 | Data::CouchDB::Exception->throw( | ||
| 366 | uri => $self->log_uri, | ||
| 367 | error => $_, | ||
| 368 | message => "Creating " . (($doc) ? $doc : "new") . " in " . $self->db . " failed: $_", | ||
| 369 | ); | ||
| 370 | }; | ||
| 371 | |||
| 372 | return $couch_doc->id; | ||
| 373 | } | ||
| 374 | |||
| 375 | =head2 delete_document | ||
| 376 | |||
| 377 | Deletes a couch document | ||
| 378 | |||
| 379 | Usage, | ||
| 380 | $couchdb->delete_document($db, $doc_id); | ||
| 381 | |||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | sub delete_document { | ||
| 386 | my $self = shift; | ||
| 387 | my $doc = shift; | ||
| 388 | my $ex_params = shift || {}; | ||
| 389 | |||
| 390 | my $couch_doc = $self->_doc($doc); | ||
| 391 | |||
| 392 | try { | ||
| 393 | $couch_doc->retrieve; | ||
| 394 | $couch_doc->delete; | ||
| 395 | } | ||
| 396 | catch { | ||
| 397 | Data::CouchDB::Exception->throw( | ||
| 398 | uri => $self->log_uri, | ||
| 399 | error => $_, | ||
| 400 | message => "Deleting $doc from " . $self->db . " failed", | ||
| 401 | ); | ||
| 402 | }; | ||
| 403 | |||
| 404 | return 1; | ||
| 405 | } | ||
| 406 | |||
| 407 | =head2 create_database | ||
| 408 | |||
| 409 | Creates a CouchDB Database. | ||
| 410 | |||
| 411 | Usage, | ||
| 412 | $couchdb->create_database($db); | ||
| 413 | |||
| 414 | |||
| 415 | =cut | ||
| 416 | |||
| 417 | sub create_database { | ||
| 418 | my $self = shift; | ||
| 419 | return $self->_db->create; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub database_exists { | ||
| 423 | my $self = shift; | ||
| 424 | return $self->_couchdb_client->dbExists($self->db); | ||
| 425 | } | ||
| 426 | |||
| 427 | sub _doc { | ||
| 428 | my $self = shift; | ||
| 429 | my $doc = shift; | ||
| 430 | |||
| 431 | return $self->_db->newDoc($doc); | ||
| 432 | } | ||
| 433 | |||
| 434 | has _couchdb_client => ( | ||
| 435 | is => 'ro', | ||
| 436 | isa => 'CouchDB::Client', | ||
| 437 | lazy_build => 1, | ||
| 438 | ); | ||
| 439 | |||
| 440 | has _db => ( | ||
| 441 | is => 'ro', | ||
| 442 | isa => 'CouchDB::Client::DB', | ||
| 443 | lazy_build => 1, | ||
| 444 | ); | ||
| 445 | |||
| 446 | has '_design_doc' => ( | ||
| 447 | is => 'rw', | ||
| 448 | lazy_build => 1, | ||
| 449 | ); | ||
| 450 | |||
| 451 | has '_retrieved_design_doc' => ( | ||
| 452 | is => 'rw', | ||
| 453 | lazy_build => 1, | ||
| 454 | ); | ||
| 455 | |||
| 456 | sub _build__couchdb_client { | ||
| 457 | my $self = shift; | ||
| 458 | |||
| 459 | my $params = {}; | ||
| 460 | $params->{uri} = $self->uri; | ||
| 461 | $params->{ua} = $self->ua if ($self->ua); | ||
| 462 | |||
| 463 | my $client = CouchDB::Client->new($params); | ||
| 464 | unless ($client->testConnection) { | ||
| 465 | Data::CouchDB::ConnectionFailed->throw( | ||
| 466 | uri => $self->log_uri, | ||
| 467 | error => 'Connection to ' . $self->log_uri . ' failed', | ||
| 468 | ); | ||
| 469 | } | ||
| 470 | |||
| 471 | return $client; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub _build__db { | ||
| 475 | my $self = shift; | ||
| 476 | return $self->_couchdb_client->newDB($self->db); | ||
| 477 | } | ||
| 478 | |||
| 479 | sub _build__design_doc { | ||
| 480 | my $self = shift; | ||
| 481 | |||
| 482 | return $self->_db->newDesignDoc($self->design_doc); | ||
| 483 | } | ||
| 484 | |||
| 485 | sub _build__retrieved_design_doc { | ||
| 486 | my $self = shift; | ||
| 487 | |||
| 488 | my $design_doc = $self->_design_doc; | ||
| 489 | try { $design_doc->retrieve; } | ||
| 490 | catch { | ||
| 491 | Data::CouchDB::RetrieveFailed->throw( | ||
| 492 | db => $self->db, | ||
| 493 | uri => $self->log_uri, | ||
| 494 | document => $self->design_doc, | ||
| 495 | error => $_, | ||
| 496 | ); | ||
| 497 | }; | ||
| 498 | |||
| 499 | return $self->_design_doc; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub _build_uri { | ||
| 503 | my $self = shift; | ||
| 504 | my $protocol = $self->protocol; | ||
| 505 | my $url = $protocol; | ||
| 506 | |||
| 507 | # If couchdb is not set then it has no password | ||
| 508 | if ($self->port ne 5984 and $self->couchdb) { | ||
| 509 | $url .= 'couchdb:' . $self->couchdb . '@'; | ||
| 510 | } | ||
| 511 | |||
| 512 | $url .= $self->host . ':' . $self->port . '/'; | ||
| 513 | return $url; | ||
| 514 | } | ||
| 515 | |||
| 516 | sub _build_log_uri { | ||
| 517 | my $self = shift; | ||
| 518 | my $uri = $self->protocol; | ||
| 519 | |||
| 520 | return $self->protocol . $self->host . ':' . $self->port . '/'; | ||
| 521 | } | ||
| 522 | |||
| 523 | sub _build_protocol { | ||
| 524 | my $self = shift; | ||
| 525 | my $port = $self->port; | ||
| 526 | my $protocol; | ||
| 527 | |||
| 528 | if ($port eq 6984) { | ||
| 529 | $protocol = 'https://'; | ||
| 530 | } elsif ($port eq 5984) { | ||
| 531 | $protocol = 'http://'; | ||
| 532 | } | ||
| 533 | |||
| 534 | return $protocol; | ||
| 535 | } | ||
| 536 | |||
| 537 | sub _build_ua { | ||
| 538 | my $self = shift; | ||
| 539 | |||
| 540 | my $ua = LWP::UserAgent->new(); | ||
| 541 | $ua->ssl_opts( | ||
| 542 | verify_hostname => 0, | ||
| 543 | SSL_verify_mode => SSL_VERIFY_NONE | ||
| 544 | ); | ||
| 545 | |||
| 546 | return $ua; | ||
| 547 | } | ||
| 548 | |||
| 549 | __PACKAGE__->meta->make_immutable; | ||
| 550 | |||
| 551 | 1; | ||
| 552 |