# prepared_statement.hxx

    00001 /*-------------------------------------------------------------------------
    00002  *
    00003  *   FILE
    00004  *      pqxx/prepared_statement.hxx
    00005  *
    00006  *   DESCRIPTION
    00007  *      Helper classes for defining and executing prepared statements
    00008  *   See the connection_base hierarchy for more about prepared statements
    00009  *
    00010  * Copyright (c) 2006-2008, Jeroen T. Vermeulen <jtv@xs4all.nl>
    00011  *
    00012  * See COPYING for copyright license.  If you did not receive a file called
    00013  * COPYING with this source code, please notify the distributor of this mistake,
    00014  * or contact the author.
    00015  *
    00016  *-------------------------------------------------------------------------
    00017  */
    00018 #ifndef PQXX_H_PREPARED_STATEMENT
    00019 #define PQXX_H_PREPARED_STATEMENT
    00020
    00021 #include "pqxx/compiler-public.hxx"
    00022 #include "pqxx/compiler-internal-pre.hxx"
    00023
    00024 #include "pqxx/util"
    00025
    00026 namespace pqxx
    00027 {
    00028 class connection_base;
    00029 class transaction_base;
    00030 class result;
    00031
    00033 namespace prepare
    00034 {
    00036
    00045 enum param_treatment
    00046 {
    00048   treat_binary,
    00050   treat_string,
    00052   treat_bool,
    00054   treat_direct
    00055 };
    00056
    00057
    00059
    00067 class PQXX_LIBEXPORT declaration
    00068 {
    00069 public:
    00070   declaration(connection_base &, const PGSTD::string &statement);
    00071
    00073   const declaration &
    00074   operator()(const PGSTD::string &sqltype, param_treatment=treat_direct) const;
    00075
    00076 private:
    00078   declaration &operator=(const declaration &);
    00079
    00080   connection_base &m_home;
    00081   const PGSTD::string m_statement;
    00082 };
    00083
    00084
    00086 class PQXX_LIBEXPORT invocation
    00087 {
    00088 public:
    00089   invocation(transaction_base &, const PGSTD::string &statement);
    00090
    00092   result exec() const;
    00093
    00095   invocation &operator()();
    00096
    00098
    00101   template<typename T>
    00102     invocation &operator()(const T &v)
    00103   {
    00104     const bool nonnull = !pqxx::string_traits<T>::is_null(v);
    00105     return setparam((nonnull ? pqxx::to_string(v) : ""), nonnull);
    00106   }
    00107
    00109
    00113   template<typename T>
    00114     invocation &operator()(const T &v, bool nonnull)
    00115         { return setparam((nonnull ? pqxx::to_string(v) : ""), nonnull); }
    00116
    00118
    00136   template<typename T>
    00137     invocation &operator()(T *v, bool nonnull=true)
    00138         { return setparam((v ? to_string(v) : ""), nonnull); }
    00139
    00141
    00145   invocation &operator()(const char *v, bool nonnull=true)
    00146         { return setparam((v ? to_string(v) : ""), nonnull); }
    00147
    00148 private:
    00150   invocation &operator=(const invocation &);
    00151
    00152   transaction_base &m_home;
    00153   const PGSTD::string m_statement;
    00154   PGSTD::vector<PGSTD::string> m_values;
    00155   PGSTD::vector<bool> m_nonnull;
    00156
    00157   invocation &setparam(const PGSTD::string &, bool nonnull);
    00158 };
    00159
    00160
    00161 namespace internal
    00162 {
    00164 struct PQXX_LIBEXPORT prepared_def
    00165 {
    00167   struct param
    00168   {
    00169     PGSTD::string sqltype;
    00170     param_treatment treatment;
    00171
    00172     param(const PGSTD::string &SQLtype, param_treatment);
    00173   };
    00174
    00176   PGSTD::string definition;
    00178   PGSTD::vector<param> parameters;
    00180   bool registered;
    00182   bool complete;
    00183
    00184   prepared_def();
    00185   explicit prepared_def(const PGSTD::string &);
    00186
    00187   void addparam(const PGSTD::string &sqltype, param_treatment);
    00188 };
    00189
    00191 struct PQXX_PRIVATE get_sqltype
    00192 {
    00193   template<typename IT> const PGSTD::string &operator()(IT i)
    00194   {
    00195     return i->sqltype;
    00196   }
    00197 };
    00198
    00199 } // namespace pqxx::prepare::internal
    00200 } // namespace pqxx::prepare
    00201 } // namespace pqxx
    00202
    00203 #include "pqxx/compiler-internal-post.hxx"
    00204
    00205 #endif
    00206