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:
2010-09-18 00:51:12 +00:00
parent 8b1db3304f
commit a589e5a090
52 changed files with 3261 additions and 1913 deletions

22
db/Makefile Executable file
View File

@@ -0,0 +1,22 @@
include Makefile.o.dep
all: $(o)
.SUFFIXES: .cpp .o
.cpp.o:
$(CXX) -c $(CXXFLAGS) $<
depend:
makedepend -Y. -I.. -I../../ezc/src -f- *.cpp > Makefile.dep
echo -n "o = " > Makefile.o.dep
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
clean:
rm -f *.o
include Makefile.dep

15
db/Makefile.dep Executable file
View File

@@ -0,0 +1,15 @@
# DO NOT DELETE
db.o: db.h dbbase.h dbconn.h dbtextstream.h ../core/textstream.h
db.o: dbitemquery.h ../core/item.h dbitemcolumns.h ../core/user.h
db.o: ../core/group.h ../core/thread.h ../core/error.h ../core/log.h
db.o: ../core/dircontainer.h ../core/item.h ../core/ugcontainer.h
db.o: ../core/ticket.h ../core/log.h ../core/misc.h
dbbase.o: dbbase.h dbconn.h dbtextstream.h ../core/textstream.h ../core/log.h
dbbase.o: ../core/error.h ../core/log.h
dbconn.o: dbconn.h dbtextstream.h ../core/textstream.h ../core/log.h
dbconn.o: ../core/error.h ../core/log.h
dbitemcolumns.o: dbitemcolumns.h ../core/item.h dbbase.h dbconn.h
dbitemcolumns.o: dbtextstream.h ../core/textstream.h
dbitemquery.o: dbitemquery.h ../core/item.h
dbtextstream.o: dbtextstream.h ../core/textstream.h

1
db/Makefile.o.dep Executable file
View File

@@ -0,0 +1 @@
o = db.o dbbase.o dbconn.o dbitemcolumns.o dbitemquery.o dbtextstream.o

1424
db/db.cpp

File diff suppressed because it is too large Load Diff

169
db/db.h
View File

@@ -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

View File

@@ -1,99 +0,0 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "db.h"
void Db::ItemColumns::SetColumns(PGresult * r)
{
// PQfnumber returns -1 if there is no such a column
id = PQfnumber(r, "id");
user_id = PQfnumber(r, "user_id");
group_id = PQfnumber(r, "group_id");
privileges = PQfnumber(r, "privileges");
date_creation = PQfnumber(r, "date_creation");
date_modification = PQfnumber(r, "date_modification");
url = PQfnumber(r, "url");
type = PQfnumber(r, "type");
parent_id = PQfnumber(r, "parent_id");
content_id = PQfnumber(r, "content_id");
default_item = PQfnumber(r, "default_item");
subject = PQfnumber(r, "subject");
content = PQfnumber(r, "content");
content_type = PQfnumber(r, "content_type");
guest_name = PQfnumber(r, "guest_name");
auth = PQfnumber(r, "auth");
auth_path = PQfnumber(r, "auth_path");
html_template = PQfnumber(r, "template");
modification_user_id = PQfnumber(r, "modification_user_id");
}
void Db::ItemColumns::SetItem(PGresult * r, long row, Item & item)
{
if( id != -1 ) item.id = atol( Db::AssertValue(r, row, id) );
if( user_id != -1 ) item.user_id = atol( Db::AssertValue(r, row, user_id) );
if( group_id != -1 ) item.group_id = atol( Db::AssertValue(r, row, group_id) );
if( privileges != -1 ) item.privileges = atoi( Db::AssertValue(r, row, privileges) );
if( date_creation != -1 ) item.date_creation = ConvertTime( Db::AssertValue(r, row, date_creation) );
if( date_modification != -1 ) item.date_modification = ConvertTime( Db::AssertValue(r, row, date_modification) );
if( url != -1 ) item.url = Db::AssertValue(r, row, url);
if( type != -1 ) item.type = static_cast<Item::Type>( atoi(Db::AssertValue(r, row, type)) );
if( parent_id != -1 ) item.parent_id = atol( Db::AssertValue(r, row, parent_id) );
if( content_id != -1 ) item.content_id = atol( Db::AssertValue(r, row, content_id) );
if( default_item != -1 ) item.default_item = atol( Db::AssertValue(r, row, default_item) );
if( subject != -1 ) item.subject = Db::AssertValue(r, row, subject);
if( content != -1 ) item.content = Db::AssertValue(r, row, content);
if( content_type != -1 ) item.content_type = static_cast<Item::ContentType>( atoi(Db::AssertValue(r, row, content_type)) );
if( guest_name != -1 ) item.guest_name = Db::AssertValue(r, row, guest_name);
if( auth != -1 ) item.auth = static_cast<Item::Auth>( atoi(Db::AssertValue(r, row, auth)) );
if( auth_path != -1 ) item.auth_path = Db::AssertValue(r, row, auth_path);
if( html_template != -1 ) item.html_template = Db::AssertValue(r, row, html_template);
if( modification_user_id != -1 ) item.modification_user_id = atol( Db::AssertValue(r, row, modification_user_id) );
}
void Db::TicketColumns::SetColumns(PGresult * r)
{
// PQfnumber returns -1 if there is no such a column
id = PQfnumber(r, "id");
dir_id = PQfnumber(r, "dir_id");
parent_id = PQfnumber(r, "parent_id");
type = PQfnumber(r, "type");
status = PQfnumber(r, "status");
priority = PQfnumber(r, "priority");
category = PQfnumber(r, "category");
expected = PQfnumber(r, "expected");
progress = PQfnumber(r, "progress");
item_id = PQfnumber(r, "item_id");
}
void Db::TicketColumns::SetTicket(PGresult * r, long row, Ticket & ticket)
{
if( id != -1 ) ticket.id = atol( Db::AssertValue(r, row, id) );
if( dir_id != -1 ) ticket.dir_id = atol( Db::AssertValue(r, row, dir_id) );
if( parent_id != -1 ) ticket.parent_id = atol( Db::AssertValue(r, row, parent_id) );
if( type != -1 ) ticket.type = atoi( Db::AssertValue(r, row, type) );
if( status != -1 ) ticket.status = atoi( Db::AssertValue(r, row, status) );
if( priority != -1 ) ticket.priority = atoi( Db::AssertValue(r, row, priority) );
if( category != -1 ) ticket.category = atoi( Db::AssertValue(r, row, category) );
if( expected != -1 ) ticket.expected = atoi( Db::AssertValue(r, row, expected) );
if( progress != -1 ) ticket.progress = atoi( Db::AssertValue(r, row, progress) );
if( item_id != -1 ) ticket.item_id = atol( Db::AssertValue(r, row, item_id) );
}

301
db/dbbase.cpp Executable file
View File

@@ -0,0 +1,301 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "dbbase.h"
#include "core/log.h"
#include "core/error.h"
#include <stdlib.h>
#include <limits.h>
#include <limits>
DbBase::DbBase()
{
db_conn = 0;
log_queries = false;
}
void DbBase::SetConn(DbConn * conn)
{
db_conn = conn;
}
void DbBase::SetConn(DbConn & conn)
{
db_conn = &conn;
}
DbConn * DbBase::GetConn()
{
return db_conn;
}
void DbBase::LogQueries(bool log_q)
{
log_queries = log_q;
}
PGresult * DbBase::AssertQuery(const char * q)
{
if( log_queries )
log << log1 << "Db: executing query: " << q << logend;
bool bad_query = false;
PGresult * r = PQexec(db_conn->GetPgConn(), q);
if( !r )
{
bad_query = true;
if( PQstatus(db_conn->GetPgConn()) != CONNECTION_OK )
{
db_conn->AssertConnection();
r = PQexec(db_conn->GetPgConn(), q);
if( r )
bad_query = false;
}
}
if( bad_query )
{
log << log1 << "Db: Problem with this query: \"" << q << '\"' << logend;
log << log1 << "Db: " << PQerrorMessage(db_conn->GetPgConn()) << logend;
throw Error(WINIX_ERR_DB_INCORRECT_QUERY);
}
return r;
}
PGresult * DbBase::AssertQuery(const std::string & q)
{
return AssertQuery(q.c_str());
}
PGresult * DbBase::AssertQuery(const DbTextStream & query)
{
return AssertQuery(query.CStr());
}
void DbBase::AssertResult(PGresult * r, ExecStatusType t)
{
if( PQresultStatus(r) != t )
{
log << "Db: Incorrect result status: " << PQerrorMessage(db_conn->GetPgConn()) << logend;
throw Error(WINIX_ERR_DB_INCORRENT_RESULT_STATUS);
}
}
int DbBase::AssertColumn(PGresult * r, const char * column_name)
{
int c = PQfnumber(r, column_name);
if( c == -1 )
{
log << log1 << "Db: there is no column: " << column_name << logend;
throw Error(WINIX_ERR_DB_NO_COLUMN);
}
return c;
}
const char * DbBase::AssertValue(PGresult * r, int row, int col)
{
const char * res = PQgetvalue(r, row, col);
if( !res )
{
log << log1 << "Db: there is no such an item in the result, row:" << row << ", col:" << col << logend;
throw Error(WINIX_ERR_NO_ITEM);
}
return res;
}
long DbBase::AssertValueLong(PGresult * r, int row, int col)
{
return strtol( AssertValue(r, row, col), 0, 10 );
}
int DbBase::AssertValueInt(PGresult * r, int row, int col)
{
return (int)strtol( AssertValue(r, row, col), 0, 10 );
}
unsigned long DbBase::AssertValueULong(PGresult * r, int row, int col)
{
return strtoul( AssertValue(r, row, col), 0, 10 );
}
unsigned int DbBase::AssertValueUInt(PGresult * r, int row, int col)
{
return (unsigned int)strtoul( AssertValue(r, row, col), 0, 10 );
}
void DbBase::ClearResult(PGresult * r)
{
if( r )
PQclear(r);
}
bool DbBase::IsNull(PGresult * r, int row, int col)
{
return PQgetisnull(r, row, col) == 1;
}
int DbBase::Rows(PGresult * r)
{
// PQntuples - Returns the number of rows (tuples) in the query result. Because it returns
// an integer result, large result sets might overflow the return value on 32-bit operating systems.
return PQntuples(r);
}
int DbBase::Cols(PGresult * r)
{
// PQnfields - Returns the number of columns (fields) in each row of the query result.
return PQnfields(r);
}
long DbBase::AffectedRows(PGresult * r)
{
// PQcmdTuples - This function returns a string containing the number of rows affected by the SQL
// statement that generated the PGresult. This function can only be used following the execution
// of an INSERT, UPDATE, DELETE, MOVE, FETCH, or COPY statement, or [...]
char * rows_str = PQcmdTuples(r); // can be an empty string
long rows = 0;
if( rows_str )
{
rows = strtol(rows_str, 0, 10);
// strtol - If an overflow or underflow occurs, errno is set to ERANGE
// and the function return value is clamped according to the following table:
// Function underflow overflow
// strtol() LONG_MIN LONG_MAX
if( rows < 0 )
rows = 0;
}
return rows;
}
long DbBase::AssertCurrval(const char * table)
{
PGresult * r;
bquery.Clear();
bquery << R("select currval(")
<< table
<< R(");");
r = AssertQuery(bquery);
AssertResult(r, PGRES_TUPLES_OK);
if( Rows(r) != 1 )
{
log << log1 << "Db: error (currval) for table: " << table << ", " << PQerrorMessage(db_conn->GetPgConn()) << logend;
throw Error(WINIX_ERR_DB_ERR_CURRVAL);
}
return AssertValueLong(r, 0, 0);
}
tm DbBase::ConvertTime(const char * str)
{
tm t;
memset(&t, 0, sizeof(t));
if( !str )
return t;
size_t len = strlen(str);
if( len != 19 )
{
// the format must be like this: 2008-12-31 22:30:00
log << log1 << "DbBase: ConvertTime: unknown time format: \"" << str << "\"";
return t;
}
t.tm_year = atoi(str + 0) - 1900; /* year - 1900 */
t.tm_mon = atoi(str + 5) - 1; /* month of year (0 - 11) */
t.tm_mday = atoi(str + 8); /* day of month (1 - 31) */
t.tm_hour = atoi(str + 11); /* hours (0 - 23) */
t.tm_min = atoi(str + 14); /* minutes (0 - 59) */
t.tm_sec = atoi(str + 17); /* seconds (0 - 60) */
// t.tm_wday = 0; /* day of week (Sunday = 0) */
// t.tm_yday = 0; /* day of year (0 - 365) */
// t.tm_isdst = 0; /* is summer time in effect? */
// t.tm_zone = 0; // const_cast<char*>(""); /* abbreviation of timezone name */
return t;
}
const char * DbBase::ConvertTime(const tm & t)
{
// not thread safe
static char buffer[100];
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
t.tm_year + 1900,
t.tm_mon + 1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
return buffer;
}

75
db/dbbase.h Executable file
View File

@@ -0,0 +1,75 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_db_dbbase
#define headerfile_winix_db_dbbase
#include "dbconn.h"
#include "dbtextstream.h"
class DbBase
{
public:
DbBase();
void SetConn(DbConn * conn);
void SetConn(DbConn & conn);
DbConn * GetConn();
void LogQueries(bool log_q);
PGresult * AssertQuery(const char * query);
PGresult * AssertQuery(const std::string & query);
PGresult * AssertQuery(const DbTextStream & query);
void AssertResult(PGresult * r, ExecStatusType t);
static int AssertColumn(PGresult * r, const char * column_name);
static const char * AssertValue(PGresult * r, int row, int col);
static long AssertValueLong(PGresult * r, int row, int col);
static int AssertValueInt(PGresult * r, int row, int col);
static unsigned long AssertValueULong(PGresult * r, int row, int col);
static unsigned int AssertValueUInt(PGresult * r, int row, int col);
void ClearResult(PGresult * r);
long AssertCurrval(const char * table);
bool IsNull(PGresult * r, int row, int col);
int Rows(PGresult * r);
int Cols(PGresult * r);
long AffectedRows(PGresult * r);
static tm ConvertTime(const char * str);
static const char * ConvertTime(const tm & t); // warning: it uses its own static buffer
protected:
// a helper method for escaping strings
template<class RawType>
DbTextStream::RawText<RawType> R(const RawType & par)
{
return DbTextStream::RawText<RawType>(par);
}
DbConn * db_conn;
bool log_queries;
private:
DbTextStream bquery;
};
#endif

153
db/dbconn.cpp Executable file
View File

@@ -0,0 +1,153 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "dbconn.h"
#include "core/log.h"
#include "core/error.h"
DbConn::DbConn()
{
pg_conn = 0;
}
DbConn::~DbConn()
{
Close();
}
PGconn * DbConn::GetPgConn()
{
return pg_conn;
}
void DbConn::SetConnParam(const std::string & d, const std::string & u, const std::string & p)
{
db_database = d;
db_user = u;
db_pass = p;
}
void DbConn::Connect()
{
Close();
conn_info.Clear();
conn_info.SetExtented(false);
conn_info << R("dbname=") << db_database
<< R(" user=") << db_user
<< R(" password=") << db_pass;
pg_conn = PQconnectdb(conn_info.CStr());
// warning! pg_conn can be not null but there cannnot be a connection established
// use PQstatus(pg_conn) to check whether the connection works fine
}
void DbConn::LogConnectionSocket()
{
log << log2 << "Db: connection to the database works fine" << logend;
log << log3 << "Db: connection socket: " << PQsocket(pg_conn) << logend;
}
void DbConn::WaitForConnection()
{
if( !pg_conn || PQstatus(pg_conn) != CONNECTION_OK )
{
log << log3 << "Db: waiting for the db to be ready...." << logend;
while( !AssertConnection(false, false) )
sleep(5);
LogConnectionSocket();
}
}
void DbConn::Close()
{
if( pg_conn )
{
PQfinish(pg_conn);
pg_conn = 0;
}
}
bool DbConn::AssertConnection(bool put_log, bool throw_if_no_connection)
{
bool was_connection = true;
if( !pg_conn )
{
was_connection = false;
Connect();
}
else
if( PQstatus(pg_conn) != CONNECTION_OK )
{
if( put_log )
log << log2 << "Db: connection to the database is lost, trying to recover" << logend;
was_connection = false;
PQreset(pg_conn);
}
if( pg_conn && PQstatus(pg_conn) == CONNECTION_OK )
{
if( !was_connection )
{
if( put_log )
LogConnectionSocket();
SetDbParameters();
}
return true;
}
else
{
if( put_log )
log << log1 << "Db: connection to db server cannot be established" << logend;
if( throw_if_no_connection )
throw Error(WINIX_ERR_DB_FATAL_ERROR_DURING_CONNECTING);
return false;
}
}
void DbConn::SetDbParameters()
{
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
log << log1 << "Db: Can't set the proper client encoding" << logend;
}

55
db/dbconn.h Executable file
View File

@@ -0,0 +1,55 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_db_dbconn
#define headerfile_winix_db_dbconn
#include <string>
#include <libpq-fe.h>
#include "dbtextstream.h"
class DbConn
{
public:
DbConn();
~DbConn();
void SetConnParam(const std::string & database, const std::string & user, const std::string & pass);
void Connect();
void WaitForConnection();
void Close();
bool AssertConnection(bool put_log = true, bool throw_if_no_connection = true);
void SetDbParameters();
PGconn * GetPgConn();
private:
void LogConnectionSocket();
PGconn * pg_conn;
std::string db_database, db_user, db_pass;
DbTextStream conn_info;
// a helper method for escaping strings
template<class RawType>
DbTextStream::RawText<RawType> R(const RawType & par)
{
return DbTextStream::RawText<RawType>(par);
}
};
#endif

66
db/dbitemcolumns.cpp Executable file
View File

@@ -0,0 +1,66 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "dbitemcolumns.h"
#include "dbbase.h"
void DbItemColumns::SetColumns(PGresult * r)
{
// PQfnumber returns -1 if there is no such a column
id = PQfnumber(r, "id");
user_id = PQfnumber(r, "user_id");
group_id = PQfnumber(r, "group_id");
privileges = PQfnumber(r, "privileges");
date_creation = PQfnumber(r, "date_creation");
date_modification = PQfnumber(r, "date_modification");
url = PQfnumber(r, "url");
type = PQfnumber(r, "type");
parent_id = PQfnumber(r, "parent_id");
content_id = PQfnumber(r, "content_id");
default_item = PQfnumber(r, "default_item");
subject = PQfnumber(r, "subject");
content = PQfnumber(r, "content");
content_type = PQfnumber(r, "content_type");
guest_name = PQfnumber(r, "guest_name");
auth = PQfnumber(r, "auth");
auth_path = PQfnumber(r, "auth_path");
html_template = PQfnumber(r, "template");
modification_user_id = PQfnumber(r, "modification_user_id");
}
void DbItemColumns::SetItem(PGresult * r, long row, Item & item)
{
if( id != -1 ) item.id = DbBase::AssertValueLong(r, row, id);
if( user_id != -1 ) item.user_id = DbBase::AssertValueLong(r, row, user_id);
if( group_id != -1 ) item.group_id = DbBase::AssertValueLong(r, row, group_id);
if( privileges != -1 ) item.privileges = DbBase::AssertValueInt(r, row, privileges);
if( date_creation != -1 ) item.date_creation = DbBase::ConvertTime( DbBase::AssertValue(r, row, date_creation) );
if( date_modification != -1 ) item.date_modification = DbBase::ConvertTime( DbBase::AssertValue(r, row, date_modification) );
if( url != -1 ) item.url = DbBase::AssertValue(r, row, url);
if( type != -1 ) item.type = static_cast<Item::Type>( DbBase::AssertValueInt(r, row, type) );
if( parent_id != -1 ) item.parent_id = DbBase::AssertValueLong(r, row, parent_id);
if( content_id != -1 ) item.content_id = DbBase::AssertValueLong(r, row, content_id);
if( default_item != -1 ) item.default_item = DbBase::AssertValueLong(r, row, default_item);
if( subject != -1 ) item.subject = DbBase::AssertValue(r, row, subject);
if( content != -1 ) item.content = DbBase::AssertValue(r, row, content);
if( content_type != -1 ) item.content_type = static_cast<Item::ContentType>( DbBase::AssertValueInt(r, row, content_type) );
if( guest_name != -1 ) item.guest_name = DbBase::AssertValue(r, row, guest_name);
if( auth != -1 ) item.auth = static_cast<Item::Auth>( DbBase::AssertValueInt(r, row, auth) );
if( auth_path != -1 ) item.auth_path = DbBase::AssertValue(r, row, auth_path);
if( html_template != -1 ) item.html_template = DbBase::AssertValue(r, row, html_template);
if( modification_user_id != -1 ) item.modification_user_id = DbBase::AssertValueLong(r, row, modification_user_id);
}

50
db/dbitemcolumns.h Executable file
View File

@@ -0,0 +1,50 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_db_dbitemcolumns
#define headerfile_winix_db_dbitemcolumns
#include <libpq-fe.h>
#include "core/item.h"
struct DbItemColumns
{
int id;
int user_id;
int group_id;
int privileges;
int date_creation;
int date_modification;
int url;
int type;
int parent_id;
int content_id;
int default_item;
int subject;
int content;
int content_type;
int guest_name;
int auth;
int auth_path;
int modification_user_id;
int html_template;
void SetColumns(PGresult * r);
void SetItem(PGresult * r, long row, Item & item);
};
#endif

97
db/dbitemquery.cpp Executable file
View File

@@ -0,0 +1,97 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "dbitemquery.h"
DbItemQuery::DbItemQuery()
{
sort_asc = true;
auth_equal = true;
SetAll(true, false);
id = -1;
parent_id = -1;
type = Item::none;
auth = Item::auth_none;
}
void DbItemQuery::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 DbItemQuery::SetAllWhere(bool where_)
{
where_id = where_;
where_parent_id = where_;
where_type = where_;
where_auth = where_;
}
void DbItemQuery::SetAll(bool sel, bool where_)
{
SetAllSel(sel);
SetAllWhere(where_);
}
void DbItemQuery::WhereId(long id_)
{
where_id = true;
id = id_;
}
void DbItemQuery::WhereParentId(long parent_id_)
{
where_parent_id = true;
parent_id = parent_id_;
}
void DbItemQuery::WhereType(Item::Type type_)
{
where_type = true;
type = type_;
}
void DbItemQuery::WhereAuth(Item::Auth st, bool equal)
{
where_auth = true;
auth = st;
auth_equal = equal;
}

67
db/dbitemquery.h Executable file
View File

@@ -0,0 +1,67 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_db_dbitemquery
#define headerfile_winix_db_dbitemquery
#include "core/item.h"
struct DbItemQuery
{
// 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;
DbItemQuery();
void SetAllSel(bool sel);
void SetAllWhere(bool where_);
void SetAll(bool sel, bool where_);
void WhereId(long id_);
void WhereParentId(long parent_id_);
void WhereType(Item::Type type_);
void WhereAuth(Item::Auth st, bool equal = true);
};
#endif

364
db/dbtextstream.cpp Executable file
View File

@@ -0,0 +1,364 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "dbtextstream.h"
DbTextStream::DbTextStream()
{
was_param = false;
ext_escape = true;
}
void DbTextStream::SetExtented(bool ext)
{
ext_escape = ext;
}
/*
without escaping
*/
DbTextStream & DbTextStream::PutText(const char * str)
{
buffer += str;
was_param = false;
return *this;
}
DbTextStream & DbTextStream::PutText(const std::string * str)
{
return PutText(str->c_str());
}
DbTextStream & DbTextStream::PutText(const std::string & str)
{
return PutText(str.c_str());
}
DbTextStream & DbTextStream::operator<<(const DbTextStream::RawText<const char*> & raw)
{
return PutText(raw.par);
}
DbTextStream & DbTextStream::operator<<(DbTextStream::RawText<std::string> raw)
{
return PutText(raw.par.c_str());
}
DbTextStream & DbTextStream::operator<<(RawText<char> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<int> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<long> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<unsigned int> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<unsigned long> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<double> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<void*> raw)
{
TextStream::operator<<(raw.par);
was_param = false;
return *this;
}
/*
with escaping
*/
// !! sprawdzic jej dzialanie dla kolumn bytea (binarnych)
DbTextStream & DbTextStream::EBinPutChar(char c_)
{
char buf[10];
int c = (unsigned char)c_;
if( (c>=0 && c<=31) || c>=127 || c==39 || c==92 )
{
sprintf(buf, "\\\\%03o", c);
buffer += buf;
}
else
{
buffer += c;
}
return *this;
}
DbTextStream & DbTextStream::ETextPutChar(char c)
{
if( c == '\\' )
buffer += "\\\\";
else
if( c == '\'' )
buffer += "\\\'"; // don't use "''" because we use the method for PQconnectdb too
else
if( c != 0 )
buffer += c;
return *this;
}
DbTextStream & DbTextStream::EPutText(const char * str)
{
if( was_param )
buffer += ", ";
if( ext_escape )
buffer += 'E';
buffer += '\'';
for( ; *str ; ++str )
ETextPutChar(*str);
buffer += '\'';
was_param = true;
return *this;
}
DbTextStream & DbTextStream::EPutText(const std::string * str)
{
return EPutText(str->c_str());
}
DbTextStream & DbTextStream::EPutText(const std::string & str)
{
return EPutText(str.c_str());
}
// this method can escaped 0 in the middle of the string
DbTextStream & DbTextStream::EPutBin(const char * str, size_t len)
{
if( was_param )
buffer += ", ";
if( ext_escape )
buffer += 'E';
buffer += '\'';
for(size_t i = 0 ; i < len ; ++i)
EBinPutChar(str[i]);
buffer += '\'';
was_param = true;
return *this;
}
DbTextStream & DbTextStream::EPutBin(const std::string * str)
{
return EPutBin(str->c_str(), str->size());
}
DbTextStream & DbTextStream::EPutBin(const std::string & str)
{
return EPutBin(str.c_str(), str.size());
}
DbTextStream & DbTextStream::operator<<(const char * str)
{
return EPutText(str);
}
DbTextStream & DbTextStream::operator<<(const std::string * str)
{
return EPutText(str);
}
DbTextStream & DbTextStream::operator<<(const std::string & str)
{
return EPutText(str);
}
DbTextStream & DbTextStream::operator<<(char v)
{
if( was_param )
buffer += ", ";
if( ext_escape )
buffer += 'E';
buffer += '\'';
ETextPutChar(v);
buffer += '\'';
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(int v)
{
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(long v)
{
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(unsigned int v)
{
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(unsigned long v)
{
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(double v)
{
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(const void * v)
{
if( was_param )
buffer += ", ";
buffer += '\''; // !! not needed here?
TextStream::operator<<(v);
buffer += '\'';
was_param = true;
return *this;
}

144
db/dbtextstream.h Executable file
View File

@@ -0,0 +1,144 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_core_dbtextstream
#define headerfile_winix_core_dbtextstream
#include "core/textstream.h"
/*
DbTextStream is used as a buffer for creating a database's query
By default all operators<< espace its string artuments. If you don't want
to escape an argument you should use a helper function R() (raw argument)
note: you have to define the function yourself, we do not provide it
because such a short name would make a mess in namespaces
sample:
create a helper function R as follows:
template<class RawType>
DbTextStream::RawText<RawType> R(const RawType & par)
{
return DbTextStream::RawText<RawType>(par);
}
now you can use DbTextStream in an easy way:
DbTextStream query;
std::string key = "some string";
query << R("select * from table where key=") << key << R(";");
in above example only the key is escaped.
Also with escaping operators<< insert commas between parameters, e.g.:
query << R("insert into table (key1, key2, key3) values (")
<< key1
<< key2
<< key3
<< R(");");
between key1 key2 and key3 are commas inserted automatically
*/
class DbTextStream : public TextStream
{
public:
/*
a helper struct to select a proper operator<<
(for non-escaping versions of these operators)
*/
template<class RawType>
struct RawText
{
const RawType & par;
RawText(const RawText<RawType> & p) : par(p.par) {}
RawText(const RawType & p) : par(p) {}
};
DbTextStream();
// extented escaping: adding E character before the first quote e.g. E'string'
// default: true
void SetExtented(bool ext);
/*
without escaping
*/
DbTextStream & PutText(const char *);
DbTextStream & PutText(const std::string *);
DbTextStream & PutText(const std::string &);
/*
we need this template operator for such calling:
dbtextstream_object << R("some string");
"some string" is actually a table (not a pointer)
*/
template<size_t str_size>
DbTextStream & operator<<(const RawText<char [str_size]> & raw) { return PutText(raw.par); }
DbTextStream & operator<<(const RawText<const char*> & raw);
DbTextStream & operator<<(RawText<std::string> raw);
DbTextStream & operator<<(RawText<char> raw);
DbTextStream & operator<<(RawText<int> raw);
DbTextStream & operator<<(RawText<long> raw);
DbTextStream & operator<<(RawText<unsigned int> raw);
DbTextStream & operator<<(RawText<unsigned long> raw);
DbTextStream & operator<<(RawText<double> raw);
DbTextStream & operator<<(RawText<void*> raw);
/*
with escaping
*/
DbTextStream & EBinPutChar(char c);
DbTextStream & ETextPutChar(char c);
DbTextStream & EPutText(const char * str);
DbTextStream & EPutText(const std::string * str);
DbTextStream & EPutText(const std::string & str);
DbTextStream & EPutBin(const char * str, size_t len);
DbTextStream & EPutBin(const std::string * str);
DbTextStream & EPutBin(const std::string & str);
DbTextStream & operator<<(const char * str);
DbTextStream & operator<<(const std::string * str);
DbTextStream & operator<<(const std::string & str);
DbTextStream & operator<<(char);
DbTextStream & operator<<(int);
DbTextStream & operator<<(long);
DbTextStream & operator<<(unsigned int);
DbTextStream & operator<<(unsigned long);
DbTextStream & operator<<(double);
DbTextStream & operator<<(const void *);
private:
bool was_param;
bool ext_escape;
};
#endif