None available.
sub __processNewQuery
{ my ($self, $query) = @_;
$self->set('batched_filterlist', undef);
$self->set('sql', undef);
$self->set('batch_filterSQL', undef);
$self->set('batch_rows_processed', 0);
}
} |
sub _fillAttributeTableWith
{ my ($self, $query, $table, $batch_start,$batch_size) = @_;
if ($self->get('batched_filterlist')) {
$batch_start = $self->get('batch_rows_processed');
}
my $counter = 0;
my $rows_added = 0;
if ($self->serverType eq "web"){
my $filters = $query->getAllFilters;
foreach my $filter (@$filters){
if ($filter->isa("BioMart::Configuration::FilterList")){
if ($filter->batching) {
$self->set('batch_rows_processed', 0);
$batch_start = $self->get('batch_rows_processed');
$self->_setExhausted($filter->exhausted);
$self->set('batched_filterlist', $filter);
}
else{
if (!$self->get('explicit_batching') &&
!$self->get('batched_filterlist')) {
$batch_start = 0;
$batch_size = 0;
}
}
}
}
if (!$self->get('explicit_batching') &&
!$self->get('batched_filterlist')) {
$batch_start = 0;
$batch_size = 0;
}
my $location = $self->getParam('configurator')->get('location');
my $xml = $query->toXML($batch_start,$batch_size,0);
my $logger=Log::Log4perl->get_logger(__PACKAGE__);
$logger->debug("QUERY XML: $xml");
foreach my $el($location->getResultSet("","POST",$xml)){
if ($el =~ /^\s/) {next;}
$rows_added++;
$el .= "\tend";
my @clean=split(/\t/,$el);
$table->addRow([@clean[0..scalar(@clean)-2]]);
}
return $rows_added;
}
else { my $oracle = 0;
if ($self->getParam('configurator')->get('location')->
databaseType eq 'oracle'){
$oracle = 1;
}
my $sql= $self->_generateSQL($query, $batch_start, $batch_size,$oracle);
if ($oracle && $self->get('batched_filterlist')) {
$batch_start = $self->get('batch_rows_processed');
}
my $dbh = $self->_getDBH;
my $batch;
eval {
my $sth = $dbh->prepare($sql);
$sth->execute;
$batch = $sth->fetchall_arrayref;
$sth->finish;
};
BioMart::Exception::Database->throw("Error during query execution: ".
$dbh->errstr."\n") if $@;
$dbh->disconnect;
foreach my $row (@{$batch}){
$counter++;
if ($oracle){
if ( $counter > $batch_start ) {
$table->addRow($row);
$rows_added++;
}
} else{
$table->addRow($row);
$rows_added++;
}
}
}
return $rows_added; } |
sub _generateSQL
{ my ($self, $query, $batch_start, $batch_size, $oracle) = @_;
my $sql = $self->get('sql');
unless ($sql) {
my ($select, $from, $where, $orderby, $comma, $and) = ('')x6;
my %tables;
my %joinTables;
my $schema;
my $attributes = $query->getAllAttributes;
foreach my $attribute (@$attributes){
if($attribute->table ne "main"){
$schema=$self->schema.".";
}
else{
$schema="";
}
$select .= $comma.$schema.$attribute->toSQL;
my $table = $attribute->table;
$tables{$table} = 1;
if ($table eq 'main'){
my $keys = $self->get('keys');
foreach my $key (reverse @$keys){
last if (uc($joinTables{'main'}) eq uc($key));
if (uc($attribute->key) eq uc($key)){
$joinTables{'main'} = $key;
last;
}
}
}
else{ $joinTables{$self->schema.".".$table} = $attribute->key;
}
$comma = ', ';
}
my $filters = $query->getAllFilters;
foreach my $filter (@$filters){
if ($filter->isa("BioMart::Configuration::FilterList")
|| $filter->isa("BioMart::Configuration::FilterList_List") ){
if ($filter->batching) {
$self->set('batched_filterlist', $filter);
$where .= $and.REPLACEBFILTER;
}
else {
$where .= $and.$filter->toSQL($oracle);
}
my $list_filters = $filter->getAllFilters;
foreach my $list_filter (@$list_filters){
my $table = $list_filter->table;
$tables{$table} = 1;
if ($table eq 'main'){
my $keys = $self->get('keys');
foreach my $key (reverse @$keys){
last if (uc($joinTables{'main'}) eq uc($key));
if (uc($list_filter->attribute->key) eq uc($key)){
$joinTables{'main'} = $key;
last;
}
}
}
else{ $joinTables{$table} = $list_filter->attribute->key;
}
}
}
else{ $where .= $and.$filter->toSQL($oracle);
my $table = $filter->table;
$tables{$table} = 1;
if ($table eq 'main'){
my $keys = $self->get('keys');
foreach my $key (reverse @$keys){
last if (uc($joinTables{'main'}) eq uc($key));
if (uc($filter->attribute->key) eq uc($key)){
$joinTables{'main'} = $key;
last;
}
}
}
else{ $joinTables{$table} = $filter->attribute->key;
}
}
$and = ' AND ';
}
my ($main,$i);
if (%joinTables){
my $keys = $self->get('keys');
$i = scalar @$keys - 1;
OUTER:foreach my $key (reverse @$keys){
foreach my $join_table (keys %joinTables){
if (uc($joinTables{$join_table}) eq uc($key)){
last OUTER;
}
}
$i--;
}
}
else{
$i = 0; }
my $mains = $self->get('mains');
$main = $$mains[$i];
my $ct = $self->getConfigurationTree($query->
getInterfaceForDataset($self->name));
$comma = '';
my $subselect;
my $attribute_lists = $query->getAllAttributeLists;
foreach my $attribute_list (reverse @$attribute_lists){
$subselect = '';
my $attributeString = $attribute_list->attributeString;
my @attributeNames = split(/,/,$attributeString);
foreach my $attributeName (@attributeNames){
my $keys = $self->get('keys');
my $attribute;
my $j = $i; while ($j > -1){
my $key = $$keys[$j];
$attribute = $ct->getAttributeByNameKey($attributeName,
$key);
last if ($attribute);
$j--;
}
if (!$attribute){
$j = $i + 1;
my $keys = $self->get('keys');
while ($j <= (scalar (@$keys - 1))){
my $key = $$keys[$j];
$attribute = $ct->getAttributeByNameKey($attributeName,
$key);
if ($attribute){
$main = $$mains[$j];
last;
}
$j++;
}
}
if (!$attribute){
$attribute = $ct->getAttributeByName($attributeName);
}
if (!$attribute){
$attribute = $attribute_list->
getAttributeByName($attributeName);
}
$subselect .= $comma.$attribute->toSQL;
my $table = $attribute->table;
$tables{$table} = 1;
if ($table eq 'main'){
my $keys = $self->get('keys');
my $k = scalar @$keys - 1;
foreach my $key (reverse @$keys){
last if (uc($joinTables{'main'}) eq uc($key));
if (uc($attribute->key) eq uc($key)){
$joinTables{'main'} = $key;
$main = $$mains[$k];
last;
}
$k--;
}
}
else{ $joinTables{$table} = $attribute->key;
}
$comma = ', ';
}
if ($select){ $select = $subselect.$comma.$select;
}
else{
$select = $subselect;
}
$comma = '';
my $orderByString = $attribute_list->orderByString;
my @orderByAttNames;
@orderByAttNames = split( /\,/, $orderByString)
if ($orderByString);
foreach my $attributeName (@orderByAttNames){
my $keys = $self->get('keys');
my $attribute;
my $j = $i; while ($j > -1){
my $key = $$keys[$j];
$attribute = $ct->getAttributeByNameKey($attributeName,
$key);
last if ($attribute);
$j--;
}
if (!$attribute){
$j = $i + 1;
my $keys = $self->get('keys');
while ($j <= (scalar (@$keys - 1))){
my $key = $$keys[$j];
$attribute = $ct->getAttributeByNameKey($attributeName,
$key);
last if ($attribute);
$j++;
}
}
$orderby .= $comma.$attribute->toSQL;
$comma = ', ';
}
$comma = '';
}
if ($query->orderBy()) {
unless ($orderby) {
my @orderByNames = map { $_->name } @{$query->orderBy()};
foreach my $attributeName (@orderByNames){
my $keys = $self->get('keys');
my $attribute;
my $j = $i; while ($j > -1){
my $key = $$keys[$j];
$attribute = $ct->getAttributeByNameKey($attributeName,
$key);
last if ($attribute);
$j--;
}
if (!$attribute){
$j = $i + 1;
my $keys = $self->get('keys');
while ($j <= (scalar (@$keys - 1))){
my $key = $$keys[$j];
$attribute = $ct->getAttributeByNameKey(
$attributeName,$key);
last if ($attribute);
$j++;
}
}
$orderby .= $comma.$attribute->toSQL;
$comma = ', ';
}
}
}
if (%joinTables){
my $keys = $self->get('keys');
$i = scalar @$keys - 1;
OUTER:foreach my $key (reverse @$keys){
foreach my $join_table (keys %joinTables){
if (uc($joinTables{$join_table}) eq uc($key)){
last OUTER;
}
}
$i--;
}
}
else{
$i = 0; }
$mains = $self->get('mains');
$main = $$mains[$i];
foreach my $table (keys %tables){
if ($table ne "main"){
$from .= $self->schema.".".$table.', ';
}
}
$from .= $self->schema.".".$main.' main';
foreach my $join_table (keys %joinTables){
next if $join_table eq "main";
$where .= $and."main.".$joinTables{$join_table}."=".$join_table
.".".$joinTables{$join_table};
$and = ' AND ';
}
$sql = 'SELECT '.$select.' FROM '.$from;
if ($where){
$sql .= ' WHERE '.$where;
}
my $restricted_pk = $self->getConfigurationTree($query->
getInterfaceForDataset($self->name))->primaryKeyRestriction;
if ($restricted_pk){
my $or = '';
my $key = ${$self->get('keys')}[0];
my $restrictedSQL = '(';
my @restrictions = split(/,/,$restricted_pk);
foreach(@restrictions){
my ($start_restriction,$end_restiction) = split(/\-/,$_);
$restrictedSQL .= $or.'main.'.$key.' BETWEEN '.
$start_restriction.' AND '.$end_restiction;
$or = ' OR ';
}
$restrictedSQL .= ')';
if ($where){
$sql .= ' AND '.$restrictedSQL;
}
else{
$sql .= ' WHERE '.$restrictedSQL;
$where = $restrictedSQL;
}
}
if ($self->get('explicit_batching') ||
$self->get('batched_filterlist')) {
if ($oracle){
if ($orderby) {
$sql .= ' ORDER BY '.$orderby;
my @origfields = split ", ",$select;
my %saw;
my @unique_qualified_fields =
grep (!$saw{$_}++, @origfields );
my $uniqueselect = join ", ", @unique_qualified_fields;
$sql =~ s/$select/$uniqueselect/g;
my @unqualified_fields =
map { $_ =~ s/[^\.]+\.//g;$_; } @origfields;
my $unqualified_select = join ", ", @unqualified_fields;
$sql = "SELECT $unqualified_select FROM ($sql) WHERE ".
REPLACELIMIT;
}
elsif ($where){
$sql .= ' AND '.REPLACELIMIT;
}
else{
$sql .= ' WHERE '.REPLACELIMIT;
}
} else{ if ($orderby) {
$sql .= ' ORDER BY '.$orderby;
}
$sql .= REPLACELIMIT;
}
}
$self->set('sql', $sql);
}
my $batched_filterlist = $self->get('batched_filterlist');
if ($batched_filterlist) {
my $sub = $self->get('batch_filterSQL');
unless ($sub) {
$sub = $batched_filterlist->toSQL($oracle);
$self->set('batch_filterSQL', $sub);
$self->set('batch_rows_processed', 0);
}
$batch_start = $self->get('batch_rows_processed');
my $replace = REPLACEBFILTER;
$sql =~ s/$replace/$sub/;
$self->_setExhausted($batched_filterlist->exhausted);
$self->set('batched_filterlist', $batched_filterlist);
}
if ($self->get('explicit_batching') || $batched_filterlist) {
my $limit;
if ($oracle) {
my $rownum_limit = $batch_size + $batch_start + 1;
$limit = ' rowNum < '.$rownum_limit;
}
elsif ($self->getParam('configurator')->get('location')->databaseType
eq 'postgres'){
$limit = ' LIMIT ';
if ($batch_start){
$limit .= $batch_size.' OFFSET ';
$limit .= $batch_start;
}
else {
$limit .= $batch_size;
}
}
elsif ($self->getParam('configurator')->get('location')->databaseType
eq 'mysql'){
$limit = ' LIMIT ';
if ($batch_start){
$limit .= $batch_start.',';
}
$limit .= $batch_size;
}
else {
BioMart::Exception::Query->throw("Unsupported RDBMS type: ".$self->getParam('configurator')->get('location')->databaseType ."Currently supported: mysql, oracle and postgres");
}
my $replace = REPLACELIMIT;
$sql =~ s/$replace/$limit/;
}
my $logger=Log::Log4perl->get_logger(__PACKAGE__);
$logger->info("QUERY SQL: $sql");
return $sql; } |
sub _getConfigurationTree
{ my ($self,$interface,$dsCounter)=@_;
return $self->getParam('configurator')->getConfigurationTree(
$self->virtualSchema, $self->name, $interface,$dsCounter); } |
sub _getCount
{
my ($self, @param) = @_;
my $ret;
my $batching;
local($^W) = 0; my(%param) = @param;
my $query = $param{'query'};
$self->_processNewQuery($query);
if ($self->serverType eq "web"){
my $location = $self->getParam('configurator')->get('location');
my $xml = $query->toXML(0,0,1);
my $logger=Log::Log4perl->get_logger(__PACKAGE__);
$logger->info("COUNT XML: $xml");
my @results = $location->getResultSet("","POST",$xml);
return $results[0];
}
my ($sql, $select, $from, $where, $limit, $comma, $and) = ('')x7;
my %tables;
my %joinTables;
my $oracle = 0;
if ($self->getParam('configurator')->get('location')->databaseType eq
'oracle'){
$oracle = 1;
}
$select = 'COUNT(*)';
my $filtList_List_flag = 0;
my $filters = $query->getAllFilters;
FILTERS: foreach my $filter (@$filters){
$where .= $and.$filter->toSQL($oracle);
if ($filter->isa("BioMart::Configuration::FilterList")
|| $filter->isa("BioMart::Configuration::FilterList_List")){
if ($filter->isa("BioMart::Configuration::FilterList_List")){
$filtList_List_flag = 1;
}
if ($filter->batching) {
$ret = 1;
$batching = 1;
last FILTERS;
}
my $list_filters = $filter->getAllFilters;
foreach my $list_filter (@$list_filters){
my $table = $list_filter->table;
$tables{$table} = 1;
if (!(($table =~ /main$/) && ($list_filter->attribute->key eq
($self->get('keys')->[0])))){
$joinTables{$table} = $list_filter->attribute->key;
}
}
}
else{
my $table = $filter->table;
$tables{$table} = 1;
if (!(($table =~ /main$/) && ($filter->attribute->key eq
($self->get('keys')->[0])))){
$joinTables{$table} = $filter->attribute->key;
}
}
$and = ' AND ';
}
if ($batching) {
return [ $ret ];
}
my ($main,$i);
my $keys = $self->get('keys');
if (%joinTables){
$i = scalar @$keys - 1;
OUTER:foreach my $key (reverse @$keys){
foreach my $join_table (keys %joinTables){
if (uc($joinTables{$join_table}) eq uc($key)){
last OUTER;
}
}
$i--;
}
}
else{
$i = 0; }
my $mains = $self->get('mains');
$main = $$mains[$i];
if ($i != 0 || $filtList_List_flag){
$select = 'COUNT(DISTINCT main.'.$$keys[0].')';
}
foreach my $table (keys %tables){
if ($table !~ /main$/){
$from .= $self->schema.".".$table.', ';
}
}
$from .= $self->schema.".".$main.' main';
foreach my $join_table (keys %joinTables){
next if $join_table eq "main";
$where .= $and."main.".$joinTables{$join_table}."=".$join_table.".".
$joinTables{$join_table};
$and = ' AND ';
}
$sql = 'SELECT '.$select.' FROM '.$from;
if ($where){
$sql .= ' WHERE '.$where;
}
if ($limit){
$sql .= ' LIMIT '.$limit;
}
my $restricted_pk = $self->getConfigurationTree($query->
getInterfaceForDataset($self->name))->primaryKeyRestriction;
if ($restricted_pk){
my $or = '';
my $key = ${$self->get('keys')}[0];
my $restrictedSQL = '(';
my @restrictions = split(/,/,$restricted_pk);
foreach(@restrictions){
my ($start_restriction,$end_restiction) = split(/\-/,$_);
$restrictedSQL .= $or.'main.'.$key.' BETWEEN '.$start_restriction.
' AND '.$end_restiction;
$or = ' OR ';
}
$restrictedSQL .= ')';
if ($where){
$sql .= ' AND '.$restrictedSQL;
}
else{
$sql .= ' WHERE '.$restrictedSQL;
$where = $restrictedSQL;
}
}
my $logger=Log::Log4perl->get_logger(__PACKAGE__);
$logger->info("COUNT SQL: $sql");
my $dbh = $self->_getDBH;
my $sth = $dbh->prepare($sql);
unless ($sth) {
BioMart::Exception::Database->throw("Couldnt connect to Database: ".
$dbh->errstr."\n");
}
$sth->{RaiseError} = 0;
$sth->execute || warn($sth->errstr);
$ret = ${$sth->fetchrow_arrayref}[0];
$sth->finish;
$dbh->disconnect;
return $ret;
}
1; } |
sub _getResultTable
{
my ($self, @param) = @_;
local($^W) = 0; my(%param) = @param;
my $table = $param{'table'};
my $query = $param{'query'};
my $batch_start = $param{'batch_start'} || 0;
my $batch_size = $param{'batch_size'};
my $rows_added = $self->_fillAttributeTableWith($query, $table,
$batch_start,$batch_size);
$self->_setExhausted(1) unless ($self->get('explicit_batching') ||
$self->get('batched_filterlist'));
if ($rows_added < 1) {
if ($self->get('explicit_batching')) {
$self->_setExhausted(1);
return undef;
}
else {
$self->set('batch_filterSQL', undef);
}
}
elsif ($self->get('batched_filterlist')) {
my $batch_rows_processed = $self->get('batch_rows_processed');
$batch_rows_processed += $rows_added;
$self->set('batch_rows_processed', $batch_rows_processed);
}
return $table; } |
sub _new
{
my ($self, @param) = @_;
$self->SUPER::_new(@param);
$self->attr('mains',[]);
$self->attr('keys',[]);
$self->attr('batch_size', 0);
$self->attr('batched_filterlist', undef);
$self->attr('sql', undef);
$self->attr('batch_filterSQL', undef);
$self->attr('batch_rows_processed', 0);
}
} |