added models Thread and ThreadFiles in thread plugin

This commit is contained in:
2021-05-21 23:06:48 +02:00
parent 86ef2529b1
commit b2cffa39e1
12 changed files with 478 additions and 162 deletions

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2014, Tomasz Sowa
* Copyright (c) 2010-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -45,18 +45,18 @@ namespace Thread
Error TDb::AddThread(const Thread & thread)
{
query.Clear();
query << R("insert into plugins.thread (file_id, replies, last_item, closed) values (")
<< thread.file_id
<< thread.replies
<< thread.last_item_id
<< (thread.closed ? 1 : 0 )
<< R(");");
return DoCommand(query);
}
//Error TDb::AddThread(const Thread & thread)
//{
// query.Clear();
// query << R("insert into plugins.thread (file_id, replies, last_item, closed) values (")
// << thread.file_id
// << thread.replies
// << thread.last_item_id
// << (thread.closed ? 1 : 0 )
// << R(");");
//
//return DoCommand(query);
//}
@@ -65,10 +65,10 @@ void TDb::SetThreadColumns(PGresult * r)
cfile_id = AssertColumn(r, "file_id");
creplies = AssertColumn(r, "replies");
cclosed = AssertColumn(r, "closed");
clast_item = AssertColumn(r, "last_item");
cdate_modification = AssertColumn(r, "date_modification");
cuser_id = AssertColumn(r, "user_id");
cguest = AssertColumn(r, "guest_name");
// clast_item = AssertColumn(r, "last_item");
// cdate_modification = AssertColumn(r, "date_modification");
// cuser_id = AssertColumn(r, "user_id");
// cguest = AssertColumn(r, "guest_name");
}
@@ -77,95 +77,95 @@ void TDb::SetThread(PGresult * r, int col, Thread & thread)
thread.file_id = AssertValueLong(r, col, cfile_id);
thread.replies = AssertValueLong(r, col, creplies);
thread.closed = AssertValueBool(r, col, cclosed);
thread.last_item_id = AssertValueLong(r, col, clast_item);
thread.last_item_date_modification = AssertValueDate(r, col, cdate_modification);
thread.last_item_user_id = AssertValueLong(r, col, cuser_id);
AssertValueWide(r, col, cguest, thread.last_item_guest_name);
//thread.last_item_id = AssertValueLong(r, col, clast_item);
//thread.last_item_date_modification = AssertValueDate(r, col, cdate_modification);
//thread.last_item_user_id = AssertValueLong(r, col, cuser_id);
//AssertValueWide(r, col, cguest, thread.last_item_guest_name);
}
Error TDb::GetThread(long file_id, Thread & thread)
{
PGresult * r = 0;
Error status = WINIX_ERR_OK;
try
{
query.Clear();
query << R("select thread.file_id, thread.replies, thread.closed, thread.last_item, "
"item.date_modification, item.user_id, item.guest_name "
"from plugins.thread left join core.item on thread.last_item = item.id "
"where thread.file_id = ") << file_id << R(";");
r = AssertQuery(query);
AssertResult(r, PGRES_TUPLES_OK);
int rows = Rows(r);
if( rows > 1 )
log << log1 << "ThreadDb: there is more than one thread with file_id: " << file_id << logend;
else
if( rows == 0 )
throw Error(WINIX_ERR_NO_THREAD);
SetThreadColumns(r);
SetThread(r, 0, thread);
}
catch(const Error & e)
{
status = e;
}
ClearResult(r);
return status;
}
//Error TDb::GetThread(long file_id, Thread & thread)
//{
// PGresult * r = 0;
// Error status = WINIX_ERR_OK;
//
// try
// {
// query.Clear();
// query << R("select thread.file_id, thread.replies, thread.closed, thread.last_item, "
// "item.date_modification, item.user_id, item.guest_name "
// "from plugins.thread left join core.item on thread.last_item = item.id "
// "where thread.file_id = ") << file_id << R(";");
//
// r = AssertQuery(query);
// AssertResult(r, PGRES_TUPLES_OK);
//
// int rows = Rows(r);
//
// if( rows > 1 )
// log << log1 << "ThreadDb: there is more than one thread with file_id: " << file_id << logend;
// else
// if( rows == 0 )
// throw Error(WINIX_ERR_NO_THREAD);
//
// SetThreadColumns(r);
// SetThread(r, 0, thread);
// }
// catch(const Error & e)
// {
// status = e;
// }
//
// ClearResult(r);
//
//return status;
//}
Error TDb::GetThreads(const std::vector<long> & file_id_tab, std::vector<Thread> & thread_tab)
{
PGresult * r = 0;
Error status = WINIX_ERR_OK;
thread_tab.clear();
if( file_id_tab.empty() )
return status;
try
{
CreateIdList(file_id_tab, list_id);
// they should be sorted by file_id (they are used in a binary search later)
query.Clear();
query << R("select thread.file_id, thread.replies, thread.closed, thread.last_item, "
"item.date_modification, item.user_id, item.guest_name "
"from plugins.thread left join core.item on thread.last_item = item.id "
"where thread.file_id in ") << R(list_id) << R(" order by file_id asc;");
r = AssertQuery(query);
AssertResult(r, PGRES_TUPLES_OK);
int rows = Rows(r);
SetThreadColumns(r);
for(int i=0 ; i<rows ; ++i)
{
SetThread(r, i, thread_temp);
thread_tab.push_back(thread_temp);
}
}
catch(const Error & e)
{
status = e;
}
ClearResult(r);
return status;
}
//Error TDb::GetThreads(const std::vector<long> & file_id_tab, std::vector<Thread> & thread_tab)
//{
// PGresult * r = 0;
// Error status = WINIX_ERR_OK;
// thread_tab.clear();
//
// if( file_id_tab.empty() )
// return status;
//
// try
// {
// CreateIdList(file_id_tab, list_id);
//
// // they should be sorted by file_id (they are used in a binary search later)
// query.Clear();
// query << R("select thread.file_id, thread.replies, thread.closed, thread.last_item, "
// "item.date_modification, item.user_id, item.guest_name "
// "from plugins.thread left join core.item on thread.last_item = item.id "
// "where thread.file_id in ") << R(list_id) << R(" order by file_id asc;");
//
// r = AssertQuery(query);
// AssertResult(r, PGRES_TUPLES_OK);
//
// int rows = Rows(r);
// SetThreadColumns(r);
//
// for(int i=0 ; i<rows ; ++i)
// {
// SetThread(r, i, thread_temp);
// thread_tab.push_back(thread_temp);
// }
// }
// catch(const Error & e)
// {
// status = e;
// }
//
// ClearResult(r);
//
//return status;
//}