File: Model.pm

package info (click to toggle)
libcli-framework-perl 0.05-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 456 kB
  • sloc: perl: 2,168; sql: 18; sh: 3; makefile: 2
file content (243 lines) | stat: -rw-r--r-- 4,760 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package My::Journal::Model;

use strict;
use warnings;
use Carp;

use DBI;

#-------

sub new {
    my ($class, %args) = @_;
    
    my $sqlite_db_path = $args{ dbpath } or croak 'path to SQLite DB file required';

    # Connect to database...
    my $dbh = DBI->connect( "dbi:SQLite:dbname=$sqlite_db_path",
        { AutoCommit => 1, RaiseError => 1, ShowErrorStatement => 1 }
    );
    bless { _dbh => $dbh }, $class;
}

sub dbh { $_[0]->{_dbh} }

#-------

sub insert_entry {
    my ($self, $entry_text) = @_;

    my $dbh = $self->dbh();

    my $entry_id;
    $dbh->begin_work(); # transaction (to get id of last-inserted record)
    eval {
        $dbh->do( q{
            INSERT INTO journal_entry (entry_text)
            VALUES (?)
        }, undef, $entry_text );
        
        my $e = $dbh->selectrow_arrayref( 'SELECT MAX(id) FROM journal_entry' );
        $entry_id = $e->[0];

        $dbh->commit();
    };
    if( $@ ) { eval { $dbh->rollback() } }

    return $entry_id;
}

sub delete_entry {
    my ($self, $id) = @_;

    my $dbh = $self->dbh();

    my $rows_affected = $dbh->do( q{
        DELETE FROM journal_entry
        WHERE id = ?
    }, undef, $id );

    return 1;
}

sub insert_tag {
    my ($self, $tag_text) = @_;

    my $dbh = $self->dbh();

    $dbh->do( q{
        INSERT INTO tag (tag_text)
        VALUES (?)
    }, undef, $tag_text );

    return 1;
}

sub add_tag_to_entry {
    my ($self, $entry_id, $tag_text) = @_;

    my $dbh = $self->dbh();

    $dbh->begin_work(); # transaction
    eval {
        my $tag_id;
        my $tag = $dbh->selectrow_arrayref(
            "SELECT id FROM tag WHERE tag_text = ?", undef, $tag_text
        );
        unless( defined $tag ) {
            # Tag does not exist already...
            $self->insert_tag( $tag_text );

            my $t = $dbh->selectrow_arrayref( 'SELECT MAX(id) FROM tag' );
            $tag_id = $t->[0];
        }
        $tag_id ||= $tag->[0];

        # Add (pre-existing or new) tag to journal entry...
        $dbh->do(
            'INSERT INTO entry2tag (entry_id, tag_id) VALUES (?, ?)',
            undef, $entry_id, $tag_id
        );
        $dbh->commit;
    };
    if( $@ ) { eval { $dbh->rollback() } }

    return 1;
}

sub remove_tag_from_entry {
    my ($self, $entry_id, $tag_id) = @_;

    my $dbh = $self->dbh();
    my $sth = $dbh->prepare_cached( q{
        DELETE FROM entry2tag
        WHERE entry_id = ?
        AND tag_id = ?
    } );
    $sth->execute( $entry_id, $tag_id );

    return 1;
}

sub clear_tags_from_entry {
    my ($self, $entry_id) = @_;

    my $dbh = $self->dbh();

    $dbh->do( q{
        DELETE FROM entry2tag
        WHERE entry_id = ?
    }, undef, $entry_id );

    return 1;
}

#-------

sub get_tag_id_by_name {
    my ($self, $tag_text) = @_;

    my $dbh = $self->dbh();

    my $t = $dbh->selectrow_arrayref(
        'SELECT id FROM tag WHERE tag_text = ?', undef, $tag_text
    );
    my $tag_id;
    if( defined $t ) {
        $tag_id = $t->[0];
    }
    return $tag_id;
}

sub entry_by_id {
    my ($self, $id) = @_;

    my $dbh = $self->dbh();

    my $sth = $dbh->prepare_cached( q{
        SELECT  id, entry_text
        FROM    journal_entry
        WHERE   id = ?
    } );
    $sth->execute( $id );

    my $row = $sth->fetchrow_hashref();
    $sth->finish();

    return $row;
}

sub entries_by_tag {
    my ($self, $tag_text) = @_;

    my $dbh = $self->dbh();

    my $sth = $dbh->prepare_cached( q{
        SELECT      e.id, e.entry_text
        FROM        journal_entry e
        INNER JOIN  entry2tag e2t ON (e2t.entry_id = e.id)
        INNER JOIN  tag t ON (t.id = e2t.tag_id)
        WHERE       t.tag_text = ?
    } );
    $sth->execute( $tag_text );
    my @entries;
    while( my $entry = $sth->fetchrow_hashref() ) {
        push @entries, $entry;
    }
    $sth->finish();
    return @entries;
}

sub all_entries {
    my ($self) = @_;

    my $dbh = $self->dbh();

    my $sth = $dbh->prepare_cached( q{
        SELECT  e.id, e.entry_text
        FROM    journal_entry e
    } );
    $sth->execute();

    my @rows;
    while( my $row = $sth->fetchrow_hashref() ) {
        push @rows, $row;
    }
    return @rows;
}

sub tags_by_entry_id {
    my ($self, $entry_id) = @_;

    my $dbh = $self->dbh();

    my $sth = $dbh->prepare_cached( q{
        SELECT      t.tag_text
        FROM        tag t
        INNER JOIN  entry2tag e2t ON (e2t.tag_id = t.id)
        WHERE       e2t.entry_id = ?
    } );
    $sth->execute( $entry_id );
    my $tags = $sth->fetchall_arrayref();

    return map { $_->[0] } @$tags;
}

#-------
1;

__END__

=pod

=head1 NAME

My::Journal::Model - Example model class for My::Journal demo app.

=head1 DEPENDENCIES

DBI

DBD::SQLite

=cut