blib/lib/Zing/Store/Mysql.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::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.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} || '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::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` int not null auto_increment primary key,
70 `key` varchar(255) not null,
71 `value` mediumtext not null,
72 `index` int default 0
73 ) engine = innodb
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 ifnull(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 ifnull(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 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::Mysql - Mysql Storage
274  
275 =cut
276  
277 =head1 ABSTRACT
278  
279 Mysql Storage Abstraction
280  
281 =cut
282  
283 =head1 SYNOPSIS
284  
285 use Test::DB::Mysql;
286 use Zing::Encoder::Dump;
287 use Zing::Store::Mysql;
288  
289 my $testdb = Test::DB::Mysql->new;
290 my $store = Zing::Store::Mysql->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 MySQL-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::mysql> backend. The C<ZING_DBNAME>
304 environment variable can be used to specify the database name (defaults to
305 "zing"). The C<ZING_DBHOST> environment variable can be used to specify the
306 database host (defaults to "localhost"). The C<ZING_DBPORT> environment
307 variable can be used to specify the database port (defaults to "3306"). The
308 C<ZING_DBUSER> environment variable can be used to specify the database
309 username (defaults to "root"). The C<ZING_DBPASS> environment variable can
310 be used to specify the database password. The C<ZING_DBZONE> environment
311 variable can be used to specify the database table name (defaults to
312 "entities").
313  
314 =cut
315  
316 =head1 INHERITS
317  
318 This package inherits behaviors from:
319  
320 L<Zing::Store>
321  
322 =cut
323  
324 =head1 LIBRARIES
325  
326 This package uses type constraints from:
327  
328 L<Zing::Types>
329  
330 =cut
331  
332 =head1 ATTRIBUTES
333  
334 This package has the following attributes:
335  
336 =cut
337  
338 =head2 client
339  
340 client(InstanceOf["DBI::db"])
341  
342 This attribute is read-only, accepts C<(InstanceOf["DBI::db"])> values, and is optional.
343  
344 =cut
345  
346 =head1 METHODS
347  
348 This package implements the following methods:
349  
350 =cut
351  
352 =head2 decode
353  
354 decode(Str $data) : HashRef
355  
356 The decode method decodes the JSON data provided and returns the data as a hashref.
357  
358 =over 4
359  
360 =item decode example #1
361  
362 # given: synopsis
363  
364 $store->decode('{"status"=>"ok"}');
365  
366 =back
367  
368 =cut
369  
370 =head2 drop
371  
372 drop(Str $key) : Int
373  
374 The drop method removes (drops) the item from the datastore.
375  
376 =over 4
377  
378 =item drop example #1
379  
380 # given: synopsis
381  
382 $store->drop('zing:main:global:model:temp');
383  
384 =back
385  
386 =cut
387  
388 =head2 encode
389  
390 encode(HashRef $data) : Str
391  
392 The encode method encodes and returns the data provided as JSON.
393  
394 =over 4
395  
396 =item encode example #1
397  
398 # given: synopsis
399  
400 $store->encode({ status => 'ok' });
401  
402 =back
403  
404 =cut
405  
406 =head2 keys
407  
408 keys(Str @keys) : ArrayRef[Str]
409  
410 The keys method returns a list of keys under the namespace of the datastore or
411 provided key.
412  
413 =over 4
414  
415 =item keys example #1
416  
417 # given: synopsis
418  
419 my $keys = $store->keys('zing:main:global:model:temp');
420  
421 =back
422  
423 =over 4
424  
425 =item keys example #2
426  
427 # given: synopsis
428  
429 $store->send('zing:main:global:model:temp', { status => 'ok' });
430  
431 my $keys = $store->keys('zing:main:global:model:temp');
432  
433 =back
434  
435 =cut
436  
437 =head2 lpull
438  
439 lpull(Str $key) : Maybe[HashRef]
440  
441 The lpull method pops data off of the top of a list in the datastore.
442  
443 =over 4
444  
445 =item lpull example #1
446  
447 # given: synopsis
448  
449 $store->lpull('zing:main:global:model:items');
450  
451 =back
452  
453 =over 4
454  
455 =item lpull example #2
456  
457 # given: synopsis
458  
459 $store->rpush('zing:main:global:model:items', { status => 'ok' });
460  
461 $store->lpull('zing:main:global:model:items');
462  
463 =back
464  
465 =cut
466  
467 =head2 lpush
468  
469 lpush(Str $key, HashRef $val) : Int
470  
471 The lpush method pushed data onto the top of a list in the datastore.
472  
473 =over 4
474  
475 =item lpush example #1
476  
477 # given: synopsis
478  
479 $store->lpush('zing:main:global:model:items', { status => '1' });
480  
481 =back
482  
483 =over 4
484  
485 =item lpush example #2
486  
487 # given: synopsis
488  
489 $store->lpush('zing:main:global:model:items', { status => '0' });
490  
491 $store->lpush('zing:main:global:model:items', { status => '0' });
492  
493 =back
494  
495 =cut
496  
497 =head2 recv
498  
499 recv(Str $key) : Maybe[HashRef]
500  
501 The recv method fetches and returns data from the datastore by its key.
502  
503 =over 4
504  
505 =item recv example #1
506  
507 # given: synopsis
508  
509 $store->recv('zing:main:global:model:temp');
510  
511 =back
512  
513 =over 4
514  
515 =item recv example #2
516  
517 # given: synopsis
518  
519 $store->send('zing:main:global:model:temp', { status => 'ok' });
520  
521 $store->recv('zing:main:global:model:temp');
522  
523 =back
524  
525 =cut
526  
527 =head2 rpull
528  
529 rpull(Str $key) : Maybe[HashRef]
530  
531 The rpull method pops data off of the bottom of a list in the datastore.
532  
533 =over 4
534  
535 =item rpull example #1
536  
537 # given: synopsis
538  
539 $store->rpull('zing:main:global:model:items');
540  
541 =back
542  
543 =over 4
544  
545 =item rpull example #2
546  
547 # given: synopsis
548  
549 $store->rpush('zing:main:global:model:items', { status => 1 });
550 $store->rpush('zing:main:global:model:items', { status => 2 });
551  
552 $store->rpull('zing:main:global:model:items');
553  
554 =back
555  
556 =cut
557  
558 =head2 rpush
559  
560 rpush(Str $key, HashRef $val) : Int
561  
562 The rpush method pushed data onto the bottom of a list in the datastore.
563  
564 =over 4
565  
566 =item rpush example #1
567  
568 # given: synopsis
569  
570 $store->rpush('zing:main:global:model:items', { status => 'ok' });
571  
572 =back
573  
574 =over 4
575  
576 =item rpush example #2
577  
578 # given: synopsis
579  
580 $store->rpush('zing:main:global:model:items', { status => 'ok' });
581  
582 $store->rpush('zing:main:global:model:items', { status => 'ok' });
583  
584 =back
585  
586 =cut
587  
588 =head2 send
589  
590 send(Str $key, HashRef $val) : Str
591  
592 The send method commits data to the datastore with its key and returns truthy.
593  
594 =over 4
595  
596 =item send example #1
597  
598 # given: synopsis
599  
600 $store->send('zing:main:global:model:temp', { status => 'ok' });
601  
602 =back
603  
604 =cut
605  
606 =head2 size
607  
608 size(Str $key) : Int
609  
610 The size method returns the size of a list in the datastore.
611  
612 =over 4
613  
614 =item size example #1
615  
616 # given: synopsis
617  
618 my $size = $store->size('zing:main:global:model:items');
619  
620 =back
621  
622 =over 4
623  
624 =item size example #2
625  
626 # given: synopsis
627  
628 $store->rpush('zing:main:global:model:items', { status => 'ok' });
629  
630 my $size = $store->size('zing:main:global:model:items');
631  
632 =back
633  
634 =cut
635  
636 =head2 slot
637  
638 slot(Str $key, Int $pos) : Maybe[HashRef]
639  
640 The slot method returns the data from a list in the datastore by its index.
641  
642 =over 4
643  
644 =item slot example #1
645  
646 # given: synopsis
647  
648 my $model = $store->slot('zing:main:global:model:items', 0);
649  
650 =back
651  
652 =over 4
653  
654 =item slot example #2
655  
656 # given: synopsis
657  
658 $store->rpush('zing:main:global:model:items', { status => 'ok' });
659  
660 my $model = $store->slot('zing:main:global:model:items', 0);
661  
662 =back
663  
664 =cut
665  
666 =head2 test
667  
668 test(Str $key) : Int
669  
670 The test method returns truthy if the specific key (or datastore) exists.
671  
672 =over 4
673  
674 =item test example #1
675  
676 # given: synopsis
677  
678 $store->rpush('zing:main:global:model:items', { status => 'ok' });
679  
680 $store->test('zing:main:global:model:items');
681  
682 =back
683  
684 =over 4
685  
686 =item test example #2
687  
688 # given: synopsis
689  
690 $store->drop('zing:main:global:model:items');
691  
692 $store->test('zing:main:global:model:items');
693  
694 =back
695  
696 =cut
697  
698 =head1 AUTHOR
699  
700 Al Newkirk, C<awncorp@cpan.org>
701  
702 =head1 LICENSE
703  
704 Copyright (C) 2011-2019, Al Newkirk, et al.
705  
706 This is free software; you can redistribute it and/or modify it under the terms
707 of the The Apache License, Version 2.0, as elucidated in the L<"license
708 file"|https://github.com/iamalnewkirk/zing-store-mysql/blob/master/LICENSE>.
709  
710 =head1 PROJECT
711  
712 L<Wiki|https://github.com/iamalnewkirk/zing-store-mysql/wiki>
713  
714 L<Project|https://github.com/iamalnewkirk/zing-store-mysql>
715  
716 L<Initiatives|https://github.com/iamalnewkirk/zing-store-mysql/projects>
717  
718 L<Milestones|https://github.com/iamalnewkirk/zing-store-mysql/milestones>
719  
720 L<Contributing|https://github.com/iamalnewkirk/zing-store-mysql/blob/master/CONTRIBUTE.md>
721  
722 L<Issues|https://github.com/iamalnewkirk/zing-store-mysql/issues>
723  
724 =cut