rdbstats  2.0.7
SelectedCols.cc
1 // File: SelectedCols.cc
2 
3 // --8<--8<--8<--8<--
4 //
5 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
6 //
7 // This file is part of rdbstats
8 //
9 // rdbstats is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // rdbstats is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the
21 // Free Software Foundation, Inc.
22 // 51 Franklin Street, Fifth Floor
23 // Boston, MA 02110-1301, USA
24 //
25 // -->8-->8-->8-->8--
26 
27 #include <iterator>
28 
29 #include "SelectedCols.h"
30 
31 SelectedCols::SelectedCols( RDB& rdbtable,
32  const std::vector< std::string >& columns,
33  const std::vector<std::string>& group,
34  const std::vector<std::string>& override
35  ) {
36 
37  // select all columns, except group columns
38  if ( columns.empty() ) {
39 
40  // --group a -- group b
41  std::vector< std::string > groupies;
42  std::vector< std::string >::const_iterator current_group( group.begin( ) ),
43  end_group( group.end( ) );
44  for ( ; current_group != end_group; ++current_group ) {
45  // --group a,b,c may have been entered, so must parse entry.
46  suplib::tok( groupies, *current_group, "," );
47  }
48 
49  //
50  // Loop through all rdb column and select the numeric columns only.
51  //
52  size_t num = rdbtable.nColumns( );
53 
54  for ( size_t ii = 0; ii < num; ii++ ) {
55 
56  std::string name = rdbtable.getColumn( ii )->getName( );
57 
58  if ( true == is_column_numeric( name, rdbtable, override ) &&
59  false == is_groupie( name, groupies ) )
60  selected_cols.push_back( name );
61 
62  }
63 
64  }
65 
66  // select the specified columns, provided they are numeric
67  else {
68  for ( int ii = 0; ii < columns.size( ); ii++ )
69  if ( true == is_column_numeric( columns[ ii ], rdbtable, override ) )
70  selected_cols.push_back( columns[ ii ] );
71  }
72 }
73 
74 bool SelectedCols::is_column_numeric( const std::string& colname,
75  RDB& rdbtable ) {
76 
77  RDBColumn::Type mytype = rdbtable.getColumn( colname )->getType( );
78 
79  if ( RDBColumn::NUMERIC == mytype )
80  return true;
81  else
82  return false;
83 }
84 
85 bool SelectedCols::is_column_numeric( const std::string& colname,
86  RDB& rdbtable,
87  const std::vector<std::string>& override ) {
88 
89  bool is_numeric( false );
90 
91  RDBColumn::Type mytype = rdbtable.getColumn( colname )->getType( );
92 
93  if ( RDBColumn::NUMERIC == mytype )
94  is_numeric = true;
95 
96  std::vector< std::string >::const_iterator
97  current_override( override.begin( ) ),
98  end_override( override.end( ) );
99 
100  for ( ; current_override != end_override; ++current_override ) {
101 
102  std::string str( *current_override );
103 
104  //
105  // The user has requested that a column is to change type.
106  //
107  std::vector< std::string > container;
108 
109  suplib::tok( container, str, "," );
110 
111  if ( 0 == colname.compare( container[ 0 ] ) ) {
112 
113  switch( container.size( ) ) {
114 
115  case 1:
116 
117  //
118  // The definition is unspecified so set to
119  // the opposite of the current definition.
120  //
121  toggle_column_definition( colname, rdbtable );
122  return is_column_numeric( colname, rdbtable );
123 
124  case 2:
125 
126  //
127  // Set the column definition to the user specified value.
128  //
129  rdbtable.getColumn( colname )->setDef( container[ 1 ] );
130  return is_column_numeric( colname, rdbtable );
131 
132  default:
133 
134  throw Exception( "Too many options for column " + colname );
135 
136  }
137 
138  }
139 
140  }
141 
142  return is_numeric;
143 
144 }
145 
146 bool SelectedCols::is_groupie( const std::string& name,
147  const std::vector<std::string>& groupies ) {
148 
149  std::vector< std::string >::const_iterator current_group( groupies.begin( ) ),
150  end_group( groupies.end( ) );
151  for ( ; current_group != end_group; ++current_group )
152  if ( 0 == name.compare( *current_group ) )
153  return true;
154 
155  return false;
156 
157 }
158 
159 void SelectedCols::print( std::ostream& os ) const {
160 
161  copy( selected_cols.begin( ), selected_cols.end( ),
162  std::ostream_iterator< std::string >( os, " " ) );
163 
164 }
165 
166 void SelectedCols::toggle_column_definition( const std::string& colname,
167  RDB& rdbtable )
168  const {
169 
170  RDBColumn::Type mytype = rdbtable.getColumn( colname )->getType( );
171  if ( RDBColumn::NUMERIC == mytype )
172  rdbtable.getColumn( colname )->setDef( "S" );
173  else
174  rdbtable.getColumn( colname )->setDef( "N" );
175 
176 
177 }
178