File Coverage

blib/lib/Rethinkdb/Query/Table.pm
Criterion Covered Total %
statement 15 126 11.9
branch 0 20 0.0
condition 0 2 0.0
subroutine 5 27 18.5
pod 22 22 100.0
total 42 197 21.3


line stmt bran cond sub pod time code
1             package Rethinkdb::Query::Table;
2 15     15   55 use Rethinkdb::Base 'Rethinkdb::Query';
  15         19  
  15         72  
3              
4 15     15   65 use Carp qw'croak carp';
  15         20  
  15         624  
5 15     15   57 use Scalar::Util 'weaken';
  15         16  
  15         466  
6              
7 15     15   56 use Rethinkdb::Protocol;
  15         13  
  15         66  
8 15     15   4790 use Rethinkdb::Util;
  15         26  
  15         97  
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 grant {
276 0     0 1   my $self = shift;
277 0           my $user = shift;
278 0           my $perms = shift;
279              
280 0           my $q = Rethinkdb::Query->new(
281             _rdb => $self->_rdb,
282             _type => $self->_termType->grant,
283             args => [ $user, $perms ]
284             );
285              
286 0           return $q;
287             }
288              
289             sub config {
290 0     0 1   my $self = shift;
291              
292 0           my $q = Rethinkdb::Query->new(
293             _parent => $self,
294             _type => $self->_termType->config,
295             );
296              
297 0           return $q;
298             }
299              
300             sub rebalance {
301 0     0 1   my $self = shift;
302              
303 0           my $q = Rethinkdb::Query->new(
304             _parent => $self,
305             _type => $self->_termType->rebalance,
306             );
307              
308 0           return $q;
309             }
310              
311             sub reconfigure {
312 0     0 1   my $self = shift;
313 0           my $args = shift;
314              
315 0           my $q = Rethinkdb::Query->new(
316             _parent => $self,
317             _type => $self->_termType->reconfigure,
318             optargs => $args
319             );
320              
321 0           return $q;
322             }
323              
324             sub status {
325 0     0 1   my $self = shift;
326              
327 0           my $q = Rethinkdb::Query->new(
328             _parent => $self,
329             _type => $self->_termType->status,
330             );
331              
332 0           return $q;
333             }
334              
335             sub wait {
336 0     0 1   my $self = shift;
337              
338 0           my $q = Rethinkdb::Query->new(
339             _parent => $self,
340             _type => $self->_termType->wait,
341             );
342              
343 0           return $q;
344             }
345              
346             1;
347              
348             =encoding utf8
349              
350             =head1 NAME
351              
352             Rethinkdb::Query::Table - RethinkDB Query Table
353              
354             =head1 SYNOPSIS
355              
356             =head1 DESCRIPTION
357              
358             L is a type of query that represents a table in a
359             database. This classes contains methods to interact with said table.
360              
361             =head1 ATTRIBUTES
362              
363             L implements the following attributes.
364              
365             =head2 name
366              
367             my $table = r->db('comics')->table('superheros');
368             say $table->name;
369              
370             The name of the table.
371              
372             =head1 METHODS
373              
374             L implements the following methods.
375              
376             =head2 create
377              
378             r->db('test')->table('dc_universe')->create->run;
379              
380             Create this table. A RethinkDB table is a collection of JSON documents.
381              
382             If successful, the operation returns an object: C<< {created => 1} >>. If a
383             table with the same name already exists, the operation returns a
384             C.
385              
386             B that you can only use alphanumeric characters and underscores for the
387             table name.
388              
389             =head2 drop
390              
391             r->db('test')->table('dc_universe')->drop->run(conn)
392              
393             Drop this table. The table and all its data will be deleted.
394              
395             If successful, the operation returns an object: C<< {dropped => 1} >>. If the
396             specified table doesn't exist a C is returned.
397              
398             =head2 index_create
399              
400             r->table('comments')->index_create('post_id')->run;
401              
402             Create a new secondary index on a table.
403              
404             =head2 index_drop
405              
406             r->table('dc')->index_drop('code_name')->run;
407              
408             Delete a previously created secondary index of this table.
409              
410             =head2 index_list
411              
412             r->table('marvel')->index_list->run;
413              
414             List all the secondary indexes of this table.
415              
416             =head2 index_rename
417              
418             r->table('marvel')->index_rename('heroId', 'awesomeId')->run;
419              
420             Rename an existing secondary index on a table. If the optional argument
421             C is specified as C, a previously existing index with the new
422             name will be deleted and the index will be renamed. If C is C
423             (the default) an error will be raised if the new index name already exists.
424              
425             =head2 index_status
426              
427             r->table('test')->index_status->run;
428             r->table('test')->index_status('timestamp')->run;
429              
430             Get the status of the specified indexes on this table, or the status of all
431             indexes on this table if no indexes are specified.
432              
433             =head2 index_wait
434              
435             r->table('test')->index_wait->run;
436             r->table('test')->index_wait('timestamp')->run;
437              
438             Wait for the specified indexes on this table to be ready, or for all indexes on
439             this table to be ready if no indexes are specified.
440              
441             =head2 changes
442              
443             my $stream = r->table('games')->changes->run(sub {
444             my ($response) = @_;
445             say Dumper $response;
446             });
447              
448             Return an infinite stream of objects representing changes to a table. Whenever
449             an C, C, C or C is performed on the table, an
450             object of the form C<< {'old_val' => ..., 'new_val' => ...} >> will be appended
451             to the stream. For an C, C will be C, and for a
452             C, C will be C.
453              
454             =head2 insert
455              
456             r->table('posts')->insert({
457             id => 1,
458             title => 'Lorem ipsum',
459             content => 'Dolor sit amet'
460             })->run;
461              
462             Insert documents into a table. Accepts a single document or an array of
463             documents.
464              
465             =head2 sync
466              
467             L ensures that writes on a given table are written to permanent storage.
468             Queries that specify soft durability C<< {durability => 'soft'} >> do not give
469             such guarantees, so sync can be used to ensure the state of these queries. A
470             call to sync does not return until all previous writes to the table are
471             persisted.
472              
473             =head2 get
474              
475             r->table('posts')->get('a9849eef-7176-4411-935b-79a6e3c56a74')->run;
476              
477             Get a document by primary key.
478              
479             If no document exists with that primary key, L will return C.
480              
481             =head2 get_all
482              
483             r->table('marvel')->get_all('man_of_steel', { index => 'code_name' })->run;
484              
485             Get all documents where the given value matches the value of the requested
486             index.
487              
488             =head2 between
489              
490             r->table('marvel')->between(10, 20)->run;
491              
492             Get all documents between two keys. Accepts three optional arguments: C,
493             C, and C. If C is set to the name of a
494             secondary index, L will return all documents where that index's value
495             is in the specified range (it uses the primary key by default). C
496             or C may be set to open or closed to indicate whether or not to
497             include that endpoint of the range (by default, C is closed and
498             C is open).
499              
500             =head2 get_intersecting
501              
502             r->table('geo')
503             ->get_intersecting(
504             r->circle( [ -122.423246, 37.770378359 ], 10, { unit => 'mi' } ),
505             { index => 'location' } )->run;
506              
507             Get all documents where the given geometry object intersects the geometry
508             object of the requested geospatial index.
509              
510             =head2 get_nearest
511              
512             r->table('geo')->get_nearest(
513             r->point( -122.422876, 37.777128 ),
514             { index => 'location', max_dist => 5000 }
515             )->run;
516              
517             Get all documents where the specified geospatial index is within a certain
518             distance of the specified point (default 100 kilometers).
519              
520             =head2 grant
521              
522             r->table('marvel')->grant( 'username', { read => r->true, write => r->false } )
523             ->run;
524              
525             Grant or deny access permissions for a user account on a table.
526              
527             =head2 config
528              
529             r->table('marvel')->config->run;
530              
531             Query (read and/or update) the configurations for individual tables.
532              
533             =head2 rebalance
534              
535             r->table('marvel')->rebalance->run;
536              
537             Rebalances the shards of a table.
538              
539             =head2 reconfigure
540              
541             r->table('marvel')->reconfigure({ shards => 2, replicas => 1 })->run;
542             r->table('marvel')->reconfigure(
543             {
544             shards => 2,
545             replicas => { wooster => 1, wayne => 1 },
546             primary_replica_tag => 'wooster'
547             }
548             )->run;
549              
550             Reconfigure a table's sharding and replication.
551              
552             =head2 status
553              
554             r->table('marvel')->status->run;
555              
556             Return the status of a table. The return value is an object providing
557             information about the table's shards, replicas and replica readiness states
558              
559             =head2 wait
560              
561             r->table('marvel')->wait->run;
562              
563             Wait for a table to be ready. A table may be temporarily unavailable
564             after creation, rebalancing or reconfiguring. The L command
565             blocks until the given table is fully up to date.
566              
567             =cut