moved winix directories to winixdsubdirectory
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@1028 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
289
winixd/plugins/ticket/tdb.cpp
Normal file
289
winixd/plugins/ticket/tdb.cpp
Normal file
@@ -0,0 +1,289 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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<rows ; ++i)
|
||||
{
|
||||
ReadTicketPar(r, tic_par, i);
|
||||
ticket.par_tab.push_back(tic_par);
|
||||
}
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Error TDb::GetTickets(const std::vector<long> & file_id_tab, std::vector<Ticket> & 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<rows ; ++i)
|
||||
{
|
||||
long file_id = AssertValueLong(r, i, cfileid);
|
||||
|
||||
if( i==0 || last_file_id != file_id )
|
||||
{
|
||||
ticket_tab.push_back(empty_ticket);
|
||||
ticket_tab.back().file_id = file_id;
|
||||
last_file_id = file_id;
|
||||
}
|
||||
|
||||
ReadTicketPar(r, tic_par, i);
|
||||
ticket_tab.back().par_tab.push_back(tic_par);
|
||||
}
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error TDb::AddTicket(const Ticket & ticket)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
for(size_t i=0 ; i<ticket.par_tab.size() ; ++i)
|
||||
{
|
||||
query.Clear();
|
||||
query << R("insert into plugins.ticket (file_id, param, intv) values (")
|
||||
<< ticket.file_id
|
||||
<< ticket.par_tab[i].param
|
||||
<< ticket.par_tab[i].intv
|
||||
//<< ticket.par_tab[i].decv
|
||||
<< R(");");
|
||||
|
||||
r = AssertQuery(query);
|
||||
AssertResult(r, PGRES_COMMAND_OK);
|
||||
}
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error TDb::RemoveAddTicket(const Ticket & ticket)
|
||||
{
|
||||
Error status = BeginTrans();
|
||||
|
||||
if( status != WINIX_ERR_OK )
|
||||
return status;
|
||||
|
||||
status = RemoveTicket(ticket.file_id);
|
||||
|
||||
if( status == WINIX_ERR_OK )
|
||||
status = AddTicket(ticket);
|
||||
|
||||
if( status == WINIX_ERR_OK )
|
||||
status = CommitTrans();
|
||||
else
|
||||
RollbackTrans();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Error TDb::RemoveTicket(long file_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
query.Clear();
|
||||
query << R("delete from plugins.ticket where file_id=") << file_id << R(";");
|
||||
|
||||
r = AssertQuery(query);
|
||||
AssertResult(r, PGRES_COMMAND_OK);
|
||||
|
||||
long rows = AffectedRows(r);
|
||||
|
||||
if( rows > 0 )
|
||||
log << log2 << "TicketDb: deleted " << rows << " rows from plugins.ticket" << logend;
|
||||
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
Reference in New Issue
Block a user