winix/winixd/plugins/export/edb.cpp

239 lines
5.3 KiB
C++

/*
* 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) 2011-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 "edb.h"
#include "core/log.h"
namespace Winix
{
namespace Export
{
void EDb::SetDirs(Dirs * pdirs)
{
dirs = pdirs;
}
void EDb::SetExportCols(PGresult * r)
{
cid = AssertColumn(r, "id");
cuser_id = AssertColumn(r, "user_id");
cdir = AssertColumn(r, "dir");
cftp_id = AssertColumn(r, "ftp_id");
cftp_dir = AssertColumn(r, "ftp_dir");
cname = AssertColumn(r, "name");
cserver = AssertColumn(r, "server");
clogin = AssertColumn(r, "login");
cpass = AssertColumn(r, "pass");
cpass_type = AssertColumn(r, "pass_type");
ccan_change_ftp_params = AssertColumn(r, "can_change_ftp_params");
ccan_change_dir = AssertColumn(r, "can_change_dir");
chttp_server = AssertColumn(r, "http_server");
}
void EDb::SetExportValues(PGresult * r, int row, Export & exp)
{
exp.Clear();
exp.id = AssertValueLong(r, row, cid);
exp.user_id = AssertValueLong(r, row, cuser_id);
exp.ftp_id = AssertValueLong(r, row, cftp_id);
exp.ftp_pass_type = AssertValueInt(r, row, cpass_type);
AssertValueWide(r, row, cdir, exp.dir);
AssertValueWide(r, row, cftp_dir, exp.ftp_dir);
AssertValueWide(r, row, cname, exp.ftp_name);
AssertValueWide(r, row, cserver, exp.ftp_server);
AssertValueWide(r, row, clogin, exp.ftp_login);
AssertValueWide(r, row, chttp_server, exp.http_server);
AssertValueWide(r, row, cpass, exp.ftp_pass);
exp.can_change_ftp_params = AssertValueBool(r, row, ccan_change_ftp_params);
exp.can_change_dir = AssertValueBool(r, row, ccan_change_dir);
}
bool EDb::GetExport(long user_id, std::vector<Export> & export_tab, bool clear_tab)
{
if( clear_tab )
export_tab.clear();
PGresult * r = 0;
bool result = true;
try
{
query.Clear();
query << R( "select export.id, user_id, dir, ftp_id, ftp_dir, can_change_ftp_params, can_change_dir, "
"http_server, name, server, login, pass, pass_type from plugins.export "
"left join plugins.export_ftp on ftp_id = export_ftp.id where user_id = ")
<< user_id
<< R(";");
r = AssertQuery(query);
AssertResult(r, PGRES_TUPLES_OK);
SetExportCols(r);
int rows = Rows(r);
for(int i=0 ; i<rows ; ++i)
{
SetExportValues(r, i, exp);
export_tab.push_back(exp);
}
}
catch(const Error &)
{
result = false;
}
ClearResult(r);
return result;
}
bool EDb::GetExport(long id, Export & exp)
{
exp.Clear();
PGresult * r = 0;
bool result = false;
try
{
query.Clear();
query << R( "select export.id, user_id, dir, ftp_id, ftp_dir, can_change_ftp_params, can_change_dir, "
"http_server, name, server, login, pass, pass_type from plugins.export "
"left join plugins.export_ftp on ftp_id = export_ftp.id where export.id = ")
<< id
<< R(";");
r = AssertQuery(query);
AssertResult(r, PGRES_TUPLES_OK);
SetExportCols(r);
if( Rows(r) == 1 )
{
SetExportValues(r, 0, exp);
result = true;
}
}
catch(const Error &)
{
}
ClearResult(r);
return result;
}
bool EDb::GetExportDirs(std::vector<ExportDir> & export_tab, bool clear_tab)
{
if( clear_tab )
export_tab.clear();
PGresult * r = 0;
bool result = true;
try
{
query.Clear();
query << R( "select export.id, dir from plugins.export;");
r = AssertQuery(query);
AssertResult(r, PGRES_TUPLES_OK);
int rows = Rows(r);
cid = AssertColumn(r, "id");
cdir = AssertColumn(r, "dir");
for(int i=0 ; i<rows ; ++i)
{
exp_dir.id = AssertValueLong(r, i, cid);
AssertValueWide(r, i, cdir, dir_temp);
Item * pdir = dirs->GetDir(dir_temp);
if( pdir )
{
exp_dir.dir_id = pdir->id;
export_tab.push_back(exp_dir);
}
else
{
slog << "There is not such a directory: " << dir_temp << " (skipping)" << logend;
}
}
}
catch(const Error &)
{
result = false;
}
ClearResult(r);
return result;
}
}
} // namespace Winix