GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoDBRow.cxx
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 #include "GoDBRow.h"
36 #include <iostream>
37 
39 {
40 }
41 
42 //-------------------------------------------------------------------------
43 
44 //-------------------------------------------------------------------------
46 {
47 }
48 
49 //-------------------------------------------------------------------------
50 
51 //-------------------------------------------------------------------------
52 void GoDBRow::SetField(const std::string& key, const std::string& value)
53 {
54  StringMapIterator it = m_MapRow.find(key);
55 
56  if ( it != m_MapRow.end() )
57  {
58  std::stringstream valueToQuery;
59  valueToQuery << "\"" << value << "\"";
60  it->second = valueToQuery.str();
61  }
62  else
63  {
64  std::cerr << "This field does not exist!!!" << std::endl;
65  }
66 }
67 
68 //-------------------------------------------------------------------------
69 
70 //-------------------------------------------------------------------------
71 std::string GoDBRow::PrintValues()
72 {
73  std::stringstream ListValues;
74 
75  for ( StringMapIterator iter = m_MapRow.begin();
76  iter != m_MapRow.end(); ++iter )
77  {
78  ListValues << iter->second << ", ";
79  }
80  size_t n = ListValues.str().length() - 2;
81  return ListValues.str().substr(0, n);
82 }
83 
84 //-------------------------------------------------------------------------
85 
86 //-------------------------------------------------------------------------
88 {
89  std::stringstream ListColumnNames;
90 
91  for ( StringMapIterator iter = m_MapRow.begin();
92  iter != m_MapRow.end();
93  ++iter )
94  {
95  ListColumnNames << iter->first << ", ";
96  }
97  size_t n = ListColumnNames.str().length() - 2;
98  return ListColumnNames.str().substr(0, n);
99 }
100 //-------------------------------------------------------------------------
101 
102 //-------------------------------------------------------------------------
104 {
105  std::stringstream StringQuery;
106 
107  for ( StringMapIterator iter = m_MapRow.begin();
108  iter != m_MapRow.end();
109  ++iter )
110  {
111  StringQuery << iter->first;
112  StringQuery << " = ";
113  StringQuery << iter->second;
114  StringQuery << ", ";
115  }
116  size_t n = StringQuery.str().length() - 2;
117  return StringQuery.str().substr(0, n);
118 }
119 
120 //-------------------------------------------------------------------------
121 
122 //-------------------------------------------------------------------------
123 std::vector< std::string > GoDBRow::GetVectorColumnNames()
124 {
125  std::vector< std::string > VectorColumnNames;
126  for ( StringMapIterator iter = m_MapRow.begin();
127  iter != m_MapRow.end();
128  ++iter )
129  {
130  VectorColumnNames.push_back(iter->first);
131  }
132  return VectorColumnNames;
133 }
134 
135 //-------------------------------------------------------------------------
136 
137 //-------------------------------------------------------------------------
140 {
141  return m_MapRow.begin();
142 }
143 
144 //-------------------------------------------------------------------------
145 
146 //-------------------------------------------------------------------------
149 {
150  return m_MapRow.end();
151 }
152 
153 //-------------------------------------------------------------------------
154 
155 //-------------------------------------------------------------------------
158 {
159  return m_MapRow.begin();
160 }
161 
162 //-------------------------------------------------------------------------
163 
164 //-------------------------------------------------------------------------
167 {
168  return m_MapRow.end();
169 }
170 
171 //-------------------------------------------------------------------------
172 
173 //-------------------------------------------------------------------------
174 std::string GoDBRow::GetMapValue(const std::string& key)
175 {
176  std::string oMapValue = "noValue";
177 
178  StringMapIterator iter = m_MapRow.find(key);
179 
180  if ( iter == m_MapRow.end() )
181  {
182  return oMapValue;
183  }
184  else
185  {
186  oMapValue = iter->second;
187 
188  // Need to test if the value is not a string previously put in the map by
189  // SetField
190  // if so, the value will be ""value"" and need to be transformed to "value".
191  // First,
192  // find the 1rst character and save it as CharacterToCompare:
193  std::string CharacterToCompare = oMapValue.substr(0, 1);
194 
195  //test if it is equal to " :
196  if ( CharacterToCompare == "\"" )
197  {
198  //if yes, remove the " at the beginning of the string and at the end:
199  oMapValue = oMapValue.substr(1, oMapValue.size() - 2);
200  }
201 
202  return oMapValue;
203  }
204 }
205 
206 //-------------------------------------------------------------------------
207 
208 //-------------------------------------------------------------------------
210  vtkMySQLDatabase *iDatabaseConnector)
211 {
212  std::vector< std::string > ResultQuery =
214  iDatabaseConnector, this->m_TableName, this->PrintColumnNames(),
215  this->m_TableIDName, ConvertToString< int >(ID) );
216 
217  if ( ResultQuery.empty() )
218  {
219  return false;
220  }
221 
222  if ( this->m_MapRow.size() != ResultQuery.size() )
223  {
224  std::cout << "pb the map and the query results are not the same size";
225  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
226  std::cout << std::endl;
227  return false;
228  }
229 
230  std::vector< std::string >::iterator iterResultQuery =
231  ResultQuery.begin();
232 
233  StringMapIterator iterMap = this->MapBegin();
234 
235  while ( iterMap != this->MapEnd() )
236  {
237  iterMap->second = *iterResultQuery;
238  ++iterMap;
239  ++iterResultQuery;
240  }
241  return true;
242 }
243 
244 //-------------------------------------------------------------------------
245 
246 //-------------------------------------------------------------------------
248 {
249  return this->m_TableName;
250 }
251 
252 //-------------------------------------------------------------------------
253 
254 //-------------------------------------------------------------------------
256 {
257  return this->m_TableIDName;
258 }
259 
260 //-------------------------------------------------------------------------
261 
262 //-------------------------------------------------------------------------
264  const std::string& iNameOfField, std::vector< FieldWithValue > & ioFieldWithValue)
265 {
266  FieldWithValue temp = { iNameOfField, this->GetMapValue(iNameOfField), "=" };
267 
268  ioFieldWithValue.push_back(temp);
269 }
270 //-------------------------------------------------------------------------
271 
272 //-------------------------------------------------------------------------
274  vtkMySQLDatabase *iDatabaseConnector)
275 {
276  if (this->GetMapValue(this->m_TableIDName) != "0")
277  {
278  DeleteRow(iDatabaseConnector,
279  this->m_TableName, this->m_TableIDName, this->GetMapValue(this->m_TableIDName) );
280  }
281 }