fix fact db structure

This commit is contained in:
Chris Cromer 2023-02-15 21:49:53 -03:00
parent 6761117e24
commit ab83a1626b
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
3 changed files with 47 additions and 9 deletions

View File

@ -29,7 +29,7 @@ void obelisk::Fact::selectFact(sqlite3* dbConnection)
sqlite3_stmt* ppStmt = nullptr;
auto result = sqlite3_prepare_v2(dbConnection,
"SELECT id, left_entity, right_entity, verb FROM fact WHERE (left_entity=? AND right_entity=? AND verb=?)",
"SELECT id, left_entity, right_entity, verb, is_true FROM fact WHERE (left_entity=? AND right_entity=? AND verb=?)",
-1,
&ppStmt,
nullptr);
@ -106,6 +106,7 @@ void obelisk::Fact::selectFact(sqlite3* dbConnection)
getLeftEntity().setId(sqlite3_column_int(ppStmt, 1));
getRightEntity().setId(sqlite3_column_int(ppStmt, 2));
getVerb().setId(sqlite3_column_int(ppStmt, 3));
setIsTrue(sqlite3_column_int(ppStmt, 4));
break;
case SQLITE_BUSY :
throw obelisk::DatabaseBusyException();
@ -135,7 +136,7 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
sqlite3_stmt* ppStmt = nullptr;
auto result = sqlite3_prepare_v2(dbConnection,
"INSERT INTO fact (left_entity, right_entity, verb) VALUES (?, ?, ?)",
"INSERT INTO fact (left_entity, right_entity, verb, is_true) VALUES (?, ?, ?, ?)",
-1,
&ppStmt,
nullptr);
@ -201,6 +202,25 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
break;
}
result = sqlite3_bind_int(ppStmt, 4, getIsTrue());
switch (result)
{
case SQLITE_OK :
break;
case SQLITE_TOOBIG :
throw obelisk::DatabaseSizeException();
break;
case SQLITE_RANGE :
throw obelisk::DatabaseRangeException();
break;
case SQLITE_NOMEM :
throw obelisk::DatabaseMemoryException();
break;
default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
break;
}
result = sqlite3_step(ppStmt);
switch (result)
{
@ -267,3 +287,13 @@ void obelisk::Fact::setVerb(obelisk::Verb verb)
{
verb_ = verb;
}
bool& obelisk::Fact::getIsTrue()
{
return isTrue_;
}
void obelisk::Fact::setIsTrue(bool isTrue)
{
isTrue_ = isTrue;
}

View File

@ -16,13 +16,15 @@ namespace obelisk
obelisk::Entity leftEntity_;
obelisk::Entity rightEntity_;
obelisk::Verb verb_;
bool isTrue_;
public:
Fact() :
id_(0),
leftEntity_(),
rightEntity_(),
verb_()
verb_(),
isTrue_(0)
{
}
@ -30,23 +32,26 @@ namespace obelisk
id_(id),
leftEntity_(),
rightEntity_(),
verb_()
verb_(),
isTrue_(0)
{
}
Fact(obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb) :
Fact(obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb, bool isTrue) :
id_(0),
leftEntity_(leftEntity),
rightEntity_(rightEntity),
verb_(verb)
verb_(verb),
isTrue_(isTrue)
{
}
Fact(int id, obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb) :
Fact(int id, obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb, bool isTrue) :
id_(id),
leftEntity_(leftEntity),
rightEntity_(rightEntity),
verb_(verb)
verb_(verb),
isTrue_(isTrue)
{
}
@ -64,6 +69,9 @@ namespace obelisk
Verb& getVerb();
void setVerb(obelisk::Verb verb);
bool& getIsTrue();
void setIsTrue(bool isTrue);
void selectFact(sqlite3* dbConnection);
void insertFact(sqlite3* dbConnection);
};

View File

@ -336,7 +336,7 @@ void obelisk::Parser::parseFact(std::vector<obelisk::Fact>& facts)
for (auto& rightEntity : rightEntities)
{
facts.push_back(
obelisk::Fact(obelisk::Entity(leftEntity), obelisk::Entity(rightEntity), obelisk::Verb(verb)));
obelisk::Fact(obelisk::Entity(leftEntity), obelisk::Entity(rightEntity), obelisk::Verb(verb), true));
}
}
}