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:
144
db/dbtextstream.h
Executable file
144
db/dbtextstream.h
Executable file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_dbtextstream
|
||||
#define headerfile_winix_core_dbtextstream
|
||||
|
||||
#include "core/textstream.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
DbTextStream is used as a buffer for creating a database's query
|
||||
By default all operators<< espace 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
|
||||
{
|
||||
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 &);
|
||||
|
||||
/*
|
||||
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<<(const RawText<char [str_size]> & raw) { return PutText(raw.par); }
|
||||
|
||||
DbTextStream & operator<<(const RawText<const char*> & raw);
|
||||
DbTextStream & operator<<(RawText<std::string> raw);
|
||||
DbTextStream & operator<<(RawText<char> 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);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
with escaping
|
||||
*/
|
||||
|
||||
DbTextStream & EBinPutChar(char c);
|
||||
DbTextStream & ETextPutChar(char c);
|
||||
|
||||
DbTextStream & EPutText(const char * str);
|
||||
DbTextStream & EPutText(const std::string * str);
|
||||
DbTextStream & EPutText(const std::string & 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<<(char);
|
||||
DbTextStream & operator<<(int);
|
||||
DbTextStream & operator<<(long);
|
||||
DbTextStream & operator<<(unsigned int);
|
||||
DbTextStream & operator<<(unsigned long);
|
||||
DbTextStream & operator<<(double);
|
||||
DbTextStream & operator<<(const void *);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool was_param;
|
||||
bool ext_escape;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user