statement.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 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_ORACLE_SQLSTATEMENT_H
30 #define TNTDB_ORACLE_SQLSTATEMENT_H
31 
32 #include <tntdb/iface/istatement.h>
33 #include <oci.h>
34 #include <vector>
35 #include <string>
36 #include <string.h>
37 #include <map>
38 #include <tntdb/oracle/datetime.h>
39 #include <tntdb/oracle/blob.h>
40 #include <tntdb/oracle/number.h>
41 #include <tntdb/blob.h>
42 
43 namespace tntdb
44 {
45  namespace oracle
46  {
47  class Connection;
48  class Cursor;
49 
50  class Statement : public IStatement
51  {
52  friend class Cursor;
53 
54  Connection* conn;
55  std::string query;
56  OCIStmt* stmtp;
57 
58  class Bind : public cxxtools::RefCounted
59  {
60  public:
61  OCIBind* ptr;
62  std::vector<char> data;
63  sb2 indicator;
64  Datetime datetime;
65  Blob blob;
66  Number number;
67 
68  const char* boundPtr;
69  int boundType;
70  unsigned boundLength;
71 
72  Bind()
73  : ptr(0),
74  indicator(0),
75  boundPtr(0),
76  boundType(0),
77  boundLength(0)
78  { }
79  void setData(const std::string& value)
80  {
81  data.resize(value.size());
82  value.copy(&data[0], value.size());
83  }
84  void setData(const char* value, unsigned size)
85  {
86  data.resize(size);
87  memcpy(&data[0], value, size);
88  }
89  void setNull(bool sw = true)
90  {
91  indicator = sw ? -1 : 0;
92  }
93  };
94 
95  typedef std::map<std::string, cxxtools::SmartPtr<Bind> > BindMapType;
96  BindMapType bindMap;
97 
98  Bind* getBindPtr(const std::string& col);
99  Bind& getBind(const std::string& col) { return *getBindPtr(col); }
100 
101  public:
103  void checkError(sword ret, const char* function = 0) const
104  { conn->checkError(ret, function); }
105 
106  Statement(Connection* conn, const std::string& query);
107  ~Statement();
108 
109  // methods of IStatement
110 
111  void clear();
112  void setNull(const std::string& col);
113  void setBool(const std::string& col, bool data);
114  void setShort(const std::string& col, short data);
115  void setInt(const std::string& col, int data);
116  void setLong(const std::string& col, long data);
117  void setUnsignedShort(const std::string& col, unsigned short data);
118  void setUnsigned(const std::string& col, unsigned data);
119  void setUnsignedLong(const std::string& col, unsigned long data);
120  void setInt32(const std::string& col, int32_t data);
121  void setUnsigned32(const std::string& col, uint32_t data);
122  void setInt64(const std::string& col, int64_t data);
123  void setUnsigned64(const std::string& col, uint64_t data);
124  void setDecimal(const std::string& col, const Decimal& data);
125  void setFloat(const std::string& col, float data);
126  void setDouble(const std::string& col, double data);
127  void setChar(const std::string& col, char data);
128  void setString(const std::string& col, const std::string& data);
129  void setBlob(const std::string& col, const tntdb::Blob& data);
130  void setDate(const std::string& col, const Date& data);
131  void setTime(const std::string& col, const Time& data);
132  void setDatetime(const std::string& col, const tntdb::Datetime& data);
133 
134  size_type execute();
138  ICursor* createCursor(unsigned fetchsize);
139 
140  // getter
141  Connection* getConnection() const { return conn; }
142  OCIStmt* getHandle();
143  OCIError* getErrorHandle() const { return conn->getErrorHandle(); }
144  };
145  }
146 }
147 
148 #endif // TNTDB_ORACLE_SQLSTATEMENT_H
149