added: TextStream a class similar to std::ostringstream
but with a Clear() method
the dynamic allocated buffer can be easily reused
added: DbTextStream a special version of a stream
used to create a database string query
everything is escaped by default
added: DbBase a base class with some basic methods for communicating
with the database
added: DbConn a class for managing connection to the database
changed: some refactoring in Db class
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@655 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
169
db/db.h
169
db/db.h
@@ -7,43 +7,41 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfilecmslucoredb
|
||||
#define headerfilecmslucoredb
|
||||
#ifndef headerfile_winix_db_db
|
||||
#define headerfile_winix_db_db
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <libpq-fe.h>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
|
||||
#include "item.h"
|
||||
#include "user.h"
|
||||
#include "group.h"
|
||||
#include "thread.h"
|
||||
#include "error.h"
|
||||
#include "dircontainer.h"
|
||||
#include "ugcontainer.h"
|
||||
#include "ticket.h"
|
||||
#include "dbbase.h"
|
||||
#include "dbitemquery.h"
|
||||
#include "dbitemcolumns.h"
|
||||
|
||||
#include "core/item.h"
|
||||
#include "core/user.h"
|
||||
#include "core/group.h"
|
||||
#include "core/thread.h"
|
||||
#include "core/error.h"
|
||||
#include "core/dircontainer.h"
|
||||
#include "core/ugcontainer.h"
|
||||
#include "core/ticket.h"
|
||||
|
||||
|
||||
class Db
|
||||
|
||||
|
||||
class Db : public DbBase
|
||||
{
|
||||
public:
|
||||
|
||||
Db(bool close_at_end_ = true);
|
||||
~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);
|
||||
void WaitForConnection();
|
||||
|
||||
|
||||
bool CheckUser(const std::string & login, const std::string & password, long & user_id);
|
||||
Error AddUser(User & user, const std::string & password);
|
||||
@@ -54,91 +52,9 @@ public:
|
||||
void CheckAllUrlSubject();
|
||||
|
||||
|
||||
struct ItemQuery
|
||||
{
|
||||
// id is selected always
|
||||
bool sel_parent_id; // parent_id
|
||||
bool sel_user_id; // user_id, modification_user_id
|
||||
bool sel_group_id; // group_id
|
||||
bool sel_guest_name; // guest_name
|
||||
bool sel_privileges; // privileges
|
||||
bool sel_date; // date_creation, date_modification
|
||||
bool sel_subject; // subject
|
||||
bool sel_content; // content, content_type, (content_id)
|
||||
bool sel_url; // url
|
||||
bool sel_type; // type (dir, file, none)
|
||||
bool sel_default_item; // default_item
|
||||
bool sel_auth; // auth, auth_path
|
||||
bool sel_html_template; // template
|
||||
|
||||
bool where_id; //
|
||||
bool where_parent_id; //
|
||||
bool where_type;
|
||||
bool where_auth;
|
||||
|
||||
long id; // if where_id is true
|
||||
long parent_id; // if where_parent_id is true
|
||||
Item::Type type;
|
||||
Item::Auth auth;
|
||||
bool auth_equal; // if true means auth should be equal
|
||||
|
||||
bool sort_asc;
|
||||
|
||||
|
||||
void SetAllSel(bool sel)
|
||||
{
|
||||
sel_parent_id = sel;
|
||||
sel_user_id = sel;
|
||||
sel_group_id = sel;
|
||||
sel_guest_name = sel;
|
||||
sel_privileges = sel;
|
||||
sel_date = sel;
|
||||
sel_subject = sel;
|
||||
sel_content = sel;
|
||||
sel_url = sel;
|
||||
sel_type = sel;
|
||||
sel_default_item= sel;
|
||||
sel_auth = sel;
|
||||
sel_html_template= sel;
|
||||
}
|
||||
|
||||
void SetAllWhere(bool where_)
|
||||
{
|
||||
where_id = where_;
|
||||
where_parent_id = where_;
|
||||
where_type = where_;
|
||||
where_auth = where_;
|
||||
}
|
||||
|
||||
void SetAll(bool sel, bool where_)
|
||||
{
|
||||
SetAllSel(sel);
|
||||
SetAllWhere(where_);
|
||||
}
|
||||
|
||||
void WhereId(long id_) { where_id = true; id = id_; }
|
||||
void WhereParentId(long parent_id_) { where_parent_id = true; parent_id = parent_id_; }
|
||||
void WhereType(Item::Type type_) { where_type = true; type = type_; }
|
||||
void WhereAuth(Item::Auth st,
|
||||
bool equal = true) { where_auth = true; auth = st; auth_equal = equal; }
|
||||
|
||||
ItemQuery()
|
||||
{
|
||||
sort_asc = true;
|
||||
auth_equal = true;
|
||||
|
||||
SetAll(true, false);
|
||||
|
||||
id = -1;
|
||||
parent_id = -1;
|
||||
type = Item::none;
|
||||
auth = Item::auth_none;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void GetItems(std::vector<Item> & item_tab, const ItemQuery & item_query);
|
||||
void GetItems(std::vector<long> & item_tab, const ItemQuery & item_query);
|
||||
void GetItems(std::vector<Item> & item_tab, const DbItemQuery & item_query);
|
||||
void GetItems(std::vector<long> & item_tab, const DbItemQuery & item_query);
|
||||
|
||||
|
||||
// !! pobiera tylko jeden item (cos wymyslec innego z nazwa albo argumentem)
|
||||
@@ -171,12 +87,6 @@ public:
|
||||
long GetFileId(long parent_id, const std::string & url);
|
||||
long GetDirId(long parent_id, const std::string & url);
|
||||
|
||||
static tm ConvertTime(const char * str);
|
||||
static const char * ConvertTime(const tm & t);
|
||||
|
||||
PGconn * GetPGconn();
|
||||
|
||||
virtual void Connect();
|
||||
|
||||
|
||||
Error AddThread(Thread & thread);
|
||||
@@ -197,24 +107,9 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
PGconn * pg_conn;
|
||||
std::string db_database, db_user, db_pass;
|
||||
bool close_at_end;
|
||||
|
||||
void SetDbParameters();
|
||||
|
||||
void Close();
|
||||
|
||||
bool AssertConnection(bool put_log = true, bool throw_if_no_connection = true);
|
||||
DbTextStream query, query_create_url;
|
||||
std::string temp_url;
|
||||
|
||||
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);
|
||||
@@ -227,24 +122,15 @@ protected:
|
||||
|
||||
void CheckAllUrlSubjectModifyItem(Item & item);
|
||||
|
||||
PGresult * GetItemsQuery(const ItemQuery & iq, bool skip_other_sel = false);
|
||||
PGresult * GetItemsQuery(const DbItemQuery & iq, bool skip_other_sel = false);
|
||||
|
||||
bool DelItemDelItem(const Item & item);
|
||||
void DelItemDelContent(const Item & item);
|
||||
Error DelItemCountContents(const Item & item, long & contents);
|
||||
|
||||
|
||||
struct ItemColumns
|
||||
{
|
||||
int id, user_id, group_id, privileges, date_creation, date_modification, url, type, parent_id,
|
||||
content_id, default_item, subject, content, content_type, guest_name, auth, auth_path,
|
||||
modification_user_id, html_template;
|
||||
|
||||
void SetColumns(PGresult * r);
|
||||
void SetItem(PGresult * r, long row, Item & item);
|
||||
};
|
||||
|
||||
|
||||
// !! tymczasowo
|
||||
// bedzie wszystko w osobnym pluginie
|
||||
struct TicketColumns
|
||||
{
|
||||
int id, dir_id, parent_id, type, status, priority, category, expected, progress, item_id;
|
||||
@@ -253,10 +139,7 @@ protected:
|
||||
void SetTicket(PGresult * r, long row, Ticket & ticket);
|
||||
};
|
||||
|
||||
}; // class Db
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user