[Bio] / Sprout / ERDBLoad.pm Repository:
ViewVC logotype

Annotation of /Sprout/ERDBLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package ERDBLoad;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use PageBuilder;
8 :     use ERDB;
9 :     use Stats;
10 :    
11 :     =head1 ERDB Table Load Utility Object
12 :    
13 :     =head2 Introduction
14 :    
15 : parrello 1.3 This object is designed to assist with creating the load file for an ERDB
16 :     data relation. The user constructs the object by specifying an ERDB object
17 :     and a relation name. This create the load file for the relevant relation. The client
18 :     then passes in data lines which are written to a file, and calls
19 :     L</Finish> to close the file and get the statistics.
20 : parrello 1.1
21 : parrello 1.3 This module makes use of the internal ERDB method C<_IsPrimary>.
22 : parrello 1.1
23 :     =cut
24 :    
25 :     #
26 :    
27 :     =head2 Public Methods
28 :    
29 :     =head3 new
30 :    
31 :     C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory); >>
32 :    
33 :     Begin loading an ERDB relation.
34 :    
35 :     =over 4
36 :    
37 :     =item erdb
38 :    
39 :     ERDB object representing the target database.
40 :    
41 :     =item relationName
42 :    
43 :     Name of the relation being loaded.
44 :    
45 :     =item directory
46 :    
47 :     Name of the directory to use for the load files, WITHOUT a trailing slash.
48 :    
49 : parrello 1.2 =item estimatedRows (optional)
50 :    
51 :     Estimated maximum number of table rows. If omitted, the table will be created in
52 :     a format that permits an essentially unlimited number of rows.
53 :    
54 : parrello 1.1 =back
55 :    
56 :     =cut
57 :    
58 :     sub new {
59 :     # Get the parameters.
60 : parrello 1.2 my ($class, $erdb, $relationName, $directory, $estimatedRows) = @_;
61 : parrello 1.1 # Validate the directory name.
62 :     if (! -d $directory) {
63 :     Confess("Load directory \"$directory\" not found.");
64 :     }
65 :     # Determine the name for this relation's load file.
66 :     my $fileName = "$directory/$relationName.dtx";
67 :     # Open the output file and remember its handle.
68 :     my $fileHandle = Open(undef, ">$fileName");
69 :     # Create the $erload object.
70 :     my $retVal = {
71 :     dbh => $erdb,
72 :     fh => $fileHandle,
73 :     fileName => $fileName,
74 :     relName => $relationName,
75 :     fileSize => 0,
76 :     lineCount => 0,
77 :     stats => Stats->new(),
78 :     primary => $erdb->_IsPrimary($relationName)
79 :     };
80 :     # Bless and return it.
81 :     bless $retVal, $class;
82 :     return $retVal;
83 :     }
84 :    
85 :     =head3 Put
86 :    
87 :     C<< my = $erload->Put($field1, $field2, ..., $fieldN); >>
88 :    
89 :     Write a line of data to the load file. This may also cause the load file to be closed
90 :     and data read into the table.
91 :    
92 :     =over 4
93 :    
94 :     =item field1, field2, ..., fieldN
95 :    
96 :     List of field values to be put into the data line. The field values must be in the
97 :     order determined shown in the documentation for the table. Internal tabs and
98 :     new-lines will automatically be escaped before the data line is formatted.
99 :    
100 :     =back
101 :    
102 :     =cut
103 :     #: Return Type ;
104 :     sub Put {
105 :     # Get the ERDBLoad instance.
106 :     my $self = shift @_;
107 :     # Run through the list of field values, escaping them.
108 :     my @fields = map { Tracer::Escape($_) } @_;
109 :     # If this is a primary relation, append the new-record field.
110 :     if ($self->{primary}) {
111 :     push @fields, '0';
112 :     }
113 :     # Form a data line from the fields.
114 :     my $line = join("\t", @fields) . "\n";
115 :     # Write the new record to the load file.
116 :     my $fh = $self->{fh};
117 :     print $fh $line;
118 : parrello 1.3 # Determine how long this will make the load file.
119 :     my $lineLength = length $line;
120 : parrello 1.1 # Update the statistics.
121 :     $self->{fileSize} += $lineLength;
122 :     $self->{lineCount} ++;
123 :     }
124 :    
125 :     =head3 Finish
126 :    
127 :     C<< my $stats = $erload->Finish(); >>
128 :    
129 :     Finish loading the table. This closes the load file and loads its contents into the database.
130 :     It also creates the indexes if the DBMS uses post-indexing.
131 :    
132 :     =over 4
133 :    
134 :     =item RETURN
135 :    
136 :     Returns a statistics object describing what happened during the load and containing any
137 :     error messages.
138 :    
139 :     =back
140 :    
141 :     =cut
142 :    
143 :     sub Finish {
144 :     # Get this object instance.
145 :     my ($self) = @_;
146 : parrello 1.3 # Close the load file.
147 :     close $self->{fh};
148 : parrello 1.1 # Return the statistics object.
149 :     return $self->{stats};
150 :     }
151 :    
152 :     =head3 RelName
153 :    
154 :     C<< my $name = $erload->RelName; >>
155 :    
156 :     Name of the relation being loaded by this object.
157 :    
158 :     =cut
159 :    
160 :     sub RelName {
161 :     # Get the object instance.
162 :     my ($self) = @_;
163 :     # Return the relation name.
164 :     return $self->{relName};
165 :     }
166 :    
167 :     1;
168 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3