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

Diff of /Sprout/ERDBQuery.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Mon Jan 19 21:46:21 2009 UTC revision 1.5, Thu Apr 2 01:38:15 2009 UTC
# Line 57  Line 57 
57      my ($self) = @_;      my ($self) = @_;
58      # Declare the return variable.      # Declare the return variable.
59      my $retVal;      my $retVal;
60      # Fetch the next row in the query result set.      # Do we have a statement handle?
61      my $sth = $self->{_sth};      my $sth = $self->{_sth};
62        if (! defined $sth) {
63            # No, so we have a prepared statement. Start the query.
64            $sth = $self->_Run();
65        }
66        # Fetch the next row in the query result set.
67      my @row = $sth->fetchrow;      my @row = $sth->fetchrow;
68      # Check to see if we got any results.      # Check to see if we got any results.
69      if (@row == 0) {      if (@row == 0) {
# Line 79  Line 84 
84      return $retVal;      return $retVal;
85  }  }
86    
87    =head3 DefaultObjectName
88    
89        my $objectName = $query->DefaultObjectName();
90    
91    Return the name of this query's default entity or relationship.
92    
93    =cut
94    
95    sub DefaultObjectName {
96        # Get the parameters.
97        my ($self) = @_;
98        # Get the relation map.
99        my $map = $self->{_objectNames};
100        # Get the default object alias. This is always the first alias in the
101        # relation map.
102        my $retVal = $map->[0][0];
103        # Return the result.
104        return $retVal;
105    }
106    
107    
108  =head3 AnalyzeFieldName  =head3 AnalyzeFieldName
109    
110      my ($objectName, $fieldName, $type) = $query->AnalyzeFieldName($name);      my ($objectName, $fieldName, $type) = $query->AnalyzeFieldName($name);
# Line 106  Line 132 
132  sub AnalyzeFieldName {  sub AnalyzeFieldName {
133      # Get the parameters.      # Get the parameters.
134      my ($self, $name) = @_;      my ($self, $name) = @_;
135        # Attempt to find the field's data.
136        my ($objectName, $fieldName, $type) = $self->CheckFieldName($name);
137        # Process errors.
138        if (! defined $objectName) {
139            Confess("Field identifier \"$name\" has an invalid format.");
140        } elsif (! defined $fieldName) {
141            Confess("Object name \"$objectName\" not found in query.");
142        } elsif (! defined $type) {
143            Confess("Field name \"$fieldName\" not found in \"$objectName\".");
144        }
145        # Return the results.
146        return ($objectName, $fieldName, $type);
147    }
148    
149    
150    =head3 CheckFieldName
151    
152        my ($objectName, $fieldName, $type) = $query->CheckFieldName($name);
153    
154    Analyze a field name (such as might be found in a [[ErdbPm#GetAll]]
155    parameter list) and return the real name of the relevant entity or
156    relationship, the field name itself, and the associated type object
157    (which will be a subclass of [[ERDBTypePm]]). Unlike L</AnalyzeFIeldName>,
158    this method always returns results. If the field name is invalid, one
159    or more of the three results will be undefined.
160    
161    =over 4
162    
163    =item name
164    
165    Field name to examine, in the standard field name format used by [[ErdbPm]].
166    
167    =item RETURN
168    
169    Returns a 3-tuple containing the name of the object containing the field, the
170    base field name, and a type object describing the field's type. If the field
171    descriptor is invalid, the returned object name will be undefined. If the object
172    name is invalid, the returned field name will be undefined, and if the field
173    name is invalid, the returned type will be undefined.
174    
175    =back
176    
177    =cut
178    
179    sub CheckFieldName {
180        # Get the parameters.
181        my ($self, $name) = @_;
182      # Declare the return variables.      # Declare the return variables.
183      my ($objectName, $fieldName, $type);      my ($objectName, $fieldName, $type);
184      # Get the relation map.      # Get the relation map.
# Line 114  Line 187 
187      # relation map.      # relation map.
188      my $defaultName = $map->[0][0];      my $defaultName = $map->[0][0];
189      # Parse the field name.      # Parse the field name.
190      my $alias;      my ($alias, $fieldThing) = ERDB::ParseFieldName($name, $defaultName);
191      ($alias, $fieldName) = ERDB::ParseFieldName($name, $defaultName);      # Only proceed if we could successfully parse the field. If we couldn't,
192      if (! defined $alias) {      # everything will be going back undefined.
193          Confess("Field identifier \"$name\" has an invalid format.");      if (defined $alias) {
     } else {  
194          # Find the alias in the relation map.          # Find the alias in the relation map.
195          my ($aliasTuple) = grep { $_->[0] eq $alias } @$map;          my ($aliasTuple) = grep { $_->[0] eq $alias } @$map;
196          if (! defined $aliasTuple) {          if (! defined $aliasTuple) {
197              Confess("Object name \"$alias\" not found in query.");              # We have a bad object name, so the object name is all
198                # we return.
199                $objectName = $alias;
200          } else {          } else {
201              # Get the real object name.              # Get the real object name.
202              my $objectName = $aliasTuple->[1];              $objectName = $aliasTuple->[1];
203                # Now it's safe to return the field name.
204                $fieldName = $fieldThing;
205              # Ask the database for the field descriptor. If the field name is              # Ask the database for the field descriptor. If the field name is
206              # invalid, this will throw an error.              # invalid, this will throw an error.
207              my $fieldData = $self->{_db}->_FindField($fieldName, $objectName);              my $fieldData = $self->{_db}->_CheckField($objectName, $fieldName);
208                # Only proceed if the field exists.
209                if (defined $fieldData) {
210              # Extract the field type.              # Extract the field type.
211              my $typeName = $fieldData->{type};              my $typeName = $fieldData->{type};
212              # Get the corresponding type object.              # Get the corresponding type object.
213              $type = ERDB::GetDataTypes()->{$typeName};              $type = ERDB::GetDataTypes()->{$typeName};
214                }
215          }          }
216      }      }
217      # Return the results.      # Return the results.
218      return ($objectName, $fieldName, $type);      return ($objectName, $fieldName, $type);
219  }  }
220    
   
221  =head2 Internal Methods  =head2 Internal Methods
222    
223  =head3 _new  =head3 _new
# Line 190  Line 267 
267      return $self;      return $self;
268  }  }
269    
270    =head3 _Prepare
271    
272        $query->_Prepare($command, $parms);
273    
274    Cache the SQL command and parameter list for this query. The information
275    can be used to run the query at a future point.
276    
277    =over 4
278    
279    =item command
280    
281    SQL command to execute for this query.
282    
283    =item parms
284    
285    Parameters to feed to the query.
286    
287    =back
288    
289    =cut
290    
291    sub _Prepare {
292        # Get the parameters.
293        my ($self, $command, $parms) = @_;
294        # Stash the SQL command and the parameters.
295        $self->{_sql} = $command;
296        $self->{_parms} = $parms;
297    }
298    
299    =head3 _Run
300    
301        $query->_Run();
302    
303    Run this query. This method is used the first time Fetch is called
304    on a prepared query.
305    
306    =cut
307    
308    sub _Run {
309        # Get the parameters.
310        my ($self) = @_;
311        # Declare the return variable.
312        my $retVal;
313        # Get the database object and the SQL command.
314        my $erdb = $self->{_db};
315        my $command = $self->{_sql};
316        # Insure both are valid.
317        if (! defined $erdb) {
318            Confess("No database available to run this query.");
319        } elsif (! defined $command) {
320            Confess("Attempt to get results from an unprepared query.");
321        } else {
322            # Get the parameters. If there are no parameters, we use an empty list.
323            my $parms = $self->{_parms} || [];
324            # Create the statement handle and run the query.
325            $retVal = $erdb->_GetStatementHandle($command, $parms);
326            # Save the statement handle for Fetch to use.
327            $self->{_sth} = $retVal;
328        }
329        # Return it.
330        return $retVal;
331    }
332    
333    
334  1;  1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3