File Coverage

blib/lib/Win32/SqlServer/DTS/Assignment.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Win32::SqlServer::DTS::Assignment;
2            
3             =head1 NAME
4            
5             Win32::SqlServer::DTS::Assignment - a Perl base class to represent a DTS Dynamic Properties task Assignment object
6            
7             =head1 SYNOPSIS
8            
9             package Win32::SqlServer::DTS::Assignment::SomethingWeird;
10             use base (Win32::SqlServer::DTS::Assignment);
11            
12             #and goes on defining the child class
13            
14             =head1 DESCRIPTION
15            
16             C is a base class that should be inherited by a specialized class that defines one type of
17             Assignment object that is part of a DTS Dynamic Property task.
18            
19             This class defines some common attributes that a subclass of C. Some methods must be override too,
20             and are explained in the next sections.
21            
22             =head2 EXPORT
23            
24             None by default.
25            
26             =cut
27            
28 4     4   21775 use strict;
  4         6  
  4         116  
29 4     4   17 use warnings;
  4         5  
  4         112  
30 4     4   18 use base qw(Win32::SqlServer::DTS);
  4         4  
  4         1504  
31             use Carp qw(confess);
32             use Win32::SqlServer::DTS::AssignmentTypes;
33             use Win32::SqlServer::DTS::Assignment::DestinationFactory;
34            
35             =head2 METHODS
36            
37             =head3 new
38            
39             Instantiates a new C object. Expects as parameter a C object.
40             Unless you want to extend the C class, you will want to fetch C objects using
41             the C method from L class.
42            
43             =cut
44            
45             sub new {
46            
47             my $class = shift;
48             my $self = { _sibling => shift };
49            
50             bless $self, $class;
51            
52             my $sibling = $self->get_sibling();
53            
54             $self->{destination} = Win32::SqlServer::DTS::Assignment::DestinationFactory->create(
55             $sibling->{DestinationPropertyID} );
56            
57             # subscribing to the event of changing the destination string in the
58             # Win32::SqlServer::DTS::Assignment::Destination object
59             $self->{destination}
60             ->add_subscriber( 'changed', sub { $self->_set_destination() } );
61            
62             $self->{typename} =
63             Win32::SqlServer::DTS::AssignmentTypes->get_class_name( $sibling->SourceType() );
64            
65             $self->{type} = $sibling->SourceType();
66            
67             return $self;
68            
69             }
70            
71             =head3 get_type
72            
73             Returns the type as a numeric code for a instantied object of a subclass of C.
74            
75             =cut
76            
77             sub get_type {
78            
79             my $self = shift;
80             return $self->{type};
81            
82             }
83            
84             =head3 get_type_name
85            
86             Returns a type as a string converted from the original numeric code using L
87             abstract class to make the convertion.
88            
89             =cut
90            
91             sub get_type_name {
92            
93             my $self = shift;
94             return $self->{typename};
95            
96             }
97            
98             =head3 get_source
99            
100             This method should be override by any subclass of C. If invoked but not overrided, it will abort
101             program execution with an error message.
102            
103             =cut
104            
105             sub get_source {
106            
107             confess
108             "This method should be override by an specialized subclass of Win32::SqlServer::DTS::Assignment\n";
109            
110             }
111            
112             =head3 get_destination
113            
114             Returns a C object. See L for more details about how
115             to use Destination objects.
116            
117             A C object is not part of the official MS SQL Server DTS API, but is easier to use and
118             do not use L directly (so there are no great performance penalties).
119            
120             =cut
121            
122             sub get_destination {
123            
124             my $self = shift;
125            
126             return $self->{destination};
127            
128             }
129            
130             =head3 set_destination
131            
132             Sets the Destination string in the assignment (in other words, it writes directly in the DTS package). To be able to
133             invoke this method, the C should not have invoke the C method before, since writing
134             the DTS package requires having the C<_sibling> attribute defined.
135            
136             The method will check such condition and will abort program execution in such cases.
137            
138             Once the string is modified successfully in the package, the C will be modified as well
139             (a new instance will be created).
140            
141             =cut
142            
143             sub set_destination {
144            
145             my $self = shift;
146             my $new_string = shift;
147            
148             # modifying both attributes. set_string does some validation,
149             # so it's being called first
150             $self->{destination}->set_string($new_string);
151            
152             confess "The new string cannot be undefined"
153             unless ( defined($new_string) );
154            
155             $self->get_sibling()->DestinationPropertyID = $new_string;
156            
157             }
158            
159             # destination object is already updated, fetching the
160             # string value from the object
161             sub _set_destination {
162            
163             my $self = shift;
164            
165             $self->get_sibling()->DestinationPropertyID() =
166             $self->{destination}->get_raw_string();
167            
168             }
169            
170             =head3 get_properties
171            
172             Returns all properties from an assignment object as a hash reference, having the following keys:
173            
174             =over
175            
176             =item *
177             type
178            
179             =item *
180             source
181            
182             =item *
183             destination
184            
185             =back
186            
187             Since the method C must be overrided by subclasses of C, C will fail unless invoked
188             thru one of those subclasses.
189            
190             =cut
191            
192             sub get_properties {
193            
194             my $self = shift;
195            
196             return {
197             type => $self->get_type(),
198             source => scalar( $self->get_source() ),
199             destination => scalar( $self->get_destination() )
200             }
201            
202             }
203            
204             =head3 to_string
205            
206             Returns a string with the type, source and destination of an assignment. Useful for debugging or reporting.
207            
208             =cut
209            
210             sub to_string {
211            
212             my $self = shift;
213            
214             return $self->get_type_name
215             . " assignment\n"
216             . 'Source: '
217             . $self->get_source . "\n"
218             . 'Destination: '
219             . $self->get_destination . "\n";
220            
221             }
222            
223             1;
224            
225             __END__