Initial commit

This commit is contained in:
2020-02-12 19:57:50 +01:00
commit 8ecca3824b
13 changed files with 440 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
*.o.*
*.d.*
*.a.*
.*.swp
.AppleDouble
lib
Libraries
+3
View File
@@ -0,0 +1,3 @@
[submodule "Makefiles"]
path = Makefiles
url = https://gogs.dierkse.nl/JDierkse/Makefiles
Symlink
+1
View File
@@ -0,0 +1 @@
Makefiles/Makefile
+6
View File
@@ -0,0 +1,6 @@
#
# Makefile.conf
#
CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include
DEBUGDIR := .debug
+13
View File
@@ -0,0 +1,13 @@
#
# Makefile.target
#
SQLite.a.$(ARCH) : $(OBJECTS)
$(call build_target_library_arch,$@,$^)
SQLite.a:
$(call build_target,$@)
.DEFAULT_GOAL := SQLite.a
TARGETS += SQLite.a
Submodule
+1
Submodule Makefiles added at 61894c4588
+1
View File
@@ -0,0 +1 @@
../Makefile
+56
View File
@@ -0,0 +1,56 @@
#include "SQLiteClient.h"
#include <sqlite3.h>
#include <sstream>
#include <stdexcept>
namespace SQLite {
SQLiteClient::SQLiteClient(const std::string& filename)
{
sqlite3_open(filename.c_str(), &m_pSQLite3);
}
SQLiteClient::~SQLiteClient()
{
sqlite3_close(m_pSQLite3);
}
SQLiteClient::operator sqlite3*()
{
return m_pSQLite3;
}
SQLiteResultSet SQLiteClient::ExecuteQuery(const std::string& query)
{
SQLiteStatement statement(*this, query);
return RetrieveResultSet(statement);
}
void SQLiteClient::BeginTransaction()
{
ExecuteQuery("BEGIN TRANSACTION;");
}
void SQLiteClient::EndTransaction()
{
ExecuteQuery("END TRANSACTION;");
}
SQLiteResultSet SQLiteClient::RetrieveResultSet(SQLiteStatement& statement)
{
try
{
SQLiteResultSet resultSet(statement);
return resultSet;
}
catch (const std::exception& e)
{
std::stringstream err;
err << "SQL error: " << sqlite3_errmsg(*this);
throw std::runtime_error(err.str());
}
}
} // namespace SQLite
+202
View File
@@ -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
+32
View File
@@ -0,0 +1,32 @@
#include "SQLiteClient.h"
#include "SQLiteStatement.h"
#include <sqlite3.h>
#include <sstream>
#include <stdexcept>
namespace SQLite {
SQLiteStatement::SQLiteStatement(SQLiteClient& client, const std::string& query)
{
int returnCode = sqlite3_prepare_v2(client, query.c_str(), -1, &m_pStatement, nullptr);
if (returnCode != SQLITE_OK)
{
std::stringstream err;
err << "SQL error: " << sqlite3_errmsg(client);
throw std::runtime_error(err.str());
}
}
SQLiteStatement::~SQLiteStatement()
{
sqlite3_finalize(m_pStatement);
}
SQLiteStatement::operator sqlite3_stmt*()
{
return m_pStatement;
}
} // namespace SQLite
+35
View File
@@ -0,0 +1,35 @@
#ifndef SQLITECLIENT_H
#define SQLITECLIENT_H
#include "SQLiteResultSet.h"
#include "SQLiteStatement.h"
#include <string>
class sqlite3;
namespace SQLite {
class SQLiteClient
{
public:
SQLiteClient(const std::string& filename);
~SQLiteClient();
operator sqlite3*();
public:
SQLiteResultSet ExecuteQuery(const std::string& query);
void BeginTransaction();
void EndTransaction();
private:
SQLiteResultSet RetrieveResultSet(SQLiteStatement& statement);
private:
sqlite3* m_pSQLite3;
};
} // namespace SQLite
#endif // SQLITECLIENT_H
+56
View File
@@ -0,0 +1,56 @@
#ifndef SQLITERESULTSET_H
#define SQLITERESULTSET_H
#include "SQLiteStatement.h"
#include <string>
#include <vector>
#include <stdint.h>
namespace SQLite {
class SQLiteResultSet
{
public:
SQLiteResultSet(SQLiteStatement& statement);
public:
void AfterLast();
void BeforeFirst();
bool First();
bool IsAfterLast() const;
bool IsBeforeFirst() const;
bool IsFirst() const;
bool IsLast() const;
bool Last();
bool Next();
bool Previous();
size_t Row() const;
size_t RowsCount() const;
bool Empty() const;
uint32_t FindColumn(const std::string& columnName) const;
long double Double(uint32_t columnIndex) const;
long double Double(const std::string& columnLabel) const;
int32_t Int(uint32_t columnIndex) const;
int32_t Int(const std::string& columnLabel) const;
std::string String(uint32_t columnIndex) const;
std::string String(const std::string& columnName) const;
void Print() const;
private:
int Size() const;
int LastIndex() const;
private:
std::vector<std::string> m_columns;
std::vector<std::vector<std::string>> m_values;
int m_currentRow;
};
} // namespace SQLite
#endif // SQLITERESULTSET_H
+27
View File
@@ -0,0 +1,27 @@
#ifndef SQLITESTATEMENT_H
#define SQLITESTATEMENT_H
#include <string>
class sqlite3_stmt;
namespace SQLite {
class SQLiteClient;
class SQLiteStatement
{
public:
SQLiteStatement(SQLiteClient& client, const std::string& query);
~SQLiteStatement();
operator sqlite3_stmt*();
private:
sqlite3_stmt* m_pStatement;
};
} // namespace SQLite
#endif // SQLITESTATEMENT_H