2010-09-30 00:41:34 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2010-2014, Tomasz Sowa
|
2010-09-30 00:41:34 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2010-09-30 00:41:34 +02:00
|
|
|
*/
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
#include "tdb.h"
|
|
|
|
#include "core/log.h"
|
2010-09-30 00:41:34 +02:00
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace Ticket
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
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);
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
if( Rows(r)==1 && Cols(r)==1 )
|
|
|
|
is = AssertValueInt(r, 0, 0) != 0;
|
|
|
|
}
|
|
|
|
catch(const Error &)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-16 11:12:38 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
Error TDb::GetTicket(long file_id, Ticket & ticket)
|
2010-09-29 23:24:34 +02:00
|
|
|
{
|
|
|
|
PGresult * r = 0;
|
|
|
|
Error status = WINIX_ERR_OK;
|
2010-10-19 02:31:20 +02:00
|
|
|
ticket.Clear();
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-01-21 17:16:52 +01:00
|
|
|
// they should be sorted by param (they are used in a binary search later)
|
2010-09-29 23:24:34 +02:00
|
|
|
query.Clear();
|
2012-01-16 11:12:38 +01:00
|
|
|
query << R("select file_id, param, intv, decv from plugins.ticket "
|
2011-01-21 17:16:52 +01:00
|
|
|
"where ticket.file_id=") << file_id << R(" order by param asc;");
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
r = AssertQuery(query);
|
|
|
|
AssertResult(r, PGRES_TUPLES_OK);
|
2012-01-16 11:12:38 +01:00
|
|
|
SetTicketColumns(r);
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
int rows = Rows(r);
|
2011-01-05 22:24:11 +01:00
|
|
|
ticket.file_id = file_id;
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2012-01-16 11:12:38 +01:00
|
|
|
if( rows == 0 )
|
|
|
|
throw Error(WINIX_ERR_NO_TICKET); // !! do we need this kind or error?
|
2010-10-19 02:31:20 +02:00
|
|
|
|
2012-01-16 11:12:38 +01:00
|
|
|
if( ticket.par_tab.capacity() < ticket.par_tab.size() + rows )
|
|
|
|
ticket.par_tab.reserve(ticket.par_tab.size() + rows);
|
2011-01-24 00:23:24 +01:00
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
for(int i=0 ; i<rows ; ++i)
|
|
|
|
{
|
2012-01-16 11:12:38 +01:00
|
|
|
ReadTicketPar(r, tic_par, i);
|
|
|
|
ticket.par_tab.push_back(tic_par);
|
2010-10-19 02:31:20 +02:00
|
|
|
}
|
2010-09-29 23:24:34 +02:00
|
|
|
}
|
|
|
|
catch(const Error & e)
|
|
|
|
{
|
|
|
|
status = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
|
|
|
|
|
2012-01-16 11:12:38 +01:00
|
|
|
Error TDb::GetTickets(const std::vector<long> & file_id_tab, std::vector<Ticket> & ticket_tab, bool clear_tab)
|
2010-09-29 23:24:34 +02:00
|
|
|
{
|
|
|
|
PGresult * r = 0;
|
|
|
|
Error status = WINIX_ERR_OK;
|
2012-01-16 11:12:38 +01:00
|
|
|
|
|
|
|
if( clear_tab )
|
|
|
|
ticket_tab.clear();
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
if( file_id_tab.empty() )
|
2010-10-19 02:31:20 +02:00
|
|
|
return status;
|
|
|
|
|
2010-09-29 23:24:34 +02:00
|
|
|
try
|
|
|
|
{
|
2011-01-21 17:16:52 +01:00
|
|
|
// 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)
|
2010-09-29 23:24:34 +02:00
|
|
|
query.Clear();
|
2012-01-16 11:12:38 +01:00
|
|
|
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;");
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
r = AssertQuery(query);
|
|
|
|
AssertResult(r, PGRES_TUPLES_OK);
|
2012-01-16 11:12:38 +01:00
|
|
|
SetTicketColumns(r);
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
int rows = Rows(r);
|
2011-01-05 22:24:11 +01:00
|
|
|
long last_file_id = -1;
|
2012-01-16 11:12:38 +01:00
|
|
|
empty_ticket.Clear();
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
for(int i=0 ; i<rows ; ++i)
|
|
|
|
{
|
2012-01-16 11:12:38 +01:00
|
|
|
long file_id = AssertValueLong(r, i, cfileid);
|
2010-10-19 02:31:20 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
if( i==0 || last_file_id != file_id )
|
2010-10-19 02:31:20 +02:00
|
|
|
{
|
2012-01-16 11:12:38 +01:00
|
|
|
ticket_tab.push_back(empty_ticket);
|
2011-01-05 22:24:11 +01:00
|
|
|
ticket_tab.back().file_id = file_id;
|
|
|
|
last_file_id = file_id;
|
2010-10-19 02:31:20 +02:00
|
|
|
}
|
|
|
|
|
2012-01-16 11:12:38 +01:00
|
|
|
ReadTicketPar(r, tic_par, i);
|
|
|
|
ticket_tab.back().par_tab.push_back(tic_par);
|
2010-09-29 23:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const Error & e)
|
|
|
|
{
|
|
|
|
status = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
Error TDb::AddTicket(const Ticket & ticket)
|
2010-09-29 23:24:34 +02:00
|
|
|
{
|
|
|
|
PGresult * r = 0;
|
|
|
|
Error status = WINIX_ERR_OK;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2010-10-19 02:31:20 +02:00
|
|
|
for(size_t i=0 ; i<ticket.par_tab.size() ; ++i)
|
|
|
|
{
|
|
|
|
query.Clear();
|
2012-01-16 11:12:38 +01:00
|
|
|
query << R("insert into plugins.ticket (file_id, param, intv) values (")
|
2011-01-05 22:24:11 +01:00
|
|
|
<< ticket.file_id
|
2010-10-19 02:31:20 +02:00
|
|
|
<< ticket.par_tab[i].param
|
2012-01-16 11:12:38 +01:00
|
|
|
<< ticket.par_tab[i].intv
|
|
|
|
//<< ticket.par_tab[i].decv
|
2010-10-19 02:31:20 +02:00
|
|
|
<< R(");");
|
|
|
|
|
|
|
|
r = AssertQuery(query);
|
|
|
|
AssertResult(r, PGRES_COMMAND_OK);
|
|
|
|
}
|
2010-09-29 23:24:34 +02:00
|
|
|
}
|
|
|
|
catch(const Error & e)
|
|
|
|
{
|
|
|
|
status = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
Error TDb::RemoveAddTicket(const Ticket & ticket)
|
2010-09-29 23:24:34 +02:00
|
|
|
{
|
2010-10-19 02:31:20 +02:00
|
|
|
Error status = BeginTrans();
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
if( status != WINIX_ERR_OK )
|
|
|
|
return status;
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
status = RemoveTicket(ticket.file_id);
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
if( status == WINIX_ERR_OK )
|
|
|
|
status = AddTicket(ticket);
|
2010-09-29 23:24:34 +02:00
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
if( status == WINIX_ERR_OK )
|
|
|
|
status = CommitTrans();
|
|
|
|
else
|
|
|
|
RollbackTrans();
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
Error TDb::RemoveTicket(long file_id)
|
2010-09-29 23:24:34 +02:00
|
|
|
{
|
|
|
|
PGresult * r = 0;
|
|
|
|
Error status = WINIX_ERR_OK;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
query.Clear();
|
2011-01-05 22:24:11 +01:00
|
|
|
query << R("delete from plugins.ticket where file_id=") << file_id << R(";");
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
r = AssertQuery(query);
|
|
|
|
AssertResult(r, PGRES_COMMAND_OK);
|
|
|
|
|
|
|
|
long rows = AffectedRows(r);
|
|
|
|
|
|
|
|
if( rows > 0 )
|
2011-01-20 19:37:51 +01:00
|
|
|
log << log2 << "TicketDb: deleted " << rows << " rows from plugins.ticket" << logend;
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
catch(const Error & e)
|
|
|
|
{
|
|
|
|
status = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|