migrating SQL reports

This commit is contained in:
JT Smith 2003-12-10 23:35:41 +00:00
parent 10f3b3578d
commit adef55be5f
11 changed files with 344 additions and 137 deletions

View file

@ -17,6 +17,7 @@ package WebGUI::DatabaseLink;
use strict;
use Tie::CPHash;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
@ -31,7 +32,7 @@ This package contains utility methods for WebGUI's database link system.
=head1 SYNOPSIS
use WebGUI::DatabaseLink;
%links = WebGUI::DatabaseLink::getHash();
$hashRef = WebGUI::DatabaseLink::getList();
%databaseLink = WebGUI::DatabaseLink::get($databaseLinkId);
%using = WebGUI::Databaselink::whatIsUsing($databaseLinkId);
@ -46,15 +47,17 @@ These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 getHash ( )
=head2 getList ( )
Returns a hash containing all database links. The format is:
Returns a hash reference containing all database links. The format is:
databaseLinkId => title
=cut
sub getHash {
return WebGUI::SQL->buildHash("select databaseLinkId, title from databaseLink order by title");
sub getList {
my $list = WebGUI::SQL->buildHashRef("select databaseLinkId, title from databaseLink order by title");
$list->{'0'} = WebGUI::International::get(1076);
return $list;
}
#-------------------------------------------------------------------
@ -127,7 +130,7 @@ sub disconnect {
$class = shift;
$value = shift;
if (defined $class->{_dbh}) {
$class->{_dbh}->disconnect() unless ($class->{_databaseLink}{DSN} eq $session{config}{dsn});
$class->{_dbh}->disconnect() unless ($class->{_databaseLink}{databaseLinkId} == 0);
}
}
@ -151,7 +154,7 @@ sub dbh {
$dsn = $class->{_databaseLink}{DSN};
$username = $class->{_databaseLink}{username};
$identifier = $class->{_databaseLink}{identifier};
if ($dsn eq $session{config}{dsn}) {
if ($class->{databaseLinkId} == 0) {
$class->{_dbh} = $session{dbh};
return $session{dbh};
} elsif ($dsn =~ /\DBI\:\w+\:\w+/i) {
@ -191,7 +194,17 @@ sub new {
$class = shift;
$databaseLinkId = shift;
unless ($databaseLinkId eq "") {
%databaseLink = WebGUI::SQL->quickHash("select * from databaseLink where databaseLinkId='$databaseLinkId'");
if ($databaseLinkId == 0) {
%databaseLink = (
databaseLinkId=>0,
DSN=>$session{config}{dsn},
username=>$session{config}{dbuser},
identifier=>$session{config}{dpass},
title=>"WebGUI Database"
);
} else {
%databaseLink = WebGUI::SQL->quickHash("select * from databaseLink where databaseLinkId='$databaseLinkId'");
}
}
bless {_databaseLinkId => $databaseLinkId, _databaseLink => \%databaseLink }, $class;
}

View file

@ -250,7 +250,7 @@ Returns an HTML formatted message with the audit messages for display during deb
sub showAudit {
my $audit = getAudit();
$audit =~ s/\n/\<br\>/g;
$audit =~ s/\n/\<br\>\n/g;
return '<div style="background-color: #ffffdd;color: #000000;">'.$audit.'</div>';
}
@ -281,7 +281,7 @@ Returns an HTML formatted message with the security messages for display during
sub showSecurity {
my $security = getSecurity();
$security =~ s/\n/\<br\>/g;
$security =~ s/\n/\<br\>\n/g;
return '<div style="background-color: #800000;color: #ffffff;">'.$security.'</div>';
}
@ -296,7 +296,7 @@ Returns an HTML formatted list of the session variables for display during debug
sub showSessionVars {
my $data = getSessionVars();
$data =~ s/\n/\<br\>/g;
$data =~ s/\n/\<br\>\n/g;
return '<div style="background-color: #ffffff; color: #000000; font-size: 10pt; font-family: helvetica;">'.$data.'</div>';
}
@ -311,7 +311,7 @@ Returns an HTML formatted message for displaying the stack trace during debug op
sub showStackTrace {
my $st = getStackTrace();
$st =~ s/\n/\<br\>/g;
$st =~ s/\n/\<br\>\n/g;
return $st;
}
@ -326,7 +326,7 @@ Returns HTML formatted warnings for display during debug operations.
sub showWarnings {
my $warning = getWarnings();
$warning =~ s/\n/\<br\>/g;
$warning =~ s/\n/\<br\>\n/g;
return '<div style="background-color: #ffdddd;color: #000000;">'.$warning.'</div>';
}

View file

@ -39,6 +39,7 @@ Base forms package. Eliminates some of the normal code work that goes along with
$html = WebGUI::Form::checkList({name=>"dayOfWeek", options=>\%days});
$html = WebGUI::Form::combo({name=>"fruit",options=>\%fruit});
$html = WebGUI::Form::contentType({name=>"contentType");
$html = WebGUI::Form::databaseLink();
$html = WebGUI::Form::date({name=>"endDate", value=>$endDate});
$html = WebGUI::Form::dateTime({name=>"begin", value=>$begin});
$html = WebGUI::Form::email({name=>"emailAddress"});
@ -319,6 +320,43 @@ sub contentType {
extras=>$_[0]->{extras}
});
}
#-------------------------------------------------------------------
=head2 databaseLink ( hashRef )
Returns a select list of database links.
=over
=item name
The name field for this form element. Defaults to "databaseLinkId".
=item value
The unique identifier for the selected template. Defaults to "0", which is the WebGUI database.
=back
=cut
sub databaseLink {
my $value = $_[0]->{value} || 1;
my $name = $_[0]->{name} || "databaseLinkId";
return selectList({
name=>$name,
options=>WebGUI::DatabaseLink::getList(),
value=>[$value]
});
}
#-------------------------------------------------------------------
=head2 date ( hashRef )

View file

@ -487,6 +487,81 @@ sub contentType {
}
$self->{_data} .= $output;
}
#-------------------------------------------------------------------
=head2 date ( [name , value, label, afterEdit, extras, uiLevel ] )
Adds a database link select list to the form.
=over
=item name
The name of this form element.
=item value
The default value for this form element.
=item label
The left column label for this form row. Defaults to "Database Link".
=item afterEdit
A URL that will be acted upon after editing a database link. Typically there is a link next to the select list that reads "Edit this database link" and this is the URL to go to after editing is complete.
=item extras
If you want to add anything special to this form element like javascript actions, or stylesheet information, you'd add it in here as follows:
'onChange="this.form.submit()"'
=item uiLevel
The UI level for this field. See the WebGUI developer's site for details. Defaults to "5".
=back
=cut
sub databaseLink {
my ($output, $subtext);
my ($self, @p) = @_;
my ($name, $value, $label, $afterEdit, $extras, $uiLevel) =
rearrange([qw(name value label afterEdit extras uiLevel)], @p);
if (_uiLevelChecksOut($uiLevel)) {
$label = $label || WebGUI::International::get(1075);
if (WebGUI::Privilege::isInGroup(3)) {
#disabled until we can resolve the "new" wobject problem
#if ($afterEdit) {
# $subtext = '<a href="'.WebGUI::URL::page("op=editDatabaseLink&lid=".$value
# ."&afterEdit="
# .WebGUI::URL::escape($afterEdit)).'">'.WebGUI::International::get(0).'</a> / ';
#}
$subtext .= '<a href="'.WebGUI::URL::page("op=listDatabaseLinks").'">'
.WebGUI::International::get(981).'</a>';
}
$output = WebGUI::Form::databaseLink({
"name"=>$name,
"value"=>$value,
"extras"=>$extras
});
$output .= _subtext($subtext);
$output = $self->_tableFormRow($label,$output);
} else {
$output = WebGUI::Form::hidden({
"name"=>$name,
"value"=>$value
});
}
$self->{_data} .= $output;
}
#-------------------------------------------------------------------
=head2 date ( name [ label, value, extras, subtext, size, noDate, uiLevel ] )

View file

@ -126,6 +126,8 @@ sub www_listDatabaseLinks {
$output = helpIcon(68);
$output .= '<h1>'.WebGUI::International::get(996).'</h1>';
$sth = WebGUI::SQL->read("select * from databaseLink order by title");
$row[$i] = '<tr><td valign="top" class="tableData"></td><td valign="top" class="tableData">'.WebGUI::International::get(1076).'</td></tr>';
$i++;
while (%data = $sth->hash) {
$row[$i] = '<tr><td valign="top" class="tableData">'
.deleteIcon('op=deleteDatabaseLink&dlid='.$data{databaseLinkId})

View file

@ -46,6 +46,7 @@ Package that paginates rows of arbitrary data for display on the web.
$integer = $p->getPageNumber;
$html = $p->getPageLinks;
$html = $p->getPreviousPageLink;
$integer = $p->getRowCount;
=head1 METHODS
@ -419,6 +420,19 @@ sub getPreviousPageLink {
}
#-------------------------------------------------------------------
=head2 getRowCount ( )
Returns a count of the total number of rows in the paginator.
=cut
sub getRowCount {
return $_[0]->{_totalRows};
}
#-------------------------------------------------------------------
=head2 new ( currentURL, rowArrayRef [, paginateAfter, pageNumber, formVar ] )
@ -459,12 +473,13 @@ sub new {
$rowsPerPage = $_[3] || 25;
$formVar = $_[5] || "pn";
$pn = $_[4] || $session{form}{$formVar} || 1;
bless {_url => $currentURL, _rpp => $rowsPerPage, _rowRef => $rowRef, _formVar => $formVar, _pn => $pn}, $class;
my $totalRows = $#{$rowRef};
bless {_url => $currentURL, _rpp => $rowsPerPage, _totalRows=>$totalRows , _rowRef => $rowRef, _formVar => $formVar, _pn => $pn}, $class;
}
#-------------------------------------------------------------------
=head2 setDataByQuery ( query [, dbh ] )
=head2 setDataByQuery ( query [, dbh, unconditional ] )
Retrieves a data set from a database and replaces whatever data set was passed in through the constructor.
@ -480,17 +495,27 @@ An SQL query that will retrieve a data set.
A DBI-style database handler. Defaults to the WebGUI site handler.
=item unconditional
A boolean indicating that the query should be read unconditionally. Defaults to "0". If set to "1" and the unconditional read results in an error, the error will be returned by this method.
=back
=cut
sub setDataByQuery {
my ($sth, $pageCount, $rowCount, $dbh, $sql, $self, @row, $data);
($self, $sql, $dbh) = @_;
my ($sth, $rowCount, @row);
my ($self, $sql, $dbh, $unconditional) = @_;
$dbh ||= $session{dbh};
$sth = WebGUI::SQL->read($sql);
$pageCount = 1;
while ($data = $sth->hashRef) {
if ($unconditional) {
$sth = WebGUI::SQL->unconditionalRead($sql,$dbh);
return $sth->errorMessage if ($sth->errorCode > 0);
} else {
$sth = WebGUI::SQL->read($sql,$dbh);
}
$self->{_totalRows} = $sth->rows;
my $pageCount = 1;
while (my $data = $sth->hashRef) {
$rowCount++;
if ($rowCount/$self->{_rpp} > $pageCount) {
$pageCount++;
@ -503,6 +528,7 @@ sub setDataByQuery {
}
$sth->finish;
$self->{_rowRef} = \@row;
return "";
}
1;

View file

@ -46,13 +46,6 @@ sub new {
databaseLinkId=>{
defaultValue=>0
},
DSN=>{
defaultValue=>$session{config}{dsn}
},
username=>{
defaultValue=>$session{config}{dbuser}
},
identifier=>{},
convertCarriageReturns=>{
defaultValue=>0
},
@ -100,34 +93,8 @@ sub www_edit {
-label=>WebGUI::International::get(3,$_[0]->get("namespace")),
-value=>$_[0]->getValue("template")
);
my %databaseLinkOptions;
tie %databaseLinkOptions, 'Tie::IxHash',
"0"=>WebGUI::International::get(19,$_[0]->get("namespace")),
WebGUI::DatabaseLink::getHash();
$privileges->selectList(
-name=>"databaseLinkId",
-options=>\%databaseLinkOptions,
-label=>WebGUI::International::get(20,$_[0]->get("namespace")),
-value=>[$_[0]->getValue("databaseLinkId")],
-subtext=>(WebGUI::Privilege::isInGroup(3)) ? '<a href="'.WebGUI::URL::page("op=listDatabaseLinks").'">'.WebGUI::International::get(981).'</a>' : ""
);
$privileges->readOnly(
-value=>WebGUI::International::get(21,$_[0]->get("namespace")),
);
$privileges->text(
-name=>"DSN",
-label=>WebGUI::International::get(5,$_[0]->get("namespace")),
-value=>$_[0]->getValue("DSN")
);
$privileges->text(
-name=>"username",
-label=>WebGUI::International::get(6,$_[0]->get("namespace")),
-value=>$_[0]->getValue("username")
);
$privileges->password(
-name=>"identifier",
-label=>WebGUI::International::get(7,$_[0]->get("namespace")),
-value=>$_[0]->getValue("identifier")
$privileges->databaseLink(
-value=>$_[0]->getValue("databaseLinkId")
);
$layout->integer(
-name=>"paginateAfter",
@ -151,105 +118,85 @@ sub www_edit {
#-------------------------------------------------------------------
sub www_view {
my ($dsn, $username, $identifier, $dbLink, $query, @row, $i, $rownum, $p, $ouch, $output, $sth, $dbh, @result, @template, $temp, $col, $errorMessage, $url);
my ($query, %var, @debug);
if ($_[0]->get("preprocessMacros")) {
$query = WebGUI::Macro::process($_[0]->get("dbQuery"));
} else {
$query = $_[0]->get("dbQuery");
}
$dsn = $_[0]->get("DSN");
$username = $_[0]->get("username");
$identifier = $_[0]->get("identifier");
$output = $_[0]->displayTitle;
$output .= $_[0]->description;
$output .= WebGUI::International::get(17,$_[0]->get("namespace"))." ".$query."<p>" if ($_[0]->get("debugMode"));
# connect to external database if used
if ($_[0]->get("databaseLinkId")) {
$dbLink = WebGUI::DatabaseLink->new($_[0]->get("databaseLinkId"));
$dbh = $dbLink->dbh;
} else {
if ($dsn eq $session{config}{dsn}) {
$dbh = $session{dbh};
} elsif ($dsn =~ /\DBI\:\w+\:\w+/i) {
eval{$dbh = DBI->connect($dsn,$username,$identifier)};
if ($@) {
WebGUI::ErrorHandler::warn("SQL Report [".$_[0]->get("wobjectId")."] ".$@);
undef $dbh;
}
} else {
$output .= WebGUI::International::get(9,$_[0]->get("namespace")).'<p>' if ($_[0]->get("debugMode"));
WebGUI::ErrorHandler::warn("SQLReport [".$_[0]->get("wobjectId")."] The DSN specified is of an improper format.");
}
}
push(@debug,{'debug.output'=>WebGUI::International::get(17,$_[0]->get("namespace")).$query});
my $dbLink = WebGUI::DatabaseLink->new($_[0]->get("databaseLinkId"));
my $dbh = $dbLink->dbh;
if (defined $dbh) {
if ($query =~ /^select/i || $query =~ /^show/i || $query =~ /^describe/i) {
$sth = WebGUI::SQL->unconditionalRead($query,$dbh);
unless ($sth->errorCode < 1) {
$errorMessage = $sth->errorMessage;
$output .= WebGUI::International::get(11,$_[0]->get("namespace")).' : '.$errorMessage.'<p>' if ($_[0]->get("debugMode"));
WebGUI::ErrorHandler::warn("There was a problem with the query: ".$errorMessage);
my $url = WebGUI::URL::page('&wid='.$_[0]->get("wobjectId").'&func=view');
foreach (keys %{$session{form}}) {
unless ($_ eq "pn" || $_ eq "wid" || $_ eq "func") {
$url = WebGUI::URL::append($url, WebGUI::URL::escape($_)
.'='.WebGUI::URL::escape($session{form}{$_}));
}
}
my $p = WebGUI::Paginator->new($url,[],$_[0]->get("paginateAfter"));
my $error = $p->setDataByQuery($query,$dbh,1);
if ($error ne "") {
WebGUI::ErrorHandler::warn("There was a problem with the query: ".$error);
push(@debug,{'debug.output'=>WebGUI::International::get(11,$_[0]->get("namespace"))." ".$error});
} else {
if ($_[0]->get("template") ne "") {
@template = split(/\^\-\;/,$_[0]->get("template"));
} else {
$i = 0;
$template[0] = '<table width="100%"><tr>';
$template[1] = '<tr>';
foreach $col ($sth->getColumnNames) {
$template[0] .= '<td class="tableHeader">'.$col.'</td>';
$template[1] .= '<td class="tableData">^'.$i.';</td>';
$i++;
}
$template[0] .= '</tr>';
$template[1] .= '</tr>';
$template[2] = '</table>';
$i = 0;
}
$output .= $template[0];
while (@result = $sth->array) {
$temp = $template[1];
$temp =~ s/\^(\d*)\;/$result[$1]/g; # Shouldn't this be \d+ ?
$rownum = $i + 1;
$temp =~ s/\^rownum\;/$rownum/g;
if ($_[0]->get("convertCarriageReturns")) {
$temp =~ s/\n/\<br\>/g;
}
$row[$i] = $temp;
$i++;
}
if ($sth->rows < 1) {
$output .= $template[2];
$output .= WebGUI::International::get(18,$_[0]->get("namespace")).'<p>';
} else {
$url = WebGUI::URL::page('&wid='.$_[0]->get("wobjectId").'&func=view');
foreach (keys %{$session{form}}) {
unless ($_ eq "pn" || $_ eq "wid" || $_ eq "func") {
$url = WebGUI::URL::append($url, WebGUI::URL::escape($_)
.'='.WebGUI::URL::escape($session{form}{$_}));
my $first = 1;
my @columns;
my @rows;
my $rownum = 1;
my $rowdata = $p->getPageData;
foreach my $data (@$rowdata) {
my %row;
my $colnum = 1;
my @fields;
foreach my $name (keys %{$data}) {
if ($first) {
push(@columns,{
'column.number'=>$colnum,
'column.name'=>$name
});
}
push(@fields,{
'field.number'=>$colnum,
'field.name'=>$name,
'field.value'=>$data->{$name}
});
$colnum++;
$row{'row.field.'.$name.'.value'} = $data->{$name};
}
$p = WebGUI::Paginator->new($url,\@row,$_[0]->get("paginateAfter"));
$output .= $p->getPage($session{form}{pn});
$output .= $template[2];
$output .= $p->getBar($session{form}{pn});
$row{'row.number'} = $rownum;
$row{'row.field_loop'} = \@fields;
push(@rows,\%row);
$first = 0;
$rownum++;
}
$sth->finish;
$var{columns_loop} = \@columns;
$var{rows_loop} = \@rows;
$var{'rows.count'} = $p->getRowCount;
$var{'rows.count.isZero'} = ($p->getRowCount < 1);
$var{'rows.count.isZero.label'} = WebGUI::International::get(18,$_[0]->get("namespace"));
$var{firstPage} = $p->getFirstPageLink;
$var{lastPage} = $p->getLastPageLink;
$var{nextPage} = $p->getNextPageLink;
$var{pageList} = $p->getPageLinks;
$var{previousPage} = $p->getPreviousPageLink;
$var{multiplePages} = ($p->getNumberOfPages > 1);
$var{numberOfPages} = $p->getNumberOfPages;
$var{pageNumber} = $p->getPageNumber;
}
} else {
$output .= WebGUI::International::get(10,$_[0]->get("namespace")).'<p>' if ($_[0]->get("debugMode"));
push(@debug,{'debug.output'=>WebGUI::International::get(10,$_[0]->get("namespace"))});
WebGUI::ErrorHandler::warn("SQLReport [".$_[0]->get("wobjectId")."] The SQL query is improperly formatted.");
}
if ($dbLink) {
$dbLink->disconnect;
} else {
$dbh->disconnect() unless ($dsn eq $session{config}{dsn});
}
$dbLink->disconnect;
} else {
$output .= WebGUI::International::get(12,$_[0]->get("namespace")).'<p>' if ($_[0]->get("debugMode"));
push(@debug,{'debug.output'=>WebGUI::International::get(12,$_[0]->get("namespace"))});
WebGUI::ErrorHandler::warn("SQLReport [".$_[0]->get("wobjectId")."] Could not connect to database.");
}
return $output;
$var{'debug_loop'} = \@debug;
return $_[0]->processTemplate($_[0]->get("templateId"),\%var);
}