GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoDBRow.h
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #ifndef __GoDBRow_h
36 #define __GoDBRow_h
37 
38 #include "ConvertToStringHelper.h"
39 #include "vtkMySQLDatabase.h"
40 #include <string>
41 #include <sstream>
42 #include <map>
43 #include <vector>
44 
45 #include "QGoIOConfigure.h"
47 #include "QueryDataBaseHelper.h"
48 
55 class QGOIO_EXPORT GoDBRow
56 {
57 public:
58 
59  typedef std::map< std::string, std::string > StringMapType;
60  typedef StringMapType::iterator StringMapIterator;
61  typedef StringMapType::const_iterator StringMapConstIterator;
62 
63  GoDBRow();
64  virtual ~GoDBRow();
65 
72  template< typename T >
73  void SetField(const std::string& key, const T& value)
74  {
75  StringMapIterator it = m_MapRow.find(key);
76 
77  if ( it != m_MapRow.end() )
78  {
79  it->second = ConvertToString< T >(value);
80  }
81  else
82  {
83  std::cerr << "This field does not exist!!!" << std::endl;
84  }
85  }
86 
94  void SetField(const std::string& key, const std::string& value);
95 
100  std::string GetTableName();
101 
106  std::string GetTableIDName();
107 
112  std::string PrintValues();
113 
118  std::string PrintColumnNames();
119 
120 
121  std::vector< std::string > PrintColumnsAndValues();
122 
127  std::vector< std::string > GetVectorColumnNames();
128 
133  std::string PrintColumnNamesWithValues();
134 
139  StringMapConstIterator ConstMapBegin();
140 
145  StringMapConstIterator ConstMapEnd();
146 
151  StringMapIterator MapBegin();
152 
157  StringMapIterator MapEnd();
158 
166  std::string GetMapValue(const std::string& key);
167 
168  template<typename T>
169  T GetMapValue(const std::string& key)
170  {
171  std::string Value = this->GetMapValue(key);
172  return ss_atoi<T>(Value);
173  }
174 
180  friend std::ostream & operator<<(std::ostream & os, const GoDBRow & c)
181  {
182  for ( StringMapConstIterator it = c.m_MapRow.begin();
183  it != c.m_MapRow.end();
184  ++it )
185  {
186  os << it->first << " = " << it->second << std::endl;
187  }
188 
189  return os;
190  }
191 
199  virtual bool SetValuesForSpecificID(int ID, vtkMySQLDatabase *iDatabaseConnector);
200 
205  void DeleteFromDB(vtkMySQLDatabase *iDatabaseConnector);
206 
207 protected:
208 
212  virtual void InitializeMap() = 0;
213 
220  void AddConditions(const std::string& iNameOfField,
221  std::vector<FieldWithValue>& ioFieldWithValue);
222 
224  std::string m_TableName;
225  std::string m_TableIDName;
226 
227 };
228 
229 #endif
T GetMapValue(const std::string &key)
Definition: GoDBRow.h:169
abstract class manages a map with keys matching fields of a gofiguredatabase table and values of the ...
Definition: GoDBRow.h:55
friend std::ostream & operator<<(std::ostream &os, const GoDBRow &c)
print the keys and values of the map in a cout
Definition: GoDBRow.h:180
std::string m_TableIDName
Definition: GoDBRow.h:225
std::map< std::string, std::string > StringMapType
Definition: GoDBRow.h:59
StringMapType::const_iterator StringMapConstIterator
Definition: GoDBRow.h:61
std::string m_TableName
Definition: GoDBRow.h:224
StringMapType::iterator StringMapIterator
Definition: GoDBRow.h:60
void SetField(const std::string &key, const T &value)
convert the value into a string and assign it to the key in the map
Definition: GoDBRow.h:73
StringMapType m_MapRow
Definition: GoDBRow.h:223