Model/DBM.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« Apps/Database/Schema.pm ↑ Summary Apps/Database/ExtractData.pm »
1 package XAS::Model::DBM;
2  
3 our $VERSION = '0.01';
4  
5 use XAS::Class
6 0 ▶ debug => 0,
Subroutine BEGIN: called CC=1 SCAR=2.6
7 version => $VERSION,
8 base => 'XAS::Base',
9 mixins => 'create find search search_like find_or_create update_or_create
10 update_or_new count populate read_record create_record
11 delete_record update_record load_records delete_records',
12 0 ;
13  
14 # ---------------------------------------------------------------------
15 # Database usability mixin functions
16 # ---------------------------------------------------------------------
17  
18 sub create {
19 my $class = shift;
20 my $schema = shift;
21  
22 return $schema->resultset( $class->table_name )->create( @_ );
23  
24 }
25  
26 sub find {
27 my $class = shift;
28 my $schema = shift;
29  
30 return $schema->resultset( $class->table_name )->find( @_ );
31  
32 }
33  
34 sub search {
35 my $class = shift;
36 my $schema = shift;
37  
38 return $schema->resultset( $class->table_name )->search( @_ );
39  
40 }
41  
42 sub search_like {
43 my $class = shift;
44 my $schema = shift;
45  
46 return $schema->resultset( $class->table_name )->search_like( @_ );
47  
48 }
49  
50 sub find_or_create {
51 my $class = shift;
52 my $schema = shift;
53  
54 return $schema->resultset( $class->table_name )->find_or_create( @_ );
55  
56 }
57  
58 sub update_or_create {
59 my $class = shift;
60 my $schema = shift;
61  
62 return $schema->resultset( $class->table_name )->update_or_create( @_ );
63  
64 }
65  
66 sub update_or_new {
67 my $class = shift;
68 my $schema = shift;
69  
70 return $schema->resultset( $class->table_name )->update_or_new( @_ );
71  
72 }
73  
74 sub count {
75 my $class = shift;
76 my $schema = shift;
77  
78 return $schema->resultset( $class->table_name )->count( @_ );
79  
80 }
81  
82 sub populate {
83 my $class = shift;
84 my $schema = shift;
85  
86 return $schema->resultset( $class->table_name )->populate( @_ );
87  
88 }
89  
90 sub load_records {
91 my $class = shift;
92 my $schema = shift;
93  
94 my @datum;
95 my @columns = $class->columns;
96  
97 # find the records
98  
99 if (my $rs = $class->search($schema, @_ )) {
100  
101 # transfer the data
102  
103 while (my $row = $rs->next) {
104  
105 my $data;
106  
107 foreach my $column (@columns) {
108  
109 my $info = $class->column_info($column);
110  
111 if ($info->{data_type} =~ m/^(datetime | timestamp)/x ) {
112  
113 my $dt = '';
114  
115 if ($row->$column) {
116  
117 $dt = sprintf("%s", $row->$column);
118 $dt =~ s/T/ /;
119  
120 }
121  
122 $data->{$column} = $dt;
123  
124 } else {
125  
126 $data->{$column} = $row->$column;
127  
128 }
129  
130 }
131  
132 push(@datum, $data);
133  
134 }
135  
136 }
137  
138 return \@datum;
139  
140 }
141  
142 sub delete_records {
143 my $class = shift;
144 my $schema = shift;
145  
146 $schema->txn_do(sub {
147  
148 $schema->resultset( $class->table_name )->search( @_ )->delete_all;
149  
150 });
151  
152 }
153  
154 sub read_record {
155 my $class = shift;
156 my $schema = shift;
157  
158 my $data = undef;
159 my @columns = $class->columns;
160  
161 # find the record
162  
163 if (my $row = $class->find($schema, @_ )) {
164  
165 # transfer the data
166  
167 foreach my $column (@columns) {
168  
169 my $info = $class->column_info($column);
170  
171 if ($info->{data_type} =~ m/^(datetime | timestamp)/x ) {
172  
173 my $dt = '';
174  
175 if ($row->$column) {
176  
177 $dt = sprintf("%s", $row->$column);
178 $dt =~ s/T/ /;
179  
180 }
181  
182 $data->{$column} = $dt;
183  
184 } else {
185  
186 $data->{$column} = $row->$column;
187  
188 }
189  
190 }
191  
192 }
193  
194 return $data;
195  
196 }
197  
198 sub create_record {
199 my $class = shift;
200 my $schema = shift;
201 my $record = shift;
202  
203 my $rec = undef;
204 my $data = undef;
205 my @columns = $class->columns;
206  
207 # transfer and filter the data
208  
209 $schema->txn_do(sub {
210  
211 foreach my $column (@columns) {
212  
213 my $info = $class->column_info($column);
214  
215 if (defined($record->{$column})) {
216  
217 next if ((defined($info->{auto_nextval}) ||
218 (defined($info->{is_auto_increment}))));
219  
220 $data->{$column} = $record->{$column};
221  
222 }
223  
224 }
225  
226 # create the record
227  
228 $class->create($schema, $data);
229  
230 while (my ($key, $value) = each(%$record)) {
231  
232 $rec->{$key} = $value;
233  
234 }
235  
236 });
237  
238 return $rec;
239  
240 }
241  
242 sub delete_record {
243 my $class = shift;
244 my $schema = shift;
245 my $record = shift;
246  
247 my $data = undef;
248 my $criteria = {
249 id => $record->{id}
250 };
251  
252 $schema->txn_do(sub {
253  
254 if (my $row = $class->find($schema, $criteria)) {
255  
256 $row->delete();
257  
258 while (my ($key, $value) = each(%$record)) {
259  
260 $data->{$key} = $value;
261  
262 }
263  
264 }
265  
266 });
267  
268 return $data;
269  
270 }
271  
272 sub update_record {
273 my $class = shift;
274 my $schema = shift;
275 my $record = shift;
276  
277 my $data = undef;
278 my @columns = $class->columns;
279 my $criteria = {
280 id => $record->{id}
281 };
282  
283 # retrieve the record
284  
285 $schema->txn_do(sub {
286  
287 if (my $row = $class->find($schema, $criteria)) {
288  
289 # transfer the data
290  
291 foreach my $column (@columns) {
292  
293 my $info = $class->column_info($column);
294  
295 if (defined($record->{$column})) {
296  
297 next if ((defined($info->{auto_nextval}) ||
298 (defined($info->{is_auto_increment}))));
299  
300 $row->$column($record->{$column});
301  
302 }
303  
304 }
305  
306 # update the record
307  
308 $row->update();
309  
310 while (my ($key, $value) = each(%$record)) {
311  
312 $data->{$key} = $value;
313  
314 }
315  
316 }
317  
318 });
319  
320 return $data;
321  
322 }
323  
324 1;
325  
326 __END__
« Apps/Database/Schema.pm ↑ Summary Apps/Database/ExtractData.pm »