|
|
@@ -0,0 +1,202 @@
|
|
|
+#include "SQLiteResultSet.h"
|
|
|
+#include <sqlite3.h>
|
|
|
+#include <algorithm>
|
|
|
+#include <iostream>
|
|
|
+#include <stdexcept>
|
|
|
+
|
|
|
+
|
|
|
+namespace SQLite {
|
|
|
+
|
|
|
+SQLiteResultSet::SQLiteResultSet(SQLiteStatement& statement) :
|
|
|
+ m_currentRow(-1)
|
|
|
+{
|
|
|
+ int columnCount = sqlite3_column_count(statement);
|
|
|
+ for (int i = 0; i < columnCount; ++i)
|
|
|
+ m_columns.push_back(std::string(reinterpret_cast<const char*>(sqlite3_column_name(statement, i))));
|
|
|
+
|
|
|
+ int returnCode = 0;
|
|
|
+ while ((returnCode = sqlite3_step(statement)) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ std::vector<std::string> row;
|
|
|
+ for (int i = 0; i < columnCount; ++i)
|
|
|
+ row.push_back(std::string(reinterpret_cast<const char*>(sqlite3_column_text(statement, i))));
|
|
|
+
|
|
|
+ m_values.push_back(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(returnCode != SQLITE_DONE)
|
|
|
+ {
|
|
|
+ throw std::runtime_error("Error Creating SQLiteResultSet.");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SQLiteResultSet::AfterLast()
|
|
|
+{
|
|
|
+ m_currentRow >= Size();
|
|
|
+}
|
|
|
+
|
|
|
+void SQLiteResultSet::BeforeFirst()
|
|
|
+{
|
|
|
+ m_currentRow = -1;
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::First()
|
|
|
+{
|
|
|
+ if (m_values.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ m_currentRow = 0;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::IsAfterLast() const
|
|
|
+{
|
|
|
+ return m_currentRow >= Size();
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::IsBeforeFirst() const
|
|
|
+{
|
|
|
+ return m_currentRow < 0;
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::IsFirst() const
|
|
|
+{
|
|
|
+ return m_currentRow == 0;
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::IsLast() const
|
|
|
+{
|
|
|
+ return m_currentRow == LastIndex();
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::Last()
|
|
|
+{
|
|
|
+ if (m_values.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ m_currentRow = LastIndex();
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::Next()
|
|
|
+{
|
|
|
+ if (m_values.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (m_currentRow >= LastIndex())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ ++m_currentRow;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::Previous()
|
|
|
+{
|
|
|
+ if (m_values.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (m_currentRow <= 0)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ --m_currentRow;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+size_t SQLiteResultSet::Row() const
|
|
|
+{
|
|
|
+ return m_currentRow;
|
|
|
+}
|
|
|
+
|
|
|
+size_t SQLiteResultSet::RowsCount() const
|
|
|
+{
|
|
|
+ return m_values.size();
|
|
|
+}
|
|
|
+
|
|
|
+bool SQLiteResultSet::Empty() const
|
|
|
+{
|
|
|
+ return m_values.size() == 0;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t SQLiteResultSet::FindColumn(const std::string& columnName) const
|
|
|
+{
|
|
|
+ if (std::find(m_columns.begin(), m_columns.end(), columnName) != m_columns.end())
|
|
|
+ return std::find(m_columns.begin(), m_columns.end(), columnName) - m_columns.begin();
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+long double SQLiteResultSet::Double(uint32_t columnIndex) const
|
|
|
+{
|
|
|
+ if (m_currentRow < 0 || m_currentRow > LastIndex())
|
|
|
+ throw std::runtime_error("Invalid Row Selected.");
|
|
|
+
|
|
|
+ return std::stod(m_values[m_currentRow][columnIndex]);
|
|
|
+}
|
|
|
+
|
|
|
+long double SQLiteResultSet::Double(const std::string& columnName) const
|
|
|
+{
|
|
|
+ int index = FindColumn(columnName);
|
|
|
+
|
|
|
+ if (index < 0)
|
|
|
+ throw std::runtime_error("Invalid Column Name.");
|
|
|
+
|
|
|
+ return Double(index);
|
|
|
+}
|
|
|
+
|
|
|
+int32_t SQLiteResultSet::Int(uint32_t columnIndex) const
|
|
|
+{
|
|
|
+ if (m_currentRow < 0 || m_currentRow > LastIndex())
|
|
|
+ throw std::runtime_error("Invalid Row Selected.");
|
|
|
+
|
|
|
+ return std::stoi(m_values[m_currentRow][columnIndex]);
|
|
|
+}
|
|
|
+
|
|
|
+int32_t SQLiteResultSet::Int(const std::string& columnName) const
|
|
|
+{
|
|
|
+ int index = FindColumn(columnName);
|
|
|
+
|
|
|
+ if (index < 0)
|
|
|
+ throw std::runtime_error("Invalid Column Name.");
|
|
|
+
|
|
|
+ return Int(index);
|
|
|
+}
|
|
|
+
|
|
|
+std::string SQLiteResultSet::String(uint32_t columnIndex) const
|
|
|
+{
|
|
|
+ if (m_currentRow < 0 || m_currentRow > LastIndex())
|
|
|
+ throw std::runtime_error("Invalid Row Selected.");
|
|
|
+
|
|
|
+ return m_values[m_currentRow][columnIndex];
|
|
|
+}
|
|
|
+
|
|
|
+std::string SQLiteResultSet::String(const std::string& columnName) const
|
|
|
+{
|
|
|
+ int index = FindColumn(columnName);
|
|
|
+
|
|
|
+ if (index < 0)
|
|
|
+ throw std::runtime_error("Invalid Column Name.");
|
|
|
+
|
|
|
+ return String(index);
|
|
|
+}
|
|
|
+
|
|
|
+void SQLiteResultSet::Print() const
|
|
|
+{
|
|
|
+ for (auto rowIterator = m_values.begin(); rowIterator != m_values.end(); ++rowIterator)
|
|
|
+ {
|
|
|
+ auto columnNameIterator = m_columns.begin();
|
|
|
+ for (auto columnIterator = rowIterator->begin(); columnIterator != rowIterator->end(); ++columnIterator, ++columnNameIterator)
|
|
|
+ std::cout << *columnNameIterator << " = " << *columnIterator << std::endl;
|
|
|
+ std::cout << std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int SQLiteResultSet::Size() const
|
|
|
+{
|
|
|
+ return m_values.size();
|
|
|
+}
|
|
|
+
|
|
|
+int SQLiteResultSet::LastIndex() const
|
|
|
+{
|
|
|
+ return m_values.size() - 1;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace SQLite
|