winix/winixd/db/dbtextstream.h

242 lines
7.3 KiB
C++

/*
* 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