2022-10-25 17:08:36 -03:00
|
|
|
#include "models/entity.h"
|
2022-11-29 00:06:35 -03:00
|
|
|
#include "models/error.h"
|
2022-10-25 17:08:36 -03:00
|
|
|
|
2022-11-01 01:04:41 -03:00
|
|
|
const char* obelisk::Entity::createTable()
|
2022-10-25 17:08:36 -03:00
|
|
|
{
|
2022-11-01 01:04:41 -03:00
|
|
|
return R"(
|
|
|
|
CREATE TABLE "entity" (
|
|
|
|
"id" INTEGER NOT NULL UNIQUE,
|
|
|
|
"name" TEXT NOT NULL CHECK(trim(name) != '') UNIQUE,
|
|
|
|
PRIMARY KEY("id" AUTOINCREMENT)
|
|
|
|
);
|
|
|
|
)";
|
2022-10-25 17:08:36 -03:00
|
|
|
}
|
|
|
|
|
2023-02-16 00:44:32 -03:00
|
|
|
void obelisk::Entity::selectByName(sqlite3* dbConnection)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
if (dbConnection == nullptr)
|
|
|
|
{
|
|
|
|
throw obelisk::DatabaseException("database isn't open");
|
|
|
|
}
|
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
sqlite3_stmt* ppStmt = nullptr;
|
|
|
|
|
2022-12-13 17:35:14 -03:00
|
|
|
auto result = sqlite3_prepare_v2(dbConnection, "SELECT id, name FROM entity WHERE name=?", -1, &ppStmt, nullptr);
|
2022-11-29 00:06:35 -03:00
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
if (result != SQLITE_OK)
|
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
|
2022-11-29 00:06:35 -03:00
|
|
|
result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_STATIC);
|
|
|
|
switch (result)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
case SQLITE_OK :
|
|
|
|
break;
|
|
|
|
case SQLITE_TOOBIG :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseSizeException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
case SQLITE_RANGE :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseRangeException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
case SQLITE_NOMEM :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseMemoryException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
|
|
|
break;
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = sqlite3_step(ppStmt);
|
2022-11-29 00:06:35 -03:00
|
|
|
switch (result)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
case SQLITE_DONE :
|
|
|
|
// no rows in the database
|
|
|
|
break;
|
|
|
|
case SQLITE_ROW :
|
|
|
|
setId(sqlite3_column_int(ppStmt, 0));
|
|
|
|
setName((char*) sqlite3_column_text(ppStmt, 1));
|
|
|
|
break;
|
|
|
|
case SQLITE_BUSY :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseBusyException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
case SQLITE_MISUSE :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseMisuseException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
|
|
|
break;
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
|
2022-11-29 00:06:35 -03:00
|
|
|
result = sqlite3_finalize(ppStmt);
|
|
|
|
|
|
|
|
if (result != SQLITE_OK)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 00:40:43 -03:00
|
|
|
void obelisk::Entity::insert(sqlite3* dbConnection)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
if (dbConnection == nullptr)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
throw obelisk::DatabaseException("database isn't open");
|
|
|
|
}
|
2022-11-26 00:32:06 -03:00
|
|
|
|
|
|
|
sqlite3_stmt* ppStmt = nullptr;
|
|
|
|
|
2022-12-13 17:35:14 -03:00
|
|
|
auto result = sqlite3_prepare_v2(dbConnection, "INSERT INTO entity (name) VALUES (?)", -1, &ppStmt, nullptr);
|
2022-11-26 00:32:06 -03:00
|
|
|
if (result != SQLITE_OK)
|
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:35:14 -03:00
|
|
|
result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT);
|
2022-11-29 00:06:35 -03:00
|
|
|
switch (result)
|
2022-11-26 00:32:06 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
case SQLITE_OK :
|
|
|
|
break;
|
|
|
|
case SQLITE_TOOBIG :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseSizeException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
case SQLITE_RANGE :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseRangeException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
case SQLITE_NOMEM :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseMemoryException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
|
|
|
break;
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = sqlite3_step(ppStmt);
|
2022-11-29 00:06:35 -03:00
|
|
|
switch (result)
|
2022-11-27 23:58:40 -03:00
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
case SQLITE_DONE :
|
|
|
|
setId((int) sqlite3_last_insert_rowid(dbConnection));
|
|
|
|
sqlite3_set_last_insert_rowid(dbConnection, 0);
|
|
|
|
break;
|
|
|
|
case SQLITE_CONSTRAINT :
|
2022-12-13 17:35:14 -03:00
|
|
|
throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection));
|
2022-11-29 00:06:35 -03:00
|
|
|
case SQLITE_BUSY :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseBusyException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
case SQLITE_MISUSE :
|
2022-12-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseMisuseException();
|
2022-11-29 00:06:35 -03:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
|
|
|
break;
|
2022-11-27 23:58:40 -03:00
|
|
|
}
|
2022-11-26 00:32:06 -03:00
|
|
|
|
|
|
|
result = sqlite3_finalize(ppStmt);
|
|
|
|
if (result != SQLITE_OK)
|
|
|
|
{
|
2022-11-29 00:06:35 -03:00
|
|
|
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
|
2022-11-26 00:32:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int& obelisk::Entity::getId()
|
2022-10-25 17:08:36 -03:00
|
|
|
{
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obelisk::Entity::setId(int id)
|
|
|
|
{
|
|
|
|
id_ = id;
|
|
|
|
}
|
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
std::string& obelisk::Entity::getName()
|
2022-10-25 17:08:36 -03:00
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obelisk::Entity::setName(std::string name)
|
|
|
|
{
|
2022-11-01 01:04:41 -03:00
|
|
|
name_ = name;
|
2022-10-25 17:08:36 -03:00
|
|
|
}
|