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

Diff of /Sprout/ERDBQueryConsole.pm

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

revision 1.4, Thu Jun 4 18:20:50 2009 UTC revision 1.6, Tue Jun 30 19:53:01 2009 UTC
# Line 46  Line 46 
46    
47  =item erdb  =item erdb
48    
49  [[ErdbPm]] database object for the current database.  L<ERDB> database object for the current database.
50    
51  =item query  =item query
52    
53  [[ERDBQueryPm]] object for obtaining the query results.  L<ERDBQuery> object for obtaining the query results.
54    
55  =item fields  =item fields
56    
# Line 95  Line 95 
95    
96  =item db  =item db
97    
98  Database against which to run the query. This can be either an [[ErdbPm]]  Database against which to run the query. This can be either an L<ERDB>
99  object for the database or a string containing the database name.  object for the database or a string containing the database name.
100    
101  =item options  =item options
# Line 113  Line 113 
113  TRUE if the user is privileged and can make unlimited queries. The default  TRUE if the user is privileged and can make unlimited queries. The default
114  is FALSE.  is FALSE.
115    
116    =item raw
117    
118    TRUE to return the results in raw form rather than in HTML form.
119    
120  =back  =back
121    
122  =cut  =cut
# Line 122  Line 126 
126      my ($class, $db, %options) = @_;      my ($class, $db, %options) = @_;
127      # Get the options.      # Get the options.
128      my $secure = $options{secure} || 0;      my $secure = $options{secure} || 0;
129        my $raw = $options{raw} || 0;
130      # Get access to the database.      # Get access to the database.
131      my $erdb;      my $erdb;
132      if (! ref $db) {      if (! ref $db) {
# Line 133  Line 138 
138      my $retVal = {      my $retVal = {
139                      erdb => $erdb,                      erdb => $erdb,
140                      secure => $secure,                      secure => $secure,
141                        raw => $raw,
142                   };                   };
143      # Bless and return it.      # Bless and return it.
144      bless $retVal, $class;      bless $retVal, $class;
# Line 334  Line 340 
340      my @items = $eq->GetRow();      my @items = $eq->GetRow();
341    
342  Get the next row of data from the query. Each row will consist of a list  Get the next row of data from the query. Each row will consist of a list
343  of HTML strings, one per result column, in the same order the field names  of HTML strings (in normal mode) or PERL objects (in raw mode), one per result
344  appeared when the query was submitted.  column, in the same order the field names appeared when the query was submitted.
345    
346  If the query is complete, an empty list will be returned.  If the query is complete, an empty list will be returned.
347    
# Line 355  Line 361 
361          $self->AddStat(duration => time() - $start);          $self->AddStat(duration => time() - $start);
362          # Only proceed if a record was found.          # Only proceed if a record was found.
363          if (defined $record) {          if (defined $record) {
364                $self->AddStat(records => 1);
365              # Now we have the data for this row, and it's time to              # Now we have the data for this row, and it's time to
366              # stuff it into the return list. Loop through the fields.              # stuff it into the return list. Loop through the fields.
367              for my $field (@{$self->{fields}}) {              for my $field (@{$self->{fields}}) {
368                  # Get the values for this field.                  # Get the values for this field.
369                  my @values = $record->Value($field->{name});                  my @values = $record->Value($field->{name});
370                  $self->AddStat(values => scalar(@values));                  $self->AddStat(values => scalar(@values));
371                  # Get the field type.                  # Are we returning raw data or HTML?
372                    if (! $self->{raw}) {
373                        # Here we are in HTML mode. Get the field type.
374                  my $type = $field->{type};                  my $type = $field->{type};
375                  # Convert the values to HTML and string them together.                  # Convert the values to HTML and string them together.
376                  my $cell = join("<br />", map { $type->html($_) } @values);                      my $cell = join("<br /><hr /><br />",
377                                        map { $type->html($_) } @values);
378                  # Put the result into the output list.                  # Put the result into the output list.
379                  push @retVal, $cell;                  push @retVal, $cell;
380                    } elsif ($field->{secondary}) {
381                        # This is a raw secondary field. It's returned as a list reference.
382                        push @retVal, \@values;
383                    } else {
384                        # This is a raw primary field. It's returned as a scalar.
385                        # Note that if the field is empty, we'll be stuffing an
386                        # undefined value in its position of this row.
387                        push @retVal, $values[0];
388                    }
389              }              }
390          }          }
391      }      }
# Line 407  Line 426 
426    
427  =cut  =cut
428    
429    use constant GET_VAR_NAME => { Get => '$qh', GetFlat => '@results',
430                                   GetAll => '@rows' };
431    
432  sub GetCode {  sub GetCode {
433      # Get the parameters.      # Get the parameters.
434      my ($self, $dbVarName, $codeStyle, @parameters) = @_;      my ($self, $dbVarName, $codeStyle, @parameters) = @_;
# Line 452  Line 474 
474      $quotedObjectNameString =~ s/\s+/ /;      $quotedObjectNameString =~ s/\s+/ /;
475      # Quote the filter string.      # Quote the filter string.
476      my $quotedFilterString = Quotify($self->{filterString});      my $quotedFilterString = Quotify($self->{filterString});
     # The result from the Get call depends on the type: a list for  
     # GetAll, a scalar for Get.  
     my $getResultName = ($codeStyle eq 'Get' ? '$qh' : '@resultRows');  
477      # Not we compute the function name. It's the same as the code style      # Not we compute the function name. It's the same as the code style
478      # unless we're doing a GetAll and there's only one field. In that case      # unless we're doing a GetAll and there's only one field. In that case
479      # we do a GetFlat.      # we do a GetFlat.
480      my $getName = ($codeStyle eq 'GetAll' && scalar(@$fields) == 1 ? 'GetFlat' : $codeStyle);      my $getName = ($codeStyle eq 'GetAll' && scalar(@$fields) == 1 ? 'GetFlat' : $codeStyle);
481        # The result from the Get call depends on the type: a list for
482        # GetAll or GetFlat, a scalar for Get.
483        my $getResultName = GET_VAR_NAME->{$getName};
484      # Build the Get. It's multiple lines, so we need to compute how far to      # Build the Get. It's multiple lines, so we need to compute how far to
485      # indent the secondary lines. In addition, we need to decide here whether      # indent the secondary lines. In addition, we need to decide here whether
486      # we're doing a Get or a GetAll.      # we're doing a Get or a GetAll.
# Line 472  Line 494 
494      if (! @parmStrings) {      if (! @parmStrings) {
495          push @pieces, "[]";          push @pieces, "[]";
496      } else {      } else {
497            # Here we have a list of parameters. The first begins with a left bracket.
498          push @pieces, "[" . shift(@parmStrings);          push @pieces, "[" . shift(@parmStrings);
499          push @pieces, @parmStrings;          # If there's more than one, we need to do some comma-joining.
500            while (my $piece = shift @parmStrings) {
501                # Put a comma at the end of the last piece.
502                $pieces[$#pieces] .= ",";
503                # Add the next piece.
504                push @pieces, $piece;
505            }
506            # Put a right bracket on the last piece.
507          $pieces[$#pieces] .= "]";          $pieces[$#pieces] .= "]";
508      }      }
509      # If this is a GetAll, the field names go in here, too.      # If this is a GetAll, the field names go in here, too.
# Line 584  Line 614 
614      return $retVal;      return $retVal;
615  }  }
616    
617    =head3 Messages
618    
619        my $messages = $eq->Messages();
620    
621    Return the error and status messages for the current query as a single string.
622    
623    =cut
624    
625    sub Messages {
626        # Get the parameters.
627        my ($self) = @_;
628        # Return the queued messages.
629        return join("\n", $self->{stats}->Messages());
630    }
631    
632    
633    
634  =head3 SplitFields  =head3 SplitFields
635    
636      my @fields = ERDBQueryConsole::SplitFields($fieldString);      my @fields = ERDBQueryConsole::SplitFields($fieldString);

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.6

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3