root/lang/perl/CouchDB-Object/trunk/lib/CouchDB/Object/Database.pm @ 18660

Revision 18660, 3.1 kB (checked in by masaki, 5 years ago)

Database#query 追加,テストも追加,Utils やめて Role にした

Line 
1package CouchDB::Object::Database;
2
3use Moose;
4use MooseX::Types::URI qw(Uri);
5use Data::Dump::Streamer;
6use HTTP::Headers;
7use JSON::XS ();
8use URI::Escape qw(uri_escape_utf8);
9
10with 'CouchDB::Object::Role::Client';
11
12has 'name' => (
13    is       => 'rw',
14    isa      => 'Str',
15    required => 1,
16);
17
18has 'server' => (
19    is       => 'rw',
20    isa      => Uri,
21    coerce   => 1,
22    required => 1,
23);
24
25no Moose;
26
27our $VERSION = '0.01';
28
29sub uri {
30    my $self = shift;
31
32    my $uri = $self->server->clone;
33    $uri->path_segments($self->name, '');
34    $uri->canonical;
35}
36
37sub create {
38    my $self = shift;
39    return $self->request(PUT => $self->uri);
40}
41
42sub drop {
43    my $self = shift;
44    return $self->request(DELETE => $self->uri);
45}
46
47sub info {
48    my $self = shift;
49    return $self->request(GET => $self->uri);
50}
51
52sub compact {
53    # TODO: implements
54}
55
56sub all_docs {
57    my ($self, $args) = @_;
58    return $self->request(GET => $self->uri_for('_all_docs', $args));
59}
60
61sub open_doc {
62    my ($self, $id, $args) = @_;
63    return $self->request(GET => $self->uri_for($id, $args));
64}
65
66sub 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
84sub 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
93sub bulk_docs {
94    # TODO: implements
95}
96
97sub 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
111sub view {
112    # TODO: implements
113}
114
115# from CouchDB::View::Document
116sub _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
1241;
125
126=head1 NAME
127
128CouchDB::Object::Database - Interface to CouchDB database
129
130=head1 SYNOPSIS
131
132=head1 METHODS
133
134=head2 new(name => $dbname, server => $uri)
135
136Returns the L<CouchDB::Object::Database> object
137
138=head2 name
139
140Returns the database name
141
142=head2 uri
143
144Returns the database uri
145
146=head2 create
147
148=head2 drop
149
150=head2 info
151
152=head2 compact
153
154=head1 AUTHOR
155
156NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt>
157
158=head1 LICENSE
159
160This library is free software; you can redistribute it and/or modify
161it under the same terms as Perl itself.
162
163=head1 SEE ALSO
164
165L<CouchDB::Object::Document>, L<CouchDB::Object::Response>
166
167=cut
Note: See TracBrowser for help on using the browser.