From ffb0268f5c1dfca0c0f56b89e46d2a01ef116f59 Mon Sep 17 00:00:00 2001 From: lissav Date: Tue, 22 Mar 2011 18:45:45 +0000 Subject: [PATCH] Start of change to getAllAttribsWhere to support input array, testing still to be done git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@9130 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT/xCAT/Table.pm | 119 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/perl-xCAT/xCAT/Table.pm b/perl-xCAT/xCAT/Table.pm index 9f4a65fcc..235022273 100644 --- a/perl-xCAT/xCAT/Table.pm +++ b/perl-xCAT/xCAT/Table.pm @@ -2642,14 +2642,20 @@ sub getAllEntries Description: Get all attributes with "where" clause - Warning, because we support mulitiple databases (SQLite,MySQL and DB2) that + When using a general Where clause with SQL statement then + because we support mulitiple databases (SQLite,MySQL and DB2) that require different syntax. Any code using this routine, must call the Utils->getDBName routine and code the where clause that is appropriate for each supported database. + When the input is the array of attr val strings, the routine will + build the correct Where clause for the database we are running. + Arguments: Database Handle Where clause + or + array of attrval strings to be build into a Where clause Returns: Array of attributes Globals: @@ -2657,10 +2663,20 @@ sub getAllEntries Error: Example: + General Where clause: + $nodelist->getAllAttribsWhere("groups like '%".$atom."%'",'node','group'); returns node and group attributes $nodelist->getAllAttribsWhere("groups like '%".$atom."%'",'ALL'); returns all attributes + + Input of attrval strings + + $nodelist->getAllAttribsWhere(array of attr val,'node','group'); + returns node and group attributes + $nodelist->getAllAttribsWhere(array of attr val,'ALL'); + returns all attributes + Comments: none @@ -2675,11 +2691,18 @@ sub getAllAttribsWhere if ($dbworkerpid) { return dbc_call($self,'getAllAttribsWhere',@_); } - my $whereclause = shift; + my $clause = shift; + my $whereclause; my @attribs = @_; my @results = (); my $query; my $query2; + if(ref($clause) eq 'ARRAY'){ + $whereclause = &buildWhereClause($clause); + } else { + $whereclause = $clause; + } + # delimit the disable column based on the DB my $disable= &delimitcol("disable"); @@ -3570,5 +3593,97 @@ sub delimitcol { } return $attrout; } +#-------------------------------------------------------------------------- + +=head3 + + Description: buildwhereclause + + Arguments: + Array of the following + attr val where the operator can be the following: + == + != + =~ + !~ + > + < + >= + <= + + Returns: + Where clause with SQL appropriate for the running DB + Globals: + + Error: + + Example: + + my $whereclause=buildWhereClause(@array); + +=cut + +#-------------------------------------------------------------------------------- +sub buildWhereClause { + my $attrvalstr=shift; # array of atrval strings + my $whereclause; # Where Clause + my $firstpass=1; + foreach my $m (@{$attrvalstr}) + { + my $attr; + my $val; + my $operator; + if ($firstpass == 1) { # first pass no AND + $firstpass = 0; + } else { # add an AND + $whereclause .=" AND "; + } + + if ($m =~ /^[^=]*\==/) { #attr==val + ($attr, $val) = split /==/,$m,2; + $operator=' = '; + } elsif ($m =~ /^[^=]*=~/) { #attr=~val + ($attr, $val) = split /=~/,$m,2; + $val =~ s/^\///; + $val =~ s/\/$//; + $operator=' like '; + } elsif ($m =~ /^[^=]*\!=/) { #attr!=val + ($attr,$val) = split /!=/,$m,2; + $operator=' != '; + } elsif ($m =~ /[^=]*!~/) { #attr!~val + ($attr,$val) = split /!~/,$m,2; + $val =~ s/^\///; + $val =~ s/\/$//; + $operator=' not like '; + } elsif ($m =~ /^[^=]*\<=/) { #attr<=val + ($attr, $val) = split /<=/,$m,2; + $operator=' <= '; + } elsif ($m =~ /^[^=]*\=/) { #attr>=val + ($attr, $val) = split />=/,$m,2; + $operator=' >= '; + } elsif ($m =~ /^[^=]*\>/) { #attr>val + ($attr, $val) = split />/,$m,2; + $operator=' > '; + } else { + xCAT::MsgUtils->message("S", "Unsupported operator:$m on -w flag input, could not build a Where Clause."); + $whereclause=""; + return $whereclause; + } + my $delimitedattr= &delimitcol($attr); + $whereclause .=$delimitedattr; + $whereclause .=$operator; + #$whereclause .="(\'"; + $whereclause .="\'"; + $whereclause .=$val; + #$whereclause .="\')"; + $whereclause .="\'"; + + } + return $whereclause; + +} 1;