WIP: remove the old database abstraction layer

remove such classes:
- DbBase
- DbConn
- DbTextStream
- Db

while here:
- remove: TextStream, SLog, TexTextStream
This commit is contained in:
2024-06-22 18:03:54 +02:00
parent 5d457f3d4b
commit 6aa100f12c
138 changed files with 6658 additions and 12402 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -204,12 +204,9 @@ using namespace Ticket;
info.plugin->Assign(WINIX_END_REQUEST, EndRequest);
info.plugin->Assign(WINIX_PL_TICKET_LOAD_TICKETS, ShowTicketsFromDir);
tdb.set_dependency((WinixModelDeprecated*)info.functions);
tdb.SetConn(info.db->GetConn());
tdb.LogQueries(info.config->log_db_query);
ticket_info.set_dependency((WinixRequest*)info.functions);
ticket_info.SetDb(info.db);
ticket_info.set_dependency((WinixModelDeprecated*)info.functions);
ticket_info.SetTDb(&tdb);
//ticket_info.SetConfig(info.config);
ticket_info.SetSystem(info.system);

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2014, Tomasz Sowa
* Copyright (c) 2010-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -33,257 +33,114 @@
*/
#include "tdb.h"
#include "core/log.h"
namespace Winix
{
namespace Ticket
namespace Winix::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);
morm::Finder<TicketParam> finder(model_connector);
if( Rows(r)==1 && Cols(r)==1 )
is = AssertValueInt(r, 0, 0) != 0;
}
catch(const Error &)
{
}
ClearResult(r);
// it would be better to have a SelectHelper in winix models
// so we could use a 'select count(*)'
TicketParam ticket_param = finder.select().where().eq(L"file_id", file_id).raw("LIMIT 1").get();
return is;
return ticket_param.found();
}
void TDb::SetTicketColumns(PGresult * r)
void TDb::GetTicket(long file_id, Ticket & ticket)
{
cfileid = AssertColumn(r, "file_id");
cparam = AssertColumn(r, "param");
cintv = AssertColumn(r, "intv");
cdecv = AssertColumn(r, "decv");
}
morm::Finder<TicketParam> finder(model_connector);
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;
ticket.file_id = file_id;
ticket.par_tab = finder.select().where().eq(L"file_id", file_id).get_vector();
}
Error TDb::GetTickets(const std::vector<long> & file_id_tab, std::vector<Ticket> & ticket_tab, bool clear_tab)
void 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
if( !file_id_tab.empty() )
{
// 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;");
morm::Finder<TicketParam> finder(model_connector);
std::vector<TicketParam> tmp;
finder.select().where().in(L"file_id", file_id_tab).raw("ORDER BY file_id, param").get_vector(tmp);
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)
for(size_t i=0 ; i < tmp.size() ; ++i)
{
long file_id = AssertValueLong(r, i, cfileid);
if( i==0 || last_file_id != file_id )
if( i==0 || last_file_id != tmp[i].file_id )
{
ticket_tab.push_back(empty_ticket);
ticket_tab.back().file_id = file_id;
last_file_id = file_id;
ticket_tab.back().file_id = tmp[i].file_id;
last_file_id = tmp[i].file_id;
}
ReadTicketPar(r, tic_par, i);
ticket_tab.back().par_tab.push_back(tic_par);
ticket_tab.back().par_tab.push_back(tmp[i]);
}
}
catch(const Error & e)
{
status = e;
}
ClearResult(r);
return status;
}
Error TDb::AddTicket(const Ticket & ticket)
bool TDb::AddTicket(Ticket & ticket)
{
PGresult * r = 0;
Error status = WINIX_ERR_OK;
try
bool status = true;
for(TicketParam & ticket_param : ticket.par_tab)
{
for(size_t i=0 ; i<ticket.par_tab.size() ; ++i)
if( !ticket_param.insert() )
status = false;
}
return status;
}
bool TDb::RemoveAddTicket(Ticket & ticket)
{
morm::Transaction transaction(model_connector);
bool ok = true;
ok = ok && RemoveTicket(ticket.file_id);
ok = ok && AddTicket(ticket);
transaction.set_successful(ok);
if( !transaction.finish() )
ok = false;
return ok;
}
bool TDb::RemoveTicket(long file_id)
{
bool status = false;
if( model_connector )
{
morm::DbConnector * db_connector = model_connector->get_db_connector();
if( db_connector )
{
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);
pt::TextStream sql;
sql << "DELETE FROM plugins.ticket WHERE file_id=" << file_id;
status = db_connector->query(sql);
}
}
catch(const Error & e)
{
status = e;
}
ClearResult(r);
return status;
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

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2014, Tomasz Sowa
* Copyright (c) 2010-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -37,49 +37,34 @@
#include <vector>
#include "ticket.h"
#include "db/dbbase.h"
#include "core/error.h"
#include "core/winixmodeldeprecated.h"
namespace Winix
namespace Winix::Ticket
{
namespace Ticket
{
class TDb : public DbBase
class TDb : public WinixModelDeprecated
{
public:
bool IsTicket(long file_id);
Error GetTicket(long file_id, Ticket & ticket);
Error GetTickets(const std::vector<long> & file_id_tab, std::vector<Ticket> & ticket_tab, bool clear_tab = true);
Error AddTicket(const Ticket & ticket);
Error RemoveAddTicket(const Ticket & ticket); // first removing and then adding a ticket
Error RemoveTicket(long file_id);
bool IsTicket(long file_id);
void GetTicket(long file_id, Ticket & ticket);
void GetTickets(const std::vector<long> & file_id_tab, std::vector<Ticket> & ticket_tab, bool clear_tab = true);
bool AddTicket(Ticket & ticket);
bool RemoveAddTicket(Ticket & ticket); // first removing and then adding a ticket
bool RemoveTicket(long file_id);
private:
DbTextStream query;
Ticket empty_ticket;
Ticket::TicketParam tic_par;
int cfileid;
int cparam;
int cintv;
int cdecv;
void SetTicketColumns(PGresult * r);
void ReadTicketPar(PGresult * r, Ticket::TicketParam & par, int row);
};
} // namespace
} // namespace Winix
}
#endif

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2008-2021, Tomasz Sowa
* Copyright (c) 2008-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -77,7 +77,7 @@ bool is_value; // true if value for such a param was defined
bool is_in_ticket_par; // true if the value is defined in ticket.par_tab
// else the value can be defined in meta (or not at all)
Ticket::TicketParam * ticket_par; // if is_in_ticket_par is true
TicketParam * ticket_par; // if is_in_ticket_par is true
pt::Space * value_meta; // if is_in_ticket_par is false and if there is such a value in meta

View File

@@ -0,0 +1,68 @@
/*
* 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) 2024, 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 "ticket.h"
#include <algorithm>
namespace Winix::Ticket
{
Ticket::Ticket()
{
Clear();
}
void Ticket::Clear()
{
file_id = -1;
par_tab.clear();
}
void Ticket::SortParTab()
{
std::sort(par_tab.begin(), par_tab.end(), [](const TicketParam & par1, const TicketParam & par2) -> bool {
return par1.param < par2.param;
});
}
}

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2008-2014, Tomasz Sowa
* Copyright (c) 2008-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -36,73 +36,32 @@
#define headerfile_winix_plugins_ticket_ticket
#include <vector>
#include <string>
#include <algorithm>
#include "ticketparam.h"
namespace Winix
namespace Winix::Ticket
{
namespace Ticket
class Ticket
{
public:
struct Ticket
{
struct TicketParam
{
long param;
long intv;
std::wstring decv; // !! temporarily as a string (in the future there'll be a Dec type from ttmath)
void Clear()
{
param = 0;
intv = 0;
decv.clear();
}
TicketParam()
{
Clear();
}
};
Ticket();
long file_id;
typedef std::vector<TicketParam> ParTab;
ParTab par_tab;
void Clear();
void SortParTab();
Ticket()
{
Clear();
}
void Clear()
{
file_id = -1;
par_tab.clear();
}
struct Sort
{
bool operator()(const TicketParam & par1, const TicketParam & par2)
{
return par1.param < par2.param;
}
};
void SortParTab()
{
std::sort(par_tab.begin(), par_tab.end(), Sort());
}
};
} // namespace
} // namespace Winix
}
#endif

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2021, Tomasz Sowa
* Copyright (c) 2010-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -65,12 +65,6 @@ void TicketInfo::SetTDb(TDb * ptdb)
}
void TicketInfo::SetDb(Db * pdb)
{
db = pdb;
}
//void TicketInfo::SetConfig(Config * pconfig)
//{
// config = pconfig;
@@ -261,7 +255,7 @@ void TicketInfo::FindCurrentConf()
void TicketInfo::CheckMinMaxValue(pt::Space & space, Ticket::TicketParam & par)
void TicketInfo::CheckMinMaxValue(pt::Space & space, TicketParam & par)
{
std::wstring * type = space.get_wstr(L"type");
@@ -352,7 +346,7 @@ pt::Space & TicketInfo::FindAddMetaByParam(pt::Space & meta, long param)
bool TicketInfo::ReadTicketValue(pt::Space & config_param, long param_id, Ticket::TicketParam & par, const std::wstring & value, pt::Space & meta)
bool TicketInfo::ReadTicketValue(pt::Space & config_param, long param_id, TicketParam & par, const std::wstring & value, pt::Space & meta)
{
if( config_param.is_equal(L"type", L"integer") ||
config_param.is_equal(L"type", L"progress") ||
@@ -491,7 +485,8 @@ void TicketInfo::ReadTicketParam(pt::Space & config_param, Ticket & ticket, long
void TicketInfo::ReadTicketParam(Ticket & ticket, long param_id, const std::wstring & value, pt::Space & meta)
{
ticket_param.Clear();
ticket_param.set_connector(model_connector);
ticket_param.clear();
pt::Space::TableType * params = cur_conf->get_table(L"params");
if( params )
@@ -640,7 +635,7 @@ void TicketInfo::RemoveTicket(long file_id)
*
*/
if( tdb->GetTicket(file_id, rm_ticket) == WINIX_ERR_OK )
if( tdb->IsTicket(file_id) )
{
tdb->RemoveTicket(file_id);
}

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2021, Tomasz Sowa
* Copyright (c) 2010-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -39,7 +39,6 @@
#include "ticket.h"
#include "core/system.h"
#include "functions/functions.h"
#include "db/db.h"
#include "tdb.h"
#include "space/spaceparser.h"
#include "core/winixmodeldeprecated.h"
@@ -74,7 +73,6 @@ public:
TicketInfo();
void SetTDb(TDb * ptdb);
void SetDb(Db * pdb);
//void SetConfig(Config * pconfig);
void SetSystem(System * psystem);
void SetCur(Cur * pcur);
@@ -139,7 +137,6 @@ public:
private:
Db * db;
TDb * tdb;
//Config * config;
System * system;
@@ -147,7 +144,7 @@ private:
Functions * functions;
// for reading parameters
Ticket::TicketParam ticket_param;
TicketParam ticket_param;
// for adding a new image/file to a ticket
Item file;
@@ -165,9 +162,6 @@ private:
Ticket ticket_empty;
Item item_empty;
// for removing a ticket
Ticket rm_ticket;
bool GetConfContent(const std::wstring & path);
bool ParseTicketConf(long mount_dir_id, const std::wstring & path);
void ReadTicketConf(Mounts & mounts, bool skip_existing_configs);
@@ -177,8 +171,8 @@ private:
pt::Space & FindAddMetaByParam(pt::Space & meta, long param);
void CheckMinMaxValue(pt::Space & space, Ticket::TicketParam & par);
bool ReadTicketValue(pt::Space & space, long param_id, Ticket::TicketParam & par, const std::wstring & value, pt::Space & meta);
void CheckMinMaxValue(pt::Space & space, TicketParam & par);
bool ReadTicketValue(pt::Space & space, long param_id, TicketParam & par, const std::wstring & value, pt::Space & meta);
void ReadTicketValue(pt::Space & space, long param_id, const PostFile & value, pt::Space & meta, Item & upload_dir);
void ReadTicketValue(pt::Space & space, long param_id, const PostFile & value, pt::Space & meta);
void ReadTicketParam(pt::Space & space, Ticket & ticket, long param_id, const std::wstring & value, pt::Space & meta);

View File

@@ -0,0 +1,94 @@
/*
* 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) 2024, 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 "ticketparam.h"
namespace Winix::Ticket
{
void TicketParam::fields()
{
field(L"file_id", file_id);
field(L"param", param);
field(L"intv", intv);
field(L"decv", decv);
}
void TicketParam::after_insert()
{
//get_last_sequence_for_primary_key(L"", );
}
void TicketParam::table()
{
table_name(L"plugins", L"ticket");
}
bool TicketParam::do_migration(int & current_table_version)
{
bool ok = true;
ok = ok && morm::Model::do_migration(current_table_version, 1, this, &TicketParam::do_migration_to_1);
return ok;
}
bool TicketParam::do_migration_to_1()
{
const char * str = R"sql(
CREATE TABLE plugins.ticket (
file_id bigint,
param bigint,
intv bigint,
decv numeric(16,2)
);
)sql";
return db_query(str);
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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) 2024, 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.
*
*/
#ifndef headerfile_winix_plugins_ticket_ticketparam
#define headerfile_winix_plugins_ticket_ticketparam
#include <vector>
#include <string>
#include <algorithm>
#include "model.h"
namespace Winix::Ticket
{
class TicketParam : public morm::Model
{
public:
long file_id;
long param;
long intv;
std::wstring decv;
void table();
protected:
void fields();
void after_insert();
bool do_migration(int & current_table_version);
bool do_migration_to_1();
};
}
#endif