added: function adduser
changed: errors (removed enum, there are macros now) added: error messages to locales (winix_err_NN) removed: templates: err_abuse.html err_others.html git-svn-id: svn://ttmath.org/publicrep/winix/trunk@593 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
138
core/db.cpp
138
core/db.cpp
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2009, Tomasz Sowa
|
||||
@@ -108,7 +108,7 @@ bool was_connection = true;
|
||||
else
|
||||
{
|
||||
log << log1 << "Db: Connection to db server cannot be established" << logend;
|
||||
throw Error(Error::db_fatal_error_during_connecting);
|
||||
throw Error(WINIX_ERR_DB_FATAL_ERROR_DURING_CONNECTING);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ PGresult * Db::AssertQuery(const std::string & q)
|
||||
log << log1 << "Db: Problem with query: \"" << q << '\"' << logend;
|
||||
log << log1 << "Db: " << PQerrorMessage(pg_conn) << logend;
|
||||
|
||||
throw Error(Error::db_incorrect_query);
|
||||
throw Error(WINIX_ERR_DB_INCORRECT_QUERY);
|
||||
}
|
||||
|
||||
return r;
|
||||
@@ -171,7 +171,7 @@ void Db::AssertResultStatus(PGresult * r, ExecStatusType t)
|
||||
{
|
||||
log << "Db: Incorrect result status: " << PQerrorMessage(pg_conn) << logend;
|
||||
|
||||
throw Error(Error::db_incorrent_result_status);
|
||||
throw Error(WINIX_ERR_DB_INCORRENT_RESULT_STATUS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ int Db::AssertColumn(PGresult * r, const char * column_name)
|
||||
{
|
||||
log << log1 << "Db: there is no column: " << column_name << logend;
|
||||
|
||||
throw Error(Error::db_no_column);
|
||||
throw Error(WINIX_ERR_DB_NO_COLUMN);
|
||||
}
|
||||
|
||||
return c;
|
||||
@@ -200,7 +200,7 @@ const char * Db::AssertValue(PGresult * r, int row, int col)
|
||||
{
|
||||
log << log1 << "Db: there is no such an item in the result, row:" << row << ", col:" << col << logend;
|
||||
|
||||
throw Error(Error::no_item);
|
||||
throw Error(WINIX_ERR_NO_ITEM);
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -234,12 +234,12 @@ bool Db::CheckUser(std::string & login, std::string & password, long & user_id)
|
||||
int rows = PQntuples(r);
|
||||
|
||||
if( rows == 0 )
|
||||
throw Error(Error::db_incorrect_login);
|
||||
throw Error(WINIX_ERR_DB_INCORRECT_LOGIN);
|
||||
|
||||
if( rows > 1 )
|
||||
{
|
||||
log << log1 << "Db: there is more than one user: " << login << " (with the same password)" << logend;
|
||||
throw Error(Error::db_more_than_one_login);
|
||||
throw Error(WINIX_ERR_DB_MORE_THAN_ONE_LOGIN);
|
||||
}
|
||||
|
||||
int cuser_id = AssertColumn(r, "id");
|
||||
@@ -262,6 +262,40 @@ return user_ok;
|
||||
|
||||
|
||||
|
||||
Error Db::AddUser(User & user, const std::string & password)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "insert into core.user (login, password, super_user, email, cms_notify, thread_notify) values (";
|
||||
query << '\'' << Escape(user.name) << "', ";
|
||||
query << '\'' << Escape(password) << "', ";
|
||||
query << '\'' << static_cast<int>(user.super_user) << "', ";
|
||||
query << '\'' << Escape(user.email) << "', ";
|
||||
query << '\'' << user.cms_notify << "', ";
|
||||
query << '\'' << user.thread_notify << "');";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
|
||||
user.id = AssertCurrval("core.user_id_seq");
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -401,7 +435,7 @@ long Db::AssertCurrval(const char * table)
|
||||
if( PQntuples(r) != 1 )
|
||||
{
|
||||
log << log1 << "Db: error (currval) for table: " << table << ", " << PQerrorMessage(pg_conn) << logend;
|
||||
throw Error(Error::db_err_currval);
|
||||
throw Error(WINIX_ERR_DB_ERR_CURRVAL);
|
||||
}
|
||||
|
||||
long res = strtol( AssertValue(r, 0, 0), 0, 10 );
|
||||
@@ -413,7 +447,7 @@ return res;
|
||||
Error Db::AddItemIntoItem(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
bool url_without_id = false;
|
||||
|
||||
try
|
||||
@@ -468,7 +502,7 @@ return result;
|
||||
Error Db::AddItemIntoContent(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -499,14 +533,14 @@ return result;
|
||||
|
||||
Error Db::AddItem(Item & item)
|
||||
{
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
if( item.type == Item::file )
|
||||
result = AddItemIntoContent(item);
|
||||
else
|
||||
item.content_id = -1;
|
||||
|
||||
if( result == Error::ok )
|
||||
if( result == WINIX_ERR_OK )
|
||||
result = AddItemIntoItem(item);
|
||||
|
||||
return result;
|
||||
@@ -519,7 +553,7 @@ return result;
|
||||
Error Db::EditItemInItem(Item & item, bool with_url)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
bool url_without_id = false;
|
||||
|
||||
try
|
||||
@@ -578,7 +612,7 @@ return result;
|
||||
Error Db::EditItemInContent(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -607,7 +641,7 @@ return result;
|
||||
Error Db::EditItemGetId(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -620,7 +654,7 @@ Error Db::EditItemGetId(Item & item)
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
if( PQntuples(r) != 1 || PQnfields(r) != 2 )
|
||||
throw Error(Error::no_item);
|
||||
throw Error(WINIX_ERR_NO_ITEM);
|
||||
|
||||
// we cannot use AssertColumn() with a name because both columns are called 'id'
|
||||
item.id = atol( AssertValue(r, 0, 0) );
|
||||
@@ -640,7 +674,7 @@ return result;
|
||||
Error Db::EditItemGetContentId(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -654,7 +688,7 @@ Error Db::EditItemGetContentId(Item & item)
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
if( PQntuples(r) != 1 || PQnfields(r) != 1 )
|
||||
throw Error(Error::no_item);
|
||||
throw Error(WINIX_ERR_NO_ITEM);
|
||||
|
||||
item.content_id = atol( AssertValue(r, 0, 0) );
|
||||
}
|
||||
@@ -673,19 +707,19 @@ return result;
|
||||
// !! moze nazwa poprostu EditItem (nie trzeba tego ById) ? (sprawdzic czy nie koliduje z inna nazwa)
|
||||
Error Db::EditItemById(Item & item, bool with_url)
|
||||
{
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
// !! dla katalogow nie testowane jeszcze
|
||||
|
||||
if( item.type == Item::file )
|
||||
result = EditItemGetContentId(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
if( result == WINIX_ERR_OK )
|
||||
{
|
||||
if( item.type == Item::file )
|
||||
result = EditItemInContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
if( result == WINIX_ERR_OK )
|
||||
result = EditItemInItem(item, with_url);
|
||||
}
|
||||
|
||||
@@ -700,11 +734,11 @@ Error Db::EditItemByUrl(Item & item, bool with_url)
|
||||
{
|
||||
Error result = EditItemGetId(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
if( result == WINIX_ERR_OK )
|
||||
{
|
||||
result = EditItemInContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
if( result == WINIX_ERR_OK )
|
||||
result = EditItemInItem(item, with_url);
|
||||
}
|
||||
|
||||
@@ -716,7 +750,7 @@ return result;
|
||||
Error Db::EditDefaultItem(long id, long new_default_item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -735,7 +769,7 @@ Error Db::EditDefaultItem(long id, long new_default_item)
|
||||
|
||||
if( rows == 0 )
|
||||
{
|
||||
result = Error::no_item;
|
||||
result = WINIX_ERR_NO_ITEM;
|
||||
log << log1 << "Db: EditDefaultItem: no such item, id: " << id << logend;
|
||||
}
|
||||
}
|
||||
@@ -914,7 +948,7 @@ void Db::GetItem(std::vector<Item> & item_table, long id)
|
||||
Error Db::GetItem(long parent_id, const std::string & url, Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -930,7 +964,7 @@ Error Db::GetItem(long parent_id, const std::string & url, Item & item)
|
||||
int rows = PQntuples(r);
|
||||
|
||||
if( rows == 0 )
|
||||
throw Error(Error::no_item);
|
||||
throw Error(WINIX_ERR_NO_ITEM);
|
||||
|
||||
ItemColumns col;
|
||||
col.SetColumns(r);
|
||||
@@ -950,7 +984,7 @@ return result;
|
||||
Error Db::GetItemById(long item_id, Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -965,7 +999,7 @@ Error Db::GetItemById(long item_id, Item & item)
|
||||
int rows = PQntuples(r);
|
||||
|
||||
if( rows == 0 )
|
||||
throw Error(Error::no_item);
|
||||
throw Error(WINIX_ERR_NO_ITEM);
|
||||
|
||||
ItemColumns col;
|
||||
col.SetColumns(r);
|
||||
@@ -1074,7 +1108,7 @@ return result;
|
||||
Error Db::EditPrivById(Item & item, long id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1106,7 +1140,7 @@ return result;
|
||||
|
||||
Error Db::DelDirById(long id)
|
||||
{
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
PGresult * r = 0;
|
||||
const char * crows;
|
||||
|
||||
@@ -1224,7 +1258,7 @@ void Db::DelItemDelContent(const Item & item)
|
||||
|
||||
Error Db::DelItemCountContents(const Item & item, long & contents)
|
||||
{
|
||||
Error result = Error::ok;
|
||||
Error result = WINIX_ERR_OK;
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
@@ -1259,7 +1293,7 @@ long contents;
|
||||
|
||||
Error result = DelItemCountContents(item, contents);
|
||||
|
||||
if( result == Error::ok && contents == 1 )
|
||||
if( result == WINIX_ERR_OK && contents == 1 )
|
||||
DelItemDelContent(item);
|
||||
|
||||
return DelItemDelItem(item);
|
||||
@@ -1331,7 +1365,7 @@ void Db::GetUsers(UGContainer<User> & user_table)
|
||||
|
||||
User u;
|
||||
long last_id = -1;
|
||||
UGContainer<User>::Iterator iter;
|
||||
UGContainer<User>::Iterator iter = user_table.End();
|
||||
|
||||
for(int i = 0 ; i<rows ; ++i)
|
||||
{
|
||||
@@ -1348,12 +1382,16 @@ void Db::GetUsers(UGContainer<User> & user_table)
|
||||
log << log1 << "Db: get user: id:" << u.id << ", name:" << u.name << ", super_user:" << u.super_user << logend;
|
||||
|
||||
iter = user_table.PushBack( u );
|
||||
|
||||
if( iter == user_table.End() )
|
||||
log << log1 << "Db: can't add a user: " << u.name << logend;
|
||||
|
||||
last_id = u.id;
|
||||
}
|
||||
|
||||
long group_id = atol( AssertValue(r, i, cgroup_id) );
|
||||
|
||||
if( !PQgetisnull(r, i, cgroup_id) && group_id!=-1 && !user_table.Empty() )
|
||||
if( !PQgetisnull(r, i, cgroup_id) && group_id!=-1 && iter!=user_table.End() )
|
||||
{
|
||||
iter->groups.push_back(group_id);
|
||||
log << log3 << "Db: user:" << iter->name << " is a member of group_id:" << group_id << logend;
|
||||
@@ -1485,7 +1523,7 @@ return buffer;
|
||||
Error Db::GetThreadByDirId(long dir_id, Thread & thread)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1506,7 +1544,7 @@ Error Db::GetThreadByDirId(long dir_id, Thread & thread)
|
||||
log << log1 << "Db: there is more than one thread with dir_id: " << dir_id << logend;
|
||||
else
|
||||
if( rows == 0 )
|
||||
throw Error(Error::no_thread);
|
||||
throw Error(WINIX_ERR_NO_THREAD);
|
||||
|
||||
int cid = AssertColumn(r, "id");
|
||||
int cparent_id = AssertColumn(r, "parent_id");
|
||||
@@ -1542,7 +1580,7 @@ return status;
|
||||
Error Db::GetThreads(long parent_id, std::vector<Thread> & thread_tab)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1604,7 +1642,7 @@ return status;
|
||||
Error Db::AddThread(Thread & thread)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1638,7 +1676,7 @@ return status;
|
||||
Error Db::EditThreadAddItem(long dir_id, long item_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1664,7 +1702,7 @@ return status;
|
||||
Error Db::EditThreadRemoveItem(long dir_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1703,7 +1741,7 @@ return status;
|
||||
Error Db::RemoveThread(long dir_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1737,7 +1775,7 @@ return status;
|
||||
Error Db::GetTicketByDirId(long dir_id, Ticket & ticket)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1758,7 +1796,7 @@ Error Db::GetTicketByDirId(long dir_id, Ticket & ticket)
|
||||
log << log1 << "Db: there is more than one ticket with dir_id: " << dir_id << logend;
|
||||
else
|
||||
if( rows == 0 )
|
||||
throw Error(Error::no_ticket);
|
||||
throw Error(WINIX_ERR_NO_TICKET);
|
||||
|
||||
TicketColumns tc;
|
||||
|
||||
@@ -1782,7 +1820,7 @@ return status;
|
||||
Error Db::GetTickets(long parent_id, std::vector<Ticket> & ticket_tab)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1856,7 +1894,7 @@ return is_ticket;
|
||||
Error Db::AddTicket(Ticket & ticket)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1892,7 +1930,7 @@ return status;
|
||||
Error Db::EditTicketById(Ticket & ticket)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1927,7 +1965,7 @@ return status;
|
||||
Error Db::EditTicketRemoveItem(long item_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1954,7 +1992,7 @@ return status;
|
||||
Error Db::RemoveTicket(long dir_id)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = Error::ok;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
|
Reference in New Issue
Block a user