From 07511a2eb06874840cf2ea42d9c5e43870d969cb Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 9 Oct 2010 20:27:45 +0000 Subject: [PATCH] fixed: DbBase::ConvertTime(tm) should not have been used in this way as it was (it uses a static buffer) now DbTextStream can get tm struct so you don't have to use ConvertTime git-svn-id: svn://ttmath.org/publicrep/winix/trunk@662 e52654a7-88a9-db11-a3e9-0013d4bc506e --- core/system.cpp | 1 + db/db.cpp | 12 ++++++------ db/dbbase.cpp | 6 ++++++ db/dbbase.h | 1 + db/dbitemcolumns.cpp | 4 ++-- db/dbtextstream.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- db/dbtextstream.h | 6 +++++- 7 files changed, 62 insertions(+), 12 deletions(-) diff --git a/core/system.cpp b/core/system.cpp index 8d7114f..e1e0a63 100755 --- a/core/system.cpp +++ b/core/system.cpp @@ -505,6 +505,7 @@ return status; + Error System::EditFile(Item & item, bool with_url) { if( item.type == Item::dir ) diff --git a/db/db.cpp b/db/db.cpp index 04fd19a..08e7620 100755 --- a/db/db.cpp +++ b/db/db.cpp @@ -245,8 +245,8 @@ Error Db::AddItemIntoItem(Item & item) << item.modification_user_id << item.group_id << item.privileges - << ConvertTime(item.date_creation) - << ConvertTime(item.date_modification) + << item.date_creation + << item.date_modification << static_cast(item.type) << item.parent_id << item.content_id @@ -356,8 +356,8 @@ Error Db::EditItemInItem(Item & item, bool with_url) << item.modification_user_id << item.group_id << item.privileges - << ConvertTime(item.date_creation) - << ConvertTime(item.date_modification) + << item.date_creation + << item.date_modification << static_cast(item.type) << item.default_item << item.parent_id @@ -1447,7 +1447,7 @@ Error Db::GetThreadByDirId(long dir_id, Thread & thread) thread.closed = AssertValueLong(r, 0, cclosed) == 0 ? false : true; thread.items = AssertValueLong(r, 0, citems); thread.last_item.id = AssertValueLong(r, 0, clast_item); - thread.last_item.date_modification = ConvertTime( AssertValue(r, 0, cdate_modification) ); + thread.last_item.date_modification = AssertValueTm(r, 0, cdate_modification); thread.last_item.user_id = AssertValueLong(r, 0, cuser_id); } catch(const Error & e) @@ -1500,7 +1500,7 @@ Error Db::GetThreads(long parent_id, std::vector & thread_tab) thread.closed = AssertValueLong(r, i, cclosed) == 0 ? false : true; thread.items = AssertValueLong(r, i, citems); thread.last_item.id = AssertValueLong(r, i, clast_item); - thread.last_item.date_modification = ConvertTime( AssertValue(r, i, cdate_modification) ); + thread.last_item.date_modification = AssertValueTm(r, i, cdate_modification); thread.last_item.user_id = AssertValueLong(r, i, cuser_id); thread.last_item.guest_name = AssertValue(r, i, cguest_name); diff --git a/db/dbbase.cpp b/db/dbbase.cpp index c6d225e..a5b3992 100755 --- a/db/dbbase.cpp +++ b/db/dbbase.cpp @@ -165,6 +165,12 @@ unsigned int DbBase::AssertValueUInt(PGresult * r, int row, int col) } +tm DbBase::AssertValueTm(PGresult * r, int row, int col) +{ + return ConvertTime(AssertValue(r, row, col)); +} + + void DbBase::ClearResult(PGresult * r) { diff --git a/db/dbbase.h b/db/dbbase.h index 9984abd..0e4ed4f 100755 --- a/db/dbbase.h +++ b/db/dbbase.h @@ -38,6 +38,7 @@ public: 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); + static tm AssertValueTm(PGresult * r, int row, int col); void ClearResult(PGresult * r); long AssertCurrval(const char * table); diff --git a/db/dbitemcolumns.cpp b/db/dbitemcolumns.cpp index 24bb05f..968e349 100755 --- a/db/dbitemcolumns.cpp +++ b/db/dbitemcolumns.cpp @@ -44,8 +44,8 @@ void DbItemColumns::SetItem(PGresult * r, long row, Item & item) 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( date_creation != -1 ) item.date_creation = DbBase::AssertValueTm(r, row, date_creation); + if( date_modification != -1 ) item.date_modification = DbBase::AssertValueTm(r, row, date_modification); if( url != -1 ) item.url = DbBase::AssertValue(r, row, url); if( type != -1 ) item.type = static_cast( DbBase::AssertValueInt(r, row, type) ); if( parent_id != -1 ) item.parent_id = DbBase::AssertValueLong(r, row, parent_id); diff --git a/db/dbtextstream.cpp b/db/dbtextstream.cpp index c6808c0..5f6a8bc 100755 --- a/db/dbtextstream.cpp +++ b/db/dbtextstream.cpp @@ -54,7 +54,7 @@ DbTextStream & DbTextStream::PutText(const std::string & str) -DbTextStream & DbTextStream::operator<<(const DbTextStream::RawText & raw) +DbTextStream & DbTextStream::operator<<(const RawText & raw) { return PutText(raw.par); } @@ -62,7 +62,7 @@ DbTextStream & DbTextStream::operator<<(const DbTextStream::RawText -DbTextStream & DbTextStream::operator<<(DbTextStream::RawText raw) +DbTextStream & DbTextStream::operator<<(RawText raw) { return PutText(raw.par.c_str()); } @@ -131,6 +131,15 @@ return *this; } +DbTextStream & DbTextStream::operator<<(const RawText & t) +{ + buffer += ConvertTime(t.par); + was_param = false; + +return *this; +} + + @@ -357,7 +366,36 @@ return *this; - +DbTextStream & DbTextStream::operator<<(const tm & t) +{ + if( was_param ) + buffer += ", "; + + buffer += '\''; + buffer += ConvertTime(t); + buffer += '\''; + was_param = true; + +return *this; +} + + + +const char * DbTextStream::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; +} diff --git a/db/dbtextstream.h b/db/dbtextstream.h index 68697f7..a90fde1 100755 --- a/db/dbtextstream.h +++ b/db/dbtextstream.h @@ -10,6 +10,7 @@ #ifndef headerfile_winix_db_dbtextstream #define headerfile_winix_db_dbtextstream +#include #include "core/textstream.h" @@ -100,7 +101,7 @@ public: DbTextStream & operator<<(RawText raw); DbTextStream & operator<<(RawText raw); DbTextStream & operator<<(RawText raw); - + DbTextStream & operator<<(const RawText & t); /* @@ -128,8 +129,11 @@ public: DbTextStream & operator<<(unsigned long); DbTextStream & operator<<(double); DbTextStream & operator<<(const void *); + DbTextStream & operator<<(const tm & t); + static const char * ConvertTime(const tm & t); + private: bool was_param;