added: TextStream a class similar to std::ostringstream
but with a Clear() method
the dynamic allocated buffer can be easily reused
added: DbTextStream a special version of a stream
used to create a database string query
everything is escaped by default
added: DbBase a base class with some basic methods for communicating
with the database
added: DbConn a class for managing connection to the database
changed: some refactoring in Db class
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@655 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
364
db/dbtextstream.cpp
Executable file
364
db/dbtextstream.cpp
Executable file
@@ -0,0 +1,364 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "dbtextstream.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::operator<<(const DbTextStream::RawText<const char*> & raw)
|
||||
{
|
||||
return PutText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(DbTextStream::RawText<std::string> raw)
|
||||
{
|
||||
return PutText(raw.par.c_str());
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<char> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<int> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<long> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<unsigned int> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<unsigned long> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<double> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
DbTextStream & DbTextStream::operator<<(RawText<void*> raw)
|
||||
{
|
||||
TextStream::operator<<(raw.par);
|
||||
was_param = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
with escaping
|
||||
*/
|
||||
|
||||
|
||||
// !! sprawdzic jej dzialanie dla kolumn bytea (binarnych)
|
||||
DbTextStream & DbTextStream::EBinPutChar(char c_)
|
||||
{
|
||||
char buf[10];
|
||||
|
||||
int c = (unsigned char)c_;
|
||||
|
||||
if( (c>=0 && c<=31) || c>=127 || c==39 || c==92 )
|
||||
{
|
||||
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::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());
|
||||
}
|
||||
|
||||
|
||||
// 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<<(char 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user