00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _psqlstatement_h_
00021 #define _psqlstatement_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/psqldriver.h>
00025 #include <pclasses/psqlconnection.h>
00026 #include <pclasses/psqlvalue.h>
00027 #include <memory>
00028 #include <map>
00029 #include <string>
00030 #include <sstream>
00031
00032 namespace P {
00033
00034 class SQLStatement;
00035
00037
00040 class PSQL_EXPORT SQLResult {
00041 friend class SQLStatement;
00042
00043 public:
00044 SQLResult(SQLStatement& stmt);
00045 virtual ~SQLResult();
00046
00048 unsigned int columnCount() const throw();
00049
00051 std::string columnName(unsigned int pos) const;
00052
00054 bool fetch();
00055
00057 const SQLValue& operator[](const std::string& fieldName);
00058
00060 const SQLValue& operator[](unsigned int fieldPos);
00061
00062 protected:
00063 SQLResult(std::auto_ptr<SQLDriver::ResultHandle> handle);
00064
00065 private:
00066 std::auto_ptr<SQLDriver::
00067 ResultHandle> m_handle;
00068 };
00069
00071
00074 class PSQL_EXPORT SQLStatement {
00075 friend class SQLResult;
00076
00077 public:
00078 enum state_t {
00079 Initial,
00080 Prepared,
00081 Executed
00082 };
00083
00084 SQLStatement(SQLConnection& conn, const std::string& stmt = "");
00085 virtual ~SQLStatement();
00086
00088 void prepare() throw(SQLError);
00089
00091 void exec() throw(SQLError);
00092
00094 std::auto_ptr<SQLResult> result() throw(SQLError);
00095
00097 sqlcount_t affectedRows() const throw();
00098
00099 void format(const std::string& fmt, const std::map<std::string,const SQLValue*>& vals);
00100
00101 inline state_t state() const throw()
00102 { return m_state; }
00103
00104 inline SQLConnection& conn() const throw()
00105 { return m_conn; }
00106
00107 SQLStatement& operator<<(char ch);
00108 SQLStatement& operator<<(const char* str);
00109 SQLStatement& operator<<(const std::string& str);
00110 SQLStatement& operator<<(const SQLValue& val);
00111
00112 protected:
00113 const std::auto_ptr<SQLDriver::
00114 StatementHandle>& handle() const throw()
00115 { return m_handle; }
00116
00117 private:
00118 state_t m_state;
00119 SQLConnection& m_conn;
00120 std::auto_ptr<SQLDriver::
00121 StatementHandle> m_handle;
00122 std::ostringstream m_stmt;
00123 };
00124
00125 }
00126
00127 #endif