query.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _QUERY_H_
00023 #define _QUERY_H_
00024
00025 #include <string>
00026 #include <vector>
00027
00028 #ifdef _WIN32
00029 #include <windows.h>
00030 #include <odbcinst.h>
00031 #endif
00032
00033 #include <sqltypes.h>
00034 #include <sql.h>
00035 #include <sqlext.h>
00036
00037 #include <dcl/connection.h>
00038 #include <dcl/exception.h>
00039 #include <dcl/strutils.h>
00040
00041 namespace dbp {
00042 namespace odbc {
00043
00045
00048 class query_exception: public dbp::exception {
00049 public:
00051 query_exception(const std::string &msg): exception(msg) { }
00052 };
00053
00055
00060 class query {
00061 public:
00063
00066 class parameter {
00067 public:
00068 std::string name;
00069 std::string value;
00070 };
00072 typedef std::vector<parameter> parameters;
00074
00077 class field {
00078 public:
00079 field(query *parent = NULL): _query(parent) { };
00080 int number;
00081 std::string name;
00082 std::string get_value() const;
00083 private:
00084 query *_query;
00085 };
00087 typedef std::vector<field> fields;
00089 class flush { };
00091
00096 query(const connection &connection);
00098
00101 virtual ~query();
00103
00109 parameters& prepare(const std::string &statement);
00111
00116 query& operator()(const std::string &statement);
00118
00123 const fields& execute();
00125
00131 const fields& execute(const std::string &statement);
00133
00139 bool next();
00141 void set_parameter(int num, int value);
00143 void set_parameter(int num, double value);
00145 void set_parameter(int num, const std::string &value);
00147
00160 template<class T>
00161 query& operator<<(const T ¶meter) {
00162 set_parameter(_prmcnt, parameter);
00163 _prmcnt++;
00164 return *this;
00165 };
00167 void get_field(int num, int &value);
00169 void get_field(int num, double &value);
00171 void get_field(int num, std::string &value);
00173 template<class T>
00174 query& operator>>(T &var) {
00175 get_field(_fldcnt, var);
00176 _fldcnt++;
00177 return *this;
00178 };
00179 private:
00180 const connection &_db;
00181 int _prmcnt, _fldcnt;
00182 SQLHSTMT stmt;
00183 parameters _parameters;
00184 fields _fields;
00185
00186 std::string get_error() const;
00187
00188 void retrieve_metadata();
00189 void set_parameter(int num, const flush &value);
00190
00191 std::string parse(const std::string &statement);
00192 };
00193
00194 }}
00195
00196 #endif