changed: db: table item: url_subject into url
changed: item.url_subject into item.url changed: a new table: content with: id, subject, content, content_type (those from the item table) it helps to create hard links git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@466 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
280
core/db.cpp
280
core/db.cpp
@@ -186,6 +186,7 @@ return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Db::ClearResult(PGresult * r)
|
||||
{
|
||||
if( r )
|
||||
@@ -263,7 +264,7 @@ appendix[0] = 0;
|
||||
std::ostringstream query;
|
||||
|
||||
// this Escape can be put at the beginning (performance)
|
||||
query << "select id from core.item where url_subject='" << Escape(item.url_subject) << appendix << "' and parent_id='" << item.parent_id << "';";
|
||||
query << "select id from core.item where url='" << Escape(item.url) << appendix << "' and parent_id='" << item.parent_id << "';";
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
@@ -274,7 +275,7 @@ appendix[0] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
item.url_subject += appendix;
|
||||
item.url += appendix;
|
||||
is_that_url = false;
|
||||
}
|
||||
|
||||
@@ -304,15 +305,15 @@ void Db::CheckAllUrlSubjectModifyItem(Item & item)
|
||||
try
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "update core.item set url_subject=";
|
||||
query << "update core.item set url=";
|
||||
|
||||
// url_subject
|
||||
// url
|
||||
if( AddItemCreateUrlSubject(item) )
|
||||
query << '\'' << Escape(item.url_subject) << '\'';
|
||||
query << '\'' << Escape(item.url) << '\'';
|
||||
else
|
||||
{
|
||||
query << '\'' << item.id << '\'';
|
||||
item.url_subject.clear();
|
||||
item.url.clear();
|
||||
}
|
||||
|
||||
query << " where id='" << item.id << "';";
|
||||
@@ -339,7 +340,7 @@ void Db::CheckAllUrlSubject()
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query, query2;
|
||||
query << "select id, subject from core.item where url_subject is null or url_subject=''";
|
||||
query << "select item.id, subject from core.item left join core.content on item.content_id = content.id where url is null or url=''";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
@@ -350,7 +351,7 @@ void Db::CheckAllUrlSubject()
|
||||
|
||||
for(int i = 0 ; i<rows ; ++i)
|
||||
{
|
||||
item.id = atol( AssertValue(r, i, cid) );
|
||||
item.id = atol( AssertValue(r, i, cid) );
|
||||
item.subject = AssertValue(r, i, csubject);
|
||||
|
||||
CheckAllUrlSubjectModifyItem(item);
|
||||
@@ -387,8 +388,7 @@ return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Error Db::AddItem(Item & item)
|
||||
Error Db::AddItemIntoItem(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
@@ -398,34 +398,29 @@ Error Db::AddItem(Item & item)
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "insert into core.item (subject, content, url_subject, type, parent_id) values (";
|
||||
query << '\'' << Escape(item.subject) << "', ";
|
||||
query << '\'' << Escape(item.content) << "', ";
|
||||
query << "insert into core.item (user_id, group_id, privileges, type, parent_id, content_id, url) values (";
|
||||
query << '\'' << item.user_id << "', ";
|
||||
query << '\'' << item.group_id << "', ";
|
||||
query << '\'' << item.privileges << "', ";
|
||||
query << '\'' << static_cast<int>(item.type) << "', ";
|
||||
query << '\'' << item.parent_id << "', ";
|
||||
query << '\'' << item.content_id << "', ";
|
||||
|
||||
// url_subject
|
||||
url_without_id = AddItemCreateUrlSubject(item);
|
||||
|
||||
if( url_without_id )
|
||||
{
|
||||
query << '\'' << Escape(item.url_subject) << "', ";
|
||||
}
|
||||
query << '\'' << Escape(item.url) << "');";
|
||||
else
|
||||
{
|
||||
query << "currval('core.item_id_seq')" << ", ";
|
||||
}
|
||||
query << "currval('core.item_id_seq')" << ");";
|
||||
|
||||
|
||||
query << '\'' << static_cast<int>(item.type) << "', ";
|
||||
query << '\'' << item.parent_id << "' ";
|
||||
query << ");";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
|
||||
item.id = AssertCurrval("core.item_id_seq");
|
||||
|
||||
if( !url_without_id )
|
||||
ToString(item.url_subject, item.id);
|
||||
ToString(item.url, item.id);
|
||||
|
||||
}
|
||||
catch(const Error & e)
|
||||
@@ -439,7 +434,56 @@ return result;
|
||||
}
|
||||
|
||||
|
||||
Error Db::EditItem(Item & item, bool with_subject)
|
||||
|
||||
Error Db::AddItemIntoContent(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "insert into core.content (subject, content, content_type) values (";
|
||||
query << '\'' << Escape(item.subject) << "', ";
|
||||
query << '\'' << Escape(item.content) << "', ";
|
||||
query << '\'' << item.content_type << "');";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
|
||||
item.content_id = AssertCurrval("core.content_id_seq");
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error Db::AddItem(Item & item)
|
||||
{
|
||||
Error result = AddItemIntoContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
result = AddItemIntoItem(item);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Error Db::EditItemInItem(Item & item, bool with_subject)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
@@ -449,41 +493,35 @@ Error Db::EditItem(Item & item, bool with_subject)
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "update core.item set (";
|
||||
query << "update core.item set (user_id, group_id, privileges, type, parent_id,";
|
||||
|
||||
if( with_subject )
|
||||
query << "subject, url_subject, ";
|
||||
query << " url";
|
||||
|
||||
query << "content, type, parent_id) = (";
|
||||
query << ") = (";
|
||||
query << '\'' << item.user_id << "', ";
|
||||
query << '\'' << item.group_id << "', ";
|
||||
query << '\'' << item.privileges << "', ";
|
||||
query << '\'' << static_cast<int>(item.type) << "', ";
|
||||
query << '\'' << item.parent_id << "' ";
|
||||
|
||||
if( with_subject )
|
||||
{
|
||||
query << '\'' << Escape(item.subject) << "', ";
|
||||
|
||||
// url_subject
|
||||
url_without_id = AddItemCreateUrlSubject(item);
|
||||
|
||||
if( url_without_id )
|
||||
{
|
||||
query << '\'' << Escape(item.url_subject) << "', ";
|
||||
}
|
||||
query << ", '" << Escape(item.url) << "'";
|
||||
else
|
||||
{
|
||||
query << '\'' << item.id << "', ";
|
||||
}
|
||||
query << ", '" << item.id << "'";
|
||||
}
|
||||
|
||||
query << '\'' << Escape(item.content) << "', ";
|
||||
|
||||
query << '\'' << static_cast<int>(item.type) << "', ";
|
||||
query << '\'' << item.parent_id << "' ";
|
||||
query << ") where id='" << item.id << "';";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
|
||||
if( with_subject && !url_without_id )
|
||||
ToString(item.url_subject, item.id);
|
||||
ToString(item.url, item.id);
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
@@ -497,16 +535,62 @@ return result;
|
||||
|
||||
|
||||
|
||||
Error Db::EditItemInContent(Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error result = Error::ok;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
std::ostringstream query;
|
||||
query << "update core.content set (subject, content, content_type) = (";
|
||||
|
||||
query << '\'' << Escape(item.subject) << "', ";
|
||||
query << '\'' << Escape(item.content) << "', ";
|
||||
query << '\'' << item.content_type << "' ";
|
||||
|
||||
query << ") where id='" << item.content_id << "';";
|
||||
|
||||
r = AssertQuery(query.str());
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error Db::EditItem(Item & item, bool with_subject)
|
||||
{
|
||||
Error result = EditItemInContent(item);
|
||||
|
||||
if( result == Error::ok )
|
||||
result = EditItemInItem(item, with_subject);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PGresult * Db::GetItemsQuery(Item & item_ref)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "select * from core.item where type!='0' and parent_id='" << item_ref.parent_id << "'";
|
||||
query << "select * from core.item left join core.content on item.content_id = content.id where type!='0' and parent_id='" << item_ref.parent_id << "'";
|
||||
|
||||
if( item_ref.id != -1 )
|
||||
query << " and id='" << item_ref.id << "'";
|
||||
query << " and item.id='" << item_ref.id << "'";
|
||||
|
||||
if( !item_ref.url_subject.empty() )
|
||||
query << " and url_subject='" << Escape(item_ref.url_subject) << "'";
|
||||
if( !item_ref.url.empty() )
|
||||
query << " and url='" << Escape(item_ref.url) << "'";
|
||||
|
||||
query << ';';
|
||||
|
||||
@@ -557,7 +641,7 @@ void Db::GetItem(std::vector<Item> & item_table, long id)
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select * from core.item where type='1' and id='" << id << "';";
|
||||
query << "select * from core.item left join core.content on item.content_id = content.id where type='1' and item.id='" << id << "';";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
@@ -585,9 +669,11 @@ void Db::GetItem(std::vector<Item> & item_table, long id)
|
||||
}
|
||||
|
||||
|
||||
bool Db::DelItem(long id)
|
||||
|
||||
|
||||
bool Db::DelItemDelItem(const Item & item)
|
||||
{
|
||||
long rows = 0;
|
||||
long rows = 0;
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
@@ -595,7 +681,7 @@ bool Db::DelItem(long id)
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "delete from core.item where type='1' and id='" << id << "';";
|
||||
query << "delete from core.item where id='" << item.id << "';";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
@@ -607,6 +693,9 @@ bool Db::DelItem(long id)
|
||||
|
||||
if( rows > 1 )
|
||||
log << log1 << "Db: more than one item were deleted" << logend;
|
||||
else
|
||||
if( rows == 0 )
|
||||
log << log1 << "Db: no item has been deleted" << logend;
|
||||
}
|
||||
}
|
||||
catch(const Error &)
|
||||
@@ -619,6 +708,93 @@ return rows != 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Db::DelItemDelContent(const Item & item)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "delete from core.content where id='" << item.content_id << "';";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_COMMAND_OK);
|
||||
|
||||
const char * crows = PQcmdTuples(r);
|
||||
if( crows )
|
||||
{
|
||||
long rows = atol(crows);
|
||||
|
||||
if( rows > 1 )
|
||||
log << log1 << "Db: more than one content were deleted" << logend;
|
||||
else
|
||||
if( rows == 0 )
|
||||
log << log1 << "Db: no content has been deleted" << logend;
|
||||
|
||||
}
|
||||
}
|
||||
catch(const Error &)
|
||||
{
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error Db::DelItemCountContents(const Item & item, long & contents)
|
||||
{
|
||||
Error result = Error::ok;
|
||||
PGresult * r = 0;
|
||||
|
||||
try
|
||||
{
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select count('id') from core.item where content_id='" << item.content_id << "';";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
||||
contents = atol( AssertValue(r, 0, 0) );
|
||||
|
||||
log << log1 << "counters: " << contents << logend; // !!
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
result = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Db::DelItem(const Item & item)
|
||||
{
|
||||
long contents;
|
||||
|
||||
Error result = DelItemCountContents(item, contents);
|
||||
|
||||
if( result == Error::ok && contents == 1 )
|
||||
DelItemDelContent(item);
|
||||
|
||||
return DelItemDelItem(item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Db::GetDirs(DirContainer & dir_table)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
@@ -628,7 +804,7 @@ void Db::GetDirs(DirContainer & dir_table)
|
||||
AssertConnection();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "select * from core.item where type='0';";
|
||||
query << "select * from core.item left join core.content on item.content_id = content.id where type='0';";
|
||||
|
||||
r = AssertQuery( query.str() );
|
||||
AssertResultStatus(r, PGRES_TUPLES_OK);
|
||||
|
Reference in New Issue
Block a user