CGI-OptimalQuery
view release on metacpan or search on metacpan
lib/CGI/OptimalQuery/Base.pm view on Meta::CPAN
or confess "couldn't find q in schema!";
$$o{output_handler} = $$o{schema}{output_handler};
$$o{error_handler} = $$o{schema}{error_handler};
$$o{httpHeader} = $$o{schema}{httpHeader};
# check for required attributes
confess "specified select is not a hash ref!"
unless ref $$o{schema}{select} eq "HASH";
confess "specified joins is not a hash ref!"
unless ref $$o{schema}{joins} eq "HASH";
# set defaults
$$o{schema}{debug} ||= 0;
$$o{schema}{check} = $ENV{'CGI-OPTIMALQUERY_CHECK'}
if ! defined $$o{schema}{check};
$$o{schema}{check} = 0 if ! defined $$o{schema}{check};
$$o{schema}{title} ||= "";
$$o{schema}{options} ||= {};
$$o{schema}{resourceURI} ||= $ENV{OPTIMALQUERY_RESOURCES} || '/OptimalQuery';
if (! $$o{schema}{URI}) {
$_ = ($$o{q}->can('uri')) ? $$o{q}->uri() : $ENV{REQUEST_URI}; s/\?.*$//;
$$o{schema}{URI} = $_;
# disabled so we can run from command line for testing where REQUEST_URI probably isn't defined
# or die "could not find 'URI' in schema";
}
$$o{schema}{URI_standalone} ||= $$o{schema}{URI};
# make sure developer is not using illegal state_params
if (ref($$o{schema}{state_params}) eq 'ARRAY') {
foreach my $p (@{ $$o{schema}{state_params} }) {
die "cannot use reserved state param name: act" if $p eq 'act';
die "cannot use reserved state param name: module" if $p eq 'module';
die "cannot use reserved state param name: view" if $p eq 'view';
}
}
# construct optimal query object
$$o{oq} = DBIx::OptimalQuery->new(
'dbh' => $$o{schema}{dbh},
'select' => $$o{schema}{select},
'joins' => $$o{schema}{joins},
'named_filters' => $$o{schema}{named_filters},
'named_sorts' => $$o{schema}{named_sorts},
'debug' => $$o{schema}{debug},
'error_handler' => $$o{schema}{error_handler}
);
# the following code is responsible for setting the disable_sort flag for all
# multi valued selects (since it never makes since to sort a m-valued column)
my %cached_dep_multival_status;
my $find_dep_multival_status_i;
my $find_dep_multival_status;
$find_dep_multival_status = sub {
my $joinAlias = shift;
$find_dep_multival_status_i++;
die "could not resolve join alias: $joinAlias deps" if $find_dep_multival_status_i > 100;
if (! exists $cached_dep_multival_status{$joinAlias}) {
my $v;
if (exists $$o{oq}{joins}{$joinAlias}[3]{new_cursor}) { $v = 0; }
elsif (! @{ $$o{oq}{joins}{$joinAlias}[0] }) { $v = 1; }
else { $v = $find_dep_multival_status->($$o{oq}{joins}{$joinAlias}[0][0]); }
$cached_dep_multival_status{$joinAlias} = $v;
}
return $cached_dep_multival_status{$joinAlias};
};
# loop though all selects
foreach my $selectAlias (keys %{ $$o{oq}{select} }) {
$find_dep_multival_status_i = 0;
# set the disable sort flag is select is a multi value
$$o{oq}{select}{$selectAlias}[3]{disable_sort} = 1
if ! $find_dep_multival_status->($$o{oq}{select}{$selectAlias}[0][0]);
# set is_hidden flag if select does not have a nice name assigned
$$o{oq}{select}{$selectAlias}[3]{is_hidden} = 1
if ! $$o{oq}{select}{$selectAlias}[2];
# if no SQL (could be a recview) then disable sort, filter
if (! $$o{oq}{select}{$selectAlias}[1]) {
$$o{oq}{select}{$selectAlias}[3]{disable_sort} = 1;
$$o{oq}{select}{$selectAlias}[3]{disable_filter} = 1;
}
# if a select column has additional select fields specified in options, make sure that the options array is an array
if ($$o{oq}{select}{$selectAlias}[3]{select} && ref($$o{oq}{select}{$selectAlias}[3]{select}) ne 'ARRAY') {
my @x = split /\ *\,\ */, $$o{oq}{select}{$selectAlias}[3]{select};
$$o{oq}{select}{$selectAlias}[3]{select} = \@x;
}
}
# if any fields are passed into on_select, ensure they are always selected
my $on_select = $$o{q}->param('on_select');
if ($on_select =~ /[^\,]+\,(.+)/) {
my @fields = split /\,/, $1;
for (@fields) {
$$o{oq}{'select'}{$_}[3]{always_select}=1
if exists $$o{oq}{'select'}{$_};
}
}
# check schema validity
$$o{oq}->check_join_counts() if $$o{schema}{check} && ! defined $$o{q}->param('module');
# install the export tool
CGI::OptimalQuery::ExportDataTool::activate($o);
# if savedSearchUserID enable savereport and loadreport tools
$$o{schema}{savedSearchUserID} ||= undef;
if ($$o{schema}{savedSearchUserID} =~ /^\d+$/) {
CGI::OptimalQuery::LoadSearchTool::activate($o);
CGI::OptimalQuery::SaveSearchTool::activate($o);
}
# run on_init function for each enabled tool
foreach my $v (values %{ $$o{schema}{tools} }) {
$$v{on_init}->($o) if ref($$v{on_init}) eq 'CODE';
}
( run in 0.755 second using v1.01-cache-2.11-cpan-524268b4103 )