All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DBBase.cpp
Go to the documentation of this file.
1 /*
2  * DBBase.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <HSEPData/DBBase.h>
23 
24 #include <stdio.h>
25 
26 namespace HSEPData {
27 
28  string& BaseWhatMethod(string& pMessageRef, exception* pExceptionPtr) {
29 
30  char vTemp[10240];
31 
32  sprintf(vTemp,
33  "# ERR: Exception in %s (%s) on line %d\n# ERR: %s ()\n",
34  __FILE__,
35  __FUNCTION__,
36  __LINE__,
37  pExceptionPtr->what());
38 
39  pMessageRef = string(vTemp);
40 
41  return pMessageRef;
42 
43  } // BaseWhatMethod
44 
45 
46  bool QueryUniqueCycle::execute(DBResultSet* pResultSetPtr,bool pIsFirst,bool pIsLast) {
47 
48  DBMetadata vMetadata;
49  pResultSetPtr->getMetadata(vMetadata);
50 
51  vMetadata.forEach([&](DBMetadataField* xField) {
52  string vColumnName = xField->aColumnName;
53  string vValue = pResultSetPtr->getString(vColumnName);
54  aResults.put(vColumnName,vValue);
55  });
56 
57  pResultSetPtr->destroyMetadata(vMetadata); // to eliminate heap based elements
58 
59  return true; // TODO: check what need to be returned
60 
61  } // QueryUniqueCycle::execute
62 
63  string& QueryUniqueCycle::getValueRef(string& pKeyRef) {
64  return aResults.ref(pKeyRef);
65  } // QueryUniqueCycle::getValue
66 
67 
69 
70  pMetadataRef.forEach([](DBMetadataField* vField){
71  DBMetadataField* xField = vField;
72  delete xField;
73  });
74  pMetadataRef.clear();
75 
76  } // DBResultSet::destroyMetadata
77 
78  // DBStatement --------------------------------------------
79 
81  int vResult;
82  DBResultSet* vResultSet = executeQueryPtr();
83  if (vResultSet) {
84  vResult = vResultSet->forEach(pCycleRef);
85  delete vResultSet;
86  }
87  else {
88  vResult = -1;
89  }
90  return vResult;
91  } // DBStatement::cycleQuery
92 
94  int vResult;
95  DBResultSet* vResultSet = executeQueryPtr();
96  if (vResultSet) {
97  vResult = vResultSet->forEach(pFunction);
98  delete vResultSet;
99  }
100  else {
101  vResult = -1;
102  }
103  return vResult;
104  } // DBStatement::cycleQuery
105 
106  int DBStatement::cycleQuery(DBParameters& pParametersRef, ExecuteFunctionType pFunction) {
107  int vResult;
108  DBResultSet* vResultSet = executeQueryPtr(pParametersRef);
109  if (nullptr != vResultSet) {
110  vResult = vResultSet->forEach(pFunction);
111  delete vResultSet;
112  }
113  else {
114  vResult = -1;
115  }
116  return vResult;
117  }// DBStatement::cycleQuery
118 
119  int DBStatement::cycleQuery(DBParameters& pParametersRef, CycleBase& pCycleRef) {
120  int vResult;
121  DBResultSet* vResultSet = executeQueryPtr(pParametersRef);
122  if (vResultSet) {
123  vResult = vResultSet->forEach(pCycleRef);
124  delete vResultSet;
125  }
126  else {
127  vResult = -1;
128  }
129  return vResult;
130  }// DBStatement::cycleQuery
131 
132 
133  DBException::DBException(exception& pExceptionRef, WhatMethod pMethod) {
134  pMethod(aMessage, &pExceptionRef);
135  }
136  DBException::DBException(exception& pExceptionRef) {
137  BaseWhatMethod(aMessage,&pExceptionRef);
138  }
139  const char* DBException::what() const throw() {
140  return aMessage.c_str();
141  }
142 
143 } // HSEPData namespace