613 lines
8.6 KiB
C++
Executable File
613 lines
8.6 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2010-2014, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#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)
|
|
{
|
|
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<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);
|
|
AssignString(tmp_stream.CStr(), buffer, false);
|
|
tmp_stream.Clear();
|
|
|
|
was_param = false;
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
with escaping
|
|
*/
|
|
|
|
|
|
/*
|
|
old PostgreSQL Escape Format
|
|
*/
|
|
/*
|
|
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;
|
|
}
|
|
*/
|
|
|
|
// 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
|
|
|