moved winix directories to winixd subdirectory
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@1027 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
19
winixd/db/Makefile
Normal file
19
winixd/db/Makefile
Normal file
@@ -0,0 +1,19 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
|
||||
all: $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) $(CXXWINIXINCLUDEFLAGS) $<
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. $(CXXWINIXINCLUDEFLAGS) -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
|
||||
75
winixd/db/Makefile.dep
Normal file
75
winixd/db/Makefile.dep
Normal file
@@ -0,0 +1,75 @@
|
||||
# DO NOT DELETE
|
||||
|
||||
db.o: db.h dbbase.h dbconn.h dbtextstream.h ../../winix/core/textstream.h
|
||||
db.o: ../../winix/core/misc.h ../../winix/core/item.h
|
||||
db.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h
|
||||
db.o: ../../pikotools/date/date.h ../../winix/core/requesttypes.h
|
||||
db.o: ../../pikotools/textstream/textstream.h
|
||||
db.o: ../../pikotools/convert/convert.h ../../pikotools/convert/inttostr.h
|
||||
db.o: ../../pikotools/membuffer/membuffer.h
|
||||
db.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h
|
||||
db.o: ../../winix/core/winix_const.h ../../winix/core/error.h
|
||||
db.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h
|
||||
db.o: dbitemquery.h ../../winix/core/item.h dbitemcolumns.h
|
||||
db.o: ../../winix/core/user.h ../../winix/core/group.h
|
||||
db.o: ../../winix/core/dircontainer.h ../../winix/core/ugcontainer.h
|
||||
db.o: ../../winix/core/log.h ../../winix/core/textstream.h
|
||||
db.o: ../../winix/core/logmanipulators.h ../../winix/core/slog.h
|
||||
db.o: ../../winix/core/cur.h ../../winix/core/request.h
|
||||
db.o: ../../winix/core/error.h ../../winix/core/config.h
|
||||
db.o: ../../winix/core/htmlfilter.h ../../winix/templates/htmltextstream.h
|
||||
db.o: ../../pikotools/space/spacetojson.h ../../ezc/src/outstreams.h
|
||||
db.o: ../../winix/core/session.h ../../winix/core/user.h
|
||||
db.o: ../../winix/core/plugindata.h ../../winix/core/rebus.h
|
||||
db.o: ../../winix/core/ipban.h ../../winix/core/mount.h
|
||||
db.o: ../../winix/templates/locale.h ../../winix/core/log.h
|
||||
db.o: ../../winix/core/misc.h
|
||||
dbbase.o: dbbase.h dbconn.h dbtextstream.h ../../winix/core/textstream.h
|
||||
dbbase.o: ../../winix/core/misc.h ../../winix/core/item.h
|
||||
dbbase.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h
|
||||
dbbase.o: ../../pikotools/date/date.h ../../winix/core/requesttypes.h
|
||||
dbbase.o: ../../pikotools/textstream/textstream.h
|
||||
dbbase.o: ../../pikotools/convert/convert.h
|
||||
dbbase.o: ../../pikotools/convert/inttostr.h
|
||||
dbbase.o: ../../pikotools/membuffer/membuffer.h
|
||||
dbbase.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h
|
||||
dbbase.o: ../../winix/core/winix_const.h ../../winix/core/error.h
|
||||
dbbase.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h
|
||||
dbbase.o: ../../winix/core/log.h ../../winix/core/misc.h
|
||||
dbconn.o: dbconn.h dbtextstream.h ../../winix/core/textstream.h
|
||||
dbconn.o: ../../winix/core/misc.h ../../winix/core/item.h
|
||||
dbconn.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h
|
||||
dbconn.o: ../../pikotools/date/date.h ../../winix/core/requesttypes.h
|
||||
dbconn.o: ../../pikotools/textstream/textstream.h
|
||||
dbconn.o: ../../pikotools/convert/convert.h
|
||||
dbconn.o: ../../pikotools/convert/inttostr.h
|
||||
dbconn.o: ../../pikotools/membuffer/membuffer.h
|
||||
dbconn.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h
|
||||
dbconn.o: ../../winix/core/winix_const.h ../../winix/core/log.h
|
||||
dbconn.o: ../../winix/core/error.h
|
||||
dbitemcolumns.o: dbitemcolumns.h ../../winix/core/item.h dbbase.h dbconn.h
|
||||
dbitemcolumns.o: dbtextstream.h ../../winix/core/textstream.h
|
||||
dbitemcolumns.o: ../../winix/core/misc.h ../../winix/core/item.h
|
||||
dbitemcolumns.o: ../../pikotools/space/space.h
|
||||
dbitemcolumns.o: ../../pikotools/textstream/types.h
|
||||
dbitemcolumns.o: ../../pikotools/date/date.h ../../winix/core/requesttypes.h
|
||||
dbitemcolumns.o: ../../pikotools/textstream/textstream.h
|
||||
dbitemcolumns.o: ../../pikotools/convert/convert.h
|
||||
dbitemcolumns.o: ../../pikotools/convert/inttostr.h
|
||||
dbitemcolumns.o: ../../pikotools/membuffer/membuffer.h
|
||||
dbitemcolumns.o: ../../pikotools/textstream/types.h
|
||||
dbitemcolumns.o: ../../pikotools/utf8/utf8.h ../../winix/core/winix_const.h
|
||||
dbitemcolumns.o: ../../winix/core/error.h ../../pikotools/space/spaceparser.h
|
||||
dbitemcolumns.o: ../../pikotools/space/space.h ../../winix/core/log.h
|
||||
dbitemquery.o: dbitemquery.h ../../winix/core/item.h
|
||||
dbtextstream.o: dbtextstream.h ../../winix/core/textstream.h
|
||||
dbtextstream.o: ../../winix/core/misc.h ../../winix/core/item.h
|
||||
dbtextstream.o: ../../pikotools/space/space.h
|
||||
dbtextstream.o: ../../pikotools/textstream/types.h
|
||||
dbtextstream.o: ../../pikotools/date/date.h ../../winix/core/requesttypes.h
|
||||
dbtextstream.o: ../../pikotools/textstream/textstream.h
|
||||
dbtextstream.o: ../../pikotools/convert/convert.h
|
||||
dbtextstream.o: ../../pikotools/convert/inttostr.h
|
||||
dbtextstream.o: ../../pikotools/membuffer/membuffer.h
|
||||
dbtextstream.o: ../../pikotools/textstream/types.h
|
||||
dbtextstream.o: ../../pikotools/utf8/utf8.h ../../winix/core/winix_const.h
|
||||
1
winixd/db/Makefile.o.dep
Normal file
1
winixd/db/Makefile.o.dep
Normal file
@@ -0,0 +1 @@
|
||||
o = db.o dbbase.o dbconn.o dbitemcolumns.o dbitemquery.o dbtextstream.o
|
||||
1777
winixd/db/db.cpp
Normal file
1777
winixd/db/db.cpp
Normal file
File diff suppressed because it is too large
Load Diff
176
winixd/db/db.h
Normal file
176
winixd/db/db.h
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_db_db
|
||||
#define headerfile_winix_db_db
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
#include "dbbase.h"
|
||||
#include "dbitemquery.h"
|
||||
#include "dbitemcolumns.h"
|
||||
#include "core/item.h"
|
||||
#include "core/user.h"
|
||||
#include "core/group.h"
|
||||
#include "core/dircontainer.h"
|
||||
#include "core/ugcontainer.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class Db : public DbBase
|
||||
{
|
||||
public:
|
||||
|
||||
// !! przerobic tak aby GetItem zwracalo wszystkie pozycja
|
||||
// !! GetFile tylko dla plikow
|
||||
// !! GetDir tylko dla katalogow
|
||||
// !! GetFile i GetDir beda uzywac GetItem
|
||||
|
||||
Db() : item_cols(*this)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool GetUserPass(const std::wstring & login, long & user_id, UserPass & up);
|
||||
Error AddUser(User & user, const UserPass & up);
|
||||
Error ChangeUserPass(long user_id, const UserPass & up);
|
||||
Error ChangeUserEnv(long user_id, const PT::Space & space);
|
||||
Error ChangeUserAdminEnv(long user_id, const PT::Space & space);
|
||||
Error ChangeUserStatus(long user_id, int status);
|
||||
Error ChangeUserEmail(long user_id, const std::wstring & email);
|
||||
Error ChangeUserLocale(long user_id, size_t locale_id);
|
||||
Error ChangeUserTimeZone(long user_id, size_t time_zone_id);
|
||||
Error RemoveUser(long user_id);
|
||||
|
||||
Error AddItem(Item & item);
|
||||
Error EditItemById(Item & item, bool with_url = true);
|
||||
Error EditItemByUrl(Item & item, bool with_url = true);
|
||||
|
||||
|
||||
|
||||
// !! nie zwracac zadnych kodow bledow?
|
||||
void GetItems(std::vector<Item> & item_tab, const DbItemQuery & item_query);
|
||||
void GetItems(std::vector<long> & item_tab, const DbItemQuery & item_query);
|
||||
Error GetItem(Item & item, const DbItemQuery & item_query);
|
||||
|
||||
bool GetPriv(Item & item, long id);
|
||||
Error EditPrivById(Item & item, long id); // !! dlaczego tu nie ma const?
|
||||
Error EditParentUrlById(Item & item, long id);
|
||||
Error EditFileById(const Item & item, long id); // file_path, file_fs, file_type
|
||||
Error EditHasThumbById(bool has_thumb, long id);
|
||||
Error EditMetaById(const PT::Space & meta, long id);
|
||||
Error EditAdminMetaById(const PT::Space & meta, long id);
|
||||
|
||||
Error DelDirById(long id);
|
||||
Error DelFileById(long file_id);
|
||||
Error DelSymlinkById(long symlink_id);
|
||||
|
||||
Error EditSubjectById(Item & item, long id);
|
||||
|
||||
|
||||
Error DelItem(const Item & item);
|
||||
void GetDirs(DirContainer & dir_tab);
|
||||
void GetUsers(UGContainer<User> & user_tab);
|
||||
void GetGroups(UGContainer<Group> & group_tab);
|
||||
|
||||
// !! nowy interfejs
|
||||
long Size(long parent_id, Item::Type type = Item::none);
|
||||
|
||||
Error GetItemById(long item_id, Item & item);
|
||||
Error GetItem(long parent_id, const std::wstring & url, Item & item);
|
||||
Error EditLinkItem(long id, const std::wstring & link_to, int link_redirect);
|
||||
Error EditTemplateItemById(long id, const std::wstring & new_html_template);
|
||||
|
||||
long GetItemId(long parent_id, const std::wstring & url, Item::Type type);
|
||||
long GetFileId(long parent_id, const std::wstring & url);
|
||||
long GetDirId(long parent_id, const std::wstring & url);
|
||||
|
||||
Error AddHardLink(Item & item);
|
||||
Error EditSortIndexItemById(long id, int sort_index);
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
DbTextStream query, query_create_url;
|
||||
std::wstring temp_url;
|
||||
Item dir_temp;
|
||||
Item get_item_temp;
|
||||
std::wstring iq_id_list;
|
||||
DbItemColumns item_cols;
|
||||
|
||||
|
||||
bool AddItemCreateUrlSubject(Item & item);
|
||||
|
||||
Error AddItemIntoContent(Item & item);
|
||||
Error AddItemIntoItem(Item & item);
|
||||
|
||||
Error EditItemInItem(Item & item, bool with_url);
|
||||
Error EditItemInContent(Item & item);
|
||||
Error EditItemGetIdsByUrl(Item & item);
|
||||
long GetContentId(long item_id);
|
||||
|
||||
|
||||
Error DelItemDelItem(long item_id, int type);
|
||||
Error DelItemDelContent(long content_id);
|
||||
|
||||
Error IncrementContentRef(long content_id);
|
||||
Error DecrementContentRef(long content_id);
|
||||
|
||||
void GetItemsQuerySelect(const DbItemQuery & iq, DbTextStream & query, bool skip_other_sel);
|
||||
void GetItemsQueryJoin(const DbItemQuery & iq, DbTextStream & query);
|
||||
void GetItemsQueryWhere(const DbItemQuery & iq, DbTextStream & query);
|
||||
void GetItemsQueryOrder(bool sort_asc);
|
||||
void GetItemsQueryOrder(const DbItemQuery & iq, DbTextStream & query);
|
||||
void GetItemsQueryLimit(const DbItemQuery & iq, DbTextStream & query);
|
||||
PGresult * GetItemsQuery(const DbItemQuery & iq, DbTextStream & query, bool skip_other_sel = false);
|
||||
PGresult * GetItemsQuery(const DbItemQuery & iq, bool skip_other_sel = false);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
626
winixd/db/dbbase.cpp
Normal file
626
winixd/db/dbbase.cpp
Normal file
@@ -0,0 +1,626 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <limits>
|
||||
#include "dbbase.h"
|
||||
#include "core/log.h"
|
||||
#include "core/error.h"
|
||||
#include "core/misc.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
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 DbTextStream & query)
|
||||
{
|
||||
return AssertQuery(query.CStr());
|
||||
}
|
||||
|
||||
|
||||
|
||||
PGresult * DbBase::AssertQuery(const char * q, ExecStatusType t)
|
||||
{
|
||||
PGresult * r = AssertQuery(q);
|
||||
AssertResult(r, t);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
PGresult * DbBase::AssertQuery(const DbTextStream & query, ExecStatusType t)
|
||||
{
|
||||
return AssertQuery(query.CStr(), t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbBase::AssertResult(PGresult * r, ExecStatusType t)
|
||||
{
|
||||
if( PQresultStatus(r) != t )
|
||||
{
|
||||
log << log1 << "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;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & DbBase::AssertValueWide(PGresult * r, int row, int col)
|
||||
{
|
||||
const char * res = AssertValue(r, row, col);
|
||||
static std::wstring temp_wide_value; // !! IMPROVE ME add as a class field (nonstatic)
|
||||
|
||||
PT::UTF8ToWide(res, temp_wide_value);
|
||||
|
||||
return temp_wide_value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
|
||||
{
|
||||
result.clear();
|
||||
|
||||
const char * res = AssertValue(r, row, col);
|
||||
int len = PQgetlength(r, row, col);
|
||||
|
||||
if( len <= 0 )
|
||||
return;
|
||||
|
||||
UnescapeBin(res, len, result);
|
||||
}
|
||||
|
||||
|
||||
void DbBase::AssertValueWide(PGresult * r, int row, int col, std::wstring & result)
|
||||
{
|
||||
const char * res = AssertValue(r, row, col);
|
||||
PT::UTF8ToWide(res, result);
|
||||
}
|
||||
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
|
||||
bool DbBase::AssertValueBool(PGresult * r, int row, int col)
|
||||
{
|
||||
const char * s = AssertValue(r, row, col);
|
||||
return (s[0]=='t' || s[0]=='y' || s[0]=='1');
|
||||
}
|
||||
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
PT::Date DbBase::AssertValueDate(PGresult * r, int row, int col)
|
||||
{
|
||||
PT::Date date = AssertValue(r, row, col);
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
|
||||
bool DbBase::AssertValueSpace(PGresult * r, int row, int col, PT::Space & space, bool split_single)
|
||||
{
|
||||
const char * res = AssertValue(r, row, col);
|
||||
|
||||
conf_parser.SplitSingle(split_single);
|
||||
conf_parser.SetSpace(space);
|
||||
space.Clear();
|
||||
|
||||
PT::SpaceParser::Status status = conf_parser.ParseString(res);
|
||||
|
||||
if( status != PT::SpaceParser::ok )
|
||||
{
|
||||
log << log1 << "Db: a problem with parsing a PT::Space";
|
||||
|
||||
if( status == PT::SpaceParser::syntax_error )
|
||||
log << ", syntax error at line: " << conf_parser.line;
|
||||
|
||||
log << logend;
|
||||
|
||||
space.Clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void DbBase::CreateIdList(const std::vector<long> & id_tab, std::wstring & list, bool add_parentheses)
|
||||
{
|
||||
wchar_t buffer[50];
|
||||
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
||||
|
||||
list.clear();
|
||||
|
||||
if( add_parentheses )
|
||||
list += '(';
|
||||
|
||||
for(size_t i=0 ; i<id_tab.size() ; ++i)
|
||||
{
|
||||
Toa((unsigned long)id_tab[i], buffer, buffer_len);
|
||||
list += buffer;
|
||||
|
||||
if( i+1 < id_tab.size() )
|
||||
list += ',';
|
||||
}
|
||||
|
||||
if( add_parentheses )
|
||||
list += ')';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error DbBase::DoCommand(const DbTextStream & command)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
r = AssertQuery(command);
|
||||
AssertResult(r, PGRES_COMMAND_OK);
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Error DbBase::DoCommand(const char * command)
|
||||
{
|
||||
PGresult * r = 0;
|
||||
Error status = WINIX_ERR_OK;
|
||||
|
||||
try
|
||||
{
|
||||
r = AssertQuery(command);
|
||||
AssertResult(r, PGRES_COMMAND_OK);
|
||||
}
|
||||
catch(const Error & e)
|
||||
{
|
||||
status = e;
|
||||
}
|
||||
|
||||
ClearResult(r);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Error DbBase::BeginTrans()
|
||||
{
|
||||
return DoCommand("BEGIN;");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Error DbBase::RollbackTrans()
|
||||
{
|
||||
return DoCommand("ROLLBACK;");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Error DbBase::CommitTrans()
|
||||
{
|
||||
return DoCommand("COMMIT;");
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool DbBase::EndTrans(bool everything_ok)
|
||||
{
|
||||
bool result;
|
||||
|
||||
if( everything_ok )
|
||||
{
|
||||
result = (CommitTrans() == WINIX_ERR_OK);
|
||||
}
|
||||
else
|
||||
{
|
||||
RollbackTrans();
|
||||
// we return the old err code
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Error DbBase::EndTrans(Error err)
|
||||
{
|
||||
if( err == WINIX_ERR_OK )
|
||||
{
|
||||
err = CommitTrans();
|
||||
}
|
||||
else
|
||||
{
|
||||
// we return the old err code
|
||||
RollbackTrans();
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
converting from a bytea
|
||||
the old way (escape format)
|
||||
*/
|
||||
/*
|
||||
int DbBase::CharToInt(char c)
|
||||
{
|
||||
return (int)(unsigned char)(c-'0');
|
||||
}
|
||||
|
||||
bool DbBase::IsCorrectOctalDigit(char c)
|
||||
{
|
||||
return c>='0' && c<='7';
|
||||
}
|
||||
|
||||
// moves 'i' at least once
|
||||
// return -1 if there is en error
|
||||
int DbBase::UnescapeBin(const char * str, size_t & i, size_t len)
|
||||
{
|
||||
if( str[i] != '\\' )
|
||||
return str[i++];
|
||||
|
||||
i += 1;
|
||||
|
||||
if( i >= len )
|
||||
return -1;
|
||||
|
||||
if( str[i] == '\\' )
|
||||
return str[i++];
|
||||
|
||||
if( i+2 >= len )
|
||||
{
|
||||
i = len;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( !IsCorrectOctalDigit(str[i]) ||
|
||||
!IsCorrectOctalDigit(str[i+1]) ||
|
||||
!IsCorrectOctalDigit(str[i+2]) )
|
||||
{
|
||||
i += 3;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int c = 8*8*CharToInt(str[i]) + 8*CharToInt(str[i+1]) + CharToInt(str[i+2]);
|
||||
|
||||
i += 3;
|
||||
|
||||
if( c<0 || c>255 )
|
||||
return -1;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void DbBase::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out)
|
||||
{
|
||||
int c;
|
||||
size_t i = 0;
|
||||
|
||||
if( clear_out )
|
||||
out.clear();
|
||||
|
||||
while( i < len )
|
||||
{
|
||||
c = UnescapeBin(str, i, len);
|
||||
|
||||
if( c != -1 )
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
converting from a bytea
|
||||
the new way (hex format)
|
||||
*/
|
||||
|
||||
|
||||
char DbBase::UnescapeBinHexToDigit(char hex)
|
||||
{
|
||||
if( hex>='0' && hex<='9' )
|
||||
return hex - '0';
|
||||
|
||||
if( hex>='a' && hex<='z' )
|
||||
return hex - 'a' + 10;
|
||||
|
||||
if( hex>='A' && hex<='Z' )
|
||||
return hex - 'A' + 10;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void DbBase::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out)
|
||||
{
|
||||
if( clear_out )
|
||||
out.clear();
|
||||
|
||||
if( len < 2 || str[0]!='\\' || str[1]!='x' )
|
||||
{
|
||||
log << log1 << "Db: unsupported binary format (skipping)" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
for(size_t i=2 ; i + 1 < len ; i+=2 )
|
||||
{
|
||||
int c1 = UnescapeBinHexToDigit(str[i]);
|
||||
int c2 = UnescapeBinHexToDigit(str[i+1]);
|
||||
|
||||
out += ((c1 << 4) | c2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
end of converting from bytea
|
||||
*/
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
143
winixd/db/dbbase.h
Normal file
143
winixd/db/dbbase.h
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_db_dbbase
|
||||
#define headerfile_winix_db_dbbase
|
||||
|
||||
|
||||
#include "dbconn.h"
|
||||
#include "dbtextstream.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "core/error.h"
|
||||
#include "space/spaceparser.h"
|
||||
#include "date/date.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
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 DbTextStream & query);
|
||||
PGresult * AssertQuery(const char * q, ExecStatusType t);
|
||||
PGresult * AssertQuery(const DbTextStream & query, ExecStatusType t);
|
||||
void AssertResult(PGresult * r, ExecStatusType t);
|
||||
static int AssertColumn(PGresult * r, const char * column_name);
|
||||
|
||||
// static assignments
|
||||
static const char * AssertValue(PGresult * r, int row, int col);
|
||||
static const std::wstring & AssertValueWide(PGresult * r, int row, int col); // warning: this method uses a static buffer
|
||||
static void AssertValueWide(PGresult * r, int row, int col, std::wstring & result);
|
||||
static void AssertValueBin(PGresult * r, int row, int col, std::string & result);
|
||||
static long AssertValueLong(PGresult * r, int row, int col);
|
||||
static int AssertValueInt(PGresult * r, int row, int col);
|
||||
static bool AssertValueBool(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);
|
||||
static PT::Date AssertValueDate(PGresult * r, int row, int col);
|
||||
|
||||
// non static assignments
|
||||
bool AssertValueSpace(PGresult * r, int row, int col, PT::Space & space, bool split_single = true);
|
||||
|
||||
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);
|
||||
|
||||
void CreateIdList(const std::vector<long> & id_tab, std::wstring & list, bool add_parentheses = true);
|
||||
|
||||
Error DoCommand(const DbTextStream & command);
|
||||
Error DoCommand(const char * command);
|
||||
Error BeginTrans();
|
||||
Error RollbackTrans();
|
||||
Error CommitTrans();
|
||||
Error EndTrans(Error err);
|
||||
|
||||
// new API (returns only bool)
|
||||
bool EndTrans(bool everything_ok);
|
||||
|
||||
static void UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out = true);
|
||||
|
||||
|
||||
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:
|
||||
|
||||
// static int CharToInt(char c);
|
||||
// static bool IsCorrectOctalDigit(char c);
|
||||
// static int UnescapeBin(const char * str, size_t & i, size_t len);
|
||||
|
||||
static char UnescapeBinHexToDigit(char hex);
|
||||
|
||||
DbTextStream bquery;
|
||||
PT::SpaceParser conf_parser;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
186
winixd/db/dbconn.cpp
Normal file
186
winixd/db/dbconn.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include "dbconn.h"
|
||||
#include "core/log.h"
|
||||
#include "core/error.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
DbConn::DbConn()
|
||||
{
|
||||
pg_conn = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
DbConn::~DbConn()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
PGconn * DbConn::GetPgConn()
|
||||
{
|
||||
return pg_conn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbConn::SetConnParam(const std::wstring & d, const std::wstring & u, const std::wstring & 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 << logsave;
|
||||
|
||||
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, "UTF8") == -1 )
|
||||
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
84
winixd/db/dbconn.h
Normal file
84
winixd/db/dbconn.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_db_dbconn
|
||||
#define headerfile_winix_db_dbconn
|
||||
|
||||
#include <string>
|
||||
#include <libpq-fe.h>
|
||||
#include "dbtextstream.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class DbConn
|
||||
{
|
||||
public:
|
||||
|
||||
DbConn();
|
||||
~DbConn();
|
||||
|
||||
void SetConnParam(const std::wstring & database, const std::wstring & user, const std::wstring & 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::wstring 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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
123
winixd/db/dbitemcolumns.cpp
Normal file
123
winixd/db/dbitemcolumns.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "dbitemcolumns.h"
|
||||
#include "dbbase.h"
|
||||
#include "core/log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
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");
|
||||
link_to = PQfnumber(r, "link_to");
|
||||
link_redirect = PQfnumber(r, "link_redirect");
|
||||
subject = PQfnumber(r, "subject");
|
||||
content = PQfnumber(r, "content");
|
||||
content_type = PQfnumber(r, "content_type");
|
||||
guest_name = PQfnumber(r, "guest_name");
|
||||
html_template = PQfnumber(r, "template");
|
||||
modification_user_id = PQfnumber(r, "modification_user_id");
|
||||
file_path = PQfnumber(r, "file_path");
|
||||
file_fs = PQfnumber(r, "file_fs");
|
||||
file_type = PQfnumber(r, "file_type");
|
||||
has_thumb = PQfnumber(r, "has_thumb");
|
||||
hash = PQfnumber(r, "hash");
|
||||
hash_type = PQfnumber(r, "hash_type");
|
||||
file_size = PQfnumber(r, "file_size");
|
||||
ref = PQfnumber(r, "ref");
|
||||
modify_index = PQfnumber(r, "modify_index");
|
||||
sort_index = PQfnumber(r, "sort_index");
|
||||
meta = PQfnumber(r, "meta");
|
||||
ameta = PQfnumber(r, "ameta");
|
||||
}
|
||||
|
||||
|
||||
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::AssertValueDate(r, row, date_creation);
|
||||
if( date_modification != -1 ) item.date_modification = DbBase::AssertValueDate(r, row, date_modification);
|
||||
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( link_redirect != -1 ) item.link_redirect = DbBase::AssertValueInt(r, row, link_redirect);
|
||||
if( content_type != -1 ) item.content_type = static_cast<Item::ContentType>( DbBase::AssertValueInt(r, row, content_type) );
|
||||
if( modification_user_id != -1 ) item.modification_user_id = DbBase::AssertValueLong(r, row, modification_user_id);
|
||||
if( file_fs != -1 ) item.file_fs = DbBase::AssertValueInt(r, row, file_fs);
|
||||
if( file_type != -1 ) item.file_type = DbBase::AssertValueInt(r, row, file_type);
|
||||
if( has_thumb != -1 ) item.has_thumb = DbBase::AssertValueBool(r, row, has_thumb);
|
||||
if( hash != -1 ) DbBase::AssertValueWide(r, row, hash, item.hash);
|
||||
if( hash_type != -1 ) item.hash_type = DbBase::AssertValueInt(r, row, hash_type);
|
||||
if( file_size != -1 ) item.file_size = DbBase::AssertValueLong(r, row, file_size);
|
||||
|
||||
if( ref != -1 ) item.ref = DbBase::AssertValueInt(r, row, ref);
|
||||
if( modify_index != -1 ) item.modify_index = DbBase::AssertValueInt(r, row, modify_index);
|
||||
|
||||
if( url != -1 ) DbBase::AssertValueWide(r, row, url, item.url);
|
||||
if( content != -1 ) DbBase::AssertValueWide(r, row, content, item.content);
|
||||
if( subject != -1 ) DbBase::AssertValueWide(r, row, subject, item.subject);
|
||||
if( file_path != -1 ) DbBase::AssertValueWide(r, row, file_path, item.file_path);
|
||||
if( link_to != -1 ) DbBase::AssertValueWide(r, row, link_to, item.link_to);
|
||||
if( guest_name != -1 ) DbBase::AssertValueWide(r, row, guest_name, item.guest_name);
|
||||
if( html_template != -1 ) DbBase::AssertValueWide(r, row, html_template, item.html_template);
|
||||
if( sort_index != -1 ) item.sort_index = DbBase::AssertValueInt(r, row, sort_index);
|
||||
|
||||
if( meta != -1 ) db_base.AssertValueSpace(r, row, meta, item.meta);
|
||||
if( ameta != -1 ) db_base.AssertValueSpace(r, row, ameta, item.ameta);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
101
winixd/db/dbitemcolumns.h
Normal file
101
winixd/db/dbitemcolumns.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_db_dbitemcolumns
|
||||
#define headerfile_winix_db_dbitemcolumns
|
||||
|
||||
#include <libpq-fe.h>
|
||||
#include "core/item.h"
|
||||
#include "dbbase.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
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 link_to;
|
||||
int link_redirect;
|
||||
int subject;
|
||||
int content;
|
||||
int content_type;
|
||||
int guest_name;
|
||||
int modification_user_id;
|
||||
int html_template;
|
||||
int file_path;
|
||||
int file_fs;
|
||||
int file_type;
|
||||
int has_thumb;
|
||||
int hash;
|
||||
int hash_type;
|
||||
int file_size;
|
||||
int ref;
|
||||
int modify_index;
|
||||
int sort_index;
|
||||
int meta;
|
||||
int ameta;
|
||||
|
||||
DbItemColumns(DbBase & db_base_) : db_base(db_base_)
|
||||
{
|
||||
}
|
||||
|
||||
void SetColumns(PGresult * r);
|
||||
void SetItem(PGresult * r, long row, Item & item);
|
||||
|
||||
private:
|
||||
|
||||
DbBase & db_base;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
164
winixd/db/dbitemquery.cpp
Normal file
164
winixd/db/dbitemquery.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "dbitemquery.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
DbItemQuery::DbItemQuery()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void DbItemQuery::Clear()
|
||||
{
|
||||
sort_index_asc = true;
|
||||
sort_date_asc = true;
|
||||
|
||||
SetAll(true, false);
|
||||
|
||||
id = -1;
|
||||
id_tab = 0;
|
||||
parent_id = -1;
|
||||
type = Item::none;
|
||||
file_type = WINIX_ITEM_FILETYPE_NONE;
|
||||
|
||||
type_equal = true;
|
||||
file_type_equal = true;
|
||||
|
||||
limit = 0; // limit and offset not used by default
|
||||
offset = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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_link = sel;
|
||||
sel_file = sel;
|
||||
sel_html_template = sel;
|
||||
sel_sort_index = sel;
|
||||
sel_meta = sel;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbItemQuery::SetAllWhere(bool where_)
|
||||
{
|
||||
where_id = where_;
|
||||
where_parent_id = where_;
|
||||
where_type = where_;
|
||||
where_file_type = where_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbItemQuery::SetAll(bool sel, bool where_)
|
||||
{
|
||||
SetAllSel(sel);
|
||||
SetAllWhere(where_);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbItemQuery::WhereId(long id_)
|
||||
{
|
||||
where_id = true;
|
||||
id = id_;
|
||||
}
|
||||
|
||||
|
||||
void DbItemQuery::WhereIdIn(const std::vector<long> & id_tab_list)
|
||||
{
|
||||
id_tab = &id_tab_list;
|
||||
}
|
||||
|
||||
|
||||
void DbItemQuery::WhereParentId(long parent_id_)
|
||||
{
|
||||
where_parent_id = true;
|
||||
parent_id = parent_id_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbItemQuery::WhereType(Item::Type type_, bool equal)
|
||||
{
|
||||
where_type = true;
|
||||
type = type_;
|
||||
type_equal = equal;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbItemQuery::WhereFileType(int file_t, bool equal)
|
||||
{
|
||||
where_file_type = true;
|
||||
file_type = file_t;
|
||||
file_type_equal = equal;
|
||||
}
|
||||
|
||||
|
||||
void DbItemQuery::Limit(long l)
|
||||
{
|
||||
limit = l;
|
||||
}
|
||||
|
||||
|
||||
void DbItemQuery::Offset(long o)
|
||||
{
|
||||
offset = o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
112
winixd/db/dbitemquery.h
Normal file
112
winixd/db/dbitemquery.h
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_db_dbitemquery
|
||||
#define headerfile_winix_db_dbitemquery
|
||||
|
||||
#include <vector>
|
||||
#include "core/item.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
struct DbItemQuery
|
||||
{
|
||||
// id and content_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, ref, modify_index
|
||||
bool sel_url; // url
|
||||
bool sel_type; // type (dir, file, none)
|
||||
bool sel_link; // link_to, link_redirect
|
||||
bool sel_file; // file_path, file_fs, file_type, has_thumb, hash, hash_type, file_size
|
||||
bool sel_html_template; // template
|
||||
bool sel_sort_index; // sort_index
|
||||
bool sel_meta; // meta and ameta (PT::Space)
|
||||
|
||||
bool where_id; //
|
||||
bool where_parent_id; //
|
||||
bool where_type;
|
||||
bool where_file_type;
|
||||
|
||||
long id; // if where_id is true
|
||||
long parent_id; // if where_parent_id is true
|
||||
Item::Type type;
|
||||
bool type_equal;
|
||||
int file_type;
|
||||
bool file_type_equal; // if true means file_type should be equal
|
||||
bool sort_index_asc;
|
||||
bool sort_date_asc;
|
||||
|
||||
const std::vector<long> * id_tab; // != 0 if WhereIdIn was used
|
||||
|
||||
long limit;
|
||||
long offset;
|
||||
|
||||
DbItemQuery();
|
||||
void Clear();
|
||||
|
||||
void SetAllSel(bool sel);
|
||||
void SetAllWhere(bool where_);
|
||||
void SetAll(bool sel, bool where_);
|
||||
|
||||
void WhereId(long id_);
|
||||
void WhereIdIn(const std::vector<long> & id_tab_list);
|
||||
void WhereParentId(long parent_id_);
|
||||
void WhereType(Item::Type type_, bool equal = true);
|
||||
void WhereFileType(int file_t, bool equal = true);
|
||||
|
||||
void Limit(long l); // setting 0 turns off
|
||||
void Offset(long o); // setting 0 turns off
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
613
winixd/db/dbtextstream.cpp
Normal file
613
winixd/db/dbtextstream.cpp
Normal file
@@ -0,0 +1,613 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "dbtextstream.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
DbTextStream::DbTextStream()
|
||||
{
|
||||
was_param = false;
|
||||
ext_escape = true;
|
||||
}
|
||||
|
||||
|
||||
void DbTextStream::SetExtented(bool ext)
|
||||
{
|
||||
ext_escape = ext;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
without escaping
|
||||
*/
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::PutText(const char * str)
|
||||
{
|
||||
TextStream<std::string>::operator<<(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::PutText(const wchar_t * str)
|
||||
{
|
||||
TextStream<std::string>::operator<<(str);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::PutText(const std::wstring * str)
|
||||
{
|
||||
return PutText(str->c_str());
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::PutText(const std::wstring & str)
|
||||
{
|
||||
return PutText(str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<const char*> raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<const wchar_t*> raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<const std::string*> raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<const std::wstring*> raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<std::string> raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<std::wstring> raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<bool> raw)
|
||||
{
|
||||
if( raw.par )
|
||||
PutText("true");
|
||||
else
|
||||
PutText("false");
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<char> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<wchar_t> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<int> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<long> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<unsigned int> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<unsigned long> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<double> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<void*> raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<PT::Date> date)
|
||||
{
|
||||
tmp_stream.Clear();
|
||||
date.par.Serialize(tmp_stream);
|
||||
PT::WideToUTF8(tmp_stream.CStr(), buffer, false);
|
||||
tmp_stream.Clear();
|
||||
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
with escaping
|
||||
*/
|
||||
|
||||
|
||||
// get hex digit for c_ between <0, 15>
|
||||
char DbTextStream::EBinGetHex(char c)
|
||||
{
|
||||
if( c < 10 )
|
||||
return c + '0';
|
||||
|
||||
return c - 10 + 'A';
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::EBinPutChar(char c)
|
||||
{
|
||||
buffer += EBinGetHex(((unsigned char)c) >> 4);
|
||||
buffer += EBinGetHex(((unsigned char)c) & 0x0f);
|
||||
|
||||
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::ETextPutChar(wchar_t c)
|
||||
{
|
||||
if( c == '\\' )
|
||||
buffer += "\\\\";
|
||||
else
|
||||
if( c == '\'' )
|
||||
buffer += "\\\'"; // don't use "''" because we use the method for PQconnectdb too
|
||||
else
|
||||
if( c != 0 )
|
||||
PT::IntToUTF8(int(c), buffer, false);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::EPutText(const wchar_t * 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::wstring * str)
|
||||
{
|
||||
return EPutText(str->c_str());
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::EPutText(const std::wstring & 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 += "\'\\\\x";
|
||||
|
||||
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<<(const wchar_t * str)
|
||||
{
|
||||
return EPutText(str);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(const std::wstring * str)
|
||||
{
|
||||
return EPutText(str);
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(const std::wstring & str)
|
||||
{
|
||||
return EPutText(str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(bool v)
|
||||
{
|
||||
if( v )
|
||||
EPutText("true");
|
||||
else
|
||||
EPutText("false");
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
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<<(wchar_t 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<std::string>::operator<<(v);
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(long v)
|
||||
{
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
TextStream<std::string>::operator<<(v);
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(unsigned int v)
|
||||
{
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
TextStream<std::string>::operator<<(v);
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(unsigned long v)
|
||||
{
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
TextStream<std::string>::operator<<(v);
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(double v)
|
||||
{
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
TextStream<std::string>::operator<<(v);
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(const void * v)
|
||||
{
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
buffer += '\''; // !! not needed here?
|
||||
TextStream<std::string>::operator<<(v);
|
||||
buffer += '\'';
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(const std::vector<long> & tabid)
|
||||
{
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
buffer += '(';
|
||||
|
||||
for(size_t i=0 ; i<tabid.size(); ++i)
|
||||
{
|
||||
TextStream<std::string>::operator<<(tabid[i]);
|
||||
|
||||
if( i + 1 < tabid.size() )
|
||||
buffer += ',';
|
||||
}
|
||||
|
||||
buffer += ')';
|
||||
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(const PT::Space & space)
|
||||
{
|
||||
tmp_stream.Clear();
|
||||
// !! IMPROVE ME
|
||||
// we can calculate how much memory is needed before serializing
|
||||
space.Serialize(tmp_stream, true, false);
|
||||
operator<<(tmp_stream.Str());
|
||||
tmp_stream.Clear();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(const PT::Date & date)
|
||||
{
|
||||
tmp_stream.Clear();
|
||||
date.Serialize(tmp_stream);
|
||||
operator<<(tmp_stream.Str());
|
||||
tmp_stream.Clear();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
241
winixd/db/dbtextstream.h
Normal file
241
winixd/db/dbtextstream.h
Normal file
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_db_dbtextstream
|
||||
#define headerfile_winix_db_dbtextstream
|
||||
|
||||
#include <ctime>
|
||||
#include "core/textstream.h"
|
||||
#include "textstream/textstream.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
/*
|
||||
DbTextStream is used as a buffer for creating a database's query
|
||||
By default all operators<< escape 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<std::string>
|
||||
{
|
||||
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 &);
|
||||
DbTextStream & PutText(const wchar_t * str);
|
||||
DbTextStream & PutText(const std::wstring * str);
|
||||
DbTextStream & PutText(const std::wstring & str);
|
||||
|
||||
/*
|
||||
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<<(RawText<char [str_size]> raw) { return PutText(raw.par); }
|
||||
|
||||
template<size_t str_size>
|
||||
DbTextStream & operator<<(RawText<wchar_t [str_size]> raw) { return PutText(raw.par); }
|
||||
|
||||
DbTextStream & operator<<(RawText<const char*> raw);
|
||||
DbTextStream & operator<<(RawText<const wchar_t*> raw);
|
||||
DbTextStream & operator<<(RawText<const std::string*> raw);
|
||||
DbTextStream & operator<<(RawText<const std::wstring*> raw);
|
||||
DbTextStream & operator<<(RawText<std::string> raw);
|
||||
DbTextStream & operator<<(RawText<std::wstring> raw);
|
||||
DbTextStream & operator<<(RawText<bool> raw);
|
||||
DbTextStream & operator<<(RawText<char> raw);
|
||||
DbTextStream & operator<<(RawText<wchar_t> 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);
|
||||
DbTextStream & operator<<(RawText<PT::Date> date);
|
||||
|
||||
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
||||
DbTextStream & operator<<(RawText<PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw);
|
||||
|
||||
/*
|
||||
with escaping
|
||||
*/
|
||||
|
||||
DbTextStream & EBinPutChar(char c);
|
||||
DbTextStream & ETextPutChar(char c);
|
||||
DbTextStream & ETextPutChar(wchar_t c);
|
||||
|
||||
DbTextStream & EPutText(const char * str);
|
||||
DbTextStream & EPutText(const std::string * str);
|
||||
DbTextStream & EPutText(const std::string & str);
|
||||
DbTextStream & EPutText(const wchar_t * str);
|
||||
DbTextStream & EPutText(const std::wstring * str);
|
||||
DbTextStream & EPutText(const std::wstring & 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<<(const wchar_t * str);
|
||||
DbTextStream & operator<<(const std::wstring * str);
|
||||
DbTextStream & operator<<(const std::wstring & str);
|
||||
DbTextStream & operator<<(bool);
|
||||
DbTextStream & operator<<(char);
|
||||
DbTextStream & operator<<(wchar_t);
|
||||
DbTextStream & operator<<(int);
|
||||
DbTextStream & operator<<(long);
|
||||
DbTextStream & operator<<(unsigned int);
|
||||
DbTextStream & operator<<(unsigned long);
|
||||
DbTextStream & operator<<(double);
|
||||
DbTextStream & operator<<(const void *);
|
||||
DbTextStream & operator<<(const std::vector<long> & tabid);
|
||||
DbTextStream & operator<<(const PT::Space & space);
|
||||
DbTextStream & operator<<(const PT::Date & date);
|
||||
|
||||
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
||||
DbTextStream & operator<<(const PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
|
||||
|
||||
private:
|
||||
|
||||
bool was_param;
|
||||
bool ext_escape;
|
||||
TextStream<std::wstring> tmp_stream;
|
||||
|
||||
char EBinGetHex(char c);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
||||
DbTextStream & DbTextStream::operator<<(RawText<PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw)
|
||||
{
|
||||
TextStream<std::string>::operator<<(raw.par);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
||||
DbTextStream & DbTextStream::operator<<(const PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg)
|
||||
{
|
||||
typename PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size>::const_iterator i;
|
||||
|
||||
if( was_param )
|
||||
buffer += ", ";
|
||||
|
||||
if( ext_escape )
|
||||
buffer += 'E';
|
||||
|
||||
buffer += '\'';
|
||||
|
||||
for(i=arg.begin() ; i != arg.end() ; ++i)
|
||||
ETextPutChar(*i);
|
||||
|
||||
buffer += '\'';
|
||||
was_param = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user