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

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