OpenASIP  2.0
Public Member Functions | Private Attributes | List of all members
SQLiteQueryResult Class Reference

#include <SQLiteQueryResult.hh>

Inheritance diagram for SQLiteQueryResult:
Inheritance graph
Collaboration diagram for SQLiteQueryResult:
Collaboration graph

Public Member Functions

 SQLiteQueryResult (sqlite3_stmt *statement, SQLiteConnection *connection, bool init=true)
 
virtual ~SQLiteQueryResult ()
 
virtual int columns () const
 
virtual std::string columnName (std::size_t columnIndex) const
 
virtual const DataObjectdata (std::size_t columnIndex) const
 
virtual const DataObjectdata (const std::string &name) const
 
virtual bool hasNext ()
 
virtual bool next ()
 
virtual void bindInt (unsigned int position, int value)
 
virtual void bindString (unsigned int position, const std::string &value)
 
virtual void reset ()
 
- Public Member Functions inherited from RelationalDBQueryResult
virtual ~RelationalDBQueryResult ()
 
virtual int column (const std::string &name) const
 

Private Attributes

sqlite3_stmt * statement_
 the compiled SQLite statement handle More...
 
SQLiteConnectionconnection_
 sqlite connection handle More...
 
std::vector< std::string > columnNames_
 column names More...
 
std::vector< DataObjectcurrentData_
 data of the current row More...
 
std::vector< DataObjectnextData_
 data of the next row More...
 
bool dataInitialized_
 has next() been called for this query More...
 

Additional Inherited Members

- Static Public Attributes inherited from RelationalDBQueryResult
static const int UNKNOWN_INDEX = -1
 

Detailed Description

Implementation of RelationalDBQueryResult interface for SQLite.

Definition at line 50 of file SQLiteQueryResult.hh.

Constructor & Destructor Documentation

◆ SQLiteQueryResult()

SQLiteQueryResult::SQLiteQueryResult ( sqlite3_stmt *  statement,
SQLiteConnection connection,
bool  init = true 
)

Constructor.

Parameters
virtualMachineCompiled SQLite virtual machine for the query.

Definition at line 49 of file SQLiteQueryResult.cc.

52  :
53  statement_(statement),
54  connection_(connection),
55  dataInitialized_(init) {
56 
57  // initialize columnNames_ and nextData_
58  if (init) {
59  next();
60  }
61 }

References next().

Here is the call graph for this function:

◆ ~SQLiteQueryResult()

SQLiteQueryResult::~SQLiteQueryResult ( )
virtual

Destructor.

SQLite virtual machine is freed.

Definition at line 69 of file SQLiteQueryResult.cc.

69  {
70  try {
72  } catch (const RelationalDBException& e) {
74  __FILE__, __LINE__, "~SQLiteQueryResult()", e.errorMessage());
75  }
76 }

References connection_, Exception::errorMessage(), SQLiteConnection::finalizeQuery(), statement_, and Application::writeToErrorLog().

Here is the call graph for this function:

Member Function Documentation

◆ bindInt()

void SQLiteQueryResult::bindInt ( unsigned int  position,
int  value 
)
virtual

Binds int to sqlite statement at given position (1->)

Reimplemented from RelationalDBQueryResult.

Definition at line 215 of file SQLiteQueryResult.cc.

215  {
216  connection_->throwIfSQLiteError(sqlite3_bind_int(statement_, position, value));
217 }

References connection_, statement_, and SQLiteConnection::throwIfSQLiteError().

Here is the call graph for this function:

◆ bindString()

void SQLiteQueryResult::bindString ( unsigned int  position,
const std::string &  value 
)
virtual

Binds string to sqlite statement at given position (1->)

Reimplemented from RelationalDBQueryResult.

Definition at line 223 of file SQLiteQueryResult.cc.

223  {
224  connection_->throwIfSQLiteError(sqlite3_bind_text(statement_, position, value.c_str(),
225  -1, NULL));
226 }

References connection_, statement_, and SQLiteConnection::throwIfSQLiteError().

Here is the call graph for this function:

◆ columnName()

std::string SQLiteQueryResult::columnName ( std::size_t  columnIndex) const
virtual

Returns the name (title) of a column in the result set.

Parameters
columnIndexIndex of the column of which title interested.
Returns
Title. Empty string if unknown (if not supported by the driver or index out of bounds).

Reimplemented from RelationalDBQueryResult.

Definition at line 98 of file SQLiteQueryResult.cc.

98  {
99  if (columnIndex >= columnNames_.size()) {
100  return "";
101  }
102 
103  return columnNames_[columnIndex];
104 }

References columnNames_.

◆ columns()

int SQLiteQueryResult::columns ( ) const
virtual

Returns the number of columns in the result set.

Returns
Number of columns, UNKNOWN_INDEX if unknown (if not supported by the driver).

Reimplemented from RelationalDBQueryResult.

Definition at line 86 of file SQLiteQueryResult.cc.

86  {
87  return columnNames_.size();
88 }

References columnNames_.

◆ data() [1/2]

const DataObject & SQLiteQueryResult::data ( const std::string &  name) const
virtual

Returns the data of a column in the current row in the result set.

This implementation is just to silence Intel compiler's warnings about "partial implementation" of data() (because it's overriden function).

Parameters
nameName of the column of which data to return.
Returns
The data. Returns NullDataObject if the column cannot be found. Also returns NullDataObject in case the feature is not supported by the database implementation.

Reimplemented from RelationalDBQueryResult.

Definition at line 135 of file SQLiteQueryResult.cc.

135  {
136  return RelationalDBQueryResult::data(name);
137 }

References RelationalDBQueryResult::data().

Here is the call graph for this function:

◆ data() [2/2]

const DataObject & SQLiteQueryResult::data ( std::size_t  columnIndex) const
virtual

Returns the data of a column in the current row in the result set.

Parameters
columnIndexIndex of the column of which data to return.
Returns
The data. Returns NullDataObject if the index is out of bounds.

Implements RelationalDBQueryResult.

Definition at line 114 of file SQLiteQueryResult.cc.

114  {
115 
116  if (currentData_.size() == 0 || columnIndex >= currentData_.size()) {
117  return NullDataObject::instance();
118  }
119  return currentData_.at(columnIndex);
120 }

References currentData_, and NullDataObject::instance().

Referenced by next().

Here is the call graph for this function:

◆ hasNext()

bool SQLiteQueryResult::hasNext ( )
virtual

Queries if the result set contains more rows.

Returns
True if there are more rows that can be accessed with next().

Implements RelationalDBQueryResult.

Definition at line 146 of file SQLiteQueryResult.cc.

146  {
147  if (!dataInitialized_) {
148  next();
149  }
150  return nextData_.size() > 0;
151 }

References dataInitialized_, next(), and nextData_.

Referenced by next().

Here is the call graph for this function:

◆ next()

bool SQLiteQueryResult::next ( )
virtual

Advances the row cursor to next row in the result set.

In case the current row is the last row this method does nothing but returns false.

Returns
True if there are still more rows to fetch.

Implements RelationalDBQueryResult.

Definition at line 162 of file SQLiteQueryResult.cc.

162  {
163 
164  dataInitialized_ = true;
165  if (currentData_.size() > 0 && !hasNext()) {
166  return false;
167  }
168 
169  std::vector<std::string> columnNames;
170 
171  assert(statement_ != NULL);
172 
173  int columnCount = 0;
174  int dataCount = 0;
175 
176  int result = sqlite3_step(statement_);
177  columnCount = sqlite3_column_count(statement_);
178  for (int i = 0; i < columnCount; i++) {
179  columnNames.push_back(sqlite3_column_name(statement_, i));
180  }
182  if (result == SQLITE_ROW) {
183  nextData_.clear();
184  dataCount = sqlite3_data_count(statement_);
185  for (int i = 0; i < dataCount; i++) {
186  char* columnText = (char*)sqlite3_column_text(statement_, i);
188  if (columnText == NULL) {
189  data.setNull();
190  } else {
191  data.setString(columnText);
192  }
193  nextData_.push_back(data);
194  }
195  } else if (result == SQLITE_DONE) {
196  nextData_.clear();
197  } else {
198  // error occured
199  nextData_.clear();
200  return false;
201  }
202 
203  // check if it's the initialization call when one should save column
204  // names and column count
205  if (columnNames_.size() == 0 && columnNames.size() != 0) {
206  columnNames_ = columnNames;
207  }
208  return nextData_.size() > 0;
209 }

References assert, columnNames_, currentData_, data(), dataInitialized_, hasNext(), nextData_, DataObject::setNull(), DataObject::setString(), and statement_.

Referenced by hasNext(), and SQLiteQueryResult().

Here is the call graph for this function:

◆ reset()

void SQLiteQueryResult::reset ( )
virtual

Resets compiled sqlite statement for new bindings and execution.

Reimplemented from RelationalDBQueryResult.

Definition at line 232 of file SQLiteQueryResult.cc.

232  {
233  connection_->throwIfSQLiteError(sqlite3_reset(statement_));
234  // reset doesn't clear bindings
235  //connection_->throwIfSQLiteError(sqlite3_clear_bindings(statement_));
236  columnNames_.clear();
237  currentData_.clear();
238  nextData_.clear();
239  dataInitialized_ = false;
240 }

References columnNames_, connection_, currentData_, dataInitialized_, nextData_, statement_, and SQLiteConnection::throwIfSQLiteError().

Here is the call graph for this function:

Member Data Documentation

◆ columnNames_

std::vector<std::string> SQLiteQueryResult::columnNames_
private

column names

Definition at line 74 of file SQLiteQueryResult.hh.

Referenced by columnName(), columns(), next(), and reset().

◆ connection_

SQLiteConnection* SQLiteQueryResult::connection_
private

sqlite connection handle

Definition at line 72 of file SQLiteQueryResult.hh.

Referenced by bindInt(), bindString(), reset(), and ~SQLiteQueryResult().

◆ currentData_

std::vector<DataObject> SQLiteQueryResult::currentData_
private

data of the current row

Definition at line 76 of file SQLiteQueryResult.hh.

Referenced by data(), next(), and reset().

◆ dataInitialized_

bool SQLiteQueryResult::dataInitialized_
private

has next() been called for this query

Definition at line 80 of file SQLiteQueryResult.hh.

Referenced by hasNext(), next(), and reset().

◆ nextData_

std::vector<DataObject> SQLiteQueryResult::nextData_
private

data of the next row

Definition at line 78 of file SQLiteQueryResult.hh.

Referenced by hasNext(), next(), and reset().

◆ statement_

sqlite3_stmt* SQLiteQueryResult::statement_
private

the compiled SQLite statement handle

Definition at line 70 of file SQLiteQueryResult.hh.

Referenced by bindInt(), bindString(), next(), reset(), and ~SQLiteQueryResult().


The documentation for this class was generated from the following files:
SQLiteQueryResult::hasNext
virtual bool hasNext()
Definition: SQLiteQueryResult.cc:146
RelationalDBException
Definition: Exception.hh:692
DataObject
Definition: DataObject.hh:50
RelationalDBQueryResult::data
virtual const DataObject & data(std::size_t column) const =0
Definition: RelationalDBQueryResult.cc:96
Application::writeToErrorLog
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
Definition: Application.cc:224
SQLiteQueryResult::currentData_
std::vector< DataObject > currentData_
data of the current row
Definition: SQLiteQueryResult.hh:76
SQLiteQueryResult::next
virtual bool next()
Definition: SQLiteQueryResult.cc:162
SQLiteQueryResult::columnNames_
std::vector< std::string > columnNames_
column names
Definition: SQLiteQueryResult.hh:74
SQLiteQueryResult::data
virtual const DataObject & data(std::size_t columnIndex) const
Definition: SQLiteQueryResult.cc:114
assert
#define assert(condition)
Definition: Application.hh:86
SQLiteQueryResult::connection_
SQLiteConnection * connection_
sqlite connection handle
Definition: SQLiteQueryResult.hh:72
SQLiteConnection::throwIfSQLiteError
void throwIfSQLiteError(int result)
Definition: SQLiteConnection.cc:253
SQLiteQueryResult::statement_
sqlite3_stmt * statement_
the compiled SQLite statement handle
Definition: SQLiteQueryResult.hh:70
SQLiteConnection::finalizeQuery
void finalizeQuery(sqlite3_stmt *statement)
Definition: SQLiteConnection.cc:291
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
DataObject::setNull
virtual void setNull()
Definition: DataObject.cc:184
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130
SQLiteQueryResult::nextData_
std::vector< DataObject > nextData_
data of the next row
Definition: SQLiteQueryResult.hh:78
NullDataObject::instance
static NullDataObject & instance()
Definition: DataObject.cc:542
SQLiteQueryResult::dataInitialized_
bool dataInitialized_
has next() been called for this query
Definition: SQLiteQueryResult.hh:80