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

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