/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2010-2014, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ #include "tdb.h" #include "core/log.h" namespace Winix { namespace Ticket { bool TDb::IsTicket(long file_id) { PGresult * r = 0; bool is = false; try { query.Clear(); query << R("select count(*) from plugins.ticket " "where ticket.file_id=") << file_id << R(";"); r = AssertQuery(query); AssertResult(r, PGRES_TUPLES_OK); if( Rows(r)==1 && Cols(r)==1 ) is = AssertValueInt(r, 0, 0) != 0; } catch(const Error &) { } ClearResult(r); return is; } void TDb::SetTicketColumns(PGresult * r) { cfileid = AssertColumn(r, "file_id"); cparam = AssertColumn(r, "param"); cintv = AssertColumn(r, "intv"); cdecv = AssertColumn(r, "decv"); } void TDb::ReadTicketPar(PGresult * r, Ticket::TicketParam & par, int row) { par.Clear(); par.param = AssertValueInt(r, row, cparam); par.intv = AssertValueLong(r, row, cintv); AssertValueWide(r, row, cdecv, par.decv); // !! temporarily } Error TDb::GetTicket(long file_id, Ticket & ticket) { PGresult * r = 0; Error status = WINIX_ERR_OK; ticket.Clear(); try { // they should be sorted by param (they are used in a binary search later) query.Clear(); query << R("select file_id, param, intv, decv from plugins.ticket " "where ticket.file_id=") << file_id << R(" order by param asc;"); r = AssertQuery(query); AssertResult(r, PGRES_TUPLES_OK); SetTicketColumns(r); int rows = Rows(r); ticket.file_id = file_id; if( rows == 0 ) throw Error(WINIX_ERR_NO_TICKET); // !! do we need this kind or error? if( ticket.par_tab.capacity() < ticket.par_tab.size() + rows ) ticket.par_tab.reserve(ticket.par_tab.size() + rows); for(int i=0 ; i & file_id_tab, std::vector & ticket_tab, bool clear_tab) { PGresult * r = 0; Error status = WINIX_ERR_OK; if( clear_tab ) ticket_tab.clear(); if( file_id_tab.empty() ) return status; try { // ticket_tab must be sorted by file_id (they are used in a binary search later) // and items in a ticket should be sorted by param (they are used in a binary search later too) query.Clear(); query << R("select file_id, param, intv, decv from plugins.ticket " "where ticket.file_id in ") << file_id_tab << R(" order by file_id, param;"); r = AssertQuery(query); AssertResult(r, PGRES_TUPLES_OK); SetTicketColumns(r); int rows = Rows(r); long last_file_id = -1; empty_ticket.Clear(); for(int i=0 ; i 0 ) log << log2 << "TicketDb: deleted " << rows << " rows from plugins.ticket" << logend; } catch(const Error & e) { status = e; } ClearResult(r); return status; } } // namespace } // namespace Winix