From 38c2e8e06ffb306bae7c372779eacb20d81b7b8f Mon Sep 17 00:00:00 2001 From: lissav Date: Mon, 5 Aug 2013 14:17:48 -0400 Subject: [PATCH] add MAXMIN routine --- perl-xCAT/xCAT/Table.pm | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/perl-xCAT/xCAT/Table.pm b/perl-xCAT/xCAT/Table.pm index 34c73c189..9747262dd 100644 --- a/perl-xCAT/xCAT/Table.pm +++ b/perl-xCAT/xCAT/Table.pm @@ -323,6 +323,8 @@ sub handle_dbc_request { return $opentables{$tablename}->{$autocommit}->getAllNodeAttribs(@args); } elsif ($functionname eq 'getAllEntries') { return $opentables{$tablename}->{$autocommit}->getAllEntries(@args); + } elsif ($functionname eq 'getMAXMINEntries') { + return $opentables{$tablename}->{$autocommit}->getMAXMINEntries(@args); } elsif ($functionname eq 'writeAllEntries') { return $opentables{$tablename}->{$autocommit}->writeAllEntries(@args); } elsif ($functionname eq 'getAllAttribsWhere') { @@ -3988,5 +3990,98 @@ sub output_table { print $fh "\n"; return 0; } +#-------------------------------------------------------------------------- + +=head3 getMAXMINEntries + + Description: Select the rows in the Table which has the MAX and the row with the + Min value for the input attribute. + Currently only the auditlog and evenlog are setup to have such an attribute (recid). + + Arguments: + Table handle + attribute name ( e.g. recid) + + Returns: + HASH + max=> max value + min=> min value + Globals: + + Error: + + Example: + + my $tabh = xCAT::Table->new($table); + my $recs=$tabh->getEntries("recid"); # returns row with recid max value in database + # and the row with the min value. + + Comments: + none + +=cut + +#-------------------------------------------------------------------------------- +sub getMAXMINEntries +{ + my $self = shift; + my $attr = shift; + if ($dbworkerpid) { + return dbc_call($self,'getMAXMINEntries',@_); + } + my $rets; + my $query; + my $xcatcfg=get_xcatcfg(); + + # delimit the disable column based on the DB + my $disable= &delimitcol("disable"); + my $qstring; + if ($xcatcfg =~ /^DB2:/) { # for DB2 + $qstring = "SELECT MAX (\"$attr\") FROM " . $self->{tabname} . " WHERE " . $disable . " is NULL or " . $disable . " in ('0','no','NO','No','nO')"; + } else { + $qstring = "SELECT MAX($attr) FROM " . $self->{tabname} . " WHERE " . $disable . " is NULL or " . $disable . " in ('0','no','NO','No','nO')"; + } + $query = $self->{dbh}->prepare($qstring); + + $query->execute(); + while (my $data = $query->fetchrow_hashref()) + { + foreach (keys %$data) + { + if ($data->{$_} =~ /^$/) + { + $rets->{"max"} = undef; + } else { + $rets->{"max"} = $data->{$_}; + + } + last; # better only be one value for max + + } + } + $query->finish(); + if ($xcatcfg =~ /^DB2:/) { # for DB2 + $qstring = "SELECT MIN (\"$attr\") FROM " . $self->{tabname} . " WHERE " . $disable . " is NULL or " . $disable . " in ('0','no','NO','No','nO')"; + } else { + $qstring = "SELECT MIN($attr) FROM " . $self->{tabname} . " WHERE " . $disable . " is NULL or " . $disable . " in ('0','no','NO','No','nO')"; + } + $query = $self->{dbh}->prepare($qstring); + + $query->execute(); + while (my $data = $query->fetchrow_hashref()) + { + foreach (keys %$data) + { + if ($data->{$_} =~ /^$/) + { + $rets->{"min"} = undef; + } else { + $rets->{"min"} = $data->{$_}; + } + last; # better be only one value for min + } + } + return $rets; +} 1;