| 1 | package UR::DataSource::Default; | ||
| 2 | |||
| 3 | # NOTE: UR::DataSource::QueryPlan currently has conditional logic for this class | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use UR; | ||
| 8 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 9 | |||
| 10 | class UR::DataSource::Default { | ||
| 11 | is => ['UR::DataSource','UR::Singleton'], | ||
| 12 | doc => 'allows the class to describe its own loading strategy' | ||
| 13 | }; | ||
| 14 | |||
| 15 | |||
| 16 | sub create_iterator_closure_for_rule { | ||
| 17 | my($self,$rule) = @_; | ||
| 18 | |||
| 19 | my $subject_class_name = $rule->subject_class_name; | ||
| 20 | unless ($subject_class_name->can('__load__')) { | ||
| 21 | Carp::croak("Can't load from class $subject_class_name: UR::DataSource::Default requires the class to implement __load__"); | ||
| 22 | } | ||
| 23 | |||
| 24 | my $template = $rule->template; | ||
| 25 | my ($query_plan) = $self->_resolve_query_plan($template); | ||
| 26 | |||
| 27 | my $expected_headers = $query_plan->{loading_templates}[0]{property_names}; | ||
| 28 | my ($headers, $content) = $subject_class_name->__load__($rule,$expected_headers); | ||
| 29 | |||
| 30 | my $iterator; | ||
| 31 | if (ref($content) eq 'ARRAY') { | ||
| 32 | $iterator = sub { | ||
| 33 | my $next_row = shift @$content; | ||
| 34 | $content = undef if @$content == 0; | ||
| 35 | return $next_row; | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | elsif (ref($content) eq 'CODE') { | ||
| 39 | $iterator = $content; | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | Carp::confess("Expected an arrayref of properties, and then content in the form of an arrayref (rows,columns) or coderef/iterator returning rows from $subject_class_name __load__!\n"); | ||
| 43 | } | ||
| 44 | |||
| 45 | if ("@$headers" ne "@$expected_headers") { | ||
| 46 | # translate the headers into the appropriate order | ||
| 47 | my @mapping = eval { _map_fields($headers,$expected_headers);}; | ||
| 48 | if ($@) { | ||
| 49 | Carp::croak("Loading data for class $subject_class_name and boolexpr $rule failed: $@"); | ||
| 50 | } | ||
| 51 | # print Data::Dumper::Dumper($headers,$expected_headers,\@mapping); | ||
| 52 | my $orig_iterator = $iterator; | ||
| 53 | $iterator = sub { | ||
| 54 | my $result = $orig_iterator->(); | ||
| 55 | return unless $result; | ||
| 56 | my @result2 = @$result[@mapping]; | ||
| 57 | return \@result2; | ||
| 58 | }; | ||
| 59 | } | ||
| 60 | |||
| 61 | return $iterator; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub can_savepoint { 0 } | ||
| 65 | |||
| 66 | sub _map_fields { | ||
| 67 | my ($from,$to) = @_; | ||
| 68 | my $n = 0; | ||
| 69 | my %from = map { $_ => $n++ } @$from; | ||
| 70 | my @pos; | ||
| 71 | for my $field (@$to) { | ||
| 72 | my $pos = $from{$field}; | ||
| 73 | unless (defined $pos) { | ||
| 74 | #print "@$from\n@$to\n" . Carp::longmess() . "\n"; | ||
| 75 | die("Can't resolve value for '$field' from the headers returned by its __load__: ". join(', ', @$from)); | ||
| 76 | } | ||
| 77 | push @pos, $pos; | ||
| 78 | } | ||
| 79 | return @pos; | ||
| 80 | } | ||
| 81 | |||
| 82 | # Nothing to be done for rollback | ||
| 83 | sub rollback { 1;} | ||
| 84 | |||
| 85 | my @saved_objects; | ||
| 86 | sub _sync_database { | ||
| 87 | my $self = shift; | ||
| 88 | my %params = @_; | ||
| 89 | my $changed_objects = $params{changed_objects}; | ||
| 90 | |||
| 91 | my %class_can_save; | ||
| 92 | my $err = do { | ||
| 93 | local $@; | ||
| 94 | eval { | ||
| 95 | for my $obj (@$changed_objects) { | ||
| 96 | my $obj_class = $obj->class; | ||
| 97 | unless (exists $class_can_save{$obj_class}) { | ||
| 98 | $class_can_save{$obj_class} = $obj->can('__save__'); | ||
| 99 | } | ||
| 100 | if ($class_can_save{$obj_class}) { | ||
| 101 | push @saved_objects, $obj; | ||
| 102 | $obj->__save__; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | }; | ||
| 106 | $@; | ||
| 107 | }; | ||
| 108 | |||
| 109 | if ($err) { | ||
| 110 | my @failed_rollback; | ||
| 111 | do { | ||
| 112 | my $rollback_error; | ||
| 113 | while (my $obj = shift @saved_objects) { | ||
| 114 | local $@; | ||
| 115 | eval { | ||
| 116 | $obj->__rollback__; | ||
| 117 | }; | ||
| 118 | if ($@) { | ||
| 119 | $rollback_error = $@; | ||
| 120 | push @failed_rollback, $obj; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | if (@failed_rollback) { | ||
| 124 | $self->error_message('Rollback failed: ' . Data::Dumper::Dumper(\@failed_rollback)); | ||
| 125 | Carp::croak "Failed to save, and ERRORS DURING ROLLBACK:\n$err\n $rollback_error\n"; | ||
| 126 | } | ||
| 127 | }; | ||
| 128 | die $err; | ||
| 129 | } | ||
| 130 | |||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub commit { | ||
| 135 | my @failed_commit; | ||
| 136 | while (my $obj = shift @saved_objects) { | ||
| 137 | local $@; | ||
| 138 | eval { | ||
| 139 | $obj->__commit__; | ||
| 140 | }; | ||
| 141 | if ($@) { | ||
| 142 | push @failed_commit, $@ => $obj; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | if (@failed_commit) { | ||
| 147 | my @failure_messages; | ||
| 148 | for (my $i = 0; $i < @failed_commit; $i += 2) { | ||
| 149 | my($exception, $obj) = @failed_commit[$i .. $i+1]; | ||
| 150 | push @failure_messages, "$exception: ".Data::Dumper::Dumper($obj); | ||
| 151 | } | ||
| 152 | Carp::croak "Commit failed:\n" . join("\n", @failure_messages); | ||
| 153 | } | ||
| 154 | |||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; | ||
| 159 |