1 #include "../include/threadbuilder.h"
\r
2 #include "../include/messagethread.h"
\r
4 #include "../include/dbsetup.h"
\r
5 #include "../include/stringfunctions.h"
\r
6 #include "../include/option.h"
\r
8 const bool ThreadBuilder::Build(const long messageid, const long boardid, const bool bydate)
\r
12 MessageThread mt(m_db);
\r
13 std::vector<MessageThread::threadnode> m_threadmessages;
\r
14 std::string logmessage(""); // temp var to help track down exactly when corruption occurrs
\r
16 Option option(m_db);
\r
18 option.Get("LogLevel",ll);
\r
20 mt.Load(messageid,boardid,bydate);
\r
21 m_threadmessages=mt.GetNodes();
\r
23 //m_db->Execute("BEGIN;");
\r
25 // find threadid of this mesage if it already exists in a thread
\r
26 SQLite3DB::Statement st=m_db->Prepare("SELECT tblThread.ThreadID FROM tblThread INNER JOIN tblThreadPost ON tblThread.ThreadID=tblThreadPost.ThreadID WHERE tblThread.BoardID=? AND tblThreadPost.MessageID=?;");
\r
28 st.Bind(1,messageid);
\r
32 std::string temp1("");
\r
33 std::string temp2("");
\r
34 StringFunctions::Convert(boardid,temp1);
\r
35 StringFunctions::Convert(messageid,temp2);
\r
37 logmessage+="initial bound boardid=" + temp1 + " messageid=" + temp2 + " | ";
\r
41 if(st.RowReturned())
\r
43 st.ResultInt(0,threadid);
\r
47 std::string temp1("");
\r
48 StringFunctions::Convert(threadid,temp1);
\r
50 logmessage+="result threadid=" + temp1 + " | ";
\r
56 // message doesn't exist in a thread, try to find a message in the thread that is already in a thread
\r
57 for(std::vector<MessageThread::threadnode>::const_iterator i=m_threadmessages.begin(); i!=m_threadmessages.end() && threadid==-1; i++)
\r
60 st.Bind(1,(*i).m_messageid);
\r
65 std::string temp1("");
\r
66 std::string temp2("");
\r
67 StringFunctions::Convert(boardid,temp1);
\r
68 StringFunctions::Convert((*i).m_messageid,temp2);
\r
70 logmessage+="find bound boardid=" + temp1 + " messageid=" + temp2 + " | ";
\r
73 if(st.RowReturned())
\r
75 st.ResultInt(0,threadid);
\r
78 std::string temp1("");
\r
79 StringFunctions::Convert(threadid,temp1);
\r
81 logmessage+="find result threadid=" + temp1 + " | ";
\r
88 std::string temp1("");
\r
89 StringFunctions::Convert(threadid,temp1);
\r
91 logmessage+="find not found | ";
\r
99 // thread doesn't exist - create it
\r
102 st=m_db->Prepare("INSERT INTO tblThread(BoardID) VALUES(?);");
\r
103 st.Bind(0,boardid);
\r
105 threadid=st.GetLastInsertRowID();
\r
109 std::string temp1("");
\r
110 std::string temp2("");
\r
111 StringFunctions::Convert(boardid,temp1);
\r
112 StringFunctions::Convert(threadid,temp2);
\r
114 logmessage+="insert thread bind boardid=" + temp1 + " result threadid=" + temp2 + " | ";
\r
120 if(m_threadmessages.size()>0)
\r
122 SQLite3DB::Statement st2=m_db->Prepare("UPDATE tblThread SET FirstMessageID=?, LastMessageID=? WHERE ThreadID=?;");
\r
123 st2.Bind(0,m_threadmessages[0].m_messageid);
\r
124 st2.Bind(1,m_threadmessages[m_threadmessages.size()-1].m_messageid);
\r
125 st2.Bind(2,threadid);
\r
130 std::string temp1("");
\r
131 std::string temp2("");
\r
132 std::string temp3("");
\r
133 StringFunctions::Convert(m_threadmessages[0].m_messageid,temp1);
\r
134 StringFunctions::Convert(m_threadmessages[m_threadmessages.size()-1].m_messageid,temp2);
\r
135 StringFunctions::Convert(threadid,temp3);
\r
137 logmessage+="update bind fmessageid=" + temp1 + " lmessageid=" + temp2 + " threadid=" + temp3 + " | ";
\r
140 SQLite3DB::Statement st3=m_db->Prepare("DELETE FROM tblThreadPost WHERE ThreadID=?;");
\r
141 st3.Bind(0,threadid);
\r
146 std::string temp1("");
\r
147 StringFunctions::Convert(threadid,temp1);
\r
149 logmessage+="delete bind threadid=" + temp1 + " | ";
\r
152 SQLite3DB::Statement deleteotherst=m_db->Prepare("DELETE FROM tblThread WHERE ThreadID IN (SELECT tblThread.ThreadID FROM tblThreadPost INNER JOIN tblThread ON tblThreadPost.ThreadID=tblThread.ThreadID WHERE tblThread.BoardID=? AND tblThreadPost.MessageID=?);");
\r
155 SQLite3DB::Statement st4=m_db->Prepare("INSERT INTO tblThreadPost(ThreadID,MessageID,PostOrder) VALUES(?,?,?);");
\r
156 for(std::vector<MessageThread::threadnode>::const_iterator i=m_threadmessages.begin(); i!=m_threadmessages.end(); i++, count++)
\r
158 deleteotherst.Bind(0,boardid);
\r
159 deleteotherst.Bind(1,(*i).m_messageid);
\r
160 deleteotherst.Step();
\r
161 deleteotherst.Reset();
\r
165 std::string temp1("");
\r
166 std::string temp2("");
\r
167 StringFunctions::Convert(boardid,temp1);
\r
168 StringFunctions::Convert((*i).m_messageid,temp2);
\r
170 logmessage+="deleteother bind boardid=" + temp1 + " messageid=" + temp2 + " | ";
\r
173 st4.Bind(0,threadid);
\r
174 st4.Bind(1,(*i).m_messageid);
\r
181 std::string temp1("");
\r
182 std::string temp2("");
\r
183 std::string temp3("");
\r
184 StringFunctions::Convert(threadid,temp1);
\r
185 StringFunctions::Convert((*i).m_messageid,temp2);
\r
186 StringFunctions::Convert(count,temp3);
\r
188 logmessage+="insertthreadpost bind threadid=" + temp1 + " messageid=" + temp2 + " count=" + temp3 + " | ";
\r
195 SQLite3DB::Statement st2=m_db->Prepare("DELETE FROM tblThread WHERE ThreadID=?;");
\r
196 st2.Bind(0,threadid);
\r
201 std::string temp1("");
\r
202 StringFunctions::Convert(threadid,temp1);
\r
204 logmessage+="delete thread bind threadid=" + temp1 + " | ";
\r
207 m_log->trace("ThreadBuilder::Build deleted thread");
\r
212 //m_db->Execute("COMMIT;");
\r
216 m_log->trace(logmessage);
\r