blib/lib/Zing/Store/Pg.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 156 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing::Store::Pg;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry 'Zing::Types';
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 extends 'Zing::Store';
15  
16 our $VERSION = '0.02'; # VERSION
17  
18 # ATTRIBUTES
19  
20 has client => (
21 is => 'ro',
22 isa => 'InstanceOf["DBI::db"]',
23 new => 1,
24 );
25  
26 fun new_client($self) {
27 my $dbname = $ENV{ZING_DBNAME} || 'zing';
28 my $dbhost = $ENV{ZING_DBHOST} || 'localhost';
29 my $dbport = $ENV{ZING_DBPORT} || '5432';
30 my $dbuser = $ENV{ZING_DBUSER} || 'postgres';
31 my $dbpass = $ENV{ZING_DBPASS};
32 require DBI; DBI->connect(
33 join(';',
34 "dbi:Pg:dbname=$dbname",
35 $dbhost ? join('=', 'host', $dbhost) : (),
36 $dbport ? join('=', 'port', $dbport) : (),
37 ),
38 $dbuser, $dbpass,
39 {
40 AutoCommit => 1,
41 PrintError => 0,
42 RaiseError => 1
43 }
44 );
45 }
46  
47 has table => (
48 is => 'ro',
49 isa => 'Str',
50 new => 1,
51 );
52  
53 fun new_table($self) {
54 $ENV{ZING_DBZONE} || 'entities'
55 }
56  
57 # BUILDERS
58  
59 fun new_encoder($self) {
60 require Zing::Encoder::Dump; Zing::Encoder::Dump->new;
61 }
62  
63 fun BUILD($self) {
64 my $client = $self->client;
65 my $table = $self->table;
66 do {
67 $client->do(qq{
68 create table if not exists "$table" (
69 "id" serial primary key,
70 "key" varchar not null,
71 "value" text not null,
72 "index" integer default 0
73 )
74 });
75 }
76 unless (defined(do{
77 local $@;
78 local $client->{RaiseError} = 0;
79 local $client->{PrintError} = 0;
80 eval {
81 $client->do(qq{
82 select 1 from "$table" where 1 = 1
83 })
84 }
85 }));
86 return $self;
87 }
88  
89 fun DESTROY($self) {
90 $self->client->disconnect;
91 return $self;
92 }
93  
94 # METHODS
95  
96 method drop(Str $key) {
97 my $table = $self->table;
98 my $client = $self->client;
99 my $sth = $client->prepare(
100 qq{delete from "$table" where "key" = ?}
101 );
102 $sth->execute($key);
103 return $sth->rows > 0 ? 1 : 0;
104 }
105  
106 method keys(Str $query) {
107 $query =~ s/\*/%/g;
108 my $table = $self->table;
109 my $client = $self->client;
110 my $data = $client->selectall_arrayref(
111 qq{select distinct("key") from "$table" where "key" like ?},
112 {},
113 $query,
114 );
115 return [map $$_[0], @$data];
116 }
117  
118 method lpull(Str $key) {
119 my $table = $self->table;
120 my $client = $self->client;
121 my $data = $client->selectrow_arrayref(
122 qq{
123 select "t0"."id", "t0"."value"
124 from "$table" "t0" where "t0"."id" = (
125 select min("t1"."id")
126 from "$table" "t1" where "t1"."key" = ?
127 )
128 },
129 {},
130 $key,
131 );
132 if ($data) {
133 my $sth = $client->prepare(
134 qq{delete from "$table" where "id" = ?}
135 );
136 $sth->execute($data->[0]);
137 }
138 return $data ? $self->decode($data->[1]) : undef;
139 }
140  
141 method lpush(Str $key, HashRef $val) {
142 my $table = $self->table;
143 my $client = $self->client;
144 my $sth = $client->prepare(
145 qq{
146 insert into "$table" ("key", "value", "index") values (?, ?, (
147 select coalesce(min("me"."index"), 0) - 1
148 from "$table" "me" where "me"."key" = ?
149 ))
150 }
151 );
152 $sth->execute($key, $self->encode($val), $key);
153 return $sth->rows;
154 }
155  
156 method read(Str $key) {
157 my $table = $self->table;
158 my $client = $self->client;
159 my $data = $client->selectrow_arrayref(
160 qq{
161 select "value" from "$table"
162 where "key" = ? order by "id" desc limit 1
163 },
164 {},
165 $key,
166 );
167 return $data ? $data->[0] : undef;
168 }
169  
170 method recv(Str $key) {
171 my $data = $self->read($key);
172 return $data ? $self->decode($data) : $data;
173 }
174  
175 method rpull(Str $key) {
176 my $table = $self->table;
177 my $client = $self->client;
178 my $data = $client->selectrow_arrayref(
179 qq{
180 select "t0"."id", "t0"."value"
181 from "$table" "t0" where "t0"."id" = (
182 select max("t1"."id")
183 from "$table" "t1" where "t1"."key" = ?
184 )
185 },
186 {},
187 $key,
188 );
189 if ($data) {
190 my $sth = $client->prepare(
191 qq{delete from "$table" where "id" = ?}
192 );
193 $sth->execute($data->[0]);
194 }
195 return $data ? $self->decode($data->[1]) : undef;
196 }
197  
198 method rpush(Str $key, HashRef $val) {
199 my $table = $self->table;
200 my $client = $self->client;
201 my $sth = $client->prepare(
202 qq{
203 insert into "$table" ("key", "value", "index") values (?, ?, (
204 select coalesce(max("me"."index"), 0) + 1
205 from "$table" "me" where "me"."key" = ?
206 ))
207 }
208 );
209 $sth->execute($key, $self->encode($val), $key);
210 return $sth->rows;
211 }
212  
213 method send(Str $key, HashRef $val) {
214 my $set = $self->encode($val);
215 $self->write($key, $set);
216 return 'OK';
217 }
218  
219 method size(Str $key) {
220 my $table = $self->table;
221 my $client = $self->client;
222 my $data = $client->selectrow_arrayref(
223 qq{select count("key") from "$table" where "key" = ?},
224 {},
225 $key,
226 );
227 return $data->[0];
228 }
229  
230 method slot(Str $key, Int $pos) {
231 my $table = $self->table;
232 my $client = $self->client;
233 my $data = $client->selectrow_arrayref(
234 qq{
235 select "value" from "$table"
236 where "key" = ? order by "index" asc offset ? limit 1
237 },
238 {},
239 $key, $pos
240 );
241 return $data ? $self->decode($data->[0]) : undef;
242 }
243  
244 method test(Str $key) {
245 my $table = $self->table;
246 my $client = $self->client;
247 my $data = $client->selectrow_arrayref(
248 qq{select count("id") from "$table" where "key" = ?},
249 {},
250 $key,
251 );
252 return $data->[0] ? 1 : 0;
253 }
254  
255 method write(Str $key, Str $data) {
256 my $table = $self->table;
257 my $client = $self->client;
258 $client->prepare(
259 qq{delete from "$table" where "key" = ?}
260 )->execute($key);
261 $client->prepare(
262 qq{insert into "$table" ("key", "value") values (?, ?)}
263 )->execute($key, $data);
264 return $self;
265 }
266  
267 1;
268  
269 =encoding utf8
270  
271 =head1 NAME
272  
273 Zing::Store::Pg - Postgres Storage
274  
275 =cut
276  
277 =head1 ABSTRACT
278  
279 Postgres Storage Abstraction
280  
281 =cut
282  
283 =head1 SYNOPSIS
284  
285 use Test::DB::Postgres;
286 use Zing::Encoder::Dump;
287 use Zing::Store::Pg;
288  
289 my $testdb = Test::DB::Postgres->new;
290 my $store = Zing::Store::Pg->new(
291 client => $testdb->create->dbh,
292 encoder => Zing::Encoder::Dump->new
293 );
294  
295 # $store->drop;
296  
297 =cut
298  
299 =head1 DESCRIPTION
300  
301 This package provides a Postgres-specific storage adapter for use with data
302 persistence abstractions. The L</client> attribute accepts a L<DBI> object
303 configured to connect to a L<DBD::Pg> backend. The C<ZING_DBNAME> environment
304 variable can be used to specify the database name (defaults to "zing"). The
305 C<ZING_DBHOST> environment variable can be used to specify the database host
306 (defaults to "localhost"). The C<ZING_DBPORT> environment variable can be used
307 to specify the database port (defaults to "5432"). The C<ZING_DBUSER>
308 environment variable can be used to specify the database username (defaults to
309 "postgres"). The C<ZING_DBPASS> environment variable can be used to specify the
310 database password. The C<ZING_DBZONE> environment variable can be used to
311 specify the database table name (defaults to "entities").
312  
313 =cut
314  
315 =head1 INHERITS
316  
317 This package inherits behaviors from:
318  
319 L<Zing::Store>
320  
321 =cut
322  
323 =head1 LIBRARIES
324  
325 This package uses type constraints from:
326  
327 L<Zing::Types>
328  
329 =cut
330  
331 =head1 ATTRIBUTES
332  
333 This package has the following attributes:
334  
335 =cut
336  
337 =head2 client
338  
339 client(InstanceOf["DBI::db"])
340  
341 This attribute is read-only, accepts C<(InstanceOf["DBI::db"])> values, and is optional.
342  
343 =cut
344  
345 =head1 METHODS
346  
347 This package implements the following methods:
348  
349 =cut
350  
351 =head2 decode
352  
353 decode(Str $data) : HashRef
354  
355 The decode method decodes the JSON data provided and returns the data as a hashref.
356  
357 =over 4
358  
359 =item decode example #1
360  
361 # given: synopsis
362  
363 $store->decode('{"status"=>"ok"}');
364  
365 =back
366  
367 =cut
368  
369 =head2 drop
370  
371 drop(Str $key) : Int
372  
373 The drop method removes (drops) the item from the datastore.
374  
375 =over 4
376  
377 =item drop example #1
378  
379 # given: synopsis
380  
381 $store->drop('zing:main:global:model:temp');
382  
383 =back
384  
385 =cut
386  
387 =head2 encode
388  
389 encode(HashRef $data) : Str
390  
391 The encode method encodes and returns the data provided as JSON.
392  
393 =over 4
394  
395 =item encode example #1
396  
397 # given: synopsis
398  
399 $store->encode({ status => 'ok' });
400  
401 =back
402  
403 =cut
404  
405 =head2 keys
406  
407 keys(Str @keys) : ArrayRef[Str]
408  
409 The keys method returns a list of keys under the namespace of the datastore or
410 provided key.
411  
412 =over 4
413  
414 =item keys example #1
415  
416 # given: synopsis
417  
418 my $keys = $store->keys('zing:main:global:model:temp');
419  
420 =back
421  
422 =over 4
423  
424 =item keys example #2
425  
426 # given: synopsis
427  
428 $store->send('zing:main:global:model:temp', { status => 'ok' });
429  
430 my $keys = $store->keys('zing:main:global:model:temp');
431  
432 =back
433  
434 =cut
435  
436 =head2 lpull
437  
438 lpull(Str $key) : Maybe[HashRef]
439  
440 The lpull method pops data off of the top of a list in the datastore.
441  
442 =over 4
443  
444 =item lpull example #1
445  
446 # given: synopsis
447  
448 $store->lpull('zing:main:global:model:items');
449  
450 =back
451  
452 =over 4
453  
454 =item lpull example #2
455  
456 # given: synopsis
457  
458 $store->rpush('zing:main:global:model:items', { status => 'ok' });
459  
460 $store->lpull('zing:main:global:model:items');
461  
462 =back
463  
464 =cut
465  
466 =head2 lpush
467  
468 lpush(Str $key, HashRef $val) : Int
469  
470 The lpush method pushed data onto the top of a list in the datastore.
471  
472 =over 4
473  
474 =item lpush example #1
475  
476 # given: synopsis
477  
478 $store->lpush('zing:main:global:model:items', { status => '1' });
479  
480 =back
481  
482 =over 4
483  
484 =item lpush example #2
485  
486 # given: synopsis
487  
488 $store->lpush('zing:main:global:model:items', { status => '0' });
489  
490 $store->lpush('zing:main:global:model:items', { status => '0' });
491  
492 =back
493  
494 =cut
495  
496 =head2 recv
497  
498 recv(Str $key) : Maybe[HashRef]
499  
500 The recv method fetches and returns data from the datastore by its key.
501  
502 =over 4
503  
504 =item recv example #1
505  
506 # given: synopsis
507  
508 $store->recv('zing:main:global:model:temp');
509  
510 =back
511  
512 =over 4
513  
514 =item recv example #2
515  
516 # given: synopsis
517  
518 $store->send('zing:main:global:model:temp', { status => 'ok' });
519  
520 $store->recv('zing:main:global:model:temp');
521  
522 =back
523  
524 =cut
525  
526 =head2 rpull
527  
528 rpull(Str $key) : Maybe[HashRef]
529  
530 The rpull method pops data off of the bottom of a list in the datastore.
531  
532 =over 4
533  
534 =item rpull example #1
535  
536 # given: synopsis
537  
538 $store->rpull('zing:main:global:model:items');
539  
540 =back
541  
542 =over 4
543  
544 =item rpull example #2
545  
546 # given: synopsis
547  
548 $store->rpush('zing:main:global:model:items', { status => 1 });
549 $store->rpush('zing:main:global:model:items', { status => 2 });
550  
551 $store->rpull('zing:main:global:model:items');
552  
553 =back
554  
555 =cut
556  
557 =head2 rpush
558  
559 rpush(Str $key, HashRef $val) : Int
560  
561 The rpush method pushed data onto the bottom of a list in the datastore.
562  
563 =over 4
564  
565 =item rpush example #1
566  
567 # given: synopsis
568  
569 $store->rpush('zing:main:global:model:items', { status => 'ok' });
570  
571 =back
572  
573 =over 4
574  
575 =item rpush example #2
576  
577 # given: synopsis
578  
579 $store->rpush('zing:main:global:model:items', { status => 'ok' });
580  
581 $store->rpush('zing:main:global:model:items', { status => 'ok' });
582  
583 =back
584  
585 =cut
586  
587 =head2 send
588  
589 send(Str $key, HashRef $val) : Str
590  
591 The send method commits data to the datastore with its key and returns truthy.
592  
593 =over 4
594  
595 =item send example #1
596  
597 # given: synopsis
598  
599 $store->send('zing:main:global:model:temp', { status => 'ok' });
600  
601 =back
602  
603 =cut
604  
605 =head2 size
606  
607 size(Str $key) : Int
608  
609 The size method returns the size of a list in the datastore.
610  
611 =over 4
612  
613 =item size example #1
614  
615 # given: synopsis
616  
617 my $size = $store->size('zing:main:global:model:items');
618  
619 =back
620  
621 =over 4
622  
623 =item size example #2
624  
625 # given: synopsis
626  
627 $store->rpush('zing:main:global:model:items', { status => 'ok' });
628  
629 my $size = $store->size('zing:main:global:model:items');
630  
631 =back
632  
633 =cut
634  
635 =head2 slot
636  
637 slot(Str $key, Int $pos) : Maybe[HashRef]
638  
639 The slot method returns the data from a list in the datastore by its index.
640  
641 =over 4
642  
643 =item slot example #1
644  
645 # given: synopsis
646  
647 my $model = $store->slot('zing:main:global:model:items', 0);
648  
649 =back
650  
651 =over 4
652  
653 =item slot example #2
654  
655 # given: synopsis
656  
657 $store->rpush('zing:main:global:model:items', { status => 'ok' });
658  
659 my $model = $store->slot('zing:main:global:model:items', 0);
660  
661 =back
662  
663 =cut
664  
665 =head2 test
666  
667 test(Str $key) : Int
668  
669 The test method returns truthy if the specific key (or datastore) exists.
670  
671 =over 4
672  
673 =item test example #1
674  
675 # given: synopsis
676  
677 $store->rpush('zing:main:global:model:items', { status => 'ok' });
678  
679 $store->test('zing:main:global:model:items');
680  
681 =back
682  
683 =over 4
684  
685 =item test example #2
686  
687 # given: synopsis
688  
689 $store->drop('zing:main:global:model:items');
690  
691 $store->test('zing:main:global:model:items');
692  
693 =back
694  
695 =cut
696  
697 =head1 AUTHOR
698  
699 Al Newkirk, C<awncorp@cpan.org>
700  
701 =head1 LICENSE
702  
703 Copyright (C) 2011-2019, Al Newkirk, et al.
704  
705 This is free software; you can redistribute it and/or modify it under the terms
706 of the The Apache License, Version 2.0, as elucidated in the L<"license
707 file"|https://github.com/iamalnewkirk/zing-store-pg/blob/master/LICENSE>.
708  
709 =head1 PROJECT
710  
711 L<Wiki|https://github.com/iamalnewkirk/zing-store-pg/wiki>
712  
713 L<Project|https://github.com/iamalnewkirk/zing-store-pg>
714  
715 L<Initiatives|https://github.com/iamalnewkirk/zing-store-pg/projects>
716  
717 L<Milestones|https://github.com/iamalnewkirk/zing-store-pg/milestones>
718  
719 L<Contributing|https://github.com/iamalnewkirk/zing-store-pg/blob/master/CONTRIBUTE.md>
720  
721 L<Issues|https://github.com/iamalnewkirk/zing-store-pg/issues>
722  
723 =cut