OpenASIP 2.2
Loading...
Searching...
No Matches
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
 
SQLiteConnectionconnection_
 sqlite connection handle
 
std::vector< std::string > columnNames_
 column names
 
std::vector< DataObjectcurrentData_
 data of the current row
 
std::vector< DataObjectnextData_
 data of the next row
 
bool dataInitialized_
 has next() been called for this query
 

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}
sqlite3_stmt * statement_
the compiled SQLite statement handle
bool dataInitialized_
has next() been called for this query
SQLiteConnection * connection_
sqlite connection handle

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}
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
std::string errorMessage() const
Definition Exception.cc:123
void finalizeQuery(sqlite3_stmt *statement)

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}
void throwIfSQLiteError(int result)

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}
std::vector< std::string > columnNames_
column names

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 {
137}
virtual const DataObject & data(std::size_t column) const =0

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()) {
118 }
119 return currentData_.at(columnIndex);
120}
static NullDataObject & instance()
std::vector< DataObject > currentData_
data of the current row

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}
std::vector< DataObject > nextData_
data of the next row

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}
#define assert(condition)
virtual void setNull()
virtual void setString(std::string value)
virtual const DataObject & data(std::size_t columnIndex) const

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 {
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: