/* * This file is a part of Winix * and is not publicly distributed * * Copyright (c) 2010, Tomasz Sowa * All rights reserved. * */ #include "tdb.h" #include "core/log.h" namespace Thread { Error Db::GetThreadByDirId(long dir_id, Thread & thread) { PGresult * r = 0; Error status = WINIX_ERR_OK; try { query.Clear(); query << R("select thread.id, thread.parent_id, thread.dir_id, thread.closed, thread.items, " "thread.last_item, item.date_modification, item.user_id " "from core.thread left join core.item on thread.last_item = item.id " "where thread.dir_id = ") << dir_id << R(";"); r = AssertQuery(query); AssertResult(r, PGRES_TUPLES_OK); int rows = Rows(r); if( rows > 1 ) log << log1 << "Db: there is more than one thread with dir_id: " << dir_id << logend; else if( rows == 0 ) throw Error(WINIX_ERR_NO_THREAD); int cid = AssertColumn(r, "id"); int cparent_id = AssertColumn(r, "parent_id"); int cdir_id = AssertColumn(r, "dir_id"); int cclosed = AssertColumn(r, "closed"); int citems = AssertColumn(r, "items"); int clast_item = AssertColumn(r, "last_item"); int cdate_modification = PQfnumber(r, "date_modification"); // !! tych kolumn może nie być? czemu PQfnumber a nie AssertColumn? int cuser_id = PQfnumber(r, "user_id"); thread.id = AssertValueLong(r, 0, cid); thread.parent_id = AssertValueLong(r, 0, cparent_id); thread.dir_id = AssertValueLong(r, 0, cdir_id); thread.closed = AssertValueLong(r, 0, cclosed) == 0 ? false : true; thread.items = AssertValueLong(r, 0, citems); thread.last_item.id = AssertValueLong(r, 0, clast_item); thread.last_item.date_modification = AssertValueTm(r, 0, cdate_modification); thread.last_item.user_id = AssertValueLong(r, 0, cuser_id); } catch(const Error & e) { status = e; } ClearResult(r); return status; } Error Db::GetThreads(long parent_id, std::vector & thread_tab) { PGresult * r = 0; Error status = WINIX_ERR_OK; try { query.Clear(); query << R("select thread.id, thread.parent_id, thread.dir_id, thread.closed, " "thread.items, thread.last_item, item.date_modification, item.user_id, item.guest_name " "from core.thread left join core.item on thread.last_item = item.id " "where thread.parent_id = ") << parent_id << R(" order by date_modification asc;"); r = AssertQuery(query); AssertResult(r, PGRES_TUPLES_OK); int rows = Rows(r); Thread thread; int cid = AssertColumn(r, "id"); int cparent_id = AssertColumn(r, "parent_id"); int cdir_id = AssertColumn(r, "dir_id"); int cclosed = AssertColumn(r, "closed"); int citems = AssertColumn(r, "items"); int clast_item = AssertColumn(r, "last_item"); int cdate_modification = PQfnumber(r, "date_modification"); // !! czemu tutaj jest pqfnumber zamiast assertcolumn? int cuser_id = PQfnumber(r, "user_id"); int cguest_name = PQfnumber(r, "guest_name"); for(int i=0 ; i 0 ) log << log2 << "Db: deleted " << rows << " rows from core.thread" << logend; } catch(const Error & e) { status = e; } ClearResult(r); return status; } } // namespace