File: Stash.pm

package info (click to toggle)
libipc-pubsub-perl 0.29-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: perl: 1,471; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,925 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package IPC::PubSub::Cache::JiftyDBI::Stash;
use strict;
use warnings;

use Jifty::DBI::Handle;
use Jifty::DBI::SchemaGenerator;
use IPC::PubSub::Cache::JiftyDBI::Stash::Item;
use IPC::PubSub::Cache::JiftyDBI::Stash::Publisher;

use File::Temp qw/ tempfile tempdir /;

sub new {
    my $class = shift;
    my $self  = {};
    bless $self, $class;

    my %args = (
        db_init => 0,
        db_config => undef,
        db_handle => undef,
        table_prefix => 'pubsub_',
        @_
    );

    if ($args{'table_prefix'}) {

         IPC::PubSub::Cache::JiftyDBI::Stash::Item->table_prefix($args{'table_prefix'});
         IPC::PubSub::Cache::JiftyDBI::Stash::Publisher->table_prefix($args{'table_prefix'});
    }

    if ($args{'db_handle'}) {
            $self->handle($args{'db_handle'});

    } else {
    unless ( $args{'db_config'} ) {
        my $filename;
        ( undef, $filename ) = tempfile();

        $args{'db_config'} = { driver => 'SQLite', database => $filename };
    }

    $self->_connect( %{$args{'db_config'}} );

    }
    if ( $args{'db_init'} ) {
        $self->_generate_db();
    }
    return $self;
}

sub handle {
    my $self = shift;
    $self->{'handle'} = shift if (@_);
    return $self->{'handle'};
}

sub _generate_db {
    my $self = shift;
    my $gen = Jifty::DBI::SchemaGenerator->new( $self->handle );
    $gen->add_model( IPC::PubSub::Cache::JiftyDBI::Stash::Item->new( handle => $self->handle ) );
    $gen->add_model( IPC::PubSub::Cache::JiftyDBI::Stash::Publisher->new( handle => $self->handle ) );
    my @statements = $gen->create_table_sql_statements;
    $self->handle->begin_transaction;
    for my $statement (@statements) {
        my $ret = $self->handle->simple_query($statement);
    }
    $self->handle->commit;

}


sub _connect {
    my $self = shift;


    my $handle = Jifty::DBI::Handle->new();
    $handle->connect(@_);
    $self->handle($handle);
}


1;