From adef55be5fe06d2ea302584966eb82ff26e61a23 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 10 Dec 2003 23:35:41 +0000 Subject: [PATCH] migrating SQL reports --- docs/migration.txt | 4 + docs/previousVersion.sql | 2 +- docs/upgrades/upgrade_5.5.3-6.0.0.pl | 92 +++++++++++++ docs/upgrades/upgrade_5.5.3-6.0.0.sql | 10 ++ lib/WebGUI/DatabaseLink.pm | 29 +++-- lib/WebGUI/ErrorHandler.pm | 10 +- lib/WebGUI/Form.pm | 38 ++++++ lib/WebGUI/HTMLForm.pm | 75 +++++++++++ lib/WebGUI/Operation/DatabaseLink.pm | 2 + lib/WebGUI/Paginator.pm | 40 +++++- lib/WebGUI/Wobject/SQLReport.pm | 179 +++++++++----------------- 11 files changed, 344 insertions(+), 137 deletions(-) diff --git a/docs/migration.txt b/docs/migration.txt index a6bea8c8b..a5582012b 100644 --- a/docs/migration.txt +++ b/docs/migration.txt @@ -67,4 +67,8 @@ WebGUI::Form::dateTime() and WebGUI::HTMLForm->dateTime() no longer have because there is only one field to represent both the date and the time, unlike before. +5.2 Database Links + +The database links API was changed in 6.0. The getHash function was removed and +replaced with a getList function that returns a hash reference. diff --git a/docs/previousVersion.sql b/docs/previousVersion.sql index 385022ab7..b9b836ab6 100644 --- a/docs/previousVersion.sql +++ b/docs/previousVersion.sql @@ -17036,5 +17036,5 @@ update style set styleId=10 where styleId=-10; delete from style where styleId<0; update incrementer set nextValue=nextValue+1 where incrementerId='styleId'; update page set styleId=1000 where styleId=-8; -INSERT INTO style VALUES (1000,'WebGUI 6','\r\n ','\r\n^AdminBar(1);\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n
^L(17,"",2); ^AdminToggle;
\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\"home\" \n \"Email\"\r\n\"Print\" \n \"Site \"Plain\r\n^Spacer(56,1);^Spacer(26,1);
\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
^Spacer(53,59);^D(\"%c %D, %y\");
^PageTitle;
^Spacer(53,59);
\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n
^Spacer(53,1);\r\n
\r\n^FlexMenu;\r\n


\r\n\r\n
^-;^Spacer(53,1);
\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\r\n'); +INSERT INTO style VALUES (1000,'WebGUI 6','\r\n ','\r\n^AdminBar(2);\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n
^L(17,"",2); ^AdminToggle;
\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\"home\" \n \"Email\"\r\n\"Print\" \n \"Site \"Plain\r\n^Spacer(56,1);^Spacer(26,1);
\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
^Spacer(53,59);^D(\"%c %D, %y\");
^PageTitle;
^Spacer(53,59);
\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n
^Spacer(53,1);\r\n
\r\n^FlexMenu;\r\n


\r\n\r\n
^-;^Spacer(53,1);
\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\r\n'); diff --git a/docs/upgrades/upgrade_5.5.3-6.0.0.pl b/docs/upgrades/upgrade_5.5.3-6.0.0.pl index ca572f3ac..f7835a429 100644 --- a/docs/upgrades/upgrade_5.5.3-6.0.0.pl +++ b/docs/upgrades/upgrade_5.5.3-6.0.0.pl @@ -323,6 +323,98 @@ WebGUI::SQL->write("delete from incrementer where incrementerId='LinkList_linkId +#-------------------------------------------- +print "\tUpdating SQL Reports.\n" unless ($quiet); +my %dblink; +$dblink{$session{config}{dsn}} = ( + id=>0, + user=>$session{config}{dbuser} + ); +my $sth = WebGUI::SQL->read("select DSN, databaseLinkId, username, identifier, wobjectId from SQLReport"); +while (my $data = $sth->hashRef) { + my $id = undef; + next if ($data->{databaseLinkId} > 0); + foreach my $dsn (keys %dblink) { + if ($dsn eq $data->{dsn} && $dblink{$dsn}{user} eq $data->{username}) { + $id = $dblink{$dsn}{id}; + last; + } + } + unless (defined $id) { + $id = getNextId("databaseLinkId"); + my $title = $data->{username}.'@'.$data->{DSN}; + WebGUI::SQL->write("insert into databaseLink (databaseLinkId, title, DSN, username, identifier) values ($id, ".quote($title).", + ".quote($data->{DSN}).", ".quote($data->{username}).", ".quote($data->{identifier}).")"); + $dblink{$data->{DSN}} = ( + id=>$id, + user=>$data->{username} + ); + } + WebGUI::SQL->write("update SQLReport set databaseLinkId=".$id." where wobjectId=".$data->{wobjectId}); +} +$sth->finish; +WebGUI::SQL->write("alter table SQLReport drop column DSN"); +WebGUI::SQL->write("alter table SQLReport drop column username"); +WebGUI::SQL->write("alter table SQLReport drop column identifier"); +use WebGUI::DatabaseLink; +my $templateId; +my $a = WebGUI::SQL->read("select a.databaseLinkId, a.dbQuery, a.template, a.wobjectId, b.title from SQLReport a + left join wobject b on a.wobjectId=b.wobjectId"); +while (my $data = $a->hashRef) { + my $db = WebGUI::DatabaseLink->new($data->{databaseLinkId}); + if ($data->{template} ne "") { + ($templateId) = WebGUI::SQL->quickArray("select max(templateId) from template where namespace='SQLReport'"); + if ($templateId > 999) { + $templateId++; + } else { + $templateId = 1000; + } + my $b = WebGUI::SQL->unconditionalRead($data->{dbQuery},$db->dbh); + my @template = split(/\^\-\;/,$data->{template}); + my $final = ' +

+
+ + +

+ + + +

    + +
  • +
    +
+
+ '.$template[0].' + '; + my $i; + foreach my $col ($b->getColumnNames) { + my $replacement = ''; + $template[1] =~ s/\^$i\;/$replacement/g; + $i++; + } + $template[1] =~ s/\^rownum\;/\/g; + $final .= $template[1].' + + '.$template[2].' + + + '; + WebGUI::SQL->write("insert into template (templateId, name, template, namespace) values ($templateId, + ".quote($data->{title}).",".quote($final).",'SQLReport')"); + } else { + $templateId = 1; + } + WebGUI::SQL->write("insert into wobject set templateId=$templateId where wobjectId=".$data->{wobjectId}); +} +$a->finish; +WebGUI::SQL->write("alter table SQLReport drop column template"); + + + #-------------------------------------------- print "\tUpdating config file.\n" unless ($quiet); my $pathToConfig = '../../etc/'.$configFile; diff --git a/docs/upgrades/upgrade_5.5.3-6.0.0.sql b/docs/upgrades/upgrade_5.5.3-6.0.0.sql index 88356e34d..29749b3bd 100644 --- a/docs/upgrades/upgrade_5.5.3-6.0.0.sql +++ b/docs/upgrades/upgrade_5.5.3-6.0.0.sql @@ -175,4 +175,14 @@ INSERT INTO template VALUES (2,'Horizontal Login Box','
\ INSERT INTO template VALUES (2,'DHTML Admin Bar','\n\n\n\n
\n','Macro/AdminBar'); INSERT INTO template VALUES (1,'Default Admin Bar',' \n \n
\n\n \n \n \n \n \n
\n \n
\n \n
\n \n
\n
\n','Macro/AdminBar'); delete from international where internationalId=34 and namespace='WebGUI'; +delete from international where internationalId=19 and namespace='SQLReport'; +delete from international where internationalId=21 and namespace='SQLReport'; +delete from international where internationalId=5 and namespace='SQLReport'; +delete from international where internationalId=6 and namespace='SQLReport'; +delete from international where internationalId=7 and namespace='SQLReport'; +delete from international where internationalId=9 and namespace='SQLReport'; +update international set internationalId=1075, namespace='WebGUI' where internationalId=20 and namespace='SQLReport'; +insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (1076,1,'WebGUI','WebGUI Database', 1070899134,'A label indicating the the user will be selecting the default WebGUI database.'); +alter table SQLReport change column databaseLinkId databaseLinkId int not null default 0; +INSERT INTO template VALUES (1,'Default SQL Report','\n

\n
\n\n\n

\n\n\n\n

    \n \n
  • \n
    \n
\n
\n\n\n\n \n \n \n\n\n \n \n \n \n \n\n
\n\n\n
\n \n
\n
','SQLReport'); diff --git a/lib/WebGUI/DatabaseLink.pm b/lib/WebGUI/DatabaseLink.pm index 8aa8a704e..5bb4c8247 100644 --- a/lib/WebGUI/DatabaseLink.pm +++ b/lib/WebGUI/DatabaseLink.pm @@ -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; } diff --git a/lib/WebGUI/ErrorHandler.pm b/lib/WebGUI/ErrorHandler.pm index f235b0e14..af31af820 100644 --- a/lib/WebGUI/ErrorHandler.pm +++ b/lib/WebGUI/ErrorHandler.pm @@ -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/\/g; + $audit =~ s/\n/\\n/g; return '
'.$audit.'
'; } @@ -281,7 +281,7 @@ Returns an HTML formatted message with the security messages for display during sub showSecurity { my $security = getSecurity(); - $security =~ s/\n/\/g; + $security =~ s/\n/\\n/g; return '
'.$security.'
'; } @@ -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/\/g; + $data =~ s/\n/\\n/g; return '
'.$data.'
'; } @@ -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/\/g; + $st =~ s/\n/\\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/\/g; + $warning =~ s/\n/\\n/g; return '
'.$warning.'
'; } diff --git a/lib/WebGUI/Form.pm b/lib/WebGUI/Form.pm index 08d5ffb88..63b03f97d 100644 --- a/lib/WebGUI/Form.pm +++ b/lib/WebGUI/Form.pm @@ -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 ) diff --git a/lib/WebGUI/HTMLForm.pm b/lib/WebGUI/HTMLForm.pm index 57c650d15..a495f4c70 100644 --- a/lib/WebGUI/HTMLForm.pm +++ b/lib/WebGUI/HTMLForm.pm @@ -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 = ''.WebGUI::International::get(0).' / '; + #} + $subtext .= '' + .WebGUI::International::get(981).''; + } + $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 ] ) diff --git a/lib/WebGUI/Operation/DatabaseLink.pm b/lib/WebGUI/Operation/DatabaseLink.pm index a247af8fa..cd5dbab3c 100644 --- a/lib/WebGUI/Operation/DatabaseLink.pm +++ b/lib/WebGUI/Operation/DatabaseLink.pm @@ -126,6 +126,8 @@ sub www_listDatabaseLinks { $output = helpIcon(68); $output .= '

'.WebGUI::International::get(996).'

'; $sth = WebGUI::SQL->read("select * from databaseLink order by title"); + $row[$i] = ''.WebGUI::International::get(1076).''; + $i++; while (%data = $sth->hash) { $row[$i] = '' .deleteIcon('op=deleteDatabaseLink&dlid='.$data{databaseLinkId}) diff --git a/lib/WebGUI/Paginator.pm b/lib/WebGUI/Paginator.pm index b56c2cb99..40c154aef 100644 --- a/lib/WebGUI/Paginator.pm +++ b/lib/WebGUI/Paginator.pm @@ -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; diff --git a/lib/WebGUI/Wobject/SQLReport.pm b/lib/WebGUI/Wobject/SQLReport.pm index 9944d3b83..385a166d4 100644 --- a/lib/WebGUI/Wobject/SQLReport.pm +++ b/lib/WebGUI/Wobject/SQLReport.pm @@ -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)) ? ''.WebGUI::International::get(981).'' : "" - ); - $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."

" 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")).'

' 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.'

' 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] = ''; - $template[1] = ''; - foreach $col ($sth->getColumnNames) { - $template[0] .= ''; - $template[1] .= ''; - $i++; - } - $template[0] .= ''; - $template[1] .= ''; - $template[2] = '
'.$col.'^'.$i.';
'; - $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/\/g; - } - $row[$i] = $temp; - $i++; - } - if ($sth->rows < 1) { - $output .= $template[2]; - $output .= WebGUI::International::get(18,$_[0]->get("namespace")).'

'; - } 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")).'

' 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")).'

' 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); }