query.h

00001 /*
00002  * query.h
00003  * This file is part of dbPager Classes Library (DCL)
00004  *
00005  * Copyright (c) 2008 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * DCL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * DCL is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with DCL; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
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 &parameter) {
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         // Obtain the last diagnostic (error) message from ODBC system
00186         std::string get_error() const;
00187         // Retrieve resultset metadata after query executing
00188         void retrieve_metadata();
00189         void set_parameter(int num, const flush &value);
00190         // Parse the query, filling the parameter list and reconstruct the statement
00191         std::string parse(const std::string &statement);
00192 };
00193 
00194 }} // namespace
00195 
00196 #endif /*_QUERY_H_*/

 
Support This Project
SourceForge.net Logo