Isis 3 Programmer Reference
SqlQuery.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include <string>
10 #include <vector>
11 #include <iostream>
12 #include "DbProfile.h"
13 #include "Database.h"
14 #include "IString.h"
15 #include "SqlQuery.h"
16 #include "SqlRecord.h"
17 #include <QString>
18 
19 using namespace std;
20 
21 namespace Isis {
22 
30  SqlQuery::SqlQuery() : QSqlQuery(Database()), _throwIfFailed(true) { }
31 
42  SqlQuery::SqlQuery(Database &db) : QSqlQuery(db), _throwIfFailed(true) { }
43 
44 
75  SqlQuery::SqlQuery(const std::string &query, Database db) : QSqlQuery(db),
76  _throwIfFailed(true) {
77 // Execute with error detector
78  exec(query);
79  }
80 
90  SqlQuery::SqlQuery(const SqlQuery &other) : QSqlQuery(other),
91  _throwIfFailed(other._throwIfFailed) { }
92 
93 
112  bool SqlQuery::exec(const std::string &query) {
113  bool ok = this->QSqlQuery::exec(IString::ToQt(query));
114  if((!ok) && isThrowing()) {
115  string mess = "Query \'" + query + "\' failed to execute";
116  tossQueryError(mess, _FILEINFO_);
117  }
118  return (ok);
119  }
120 
133  std::string SqlQuery::getQuery() const {
134  std::string lastq = IString::ToStd(lastQuery());
135  if(lastq.empty()) lastq = IString::ToStd(executedQuery());
136  return (lastq);
137  }
138 
150  int SqlQuery::nFields() const {
151  return (record().count());
152  }
153 
166  std::string SqlQuery::fieldName(int index) const {
167  return (IString::ToStd(record().fieldName(index)));
168  }
169 
181  int SqlQuery::fieldIndex(const std::string &name) const {
182  return(record().indexOf(IString::ToQt(name)));
183  }
184 
198  std::vector<std::string> SqlQuery::fieldNameList() const {
199  std::vector<std::string> fields;
200  QSqlRecord rec = record();
201  for(int i = 0 ; i < rec.count() ; i++) {
202  fields.push_back(IString::ToStd(rec.fieldName(i)));
203  }
204  return (fields);
205  }
206 
215  std::vector<std::string> SqlQuery::fieldTypeList() const {
216  std::vector<std::string> types;
217  SqlRecord rec = getRecord();
218  for(int i = 0 ; i < rec.count() ; i++) {
219  types.push_back(rec.getType(i).toLatin1().data());
220  }
221  return (types);
222  }
223 
237  int SqlQuery::nRows() const {
238  return (size());
239  }
240 
255  return (SqlRecord(*this));
256  }
257 
278  void SqlQuery::tossQueryError(const std::string &message, const char *f, int l) const {
279  string errmess = message + " - QueryError = " +
280  IString::ToStd(lastError().text());
281  throw IException(IException::User, errmess, f, l);
282  }
283 
284 }
Isis::SqlQuery::fieldName
std::string fieldName(int index) const
Returns the column name of the resulting query at the given index.
Definition: SqlQuery.cpp:166
Isis::IString::ToStd
static std::string ToStd(const QString &str)
Converts a Qt string into a std::string.
Definition: IString.cpp:1333
Isis::SqlQuery::fieldIndex
int fieldIndex(const std::string &name) const
Returns index of column for given name.
Definition: SqlQuery.cpp:181
Isis::SqlQuery::fieldNameList
std::vector< std::string > fieldNameList() const
Returns the names of all fields in the resulting query.
Definition: SqlQuery.cpp:198
Isis::SqlRecord::getType
QString getType(int index) const
Returns the type of a field/column at the specified index.
Definition: SqlRecord.cpp:130
Isis::SqlQuery::fieldTypeList
std::vector< std::string > fieldTypeList() const
Returns the types of each field/column in a resutling query.
Definition: SqlQuery.cpp:215
Isis::SqlQuery::exec
bool exec(const std::string &query)
Execute an SQL query provided in the query string.
Definition: SqlQuery.cpp:112
Isis::SqlQuery::getQuery
std::string getQuery() const
Returns the executed query string.
Definition: SqlQuery.cpp:133
Isis::SqlQuery::nFields
int nFields() const
Returns the number of fields (columns) from query.
Definition: SqlQuery.cpp:150
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::SqlQuery::tossQueryError
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:278
std
Namespace for the standard library.
Isis::Database
Isis database class providing generalized access to a variety of databases.
Definition: Database.h:70
QSqlQuery
QSqlRecord
Isis::SqlQuery::getRecord
SqlRecord getRecord() const
Returns a SqlRecord for the current query row.
Definition: SqlQuery.cpp:254
Isis::SqlQuery::nRows
int nRows() const
Returns the count of rows resulting from the query.
Definition: SqlQuery.cpp:237
Isis::SqlQuery::SqlQuery
SqlQuery()
Default constructor.
Definition: SqlQuery.cpp:30
Isis::SqlQuery
Construct and execute a query on a database and manage result.
Definition: SqlQuery.h:138
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IString::ToQt
QString ToQt() const
Retuns the object string as a QString.
Definition: IString.cpp:869
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126
Isis::SqlQuery::isThrowing
bool isThrowing() const
Report error status when executing queries.
Definition: SqlQuery.h:153
Isis::SqlRecord
Provide simplified access to resulting SQL query row.
Definition: SqlRecord.h:48