/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2010-2021, 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::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::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 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 date) { tmp_stream.Clear(); date.par.Serialize(tmp_stream); pt::wide_to_utf8(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::int_to_utf8(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::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 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; } DbTextStream & DbTextStream::operator<<(const pt::Space & space) { tmp_stream.Clear(); // !! IMPROVE ME // we can calculate how much memory is needed before serializing space.serialize_to_space_stream(tmp_stream, true); 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