2022-11-29 00:06:35 -03:00
|
|
|
#include "models/error.h"
|
2022-11-01 01:04:41 -03:00
|
|
|
#include "models/fact.h"
|
|
|
|
|
|
|
|
const char* obelisk::Fact::createTable()
|
|
|
|
{
|
|
|
|
return R"(
|
|
|
|
CREATE TABLE "fact" (
|
|
|
|
"id" INTEGER NOT NULL UNIQUE,
|
|
|
|
"left_entity" INTEGER NOT NULL,
|
|
|
|
"right_entity" INTEGER NOT NULL,
|
|
|
|
"verb" INTEGER NOT NULL,
|
|
|
|
PRIMARY KEY("id" AUTOINCREMENT),
|
2022-11-29 00:06:35 -03:00
|
|
|
UNIQUE("left_entity", "right_entity", "verb")
|
2022-11-01 01:04:41 -03:00
|
|
|
FOREIGN KEY("verb") REFERENCES "verb"("id") ON DELETE RESTRICT,
|
|
|
|
FOREIGN KEY("right_entity") REFERENCES "entity"("id") ON DELETE RESTRICT,
|
|
|
|
FOREIGN KEY("left_entity") REFERENCES "entity"("id") ON DELETE RESTRICT
|
|
|
|
);
|
|
|
|
)";
|
|
|
|
}
|
|
|
|
|
2022-11-29 00:06:35 -03:00
|
|
|
void obelisk::Fact::selectFact(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;
|
|
|
|
|
|
|
|
auto result = sqlite3_prepare_v2(dbConnection,
|
2022-11-29 00:06:35 -03:00
|
|
|
"SELECT id, left_entity, right_entity, verb FROM fact WHERE (left_entity=? AND right_entity=? AND verb=?)",
|
2022-11-26 00:32:06 -03:00
|
|
|
-1,
|
|
|
|
&ppStmt,
|
2022-11-29 00:06:35 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
result = sqlite3_bind_int(ppStmt, 1, getLeftEntity().getId());
|
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_bind_int(ppStmt, 2, getRightEntity().getId());
|
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_bind_int(ppStmt, 3, getVerb().getId());
|
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-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));
|
|
|
|
getLeftEntity().setId(sqlite3_column_int(ppStmt, 1));
|
|
|
|
getRightEntity().setId(sqlite3_column_int(ppStmt, 2));
|
|
|
|
getVerb().setId(sqlite3_column_int(ppStmt, 3));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 00:06:35 -03:00
|
|
|
void obelisk::Fact::insertFact(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;
|
|
|
|
|
|
|
|
auto result = sqlite3_prepare_v2(dbConnection,
|
2022-11-29 00:06:35 -03:00
|
|
|
"INSERT INTO fact (left_entity, right_entity, verb) VALUES (?, ?, ?)",
|
2022-11-26 00:32:06 -03:00
|
|
|
-1,
|
|
|
|
&ppStmt,
|
2022-11-29 00:06:35 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
result = sqlite3_bind_int(ppStmt, 1, getLeftEntity().getId());
|
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_bind_int(ppStmt, 2, getRightEntity().getId());
|
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_bind_int(ppStmt, 3, getVerb().getId());
|
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-10 21:11:39 -03:00
|
|
|
throw obelisk::DatabaseConstraintException(
|
2022-11-29 00:06:35 -03:00
|
|
|
sqlite3_errmsg(dbConnection));
|
|
|
|
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::Fact::getId()
|
2022-11-01 01:04:41 -03:00
|
|
|
{
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obelisk::Fact::setId(int id)
|
|
|
|
{
|
|
|
|
id_ = id;
|
|
|
|
}
|
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
obelisk::Entity& obelisk::Fact::getLeftEntity()
|
2022-11-01 01:04:41 -03:00
|
|
|
{
|
|
|
|
return leftEntity_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obelisk::Fact::setLeftEntity(obelisk::Entity leftEntity)
|
|
|
|
{
|
|
|
|
leftEntity_ = leftEntity;
|
|
|
|
}
|
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
obelisk::Entity& obelisk::Fact::getRightEntity()
|
2022-11-01 01:04:41 -03:00
|
|
|
{
|
|
|
|
return rightEntity_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obelisk::Fact::setRightEntity(obelisk::Entity rightEntity)
|
|
|
|
{
|
|
|
|
rightEntity_ = rightEntity;
|
|
|
|
}
|
|
|
|
|
2022-11-26 00:32:06 -03:00
|
|
|
obelisk::Verb& obelisk::Fact::getVerb()
|
2022-11-01 01:04:41 -03:00
|
|
|
{
|
|
|
|
return verb_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obelisk::Fact::setVerb(obelisk::Verb verb)
|
|
|
|
{
|
|
|
|
verb_ = verb;
|
|
|
|
}
|