File Coverage

blib/lib/Rethinkdb/Query/Table.pm
Criterion Covered Total %
statement 15 121 12.4
branch 0 20 0.0
condition 0 2 0.0
subroutine 5 26 19.2
pod 21 21 100.0
total 41 190 21.5


line stmt bran cond sub pod time code
1             package Rethinkdb::Query::Table;
2 15     15   57 use Rethinkdb::Base 'Rethinkdb::Query';
  15         23  
  15         80  
3              
4 15     15   69 use Carp qw'croak carp';
  15         21  
  15         728  
5 15     15   60 use Scalar::Util 'weaken';
  15         19  
  15         492  
6              
7 15     15   57 use Rethinkdb::Protocol;
  15         17  
  15         78  
8 15     15   4873 use Rethinkdb::Util;
  15         26  
  15         110  
9              
10             has [qw{ _rdb name }];
11              
12             # primary_key = None
13             # datacenter = None
14             # durability = hard|soft
15             # cache_size = '1024MB'
16             sub create {
17 0     0 1   my $self = shift;
18 0 0         my $optargs = ref $_[0] ? $_[0] : {@_};
19              
20 0           my $q = Rethinkdb::Query->new(
21             _rdb => $self->_rdb,
22             _type => $self->_termType->table_create,
23             args => $self->name,
24             optargs => $optargs,
25             );
26              
27 0           weaken $q->{_rdb};
28 0           return $q;
29             }
30              
31             sub drop {
32 0     0 1   my $self = shift;
33              
34 0           my $q = Rethinkdb::Query->new(
35             _rdb => $self->_rdb,
36             _type => $self->_termType->table_drop,
37             args => $self->name,
38             );
39              
40 0           weaken $q->{_rdb};
41 0           return $q;
42             }
43              
44             sub index_create {
45 0     0 1   my $self = shift;
46 0           my $args = shift;
47 0 0         my $optargs = ref $_[0] ? $_[0] : {@_};
48              
49 0 0         if ( ref $optargs ne 'HASH' ) {
    0          
50 0           $args = [ $args, Rethinkdb::Util->_wrap_func($optargs) ];
51 0           $optargs = undef;
52             }
53             elsif ( $optargs->{'$reql_type$'} ) {
54 0           $args = [ $args, $optargs ];
55 0           $optargs = undef;
56             }
57              
58 0           my $q = Rethinkdb::Query->new(
59             _parent => $self,
60             _type => $self->_termType->index_create,
61             args => $args,
62             optargs => $optargs,
63             );
64              
65 0           return $q;
66             }
67              
68             sub index_drop {
69 0     0 1   my $self = shift;
70 0           my $index = shift;
71              
72 0           my $q = Rethinkdb::Query->new(
73             _parent => $self,
74             _type => $self->_termType->index_drop,
75             args => $index
76             );
77              
78 0           return $q;
79             }
80              
81             sub index_list {
82 0     0 1   my $self = shift;
83              
84 0           my $q = Rethinkdb::Query->new(
85             _parent => $self,
86             _type => $self->_termType->index_list,
87             );
88              
89 0           return $q;
90             }
91              
92             sub index_rename {
93 0     0 1   my $self = shift;
94 0           my $args = [@_];
95              
96 0           my $q = Rethinkdb::Query->new(
97             _parent => $self,
98             _type => $self->_termType->index_rename,
99             args => $args
100             );
101              
102 0           return $q;
103             }
104              
105             sub index_status {
106 0     0 1   my $self = shift;
107 0           my $indices = [@_];
108              
109 0           my $q = Rethinkdb::Query->new(
110             _parent => $self,
111             _type => $self->_termType->index_status,
112             args => $indices,
113             );
114              
115 0           return $q;
116             }
117              
118             sub index_wait {
119 0     0 1   my $self = shift;
120 0           my $indices = [@_];
121              
122 0           my $q = Rethinkdb::Query->new(
123             _parent => $self,
124             _type => $self->_termType->index_wait,
125             args => $indices,
126             );
127              
128 0           return $q;
129             }
130              
131             sub changes {
132 0     0 1   my $self = shift;
133 0           my $params = shift;
134              
135 0           my $q = Rethinkdb::Query->new(
136             _parent => $self,
137             _type => $self->_termType->changes,
138             optargs => $params,
139             );
140              
141 0           return $q;
142             }
143              
144             sub insert {
145 0     0 1   my $self = shift;
146 0           my $args = shift;
147 0           my $params = shift;
148              
149 0           my $q = Rethinkdb::Query->new(
150             _parent => $self,
151             _type => $self->_termType->insert,
152             args => Rethinkdb::Util->_expr_json($args),
153             optargs => $params,
154             );
155              
156 0           return $q;
157             }
158              
159             sub sync {
160 0     0 1   my $self = shift;
161              
162 0           my $q = Rethinkdb::Query->new(
163             _parent => $self,
164             _type => $self->_termType->sync,
165             );
166              
167 0           return $q;
168             }
169              
170             # get a document by primary key
171             # TODO: key can be other things besides string
172             sub get {
173 0     0 1   my $self = shift;
174 0           my ($key) = @_;
175              
176 0           my $q = Rethinkdb::Query->new(
177             _parent => $self,
178             _type => $self->_termType->get,
179             args => $key,
180             );
181              
182 0           return $q;
183             }
184              
185             # Get all documents where the given value matches the value of the requested index
186             sub get_all {
187 0     0 1   my $self = shift;
188              
189             # extract values
190 0           my $values = \@_;
191 0           my $params = {};
192              
193 0 0         if ( ref $values->[0] eq 'ARRAY' ) {
194 0           ( $values, $params ) = @{$values};
  0            
195             }
196              
197 0 0         if ( ref $values->[ $#{$values} ] eq 'HASH' ) {
  0            
198 0           $params = pop @{$values};
  0            
199             }
200              
201 0 0         if ( !$params->{index} ) {
202 0           $params->{index} = 'id';
203             }
204              
205 0           my $q = Rethinkdb::Query->new(
206             _parent => $self,
207             _type => $self->_termType->get_all,
208             args => $values,
209             optargs => $params,
210             );
211              
212 0           return $q;
213             }
214              
215             sub between {
216 0     0 1   my $self = shift;
217 0           my ( $lower, $upper, $index, $left_bound, $right_bound ) = @_;
218              
219 0           my $optargs = {};
220 0 0         if ( ref $index ) {
221 0           $optargs = $index;
222             }
223             else {
224 0   0       $optargs->{index} = $index || 'id';
225              
226 0 0         if ($left_bound) {
227 0           $optargs->{left_bound} = $left_bound;
228             }
229              
230 0 0         if ($right_bound) {
231 0           $optargs->{right_bound} = $right_bound;
232             }
233             }
234              
235 0           my $q = Rethinkdb::Query->new(
236             _parent => $self,
237             _type => $self->_termType->between,
238             args => [ $lower, $upper ],
239             optargs => $optargs,
240             );
241              
242 0           return $q;
243             }
244              
245             sub get_intersecting {
246 0     0 1   my $self = shift;
247 0           my $args = shift;
248 0           my $optargs = shift;
249              
250 0           my $q = Rethinkdb::Query->new(
251             _parent => $self,
252             _type => $self->_termType->get_intersecting,
253             args => $args,
254             optargs => $optargs,
255             );
256              
257 0           return $q;
258             }
259              
260             sub get_nearest {
261 0     0 1   my $self = shift;
262 0           my $args = shift;
263 0           my $optargs = shift;
264              
265 0           my $q = Rethinkdb::Query->new(
266             _parent => $self,
267             _type => $self->_termType->get_nearest,
268             args => $args,
269             optargs => $optargs,
270             );
271              
272 0           return $q;
273             }
274              
275             sub config {
276 0     0 1   my $self = shift;
277              
278 0           my $q = Rethinkdb::Query->new(
279             _parent => $self,
280             _type => $self->_termType->config,
281             );
282              
283 0           return $q;
284             }
285              
286             sub rebalance {
287 0     0 1   my $self = shift;
288              
289 0           my $q = Rethinkdb::Query->new(
290             _parent => $self,
291             _type => $self->_termType->rebalance,
292             );
293              
294 0           return $q;
295             }
296              
297             sub reconfigure {
298 0     0 1   my $self = shift;
299 0           my $args = shift;
300              
301 0           my $q = Rethinkdb::Query->new(
302             _parent => $self,
303             _type => $self->_termType->reconfigure,
304             optargs => $args
305             );
306              
307 0           return $q;
308             }
309              
310             sub status {
311 0     0 1   my $self = shift;
312              
313 0           my $q = Rethinkdb::Query->new(
314             _parent => $self,
315             _type => $self->_termType->status,
316             );
317              
318 0           return $q;
319             }
320              
321             sub wait {
322 0     0 1   my $self = shift;
323              
324 0           my $q = Rethinkdb::Query->new(
325             _parent => $self,
326             _type => $self->_termType->wait,
327             );
328              
329 0           return $q;
330             }
331              
332             1;
333              
334             =encoding utf8
335              
336             =head1 NAME
337              
338             Rethinkdb::Query::Table - RethinkDB Query Table
339              
340             =head1 SYNOPSIS
341              
342             =head1 DESCRIPTION
343              
344             L is a type of query that represents a table in a
345             database. This classes contains methods to interact with said table.
346              
347             =head1 ATTRIBUTES
348              
349             L implements the following attributes.
350              
351             =head2 name
352              
353             my $table = r->db('comics')->table('superheros');
354             say $table->name;
355              
356             The name of the table.
357              
358             =head1 METHODS
359              
360             L implements the following methods.
361              
362             =head2 create
363              
364             r->db('test')->table('dc_universe')->create->run;
365              
366             Create this table. A RethinkDB table is a collection of JSON documents.
367              
368             If successful, the operation returns an object: C<< {created => 1} >>. If a
369             table with the same name already exists, the operation returns a
370             C.
371              
372             B that you can only use alphanumeric characters and underscores for the
373             table name.
374              
375             =head2 drop
376              
377             r->db('test')->table('dc_universe')->drop->run(conn)
378              
379             Drop this table. The table and all its data will be deleted.
380              
381             If successful, the operation returns an object: C<< {dropped => 1} >>. If the
382             specified table doesn't exist a C is returned.
383              
384             =head2 index_create
385              
386             r->table('comments')->index_create('post_id')->run;
387              
388             Create a new secondary index on a table.
389              
390             =head2 index_drop
391              
392             r->table('dc')->index_drop('code_name')->run;
393              
394             Delete a previously created secondary index of this table.
395              
396             =head2 index_list
397              
398             r->table('marvel')->index_list->run;
399              
400             List all the secondary indexes of this table.
401              
402             =head2 index_rename
403              
404             r->table('marvel')->index_rename('heroId', 'awesomeId')->run;
405              
406             Rename an existing secondary index on a table. If the optional argument
407             C is specified as C, a previously existing index with the new
408             name will be deleted and the index will be renamed. If C is C
409             (the default) an error will be raised if the new index name already exists.
410              
411             =head2 index_status
412              
413             r->table('test')->index_status->run;
414             r->table('test')->index_status('timestamp')->run;
415              
416             Get the status of the specified indexes on this table, or the status of all
417             indexes on this table if no indexes are specified.
418              
419             =head2 index_wait
420              
421             r->table('test')->index_wait->run;
422             r->table('test')->index_wait('timestamp')->run;
423              
424             Wait for the specified indexes on this table to be ready, or for all indexes on
425             this table to be ready if no indexes are specified.
426              
427             =head2 changes
428              
429             my $stream = r->table('games')->changes->run(sub {
430             my ($response) = @_;
431             say Dumper $response;
432             });
433              
434             Return an infinite stream of objects representing changes to a table. Whenever
435             an C, C, C or C is performed on the table, an
436             object of the form C<< {'old_val' => ..., 'new_val' => ...} >> will be appended
437             to the stream. For an C, C will be C, and for a
438             C, C will be C.
439              
440             =head2 insert
441              
442             r->table('posts')->insert({
443             id => 1,
444             title => 'Lorem ipsum',
445             content => 'Dolor sit amet'
446             })->run;
447              
448             Insert documents into a table. Accepts a single document or an array of
449             documents.
450              
451             =head2 sync
452              
453             L ensures that writes on a given table are written to permanent storage.
454             Queries that specify soft durability C<< {durability => 'soft'} >> do not give
455             such guarantees, so sync can be used to ensure the state of these queries. A
456             call to sync does not return until all previous writes to the table are
457             persisted.
458              
459             =head2 get
460              
461             r->table('posts')->get('a9849eef-7176-4411-935b-79a6e3c56a74')->run;
462              
463             Get a document by primary key.
464              
465             If no document exists with that primary key, L will return C.
466              
467             =head2 get_all
468              
469             r->table('marvel')->get_all('man_of_steel', { index => 'code_name' })->run;
470              
471             Get all documents where the given value matches the value of the requested
472             index.
473              
474             =head2 between
475              
476             r->table('marvel')->between(10, 20)->run;
477              
478             Get all documents between two keys. Accepts three optional arguments: C,
479             C, and C. If C is set to the name of a
480             secondary index, L will return all documents where that index's value
481             is in the specified range (it uses the primary key by default). C
482             or C may be set to open or closed to indicate whether or not to
483             include that endpoint of the range (by default, C is closed and
484             C is open).
485              
486             =head2 get_intersecting
487              
488             r->table('geo')
489             ->get_intersecting(
490             r->circle( [ -122.423246, 37.770378359 ], 10, { unit => 'mi' } ),
491             { index => 'location' } )->run;
492              
493             Get all documents where the given geometry object intersects the geometry
494             object of the requested geospatial index.
495              
496             =head2 get_nearest
497              
498             r->table('geo')->get_nearest(
499             r->point( -122.422876, 37.777128 ),
500             { index => 'location', max_dist => 5000 }
501             )->run;
502              
503             Get all documents where the specified geospatial index is within a certain
504             distance of the specified point (default 100 kilometers).
505              
506             =head2 config
507              
508             r->table('marvel')->config->run;
509              
510             Query (read and/or update) the configurations for individual tables.
511              
512             =head2 rebalance
513              
514             r->table('marvel')->rebalance->run;
515              
516             Rebalances the shards of a table.
517              
518             =head2 reconfigure
519              
520             r->table('marvel')->reconfigure({ shards => 2, replicas => 1 })->run;
521             r->table('marvel')->reconfigure(
522             {
523             shards => 2,
524             replicas => { wooster => 1, wayne => 1 },
525             primary_replica_tag => 'wooster'
526             }
527             )->run;
528              
529             Reconfigure a table's sharding and replication.
530              
531             =head2 status
532              
533             r->table('marvel')->status->run;
534              
535             Return the status of a table. The return value is an object providing
536             information about the table's shards, replicas and replica readiness states
537              
538             =head2 wait
539              
540             r->table('marvel')->wait->run;
541              
542             Wait for a table to be ready. A table may be temporarily unavailable
543             after creation, rebalancing or reconfiguring. The L command
544             blocks until the given table is fully up to date.
545              
546             =cut