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

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