/* * This file is a part of Winix * and is not publicly distributed * * Copyright (c) 2010-2012, Tomasz Sowa * All rights reserved. * */ #include "dbtextstream.h" #include "utf8/utf8.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::PutText(const wchar_t * str) { PT::WideToUTF8(str, buffer, false); 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 raw) { return PutText(raw.par); } DbTextStream & DbTextStream::operator<<(RawText raw) { return PutText(raw.par); } DbTextStream & DbTextStream::operator<<(RawText raw) { return PutText(raw.par); } DbTextStream & DbTextStream::operator<<(RawText raw) { return PutText(raw.par); } DbTextStream & DbTextStream::operator<<(RawText raw) { return PutText(raw.par); } DbTextStream & DbTextStream::operator<<(RawText raw) { return PutText(raw.par); } DbTextStream & DbTextStream::operator<<(RawText raw) { if( raw.par ) PutText("true"); else PutText("false"); return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText raw) { TextStream::operator<<(raw.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText t) { buffer += ConvertTime(t.par); was_param = false; return *this; } DbTextStream & DbTextStream::operator<<(RawText date) { date.par.Serialize(*this); was_param = false; return *this; } /* with escaping */ DbTextStream & DbTextStream::EBinPutChar(char c_) { char buf[20]; int c = (unsigned char)c_; if( (c>=0 && c<=31) || c>=127 || c=='\'' || c=='\\' ) { 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::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 += '\''; 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::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; } DbTextStream & DbTextStream::operator<<(const tm & t) { if( was_param ) buffer += ", "; buffer += '\''; buffer += ConvertTime(t); buffer += '\''; was_param = true; return *this; } DbTextStream & DbTextStream::operator<<(const std::vector & tabid) { if( was_param ) buffer += ", "; buffer += '('; for(size_t i=0 ; i::operator<<(tabid[i]); if( i + 1 < tabid.size() ) buffer += ','; } 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; } DbTextStream & DbTextStream::operator<<(const PT::Space & space) { space_stream.Clear(); // !! IMPROVE ME // we can calculate how much memory is needed before serializing space.Serialize(space_stream, true, false); operator<<(space_stream.Str()); space_stream.Clear(); return *this; } DbTextStream & DbTextStream::operator<<(const PT::Date & date) { if( was_param ) buffer += ", "; buffer += '\''; date.Serialize(*this); buffer += '\''; was_param = true; return *this; }