# -*- mode: Perl; -*-

package NewsClipper::Handler::Acquisition::sql;

use vars qw( @ISA $VERSION %handlerInfo );

$handlerInfo{'Author_Name'}              = 'Jan Peter Hecking';
$handlerInfo{'Author_Email'}             = 'jhecking@informatik.uni-rostock.de';
$handlerInfo{'Maintainer_Name'}          = 'Jan Peter Hecking';
$handlerInfo{'Maintainer_Email'}         = 'jhecking@informatik.uni-rostock.de';
$handlerInfo{'Description'}              = <<'EOF';
Performs an arbitrary SQL query and returns the result as a HTML table.
EOF
$handlerInfo{'Category'}                 = 'Misc';
$handlerInfo{'URL'}                      = <<'EOF';
http://www.newsclipper.com/
EOF
$handlerInfo{'License'}                  = 'GPL';
$handlerInfo{'For_News_Clipper_Version'} = '1.18';
$handlerInfo{'Language'}                 = 'English';
$handlerInfo{'Notes'}                    = <<'EOF';
EOF
$handlerInfo{'Syntax'}                   = <<'EOF';
<input name=sql driver=sql_driver user=username pass=password query=sql_query>
EOF

use strict;
use NewsClipper::Handler;
@ISA = qw(NewsClipper::Handler);

# - The first number should be incremented when a change is made to the
#   handler that will break people's input files.
# - The second number should be incremented when a change is made that won't
#   break people's input files, but changes the functionality.
# - The third number should be incremented when only a bugfix is applied.

$VERSION = do {my @r=('0.1.0'=~/\d+/g);sprintf "%d."."%02d"x$#r,@r};

# ------------------------------------------------------------------------------

# This function is used to get the raw data from the URL.
sub Get
{
  my $self = shift;
  my $attributes = shift;

  use DBI;

  my $dbh = DBI->connect( @{$attributes}{'driver', 'user', 'pass'} );
  my $tbl_data = $dbh->selectall_arrayref( $attributes->{'query'} );

  my $table = "<table>\n";
  foreach my $row ( @{ $tbl_data } ){
    $table .= "<tr>";
    foreach my $col ( @{ $row } ){
	$table .= "<td>" . $col . "</td>";
    }
    $table .= "</tr>\n";
  }
  $table .= "</table>";

  $dbh->disconnect();

  return \$table;
}

# ------------------------------------------------------------------------------

sub GetDefaultHandlers
{
  my $self = shift;
  my $inputAttributes = shift;

  my $returnVal =<<'  EOF';
    <output name='string'>
  EOF

  return $returnVal;
}

# ------------------------------------------------------------------------------

1;
