fixed: thread plugin didn't correctly set the last item and replies (in 'thread' table)
when deleting an answer
added: ThreadInfo::Repair() method
will be used by 'fsck' winix function
added: plugins/groupitem
directory for a new plugin: 'groupitem'
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@725 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -58,7 +58,7 @@ void TDb::SetThread(PGresult * r, int col, Thread & thread)
|
||||
|
||||
|
||||
|
||||
Error TDb::GetThreadByFileId(long file_id, Thread & thread)
|
||||
Error TDb::GetThread(long file_id, Thread & thread)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
@@ -175,7 +175,7 @@ return status;
|
||||
|
||||
|
||||
|
||||
Error TDb::EditThreadAddItem(long file_id, long answer_id)
|
||||
Error TDb::AddAnswer(long file_id, long answer_id)
|
||||
{
|
||||
query.Clear();
|
||||
query << R("insert into plugins.thread_files (file_id, answer_id) values (")
|
||||
@@ -200,7 +200,7 @@ return DoCommand(query);
|
||||
|
||||
|
||||
|
||||
long TDb::FindLastItem(long file_id)
|
||||
long TDb::FindLastAnswer(long file_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
@@ -231,7 +231,7 @@ return last_item_id;
|
||||
|
||||
|
||||
|
||||
Error TDb::EditThreadRemoveItem(long item_id)
|
||||
Error TDb::RemoveAnswer(long answer_id)
|
||||
{
|
||||
PGresult * r1 = 0;
|
||||
PGresult * r2 = 0;
|
||||
@@ -239,10 +239,10 @@ Error TDb::EditThreadRemoveItem(long item_id)
|
||||
|
||||
try
|
||||
{
|
||||
// selecting files which have item_id as an answer
|
||||
// selecting files which have answer_id as an answer
|
||||
query.Clear();
|
||||
query << R("select file_id from plugins.thread_files where answer_id = ")
|
||||
<< item_id
|
||||
<< answer_id
|
||||
<< R(";");
|
||||
|
||||
r1 = AssertQuery(query);
|
||||
@@ -254,10 +254,11 @@ Error TDb::EditThreadRemoveItem(long item_id)
|
||||
for(int i=0 ; i<rows ; ++i)
|
||||
file_id_tab[i] = AssertValueLong(r1, i, 0);
|
||||
|
||||
|
||||
// deleting those answers
|
||||
query.Clear();
|
||||
query << R("delete from plugins.thread_files where answer_id = ")
|
||||
<< item_id
|
||||
<< answer_id
|
||||
<< R(";");
|
||||
|
||||
r2 = AssertQuery(query);
|
||||
@@ -269,7 +270,7 @@ Error TDb::EditThreadRemoveItem(long item_id)
|
||||
log << log2 << "ThreadDb: deleted " << rows << " rows from plugins.thread_files" << logend;
|
||||
|
||||
// setting new last_items to the files
|
||||
status = EditThreadRecalcFiles(file_id_tab);
|
||||
status = RemoveAnswerRecalcLast(file_id_tab);
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
@@ -283,22 +284,28 @@ return status;
|
||||
}
|
||||
|
||||
|
||||
Error TDb::RemoveAnswerRecalcLast(long file_id)
|
||||
{
|
||||
long last_item_id = FindLastAnswer(file_id);
|
||||
|
||||
Error TDb::EditThreadRecalcFiles(const std::vector<long> & file_id_tab)
|
||||
query.Clear();
|
||||
query << R("update plugins.thread set (replies, last_item) = (replies-1,")
|
||||
<< last_item_id
|
||||
<< R(") where file_id=")
|
||||
<< file_id
|
||||
<< R(";");
|
||||
|
||||
return DoCommand(query);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Error TDb::RemoveAnswerRecalcLast(const std::vector<long> & file_id_tab)
|
||||
{
|
||||
for(size_t i=0 ; i<file_id_tab.size() ; ++i)
|
||||
{
|
||||
long file_id = file_id_tab[i];
|
||||
long last_item_id = FindLastItem(file_id);
|
||||
|
||||
query.Clear();
|
||||
query << R("update plugins.thread set (replies, last_item) = (replies-1,")
|
||||
<< last_item_id
|
||||
<< R(") where file_id=")
|
||||
<< file_id
|
||||
<< R(";");
|
||||
|
||||
Error status = DoCommand(query);
|
||||
long file_id = file_id_tab[i];
|
||||
Error status = RemoveAnswerRecalcLast(file_id);
|
||||
|
||||
if( status != WINIX_ERR_OK )
|
||||
return status;
|
||||
@@ -308,6 +315,49 @@ return WINIX_ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
Error TDb::RecalcThread(long file_id)
|
||||
{
|
||||
long replies = CalcAnswers(file_id);
|
||||
long last_item = FindLastAnswer(file_id);
|
||||
|
||||
query.Clear();
|
||||
query << R("update plugins.thread set (replies, last_item) = (")
|
||||
<< replies
|
||||
<< last_item
|
||||
<< R(") where file_id=")
|
||||
<< file_id
|
||||
<< R(";");
|
||||
|
||||
return DoCommand(query);
|
||||
}
|
||||
|
||||
|
||||
long TDb::CalcAnswers(long file_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
long answers = 0;
|
||||
|
||||
try
|
||||
{
|
||||
query.Clear();
|
||||
query << R("select count(file_id) from plugins.thread_files where file_id=") << file_id << R(";");
|
||||
|
||||
r = AssertQuery(query);
|
||||
AssertResult(r, PGRES_TUPLES_OK);
|
||||
|
||||
if( Rows(r) == 1 )
|
||||
answers = AssertValueLong(r, 0, 0);
|
||||
}
|
||||
catch(const Error &)
|
||||
{
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return answers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -354,6 +404,64 @@ return status;
|
||||
|
||||
|
||||
|
||||
Error TDb::RemoveAnswerOnly(long answer_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
query.Clear();
|
||||
query << R("delete from plugins.thread_files where answer_id=") << answer_id << R(";");
|
||||
|
||||
r = AssertQuery(query);
|
||||
AssertResult(r, PGRES_COMMAND_OK);
|
||||
|
||||
long rows = AffectedRows(r);
|
||||
|
||||
if( rows > 0 )
|
||||
log << log2 << "ThreadDb: deleted " << rows << " rows from plugins.thread_files" << logend;
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void TDb::GetAllThreadsId(std::vector<long> & file_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
file_id.clear();
|
||||
|
||||
try
|
||||
{
|
||||
query.Clear();
|
||||
query << R("select file_id from plugins.thread;");
|
||||
|
||||
r = AssertQuery(query);
|
||||
AssertResult(r, PGRES_TUPLES_OK);
|
||||
|
||||
int rows = Rows(r);
|
||||
file_id.resize(rows);
|
||||
|
||||
for(int i=0 ; i<rows ; ++i)
|
||||
file_id[i] = AssertValueLong(r, i, 0);
|
||||
}
|
||||
catch(const Error &)
|
||||
{
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user