diff --git a/perl-xCAT/xCAT/Table.pm b/perl-xCAT/xCAT/Table.pm index 0e5e66271..b3cad1933 100644 --- a/perl-xCAT/xCAT/Table.pm +++ b/perl-xCAT/xCAT/Table.pm @@ -310,8 +310,12 @@ sub handle_dbc_request { return $opentables{$tablename}->{$autocommit}->getAllNodeAttribs(@args); } elsif ($functionname eq 'getAllEntries') { return $opentables{$tablename}->{$autocommit}->getAllEntries(@args); + } elsif ($functionname eq 'writeAllEntries') { + return $opentables{$tablename}->{$autocommit}->writeAllEntries(@args); } elsif ($functionname eq 'getAllAttribsWhere') { return $opentables{$tablename}->{$autocommit}->getAllAttribsWhere(@args); + } elsif ($functionname eq 'writeAllAttribsWhere') { + return $opentables{$tablename}->{$autocommit}->writeAllAttribsWhere(@args); } elsif ($functionname eq 'addAttribs') { return $opentables{$tablename}->{$autocommit}->addAttribs(@args); } elsif ($functionname eq 'setAttribs') { @@ -3695,5 +3699,198 @@ sub buildWhereClause { return $whereclause; } +#-------------------------------------------------------------------------- + +=head3 writeAllEntries + + Description: Read entire table and writes all entries to file + This routine was written specifically for the tabdump + command. + + Arguments: + filename or path + + Returns: + 0=good + 1=bad + Globals: + + Error: + + Example: + + my $tabh = xCAT::Table->new($table); + my $recs=$tabh->writeAllEntries($filename); + + Comments: + none + +=cut + +#-------------------------------------------------------------------------------- +sub writeAllEntries +{ + my $self = shift; + if ($dbworkerpid) { + return dbc_call($self,'writeAllEntries',@_); + } + my $filename = shift; + my $fh; + my $rc; + # open the file for write + unless (open($fh," > $filename")) { + my $msg="Unable to open $filename for write \n."; + `logger -t xcat $msg`; + return 1; + } + my $query; + my $header; + my $tabdump_header = sub { + $header = "#" . join(",", @_); + }; + $tabdump_header->(@{$self->{colnames}}); + # write the header to the file + print $fh $header; # write line to file + print $fh "\n"; + + # delimit the disable column based on the DB + my $disable= &delimitcol("disable"); + $query = $self->{dbh}->prepare('SELECT * FROM ' . $self->{tabname}); + + $query->execute(); + while (my $data = $query->fetchrow_hashref()) + { + foreach (keys %$data) + { + if ($data->{$_} =~ /^$/) + { + $data->{$_} = undef; + } + } + $rc=output_table($self->{tabname},$fh,$self,$data); + } + $query->finish(); + close $fh; + return $rc; +} + +#-------------------------------------------------------------------------- + +=head3 writeAllAttribsWhere + + Description: writes all attributes to file using the "where" clause + written for the tabdump command + + + Arguments: + array of attrval strings to be build into a Where clause + filename or path + Returns: + Outputs to filename the table header and rows + Globals: + + Error: + + Example: + + $nodelist->getAllAttribsWhere(array of attrval,$filename); + where operator can be + (==,!=,=~,!~, >, <, >=,<=) + + + + Comments: + none + +=cut + +#-------------------------------------------------------------------------------- +sub writeAllAttribsWhere +{ + + #Takes a list of attributes, returns all records in the table. + my $self = shift; + if ($dbworkerpid) { + return dbc_call($self,'writeAllAttribsWhere',@_); + } + my $clause = shift; + my $filename = shift; + my $whereclause; + my @attribs = @_; + my @results = (); + my $query; + my $query2; + my $fh; + my $rc; + # open the file for write + unless (open($fh," > $filename")) { + my $msg="Unable to open $filename for write \n."; + `logger -t xcat $msg`; + return 1; + } + my $header; + my $tabdump_header = sub { + $header = "#" . join(",", @_); + }; + $tabdump_header->(@{$self->{colnames}}); + # write the header to the file + print $fh $header; # write line to file + print $fh "\n"; + $whereclause = &buildWhereClause($clause); + + + # delimit the disable column based on the DB + my $disable= &delimitcol("disable"); + $query2='SELECT * FROM ' . $self->{tabname} . ' WHERE (' . $whereclause . ") and ($disable is NULL or $disable in ('0','no','NO','No','nO'))"; + $query = $self->{dbh}->prepare($query2); + $query->execute(); + while (my $data = $query->fetchrow_hashref()) + { + foreach (keys %$data){ + + if ($data->{$_} =~ /^$/) + { + $data->{$_} = undef; + } + } + $rc=output_table($self->{tabname},$fh,$self,$data); + } + $query->finish(); + close $fh; + return $rc ; +} +#-------------------------------------------------------------------------- + +=head3 output_table + + Description: writes table rows to file + written for the tabdump command + +=cut + +#-------------------------------------------------------------------------------- +sub output_table { + my $table = shift; + my $fh = shift; + my $tabh=shift; + my $rec=shift; + my $line = ''; + foreach (@{$tabh->{colnames}}) + { + if (defined $rec->{$_}) + { + $rec->{$_} =~ s/"/""/g; + $line = $line . '"' . $rec->{$_} . '",'; + } + else + { + $line .= ','; + } + } + $line =~ s/,$//; # remove the extra comma at the end + print $fh $line; # write line to file + print $fh "\n"; + return 0; +} 1;