| 1 | package CouchDB::Object::Database; |
|---|
| 2 | |
|---|
| 3 | use Moose; |
|---|
| 4 | use MooseX::Types::URI qw(Uri); |
|---|
| 5 | use Data::Dump::Streamer; |
|---|
| 6 | use HTTP::Headers; |
|---|
| 7 | use JSON::XS (); |
|---|
| 8 | use URI::Escape qw(uri_escape_utf8); |
|---|
| 9 | |
|---|
| 10 | with 'CouchDB::Object::Role::Client'; |
|---|
| 11 | |
|---|
| 12 | has 'name' => ( |
|---|
| 13 | is => 'rw', |
|---|
| 14 | isa => 'Str', |
|---|
| 15 | required => 1, |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | has 'server' => ( |
|---|
| 19 | is => 'rw', |
|---|
| 20 | isa => Uri, |
|---|
| 21 | coerce => 1, |
|---|
| 22 | required => 1, |
|---|
| 23 | ); |
|---|
| 24 | |
|---|
| 25 | no Moose; |
|---|
| 26 | |
|---|
| 27 | our $VERSION = '0.01'; |
|---|
| 28 | |
|---|
| 29 | sub uri { |
|---|
| 30 | my $self = shift; |
|---|
| 31 | |
|---|
| 32 | my $uri = $self->server->clone; |
|---|
| 33 | $uri->path_segments($self->name, ''); |
|---|
| 34 | $uri->canonical; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | sub create { |
|---|
| 38 | my $self = shift; |
|---|
| 39 | return $self->request(PUT => $self->uri); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | sub drop { |
|---|
| 43 | my $self = shift; |
|---|
| 44 | return $self->request(DELETE => $self->uri); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | sub info { |
|---|
| 48 | my $self = shift; |
|---|
| 49 | return $self->request(GET => $self->uri); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | sub compact { |
|---|
| 53 | # TODO: implements |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | sub all_docs { |
|---|
| 57 | my ($self, $args) = @_; |
|---|
| 58 | return $self->request(GET => $self->uri_for('_all_docs', $args)); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | sub open_doc { |
|---|
| 62 | my ($self, $id, $args) = @_; |
|---|
| 63 | return $self->request(GET => $self->uri_for($id, $args)); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | sub save_doc { |
|---|
| 67 | my ($self, $doc, $args) = @_; |
|---|
| 68 | |
|---|
| 69 | my $header = HTTP::Headers->new('Content-Type' => 'application/json'); |
|---|
| 70 | my $res = $doc->has_id |
|---|
| 71 | ? $self->request(PUT => $self->uri_for($doc->id), $header, $doc->to_json) |
|---|
| 72 | : $self->request(POST => $self->uri, $header, $doc->to_json); |
|---|
| 73 | |
|---|
| 74 | # merge |
|---|
| 75 | if ($res->is_success) { |
|---|
| 76 | my $content = $res->content; |
|---|
| 77 | $doc->id($content->id) if $content->id; |
|---|
| 78 | $doc->rev($content->rev) if $content->rev; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return $res; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | sub remove_doc { |
|---|
| 85 | my ($self, $doc, $args) = @_; |
|---|
| 86 | |
|---|
| 87 | return unless $doc->has_id and $doc->has_rev; |
|---|
| 88 | |
|---|
| 89 | my $params = { rev => $doc->rev, %{ $args || {} } }; |
|---|
| 90 | return $self->request(DELETE => $self->uri_for($doc->id, $params)); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | sub bulk_docs { |
|---|
| 94 | # TODO: implements |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub query { |
|---|
| 98 | my ($self, $map, $reduce, $lang, $args) = @_; |
|---|
| 99 | |
|---|
| 100 | my $body = { |
|---|
| 101 | language => $lang || (ref $map eq 'CODE') ? 'text/perl' : 'javascript', |
|---|
| 102 | map => _code2str($map), |
|---|
| 103 | }; |
|---|
| 104 | $body->{reduce} = _code2str($reduce) if defined $reduce; |
|---|
| 105 | $body = JSON::XS->new->encode($body); |
|---|
| 106 | |
|---|
| 107 | my $header = HTTP::Headers->new('Content-Type' => 'application/json'); |
|---|
| 108 | return $self->request(POST => $self->uri_for('_temp_view', $args), $header, $body); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | sub view { |
|---|
| 112 | # TODO: implements |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | # from CouchDB::View::Document |
|---|
| 116 | sub _code2str { |
|---|
| 117 | ref $_[0] |
|---|
| 118 | ? sprintf 'do { my $CODE1; %s; $CODE1 }', Data::Dump::Streamer->new->Data($_[0])->Out |
|---|
| 119 | : $_[0]; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | __PACKAGE__->meta->make_immutable; |
|---|
| 123 | |
|---|
| 124 | 1; |
|---|
| 125 | |
|---|
| 126 | =head1 NAME |
|---|
| 127 | |
|---|
| 128 | CouchDB::Object::Database - Interface to CouchDB database |
|---|
| 129 | |
|---|
| 130 | =head1 SYNOPSIS |
|---|
| 131 | |
|---|
| 132 | =head1 METHODS |
|---|
| 133 | |
|---|
| 134 | =head2 new(name => $dbname, server => $uri) |
|---|
| 135 | |
|---|
| 136 | Returns the L<CouchDB::Object::Database> object |
|---|
| 137 | |
|---|
| 138 | =head2 name |
|---|
| 139 | |
|---|
| 140 | Returns the database name |
|---|
| 141 | |
|---|
| 142 | =head2 uri |
|---|
| 143 | |
|---|
| 144 | Returns the database uri |
|---|
| 145 | |
|---|
| 146 | =head2 create |
|---|
| 147 | |
|---|
| 148 | =head2 drop |
|---|
| 149 | |
|---|
| 150 | =head2 info |
|---|
| 151 | |
|---|
| 152 | =head2 compact |
|---|
| 153 | |
|---|
| 154 | =head1 AUTHOR |
|---|
| 155 | |
|---|
| 156 | NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> |
|---|
| 157 | |
|---|
| 158 | =head1 LICENSE |
|---|
| 159 | |
|---|
| 160 | This library is free software; you can redistribute it and/or modify |
|---|
| 161 | it under the same terms as Perl itself. |
|---|
| 162 | |
|---|
| 163 | =head1 SEE ALSO |
|---|
| 164 | |
|---|
| 165 | L<CouchDB::Object::Document>, L<CouchDB::Object::Response> |
|---|
| 166 | |
|---|
| 167 | =cut |
|---|