USGS

Isis 3.0 Object Programmers' Reference

Home

SqlQuery.cpp
Go to the documentation of this file.
1 
22 #include <string>
23 #include <vector>
24 #include <iostream>
25 #include "DbProfile.h"
26 #include "Database.h"
27 #include "IString.h"
28 #include "SqlQuery.h"
29 #include "SqlRecord.h"
30 #include <QString>
31 
32 using namespace std;
33 
34 namespace Isis {
35 
43  SqlQuery::SqlQuery() : QSqlQuery(Database()), _throwIfFailed(true) { }
44 
55  SqlQuery::SqlQuery(Database &db) : QSqlQuery(db), _throwIfFailed(true) { }
56 
57 
88  SqlQuery::SqlQuery(const std::string &query, Database db) : QSqlQuery(db),
89  _throwIfFailed(true) {
90 // Execute with error detector
91  exec(query);
92  }
93 
103  SqlQuery::SqlQuery(const SqlQuery &other) : QSqlQuery(other),
104  _throwIfFailed(other._throwIfFailed) { }
105 
106 
125  bool SqlQuery::exec(const std::string &query) {
126  bool ok = this->QSqlQuery::exec(IString::ToQt(query));
127  if((!ok) && isThrowing()) {
128  string mess = "Query \'" + query + "\' failed to execute";
129  tossQueryError(mess, _FILEINFO_);
130  }
131  return (ok);
132  }
133 
146  std::string SqlQuery::getQuery() const {
147  std::string lastq = IString::ToStd(lastQuery());
148  if(lastq.empty()) lastq = IString::ToStd(executedQuery());
149  return (lastq);
150  }
151 
163  int SqlQuery::nFields() const {
164  return (record().count());
165  }
166 
179  std::string SqlQuery::fieldName(int index) const {
180  return (IString::ToStd(record().fieldName(index)));
181  }
182 
194  int SqlQuery::fieldIndex(const std::string &name) const {
195  return(record().indexOf(IString::ToQt(name)));
196  }
197 
211  std::vector<std::string> SqlQuery::fieldNameList() const {
212  std::vector<std::string> fields;
213  QSqlRecord rec = record();
214  for(int i = 0 ; i < rec.count() ; i++) {
215  fields.push_back(IString::ToStd(rec.fieldName(i)));
216  }
217  return (fields);
218  }
219 
228  std::vector<std::string> SqlQuery::fieldTypeList() const {
229  std::vector<std::string> types;
230  SqlRecord rec = getRecord();
231  for(int i = 0 ; i < rec.count() ; i++) {
232  types.push_back(rec.getType(i).toAscii().data());
233  }
234  return (types);
235  }
236 
250  int SqlQuery::nRows() const {
251  return (size());
252  }
253 
268  return (SqlRecord(*this));
269  }
270 
291  void SqlQuery::tossQueryError(const std::string &message, const char *f, int l) const {
292  string errmess = message + " - QueryError = " +
293  IString::ToStd(lastError().text());
294  throw IException(IException::User, errmess, f, l);
295  }
296 
297 }