added: function: default
changes the default item in a directory git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@473 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
101
core/db.cpp
101
core/db.cpp
@@ -502,7 +502,7 @@ Error Db::EditItemInItem(Item & item, bool with_subject)
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "update core.item set (user_id, group_id, privileges, type, parent_id";
|
||||
query << "update core.item set (user_id, group_id, privileges, type, default_item, parent_id";
|
||||
|
||||
if( with_subject )
|
||||
query << ", url";
|
||||
@@ -512,6 +512,7 @@ Error Db::EditItemInItem(Item & item, bool with_subject)
|
||||
query << '\'' << item.group_id << "', ";
|
||||
query << '\'' << item.privileges << "', ";
|
||||
query << '\'' << static_cast<int>(item.type) << "', ";
|
||||
query << '\'' << item.default_item << "', ";
|
||||
query << '\'' << item.parent_id << "' ";
|
||||
|
||||
if( with_subject )
|
||||
@@ -617,6 +618,7 @@ Error Db::EditItemGetContentId(Item & item)
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
// !! tutaj chyba nie ma potrzeby robic left join z core.content (nie uzywamy nic z tamtej tabeli)
|
||||
query << "select content_id from core.item left join core.content on item.content_id = content.id where item.id='";
|
||||
query << item.id << "';";
|
||||
|
||||
@@ -644,11 +646,17 @@ return result;
|
||||
// !! moze nazwa poprostu EditItem (nie trzeba tego ById) ? (sprawdzic czy nie koliduje z inna nazwa)
|
||||
Error Db::EditItemById(Item & item, bool with_subject)
|
||||
{
|
||||
Error result = EditItemGetContentId(item);
|
||||
Error result = Error::ok;
|
||||
|
||||
// !! dla katalogow nie testowane jeszcze
|
||||
|
||||
if( item.type == Item::file )
|
||||
result = EditItemGetContentId(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
{
|
||||
result = EditItemInContent(item);
|
||||
if( item.type == Item::file )
|
||||
result = EditItemInContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
result = EditItemInItem(item, with_subject);
|
||||
@@ -660,6 +668,7 @@ return result;
|
||||
|
||||
|
||||
// item.url and item.parent_id must be set
|
||||
// doesn't work with directiories
|
||||
Error Db::EditItemByUrl(Item & item, bool with_subject)
|
||||
{
|
||||
Error result = EditItemGetId(item);
|
||||
@@ -677,6 +686,43 @@ return result;
|
||||
|
||||
|
||||
|
||||
Error Db::EditDefaultItem(long id, long new_default_item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "update core.item set (default_item) = ('" << new_default_item << "') where id='" << id << "';";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
|
||||
char * rows_str = PQcmdTuples(r);
|
||||
long rows = 0;
|
||||
|
||||
if( rows_str )
|
||||
rows = atol(rows_str);
|
||||
|
||||
if( rows == 0 )
|
||||
{
|
||||
result = Error::db_no_item;
|
||||
log << log1 << "Db: EditDefaultItem: no such item, id: " << id << logend;
|
||||
}
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PGresult * Db::GetItemsQuery(Item & item_ref)
|
||||
@@ -806,6 +852,55 @@ return result;
|
||||
}
|
||||
|
||||
|
||||
long Db::GetItemId(long parent_id, const std::string & url, Item::Type type)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
long result = -1;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select id from core.item where type='" << static_cast<int>(type) << "' and item.parent_id='";
|
||||
query << parent_id << "' and item.url='" << Escape(url) << "';";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
int rows = PQntuples(r);
|
||||
|
||||
if( rows == 1 )
|
||||
result = atol( AssertValue(r, 0, 0) );
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
long Db::GetFileId(long parent_id, const std::string & url)
|
||||
{
|
||||
return GetItemId(parent_id, url, Item::file);
|
||||
}
|
||||
|
||||
|
||||
long Db::GetDirId(long parent_id, const std::string & url)
|
||||
{
|
||||
return GetItemId(parent_id, url, Item::dir);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool Db::GetPriv(Item & item, long id)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
11
core/db.h
11
core/db.h
@@ -35,6 +35,11 @@ public:
|
||||
|
||||
Db();
|
||||
~Db();
|
||||
|
||||
// !! przerobic tak aby GetItem zwracalo wszystkie pozycja
|
||||
// !! GetFile tylko dla plikow
|
||||
// !! GetDir tylko dla katalogow
|
||||
// !! GetFile i GetDir beda uzywac GetItem
|
||||
|
||||
void Init(const std::string & database, const std::string & user, const std::string & pass);
|
||||
bool CheckUser(std::string & login, std::string & password, long & user_id);
|
||||
@@ -54,7 +59,11 @@ public:
|
||||
|
||||
// !! nowy interfejs
|
||||
Error GetItem(long parent_id, const std::string & url, Item & item);
|
||||
|
||||
Error EditDefaultItem(long id, long new_default_item);
|
||||
|
||||
long GetItemId(long parent_id, const std::string & url, Item::Type type);
|
||||
long GetFileId(long parent_id, const std::string & url);
|
||||
long GetDirId(long parent_id, const std::string & url);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
@@ -343,3 +343,32 @@ void Dirs::AddDir(const Item & item)
|
||||
dir_table.PushBack(item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Dirs::SplitPath(const std::string & path, std::string & dir, std::string & file)
|
||||
{
|
||||
std::string::size_type i;
|
||||
|
||||
dir.clear();
|
||||
file.clear();
|
||||
|
||||
if( path.empty() )
|
||||
// !! moze dir ustawic na '/' ?
|
||||
return;
|
||||
|
||||
for( i=path.size()-1 ; i>0 && path[i]!='/' ; --i);
|
||||
|
||||
if( path[i] != '/' )
|
||||
{
|
||||
// we do not have any slashes '/'
|
||||
file = path;
|
||||
return;
|
||||
}
|
||||
|
||||
dir.assign(path, 0, i + 1); // +1 means with a slash at the end
|
||||
|
||||
if( i < path.size() - 1 )
|
||||
file.assign(path, i+1, path.size() - i - 1);
|
||||
}
|
||||
|
||||
@@ -82,6 +82,9 @@ public:
|
||||
Item * GetDir(long id);
|
||||
|
||||
void AddDir(const Item & item);
|
||||
|
||||
|
||||
static void SplitPath(const std::string & path, std::string & dir, std::string & file);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ public:
|
||||
deleted_item,
|
||||
privileged_item,
|
||||
added_dir,
|
||||
defaulted_dir,
|
||||
loggedout
|
||||
};
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
node,
|
||||
emacs,
|
||||
mkdir,
|
||||
default_item,
|
||||
privileges,
|
||||
rm,
|
||||
login,
|
||||
|
||||
@@ -55,6 +55,10 @@ void Functions::ReadFunctions()
|
||||
f.item.url = "mkdir";
|
||||
table.insert( std::make_pair(f.item.url, f) );
|
||||
|
||||
f.code = Function::default_item;
|
||||
f.item.url = "default";
|
||||
table.insert( std::make_pair(f.item.url, f) );
|
||||
|
||||
f.code = Function::privileges;
|
||||
f.item.url = "uprawnienia";
|
||||
table.insert( std::make_pair(f.item.url, f) );
|
||||
|
||||
Reference in New Issue
Block a user