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

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