-
Notifications
You must be signed in to change notification settings - Fork 54
/
Crud.pm
191 lines (149 loc) · 4.95 KB
/
Crud.pm
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
package C4::Crud;
# Copyright 2008 LibLime, Inc.
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
use Koha;
use C4::Context;
use C4::Koha;
use C4::Debug;
use vars qw( $debug );
=head1 NAME
Crud - Koha baseclass for Create, Read, Update, and Delete operations
=head1 SYNOPSIS
package C4::Person;
use base qw( C4::Crud );
sub _tablename {
return 'person';
}
sub _primary_key_field {
return 'person_id';
}
sub _fields {
return (
'person_id',
'name',
'birthdate',
);
}
Then, elsewhere in code,
my $person = C4::Person->new( { person_id => $person_id } );
=head1 DESCRIPTION
This class is meant to be a baseclass for Koha classes that represent
a database table.
=cut
sub new {
my $class = shift;
my $args = shift;
my $self = {};
bless( $self, $class );
if ( exists $args->{ $self->_primary_key_field() } ) {
$self->_init( { $self->_primary_key_field() => $args->{ $self->_primary_key_field() } } );
}
return $self;
}
sub insert {
my $self = shift;
# if this one already exists, just update it. Don't insert.
if ( $self->{ $self->_primary_key_field() } ) {
return $self->update();
}
my @fields_to_insert = $self->_fields();
# my @fields_to_insert = grep { exists $self->{$_} } $self->_fields();
my $sql = sprintf( 'INSERT INTO %s ( %s ) VALUES ( %s )',
$self->_tablename(),
join(', ', @fields_to_insert ),
join(', ', map {'?'} @fields_to_insert ),
);
warn $sql if $self->_debug();
warn $self->_dump if $self->_debug();
my $sth = $self->_dbh->prepare( $sql );
my $result = $sth->execute( map { $self->{$_} } @fields_to_insert );
$self->{ $self->_primary_key_field() } = $self->_dbh->{'mysql_insertid'};
$self->_init();
return $self->{ $self->_primary_key_field() };
}
sub delete {
my $self = shift;
my $sql = sprintf( 'DELETE FROM %s WHERE %s = ?',
$self->_tablename(),
$self->_primary_key_field(),
);
warn $sql if $self->_debug();
warn $self->_dump if $self->_debug();
my $sth = $self->_dbh->prepare( $sql );
my $result = $sth->execute( $self->{ $self->_primary_key_field() } );
return $result;
}
sub update {
my $self = shift;
my $sql = sprintf(
'UPDATE %s SET %s WHERE %s = ?',
$self->_tablename(),
join( ', ', map { "$_ = ?" } $self->_updatable_fields() ),
$self->_primary_key_field(),
);
warn $sql if $self->_debug();
warn $self->_dump if $self->_debug();
my $sth = $self->_dbh->prepare($sql);
my $result = $sth->execute( map { $self->{$_} } $self->_updatable_fields, $self->_primary_key_field() );
$self->_init();
return $self->{ $self->_primary_key_field() };
}
sub _init {
my $self = shift;
my $args = shift;
# If they didn't pass in a _primary_key_field, check if we have one already.
$args->{$self->_primary_key_field} = $self->{$self->_primary_key_field} unless exists $args->{$self->_primary_key_field};
# if we still don't have a $self->_primary_key_field, bail out.
return unless ( defined $args->{$self->_primary_key_field} );
my $sql = sprintf( 'SELECT %s FROM %s WHERE %s = ?',
join( ', ', $self->_fields() ),
$self->_tablename(),
$self->_primary_key_field() );
warn $sql if $self->_debug();
my $sth = $self->_dbh->prepare( $sql );
my $executed = $sth->execute( $args->{$self->_primary_key_field} );
my $result = $sth->fetchrow_hashref();
$sth->finish();
# populate our fields.
foreach my $field ( $self->_fields() ) {
$self->{$field} = $result->{$field};
}
return;
}
sub _dbh {
return C4::Context->dbh;
}
sub _dump {
my $self = shift;
my %fields = map { $_ => $self->{$_} } $self->_fields;
return Data::Dumper->Dump( [ \%fields ], [ 'fields' ] );
}
sub _debug {
return 0;
# return $debug;
}
=head3 _updatable_fields
returns list of fields that are NOT the primary_key_field. These are
fields that you can update.
=cut
sub _updatable_fields {
my $self = shift;
return grep { $_ ne $self->_primary_key_field() } $self->_fields();
}
1;