line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::CouchDB; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
1343599
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
23
|
|
|
2
|
|
|
|
|
15
|
|
4
|
2
|
|
|
2
|
|
1347
|
use Mojo::CouchDB::DB; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
17
|
|
5
|
2
|
|
|
2
|
|
131
|
use Carp qw(croak); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
113
|
|
6
|
2
|
|
|
2
|
|
12
|
use MIME::Base64 qw(encode_base64); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
775
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.0'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has 'url'; |
11
|
|
|
|
|
|
|
has 'auth'; |
12
|
|
|
|
|
|
|
has dbs => sub { state $dbs = {} }; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub db { |
15
|
2
|
|
|
2
|
1
|
5617
|
my $self = shift; |
16
|
2
|
|
|
|
|
6
|
my $db_name = shift; |
17
|
|
|
|
|
|
|
|
18
|
2
|
50
|
|
|
|
11
|
return $self->dbs->{$db_name} if exists $self->dbs->{$db_name}; |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
12
|
my $db = Mojo::CouchDB::DB->new($self->url->path($db_name), $self->auth); |
21
|
2
|
|
|
|
|
19
|
$self->dbs->{$db_name} = $db; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
15
|
return $db; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub new { |
27
|
2
|
|
|
2
|
1
|
3141
|
my $self = shift->SUPER::new; |
28
|
2
|
|
|
|
|
15
|
my $str = shift; |
29
|
2
|
|
|
|
|
5
|
my $username = shift; |
30
|
2
|
|
|
|
|
6
|
my $password = shift; |
31
|
|
|
|
|
|
|
|
32
|
2
|
50
|
|
|
|
8
|
return $self unless $str; |
33
|
|
|
|
|
|
|
|
34
|
2
|
50
|
|
|
|
12
|
chop $str if substr($str, -1) eq '/'; |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
19
|
my $url = Mojo::URL->new($str); |
37
|
2
|
50
|
|
|
|
685
|
croak "Invalid CouchDB URI string $str" unless $url->protocol =~ /^http(?:s)?$/; |
38
|
|
|
|
|
|
|
|
39
|
2
|
50
|
33
|
|
|
64
|
chomp($self->{auth} = 'Basic ' . encode_base64("$username:$password")) |
40
|
|
|
|
|
|
|
if $username and $password; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
7
|
$self->{url} = $url; |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
10
|
return $self; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
1; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=encoding utf8 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 NAME |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Mojo::CouchDB |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 SYNOPSIS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
use Mojo::CouchDB; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Create a CouchDB instance |
60
|
|
|
|
|
|
|
my $couch = Mojo::CouchDB->new('http://localhost:6984', 'username', 'password'); |
61
|
|
|
|
|
|
|
my $db = $couch->db('books'); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$db->create_db; # Create the database on the server |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Make a document |
66
|
|
|
|
|
|
|
my $book = { |
67
|
|
|
|
|
|
|
title => 'Nineteen Eighty Four', |
68
|
|
|
|
|
|
|
author => 'George Orwell' |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Save your document to the database |
72
|
|
|
|
|
|
|
$book = $db->save($book); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# If _id is assigned to a hashref, save will update rather than create |
75
|
|
|
|
|
|
|
say $book->{_id}; # Assigned when saving or getting |
76
|
|
|
|
|
|
|
$book->{title} = 'Dune'; |
77
|
|
|
|
|
|
|
$book->{author} = 'Frank Herbert' |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Re-save to update the document |
80
|
|
|
|
|
|
|
$book = $db->save($book); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Get the document as a hashref |
83
|
|
|
|
|
|
|
my $dune = $db->get($book->{_id}); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# You can also save many documents at a time |
86
|
|
|
|
|
|
|
my $books = $db->save_many([{title => 'book', author => 'John'}, { title => 'foo', author => 'bar' }])->{docs}; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 db |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $db = $couch->db('books'); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Create an instance of L<"Mojo::CouchDB::DB"> that corresponds to the database name specified in the first parameter. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 new |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $url = 'https://127.0.0.1:5984'; |
97
|
|
|
|
|
|
|
my $couch = Mojo::CouchDB->new($url, $username, $password); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Creates an instance of L<"Mojo::CouchDB">. The URL specified must include the protocol either C or C as well as the port your CouchDB instance is using. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 API |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=over 2 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item * L |
106
|
|
|
|
|
|
|
=item * L |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=back |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 AUTHOR |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Rawley Fowler, C. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 CREDITS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=over 2 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=back |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 LICENSE |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Copyright (C) 2023, Rawley Fowler and contributors. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SEE ALSO |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |