2008-12-10 05:42:49 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of CMSLU -- Content Management System like Unix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008, Tomasz Sowa
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef headerfilecoredb
|
|
|
|
#define headerfilecoredb
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
#include <cstdio>
|
2009-01-26 21:49:28 +01:00
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "item.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "dircontainer.h"
|
2008-12-21 22:17:09 +01:00
|
|
|
#include "user.h"
|
|
|
|
#include "group.h"
|
|
|
|
#include "ugcontainer.h"
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Db
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
Db(bool close_at_end_ = true);
|
2008-12-14 06:28:28 +01:00
|
|
|
~Db();
|
2008-12-31 19:28:12 +01:00
|
|
|
|
|
|
|
// !! przerobic tak aby GetItem zwracalo wszystkie pozycja
|
|
|
|
// !! GetFile tylko dla plikow
|
|
|
|
// !! GetDir tylko dla katalogow
|
|
|
|
// !! GetFile i GetDir beda uzywac GetItem
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
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);
|
|
|
|
Error AddItem(Item & item);
|
2008-12-21 22:17:09 +01:00
|
|
|
Error EditItemById(Item & item, bool with_subject = true);
|
|
|
|
Error EditItemByUrl(Item & item, bool with_subject = true);
|
2008-12-14 06:28:28 +01:00
|
|
|
void CheckAllUrlSubject();
|
|
|
|
void GetItems(std::vector<Item> & item_table, Item & item_ref);
|
|
|
|
void GetItem(std::vector<Item> & item_table, long id);
|
2008-12-21 22:17:09 +01:00
|
|
|
bool GetPriv(Item & item, long id);
|
2008-12-30 02:05:03 +01:00
|
|
|
Error EditPrivById(Item & item, long id);
|
|
|
|
|
2009-02-06 14:12:03 +01:00
|
|
|
Error DelDirById(long id);
|
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
bool DelItem(const Item & item);
|
|
|
|
void GetDirs(DirContainer & dir_table);
|
2008-12-21 22:17:09 +01:00
|
|
|
void GetUsers(UGContainer<User> & user_table);
|
|
|
|
void GetGroups(UGContainer<Group> & group_table);
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
// !! nowy interfejs
|
|
|
|
Error GetItem(long parent_id, const std::string & url, Item & item);
|
2008-12-31 19:28:12 +01:00
|
|
|
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);
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
tm ConvertTime(const char * str);
|
|
|
|
|
|
|
|
PGconn * GetPGconn();
|
|
|
|
|
2009-01-27 19:43:44 +01:00
|
|
|
virtual void Connect();
|
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
protected:
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
PGconn * pg_conn;
|
|
|
|
std::string db_database, db_user, db_pass;
|
2009-01-26 21:49:28 +01:00
|
|
|
bool close_at_end;
|
2008-12-14 06:28:28 +01:00
|
|
|
|
2009-01-27 19:43:44 +01:00
|
|
|
void SetDbParameters();
|
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
void Close();
|
2009-01-27 19:43:44 +01:00
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
void AssertConnection();
|
|
|
|
std::string Escape(const std::string & s);
|
|
|
|
std::string Escape(const char * s);
|
|
|
|
PGresult * AssertQuery(const std::string & q);
|
|
|
|
void AssertResultStatus(PGresult * r, ExecStatusType t);
|
|
|
|
static int AssertColumn(PGresult * r, const char * column_name);
|
|
|
|
static const char * AssertValue(PGresult * r, int row, int col);
|
|
|
|
void ClearResult(PGresult * r);
|
|
|
|
long AssertCurrval(const char * table);
|
|
|
|
bool AddItemCreateUrlSubject(Item & item);
|
|
|
|
|
|
|
|
Error AddItemIntoContent(Item & item);
|
|
|
|
Error AddItemIntoItem(Item & item);
|
|
|
|
|
|
|
|
Error EditItemInItem(Item & item, bool with_subject);
|
|
|
|
Error EditItemInContent(Item & item);
|
2008-12-21 22:17:09 +01:00
|
|
|
Error EditItemGetId(Item & item);
|
|
|
|
Error EditItemGetContentId(Item & item);
|
2008-12-14 06:28:28 +01:00
|
|
|
|
|
|
|
void CheckAllUrlSubjectModifyItem(Item & item);
|
|
|
|
|
|
|
|
PGresult * GetItemsQuery(Item & item_ref);
|
|
|
|
|
|
|
|
bool DelItemDelItem(const Item & item);
|
|
|
|
void DelItemDelContent(const Item & item);
|
|
|
|
Error DelItemCountContents(const Item & item, long & contents);
|
|
|
|
|
|
|
|
|
|
|
|
struct ItemColumns
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2008-12-14 06:28:28 +01:00
|
|
|
int id, user_id, group_id, privileges, url, type, parent_id, content_id, default_item, subject, content, content_type;
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
void SetColumns(PGresult * r);
|
|
|
|
void SetItem(PGresult * r, long row, Item & item);
|
|
|
|
};
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
}; // class Db
|
|
|
|
|
|
|
|
|
|
|
|
extern Db db;
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|