File Coverage

blib/lib/DBIx/DataModel/Compatibility/V1.pm
Criterion Covered Total %
statement 219 274 79.9
branch 37 52 71.1
condition 14 23 60.8
subroutine 62 81 76.5
pod 11 32 34.3
total 343 462 74.2


line stmt bran cond sub pod time code
1             package DBIx::DataModel::Compatibility::V1;
2 6     6   45 use strict;
  6         42  
  6         296  
3 6     6   60 use warnings;
  6         38  
  6         432  
4 6     6   43 no strict 'refs';
  6         13  
  6         202  
5 6     6   28 no warnings 'once';
  6         8  
  6         305  
6              
7 6     6   3705 use DBIx::DataModel::Meta;
  6         18  
  6         221  
8 6     6   4016 use DBIx::DataModel::Meta::Schema;
  6         23  
  6         294  
9 6     6   38 use DBIx::DataModel::Meta::Source;
  6         11  
  6         164  
10 6     6   27 use DBIx::DataModel::Meta::Utils;
  6         8  
  6         424  
11 6     6   3936 use DBIx::DataModel::Schema;
  6         22  
  6         248  
12 6     6   36 use DBIx::DataModel::Source;
  6         11  
  6         138  
13 6     6   24 use DBIx::DataModel::Source::Table;
  6         9  
  6         104  
14 6     6   4183 use DBIx::DataModel::Statement;
  6         19  
  6         263  
15 6     6   3332 use DBIx::DataModel::Statement::JDBC;
  6         19  
  6         219  
16 6     6   38 use SQL::Abstract::More;
  6         10  
  6         69  
17              
18             my $tmp; # used for various renaming loops
19              
20             # utility fonction for replacing 'camelCase' keys in hashs by 'camel_case'
21             sub _rename_camelCase_keys {
22 83     83   151 my $hashref = shift;
23 83         225 foreach my $key (keys %$hashref) {
24 117         180 my $new_key = $key;
25             $new_key =~ s/([a-z])([A-Z])/$1_\L$2\E/g
26 117 100       773 and $hashref->{$new_key} = delete $hashref->{$key};
27             }
28              
29             # an exception for -postSQL
30 83 50       281 $tmp = delete $hashref->{-post_sQL} and $hashref->{-post_SQL} = $tmp;
31             }
32              
33             #----------------------------------------------------------------------
34             package DBIx::DataModel;
35             #----------------------------------------------------------------------
36 6     6   1178 use strict;
  6         10  
  6         137  
37 6     6   22 use warnings;
  6         12  
  6         254  
38 6     6   26 no warnings 'redefine';
  6         10  
  6         2571  
39             my $orig_Schema = \&Schema;
40              
41             *Schema = sub {
42 9     9   22373 my ($class, $schema_class_name, %args) = @_;
43              
44             # convert args received as camelCase
45 9         37 DBIx::DataModel::Compatibility::V1::_rename_camelCase_keys(\%args);
46              
47             # extract args that should go to DBIDM::Schema and not DBIDM::Meta::Schema
48 9         14 my %singleton_args;
49 9         20 foreach my $key (qw/dbh debug dbi_prepare_method
50             sql_abstract sql_dialect/) {
51 45 100       97 $tmp = delete $args{$key} and $singleton_args{$key} = $tmp;
52             }
53              
54             # view_parent is now join_parent (not 100% correct, but the best we can do)
55 9 100       28 if (my $vp = delete $args{view_parent}) {
56 1   50     5 $args{join_parent} ||= [];
57 1 50       2 $args{join_parent} = [$args{join_parent}] unless ref $args{join_parent};
58 1         2 push @{$args{join_parent}}, @$vp;
  1         3  
59             }
60              
61             # create the Meta::Schema
62 9         41 my $schema_class = $class->$orig_Schema($schema_class_name, %args);
63              
64             # also create a Schema singleton, if needed
65 8 100       30 if (%singleton_args) {
66              
67             # recuperate existing SQLA instance, if any
68 4         6 my %sqlam_args;
69 4 50       15 if (my $sqla = delete $singleton_args{sql_abstract}) {
70             # create a fake SQLA object in order to know how many builtin ops it has
71 0         0 my $fake_sqla = SQL::Abstract->new;
72              
73             # surgery: remove builtin ops from our $sqla object
74 0         0 for my $op_name (qw/special_ops unary_ops/) {
75 0         0 my $n_builtin_ops = @{$fake_sqla->{$op_name}};
  0         0  
76 0         0 splice @{$sqla->{$op_name}}, -$n_builtin_ops;
  0         0  
77             }
78              
79             # now inject the remaining stuff in $sqla as argument for a SQLAM object
80 0 0       0 %sqlam_args = %$sqla if $sqla;
81             }
82              
83             # sql_dialect, previously passed to Schema, is now passed to SQLAM
84 4 50       13 if (my $dialect = delete $singleton_args{sql_dialect}) {
85 4 100       9 if (ref $dialect) {
86 2         15 DBIx::DataModel::Compatibility::V1::_rename_camelCase_keys($dialect);
87 2         6 $sqlam_args{$_} = $dialect->{$_} foreach keys %$dialect;
88             }
89             else {
90 2         7 $dialect =~ s/^MySQL/MySQL_old/;
91 2         7 $sqlam_args{sql_dialect} = $dialect;
92             }
93             }
94              
95              
96             # create a new SQLAM instance
97 4         35 $singleton_args{sql_abstract} = SQL::Abstract::More->new(%sqlam_args);
98              
99             # create the singleton
100 4         1408 my $singleton = $schema_class->singleton(%singleton_args);
101             }
102              
103 8         2232 return $schema_class;
104             };
105              
106              
107              
108             #----------------------------------------------------------------------
109             package DBIx::DataModel::Meta::Schema;
110             #----------------------------------------------------------------------
111 6     6   36 use strict;
  6         8  
  6         125  
112 6     6   21 use warnings;
  6         16  
  6         247  
113 6     6   23 no warnings 'redefine';
  6         8  
  6         1882  
114              
115             my $orig_Type = \&Type;
116             *Type = *ColumnType = sub {
117 2     2   11 my ($self, $type_name, %handlers) = @_;
118 2         22 my $tmp;
119 2 50       12 $tmp = delete $handlers{fromDB} and $handlers{from_DB} = $tmp;
120 2 50       12 $tmp = delete $handlers{toDB} and $handlers{to_DB} = $tmp;
121 2         15 $self->$orig_Type($type_name, %handlers);
122             };
123              
124              
125             my $orig_new = \&new;
126             *new = sub {
127 9     9   19 my ($class, %options) = @_;
128              
129 9         38 $class->$orig_new(sql_no_inner_after_left_join => 1, %options);
130             };
131              
132              
133             sub tables { # return classname instead of metadm instance
134 0     0 0 0 my $self = shift;
135 0         0 return map {$_->class} values %{$self->{table}};
  0         0  
  0         0  
136             }
137              
138             sub views {
139 0     0 0 0 my $self = shift;
140 0         0 return map {$_->class} values %{$self->{table}};
  0         0  
  0         0  
141             }
142              
143              
144             #----------------------------------------------------------------------
145             package DBIx::DataModel::Schema;
146             #----------------------------------------------------------------------
147 6     6   63 use strict;
  6         13  
  6         176  
148 6     6   24 use warnings;
  6         34  
  6         306  
149 6     6   32 no warnings 'redefine';
  6         10  
  6         258  
150 6     6   24 use DBIx::DataModel::Carp;
  6         17  
  6         38  
151              
152              
153             *_createPackage = \&DBIx::DataModel::Meta::Utils::define_class;
154             *doTransaction = \&do_transaction;
155              
156             sub _defineMethod {
157 0     0   0 my ($class, $target, $method_name, $body, $is_silent) = @_;
158 0         0 my %args = (
159             class => $target,
160             name => $method_name,
161             body => $body,
162             );
163 0 0       0 $args{check_override} = 0 if $is_silent;
164 0         0 DBIx::DataModel::Meta::Utils->define_method(%args);
165             }
166              
167             sub ColumnType {
168 2     2 0 25 my $self = shift;
169 2         10 $self->metadm->Type(@_);
170             }
171              
172              
173             sub Autoload { # installs or desinstalls an AUTOLOAD
174 2     2 0 1537 my ($class, $toggle) = @_;
175              
176 2         10 DBIx::DataModel::Source::Table->Autoload($toggle);
177             }
178              
179             sub autoInsertColumns {
180 0     0 0 0 my $class = shift;
181 0         0 return $class->metadm->auto_insert_columns;
182             }
183              
184             sub autoUpdateColumns {
185 0     0 0 0 my $class = shift;
186 0         0 return $class->metadm->auto_update_columns;
187             }
188              
189             sub noUpdateColumns {
190 0     0 0 0 my $class = shift;
191 0         0 my %no_update_column = $class->metadm->no_update_column;
192 0         0 return keys %no_update_column;
193             }
194              
195             sub AutoInsertColumns {
196 1     1 0 7527 my ($class, %handlers) = @_;
197 1         7 $class->metadm->{auto_insert_columns} = \%handlers;
198             }
199              
200             sub AutoUpdateColumns {
201 1     1 0 6633 my ($class, %handlers) = @_;
202 1         6 $class->metadm->{auto_update_columns} = \%handlers;
203             }
204              
205             sub NoUpdateColumns {
206 2     2 0 20 my ($class, @columns) = @_;
207 2         7 $class->metadm->{no_update_columns} = {map {$_ => 1} @columns};
  4         17  
208             }
209              
210              
211             sub tables {
212 0     0 0 0 my $class = shift;
213 0         0 $class->metadm->tables;
214             }
215              
216              
217             sub selectImplicitlyFor {
218 0     0 0 0 my $self = shift;
219 0         0 $self->select_implicitly_for(@_);
220             }
221              
222             sub classData {
223 0     0 0 0 my $class = shift;
224 0         0 return $class->singleton;
225             }
226              
227             sub localizeState {
228 1     1 0 14 my $class = shift;
229 1         8 return $class->localize_state;
230             }
231              
232              
233             #----------------------------------------------------------------------
234             package DBIx::DataModel::Source;
235             #----------------------------------------------------------------------
236 6     6   2888 use strict;
  6         11  
  6         163  
237 6     6   27 use warnings;
  6         10  
  6         256  
238 6     6   24 no warnings 'redefine';
  6         20  
  6         238  
239 6     6   23 use DBIx::DataModel::Carp;
  6         12  
  6         28  
240              
241             *primKey = \&primary_key;
242              
243             sub MethodFromJoin {
244 0     0 1 0 my $self = shift;
245 0         0 $self->metadm->define_navigation_method(@_);
246             }
247              
248             sub createStatement {
249 0     0 0 0 my $class = shift;
250              
251 0         0 carp "->createStatement() is obsolete, use "
252             . "->select(.., -resultAs => 'statement')";
253              
254 0         0 return $class->select(@_, -resultAs => 'statement');
255             }
256              
257             sub selectImplicitlyFor {
258 0     0 1 0 my $self = shift;
259              
260 0         0 carp "HACK: obsolete method \$source->selectImplicitlyFor() is delegated "
261             . "to \$schema->select_implicitly_for(); the semantics is not exactly "
262             . "identical";
263 0         0 $self->metadm->schema->class->select_implicitly_for(@_);
264             }
265              
266             sub _autoloader {
267 2     2   9 my $self = shift;
268 2   33     5 my $class = ref($self) || $self;
269 2         3 my $attribute = our $AUTOLOAD;
270 2         11 $attribute =~ s/^.*:://;
271 2 50       5 return if $attribute eq 'DESTROY'; # won't overload that one!
272              
273 2 50 33     32 return $self->{$attribute} if ref($self) and exists $self->{$attribute};
274              
275 0         0 croak "no $attribute method in $class"; # otherwise
276             }
277              
278             sub Autoload { # installs or desinstalls an AUTOLOAD in $package
279 4     4 0 7 my ($class, $toggle) = @_;
280              
281 4 50       11 not ref($class) or croak "Autoload is a class method";
282 4 50       8 defined($toggle) or croak "Autoload : missing toggle value";
283              
284 6     6   2505 no strict 'refs';
  6         14  
  6         836  
285 4 100       9 if ($toggle) {
286 2         2 *{"${class}::AUTOLOAD"} = \&_autoloader;
  2         23  
287             }
288             else {
289 2         2 delete ${"${class}::"}{AUTOLOAD};
  2         38  
290             }
291             }
292              
293              
294              
295             #----------------------------------------------------------------------
296             package DBIx::DataModel::Source::Table;
297             #----------------------------------------------------------------------
298 6     6   35 use strict;
  6         11  
  6         131  
299 6     6   37 use warnings;
  6         15  
  6         263  
300 6     6   28 no warnings 'redefine';
  6         10  
  6         2905  
301              
302             sub DefaultColumns {
303 0     0 1 0 my ($class, $columns) = @_;
304 0         0 $class->metadm->default_columns($columns);
305             }
306              
307             sub ColumnType {
308 4     4 1 40 my ($class, $typeName, @args) = @_;
309 4         15 $class->metadm->define_column_type($typeName, @args);
310             }
311              
312             sub ColumnHandlers {
313 2     2 1 20 my ($class, $columnName, %handlers) = @_;
314 2         10 $class->metadm->define_column_handlers($columnName, %handlers);
315             }
316              
317             sub AutoExpand {
318 2     2 1 34 my ($class, @roles) = @_;
319 2         10 $class->metadm->define_auto_expand(@roles);
320             }
321              
322             sub autoInsertColumns {
323 0     0 0 0 my $self = shift;
324 0         0 $self->metadm->auto_insert_column;
325             }
326              
327             sub autoUpdateColumns {
328 0     0 1 0 my $self = shift;
329 0         0 $self->metadm->auto_update_column;
330             }
331              
332             sub noUpdateColumns {
333 1     1 1 15 my $self = shift;
334 1         4 my %no_update_columns = $self->metadm->no_update_column;
335 1         10 return keys %no_update_columns;
336             }
337              
338             sub componentRoles {
339 0     0 0 0 my $self = shift;
340 0         0 $self->metadm->components;
341             }
342              
343             sub applyColumnHandler {
344 1     1 1 6 my $class = shift;
345 1         7 $class->apply_column_handler(@_);
346             }
347              
348             sub AutoInsertColumns {
349 0     0 0 0 my ($class, %handlers) = @_;
350 0         0 $class->metadm->{auto_insert_columns} = \%handlers;
351             }
352              
353             sub AutoUpdateColumns {
354 0     0 0 0 my ($class, %handlers) = @_;
355 0         0 $class->metadm->{auto_update_columns} = \%handlers;
356             }
357              
358             sub NoUpdateColumns {
359 2     2 0 15 my ($class, @columns) = @_;
360 2         5 $class->metadm->{no_update_columns} = {map {$_ => 1} @columns};
  2         10  
361             }
362              
363             sub blessFromDB {
364 9     9 1 10396 my $class = shift;
365 9         68 $class->bless_from_DB(@_);
366             }
367              
368             sub db_table {
369 0     0 1 0 my $class = shift;
370 0         0 return $class->metadm->db_from;
371             }
372              
373             #----------------------------------------------------------------------
374             package DBIx::DataModel::Statement;
375             #----------------------------------------------------------------------
376 6     6   48 use strict;
  6         9  
  6         143  
377 6     6   21 use warnings;
  6         17  
  6         244  
378 6     6   27 no warnings 'redefine';
  6         8  
  6         250  
379 6     6   27 use DBIx::DataModel::Carp;
  6         10  
  6         24  
380 6     6   278 use Scalar::Util qw/reftype/;
  6         11  
  6         863  
381              
382             my $orig_refine = \&refine;
383             *refine = sub {
384 72     72   160 my $self = shift;
385              
386             # parse named or positional arguments
387 72         119 my %args;
388 72 100 66     512 if ($_[0] and not ref($_[0]) and $_[0] =~ /^-/) { # called with named args
      100        
389 59         187 %args = @_;
390             }
391             else { # we were called with unnamed args (all optional!), so we try
392             # to guess which is which from their datatypes.
393 6     6   48 no warnings 'uninitialized';
  6         16  
  6         2118  
394 13 100 66     92 $args{-columns} = shift unless !@_ or reftype $_[0] eq 'HASH' ;
395 13 100 66     58 $args{-where} = shift unless !@_ or reftype $_[0] eq 'ARRAY';
396 13 100 66     44 $args{-orderBy} = shift unless !@_ or reftype $_[0] eq 'HASH' ;
397 13 50       28 croak "too many args for select()" if @_;
398             }
399              
400             # camelCase keys
401 72         234 DBIx::DataModel::Compatibility::V1::_rename_camelCase_keys(\%args);
402              
403             # -distinct => \@columns is now -columns => [-distinct => @columns]
404 72 100       202 if (my $distinct = delete $args{-distinct}) {
405 2 100       9 ref $distinct or $distinct = [$distinct];
406 2         8 unshift @$distinct, '-distinct';
407 2         6 $args{-columns} = $distinct;
408             }
409              
410             # various old ways to require -result_as => 'statement'
411             $args{-result_as} =~ s/^(cursor|iter(ator)?)/statement/i
412 72 100       205 if $args{-result_as};
413              
414             # delegate to the real refine() method
415 72         231 $self->$orig_refine(%args);
416             };
417              
418             *{rowCount} = \&row_count;
419             *{pageCount} = \&page_count;
420             *{gotoPage} = \&goto_page;
421             *{shiftPages} = \&shift_pages;
422             *{nextPage} = \&next_page;
423             *{pageBoundaries} = \&page_boundaries;
424             *{pageRows} = \&page_rows;
425              
426             #----------------------------------------------------------------------
427             package DBIx::DataModel::Statement::JDBC;
428             #----------------------------------------------------------------------
429 6     6   69 use strict;
  6         11  
  6         140  
430 6     6   23 use warnings;
  6         15  
  6         249  
431 6     6   27 no warnings 'redefine';
  6         15  
  6         217  
432 6     6   25 use DBIx::DataModel::Carp;
  6         10  
  6         22  
433              
434             *{rowCount} = \&row_count;
435              
436              
437             # simulate previous classes, now moved into the Source:: namespace, so that
438             # they can be inherited from
439             #----------------------------------------------------------------------
440             package DBIx::DataModel::Table;
441             #----------------------------------------------------------------------
442             $INC{"DBIx/DataModel/Table.pm"} = 1;
443             our @ISA = qw/DBIx::DataModel::Source::Table/;
444              
445              
446             #----------------------------------------------------------------------
447             package DBIx::DataModel::View;
448             #----------------------------------------------------------------------
449             $INC{"DBIx/DataModel/View.pm"} = 1;
450             our @ISA = qw/DBIx::DataModel::Source::Table/;
451              
452             1;
453              
454             __END__