Isis 3 Programmer Reference
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).toLatin1().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 }
Provide simplified access to resulting SQL query row.
Definition: SqlRecord.h:62
std::vector< std::string > fieldTypeList() const
Returns the types of each field/column in a resutling query.
Definition: SqlQuery.cpp:228
QString getType(int index) const
Returns the type of a field/column at the specified index.
Definition: SqlRecord.cpp:143
SqlQuery()
Default constructor.
Definition: SqlQuery.cpp:43
static std::string ToStd(const QString &str)
Converts a Qt string into a std::string.
Definition: IString.cpp:1348
std::vector< std::string > fieldNameList() const
Returns the names of all fields in the resulting query.
Definition: SqlQuery.cpp:211
Namespace for the standard library.
SqlRecord getRecord() const
Returns a SqlRecord for the current query row.
Definition: SqlQuery.cpp:267
bool isThrowing() const
Report error status when executing queries.
Definition: SqlQuery.h:167
int fieldIndex(const std::string &name) const
Returns index of column for given name.
Definition: SqlQuery.cpp:194
void tossQueryError(const std::string &message, const char *f, int l) const
Issues an IException from various sources of error states in this class.
Definition: SqlQuery.cpp:291
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
Construct and execute a query on a database and manage result.
Definition: SqlQuery.h:152
bool exec(const std::string &query)
Execute an SQL query provided in the query string.
Definition: SqlQuery.cpp:125
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
QString ToQt() const
Retuns the object string as a QString.
Definition: IString.cpp:884
Isis database class providing generalized access to a variety of databases.
Definition: Database.h:84
std::string getQuery() const
Returns the executed query string.
Definition: SqlQuery.cpp:146
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
int nFields() const
Returns the number of fields (columns) from query.
Definition: SqlQuery.cpp:163
std::string fieldName(int index) const
Returns the column name of the resulting query at the given index.
Definition: SqlQuery.cpp:179
int nRows() const
Returns the count of rows resulting from the query.
Definition: SqlQuery.cpp:250