statement.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_POSTGRESQL_IMPL_SQLSTATEMENT_H
30 #define TNTDB_POSTGRESQL_IMPL_SQLSTATEMENT_H
31 
32 #include <tntdb/iface/istatement.h>
33 #include <tntdb/bits/connection.h>
34 #include <map>
35 #include <vector>
36 #include <libpq-fe.h>
37 
38 namespace tntdb
39 {
40  namespace postgresql
41  {
42  class Connection;
43 
44  class Statement : public IStatement
45  {
46  Connection* conn;
47  std::string query;
48  std::string stmtName;
49  typedef std::map<std::string, unsigned> hostvarMapType;
50  hostvarMapType hostvarMap;
51 
52  class valueType
53  {
54  bool isNull;
55  std::string value;
56 #ifndef HAVE_PQPREPARE
57  std::string type;
58 #endif
59 
60  public:
61 #ifndef HAVE_PQPREPARE
62  valueType()
63  : type("text")
64  { }
65 #endif
66  void setNull() { isNull = true; }
67  void setValue(const std::string& v)
68  { value = v; isNull = false; }
69  const char* getValue() { return isNull ? 0 : value.data(); }
70  unsigned getLength() { return isNull ? 0 : value.size(); }
71 #ifndef HAVE_PQPREPARE
72  void setType(const std::string& t) { type = t; }
73  const std::string& getType() const { return type; }
74 #endif
75  };
76 
77  typedef std::vector<valueType> valuesType;
78  valuesType values;
79 
80  std::vector<const char*> paramValues;
81  std::vector<int> paramLengths;
82  std::vector<int> paramFormats;
83 
84  // helper-methods for setting values
85  template <typename T>
86  void setValue(const std::string& col, T data);
87 
88  template <typename T>
89  void setStringValue(const std::string& col, T data, bool binary = false);
90 
91  template <typename T>
92  void setIsoValue(const std::string& col, T data);
93 
94 #ifndef HAVE_PQPREPARE
95  void setType(const std::string& col, const std::string& type);
96 #endif
97 
98  void doPrepare();
99  PGresult* execPrepared();
100 
101  public:
102  Statement(Connection* conn, const std::string& query);
103  ~Statement();
104 
105  // methods of IStatement
106 
107  void clear();
108  void setNull(const std::string& col);
109  void setBool(const std::string& col, bool data);
110  void setShort(const std::string& col, short data);
111  void setInt(const std::string& col, int data);
112  void setLong(const std::string& col, long data);
113  void setUnsignedShort(const std::string& col, unsigned short data);
114  void setUnsigned(const std::string& col, unsigned data);
115  void setUnsignedLong(const std::string& col, unsigned long data);
116  void setInt32(const std::string& col, int32_t data);
117  void setUnsigned32(const std::string& col, uint32_t data);
118  void setInt64(const std::string& col, int64_t data);
119  void setUnsigned64(const std::string& col, uint64_t data);
120  void setDecimal(const std::string& col, const Decimal& data);
121  void setFloat(const std::string& col, float data);
122  void setDouble(const std::string& col, double data);
123  void setChar(const std::string& col, char data);
124  void setString(const std::string& col, const std::string& data);
125  void setBlob(const std::string& col, const Blob& data);
126  void setDate(const std::string& col, const Date& data);
127  void setTime(const std::string& col, const Time& data);
128  void setDatetime(const std::string& col, const Datetime& data);
129 
130  size_type execute();
134  ICursor* createCursor(unsigned fetchsize);
135 
136  // specific methods
137  const std::string& getQuery() const { return query; }
138  int getNParams() { return values.size(); }
139  const char* const* getParamValues();
140  const int* getParamLengths();
141  const int* getParamFormats()
142  { return &paramFormats[0]; }
143  PGconn* getPGConn();
144  Connection* getConnection() { return conn; }
145  };
146  }
147 }
148 
149 #endif // TNTDB_POSTGRESQL_IMPL_SQLSTATEMENT_H
150