| 1 | package MojoX::Session::Store::AsyncOrm; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'MojoX::Session::Store'; | ||
| 7 | |||
| 8 | use MIME::Base64; | ||
| 9 | use Storable qw/nfreeze thaw/; | ||
| 10 | |||
| 11 | __PACKAGE__->attr(is_async => 1); | ||
| 12 | |||
| 13 | __PACKAGE__->attr('dbh'); | ||
| 14 | __PACKAGE__->attr('class'); | ||
| 15 | __PACKAGE__->attr(sid_column => 'sid'); | ||
| 16 | __PACKAGE__->attr(expires_column => 'expires'); | ||
| 17 | __PACKAGE__->attr(data_column => 'data'); | ||
| 18 | |||
| 19 | sub create { | ||
| 20 | my ($self, $sid, $expires, $data, $cb) = @_; | ||
| 21 | |||
| 22 | $data = encode_base64(nfreeze($data)) if $data; | ||
| 23 | |||
| 24 | my $instance = $self->class->new; | ||
| 25 | |||
| 26 | $instance->column($self->sid_column => $sid); | ||
| 27 | $instance->column($self->expires_column => $expires); | ||
| 28 | $instance->column($self->data_column => $data); | ||
| 29 | |||
| 30 | $instance->create( | ||
| 31 | $self->dbh => sub { | ||
| 32 | my ($dbh, $instance, $error) = @_; | ||
| 33 | |||
| 34 | if ($error) { | ||
| 35 | $self->error($error); | ||
| 36 | return $cb->($self); | ||
| 37 | } | ||
| 38 | |||
| 39 | return $cb->($self) unless $instance; | ||
| 40 | |||
| 41 | return $cb->($self); | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub update { | ||
| 47 | my ($self, $sid, $expires, $data, $cb) = @_; | ||
| 48 | |||
| 49 | $data = encode_base64(nfreeze($data)) if $data; | ||
| 50 | |||
| 51 | $self->class->update( | ||
| 52 | $self->dbh => { | ||
| 53 | where => [$self->sid_column => $sid], | ||
| 54 | set => { | ||
| 55 | $self->expires_column => $expires, | ||
| 56 | $self->data_column => $data | ||
| 57 | } | ||
| 58 | } => sub { | ||
| 59 | my ($dbh, $instance, $error) = @_; | ||
| 60 | |||
| 61 | if ($error) { | ||
| 62 | $self->error($error); | ||
| 63 | return $cb->($self); | ||
| 64 | } | ||
| 65 | |||
| 66 | return $cb->($self) unless $instance; | ||
| 67 | |||
| 68 | return $cb->($self); | ||
| 69 | } | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub load { | ||
| 74 | my ($self, $sid, $cb) = @_; | ||
| 75 | |||
| 76 | $self->class->new($self->sid_column => $sid)->load( | ||
| 77 | $self->dbh => sub { | ||
| 78 | my ($dbh, $instance, $error) = @_; | ||
| 79 | |||
| 80 | if ($error) { | ||
| 81 | $self->error($error); | ||
| 82 | return $cb->($self); | ||
| 83 | } | ||
| 84 | |||
| 85 | return $cb->($self) unless $instance; | ||
| 86 | |||
| 87 | my $data = $instance->column($self->data_column); | ||
| 88 | |||
| 89 | $data = thaw(decode_base64($data)) if $data; | ||
| 90 | |||
| 91 | return $cb->($self, $instance->column($self->expires_column), | ||
| 92 | $data); | ||
| 93 | } | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub delete { | ||
| 98 | my ($self, $sid, $cb) = @_; | ||
| 99 | |||
| 100 | $self->class->delete( | ||
| 101 | $self->dbh => {where => [$self->sid_column => $sid]} => sub { | ||
| 102 | my ($dbh, $count, $error) = @_; | ||
| 103 | |||
| 104 | if ($error) { | ||
| 105 | $self->error($error); | ||
| 106 | return $cb->($self); | ||
| 107 | } | ||
| 108 | |||
| 109 | return $cb->($self) unless $count; | ||
| 110 | |||
| 111 | return $cb->($self, $count); | ||
| 112 | } | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |