PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/pq/preparedstatement.h

http://github.com/mozy/mordor
C Header | 175 lines | 157 code | 17 blank | 1 comment | 0 complexity | b3249ba42c89ce8910d10dd3058632cc MD5 | raw file
  1#ifndef __MORDOR_PQ_PREPAREDSTATEMENT_H__
  2#define __MORDOR_PQ_PREPAREDSTATEMENT_H__
  3// Copyright (c) 2010 - Mozy, Inc.
  4
  5#include <vector>
  6
  7#include <boost/date_time/posix_time/posix_time_types.hpp>
  8#include <boost/weak_ptr.hpp>
  9
 10#include "result.h"
 11
 12namespace Mordor {
 13
 14class IOManager;
 15class Scheduler;
 16
 17namespace PQ {
 18
 19#ifdef WIN32
 20typedef Scheduler SchedulerType;
 21#else
 22typedef IOManager SchedulerType;
 23#endif
 24
 25struct Null {};
 26struct Skip {};
 27
 28class PreparedStatement
 29{
 30    friend class Connection;
 31public:
 32    enum ResultFormat {
 33        TEXT   = 0,
 34        BINARY = 1
 35    };
 36
 37private:
 38    PreparedStatement(boost::shared_ptr<PGconn> conn,
 39        const std::string &command, const std::string &name,
 40        SchedulerType *scheduler, ResultFormat format = BINARY)
 41        : m_conn(conn),
 42          m_command(command),
 43          m_name(name),
 44          m_scheduler(scheduler),
 45          m_resultFormat(format)
 46    {}
 47
 48public:
 49    PreparedStatement(): m_resultFormat(BINARY) {}
 50
 51    void bind(size_t param, const Null &);
 52    void bind(size_t param, const char *value);
 53    void bind(size_t param, const std::string &value);
 54    void bind(size_t param, bool value);
 55    void bind(size_t param, char value);
 56    void bind(size_t param, short value);
 57    void bind(size_t param, int value);
 58    void bind(size_t param, long long value);
 59    void bind(size_t param, float value);
 60    void bind(size_t param, double value);
 61    void bind(size_t param, const boost::posix_time::ptime &value, bool timezone = false);
 62    void bindUntyped(size_t param, const std::string &value);
 63
 64    Result execute();
 65    template <class T1>
 66    Result execute(const T1 &param1)
 67    {
 68        bind(1, param1);
 69        return execute();
 70    }
 71    template <class T1, class T2>
 72    Result execute(const T1 &param1, const T2 &param2)
 73    {
 74        bind(2, param2);
 75        bind(1, param1);
 76        return execute();
 77    }
 78    template <class T1, class T2, class T3>
 79    Result execute(const T1 &param1, const T2 &param2, const T3 &param3)
 80    {
 81        bind(3, param3);
 82        bind(2, param2);
 83        bind(1, param1);
 84        return execute();
 85    }
 86    template <class T1, class T2, class T3, class T4>
 87    Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4)
 88    {
 89        bind(4, param4);
 90        bind(3, param3);
 91        bind(2, param2);
 92        bind(1, param1);
 93        return execute();
 94    }
 95    template <class T1, class T2, class T3, class T4, class T5>
 96    Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5)
 97    {
 98        bind(5, param5);
 99        bind(4, param4);
100        bind(3, param3);
101        bind(2, param2);
102        bind(1, param1);
103        return execute();
104    }
105    template <class T1, class T2, class T3, class T4, class T5, class T6>
106    Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6)
107    {
108        bind(6, param6);
109        bind(5, param5);
110        bind(4, param4);
111        bind(3, param3);
112        bind(2, param2);
113        bind(1, param1);
114        return execute();
115    }
116    template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
117    Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6, const T7 &param7)
118    {
119        bind(7, param7);
120        bind(6, param6);
121        bind(5, param5);
122        bind(4, param4);
123        bind(3, param3);
124        bind(2, param2);
125        bind(1, param1);
126        return execute();
127    }
128    template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
129    Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6, const T7 &param7, const T8 &param8)
130    {
131        bind(8, param8);
132        bind(7, param7);
133        bind(6, param6);
134        bind(5, param5);
135        bind(4, param4);
136        bind(3, param3);
137        bind(2, param2);
138        bind(1, param1);
139        return execute();
140    }
141    template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
142    Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6, const T7 &param7, const T8 &param8, const T9 &param9)
143    {
144        bind(9, param9);
145        bind(8, param8);
146        bind(7, param7);
147        bind(6, param6);
148        bind(5, param5);
149        bind(4, param4);
150        bind(3, param3);
151        bind(2, param2);
152        bind(1, param1);
153        return execute();
154    }
155
156private:
157    void bind(size_t param, const Skip &) {}
158    void setType(size_t param, Oid type);
159    void ensure(size_t count);
160
161private:
162    boost::weak_ptr<PGconn> m_conn;
163    std::string m_command;
164    std::string m_name;
165    SchedulerType *m_scheduler;
166    ResultFormat m_resultFormat;
167    std::vector<Oid> m_paramTypes;
168    std::vector<std::string> m_paramValues;
169    std::vector<const char *> m_params;
170    std::vector<int> m_paramLengths, m_paramFormats;
171};
172
173}}
174
175#endif