120 lines
2.6 KiB
C++
Executable File
120 lines
2.6 KiB
C++
Executable File
/*
|
|
* 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>
|
|
|
|
|
|
#include "log.h"
|
|
#include "item.h"
|
|
#include "misc.h"
|
|
#include "error.h"
|
|
#include "dircontainer.h"
|
|
|
|
|
|
|
|
class Db
|
|
{
|
|
|
|
protected:
|
|
|
|
PGconn * pg_conn;
|
|
|
|
std::string db_database, db_user, db_pass;
|
|
|
|
public:
|
|
|
|
Db();
|
|
~Db();
|
|
|
|
void Init(const std::string & database, const std::string & user, const std::string & pass);
|
|
void Connect();
|
|
|
|
void Close();
|
|
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);
|
|
|
|
bool CheckUser(std::string & login, std::string & password, long & user_id);
|
|
|
|
long AssertCurrval(const char * table);
|
|
|
|
|
|
bool AddItemCreateUrlSubject(Item & item);
|
|
Error AddItem(Item & item);
|
|
Error EditItem(Item & item, bool with_subject = true);
|
|
|
|
void CheckAllUrlSubjectModifyItem(Item & item);
|
|
void CheckAllUrlSubject();
|
|
|
|
PGresult * GetItemsQuery(Item & item_ref);
|
|
void GetItems(std::vector<Item> & item_table, Item & item_ref);
|
|
void GetItem(std::vector<Item> & item_table, long id);
|
|
|
|
bool DelItem(long id);
|
|
|
|
void GetDirs(DirContainer & dir_table);
|
|
|
|
|
|
|
|
struct ItemColumns
|
|
{
|
|
int id, subject, content, url_subject, type, parent_id, default_item;
|
|
|
|
void SetColumns(PGresult * r)
|
|
{
|
|
id = Db::AssertColumn(r, "id");
|
|
subject = Db::AssertColumn(r, "subject");
|
|
content = Db::AssertColumn(r, "content");
|
|
url_subject = Db::AssertColumn(r, "url_subject");
|
|
type = Db::AssertColumn(r, "type");
|
|
parent_id = Db::AssertColumn(r, "parent_id");
|
|
default_item = Db::AssertColumn(r, "default_item");
|
|
}
|
|
|
|
|
|
void SetItem(PGresult * r, long row, Item & item)
|
|
{
|
|
item.id = atol( Db::AssertValue(r, row, id) );
|
|
item.subject = Db::AssertValue(r, row, subject);
|
|
item.content = Db::AssertValue(r, row, content);
|
|
item.url_subject = Db::AssertValue(r, row, url_subject);
|
|
item.type = static_cast<Item::Type>( atoi( Db::AssertValue(r, row, type) ) );
|
|
item.parent_id = atol( Db::AssertValue(r, row, parent_id) );
|
|
item.default_item = atol( Db::AssertValue(r, row, default_item) );
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}; // class Db
|
|
|
|
|
|
extern Db db;
|
|
|
|
|
|
#endif
|
|
|