multivalue.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 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_MULTIVALUE_H
30 #define TNTDB_ORACLE_MULTIVALUE_H
31 
32 #include <tntdb/oracle/datetime.h>
33 #include <tntdb/oracle/blob.h>
34 #include <tntdb/oracle/statement.h>
35 #include <tntdb/oracle/number.h>
36 #include <oci.h>
37 #include <cxxtools/refcounted.h>
38 #include <cxxtools/smartptr.h>
39 
40 namespace tntdb
41 {
42  namespace oracle
43  {
45  {
46  OCIDefine* _defp;
47  mutable Connection* _conn;
48  ub2 _type;
49  ub2 _collen;
50  ub2* _len;
51  ub2* _nullind;
52  unsigned _n;
53  std::string _colName;
54 
55  // _data points to allocated data depending of _type:
56  //
57  // _type _data points to
58  // ------------------ -------------------------------
59  // SQLT_INT long *
60  // SQLT_UIN unsigned long *
61  // SQLT_FLT double*
62  // SQLT_NUM tntdb::oracle::Number*
63  // SQLT_VNU tntdb::oracle::Number*
64  // SQLT_DAT OCIDateTime*
65  // SQLT_TIMESTAMP OCIDateTime*
66  // SQLT_TIMESTAMP_TZ OCIDateTime*
67  // SQLT_TIMESTAMP_LTZ OCIDateTime*
68  // SQLT_BLOB tntdb::oracle::Blob*
69  // other char*
70 
71  char* _data;
72 
73  void init(Statement* stmt, OCIParam* paramp, ub4 pos);
74 
75  long longValue(unsigned n) const
76  { return reinterpret_cast<long*>(_data)[n]; }
77  unsigned long unsignedValue(unsigned n) const
78  { return reinterpret_cast<unsigned long*>(_data)[n]; }
79  double doubleValue(unsigned n) const
80  { return reinterpret_cast<double*>(_data)[n]; }
81  OCIDateTime* datetime(unsigned n) const
82  { return reinterpret_cast<OCIDateTime**>(_data)[n]; }
83  const OCINumber& number(unsigned n) const
84  { return reinterpret_cast<const OCINumber*>(_data)[n]; }
85  Decimal decimal(unsigned n) const
86  { return Number::getDecimal(&number(n), _conn->getErrorHandle()); }
87  OCILobLocator* blob(unsigned n) const
88  { return reinterpret_cast<OCILobLocator**>(_data)[n]; }
89  const char* data(unsigned n) const
90  { return &_data[n * (_collen + 16)]; }
91 
92  // make non copyable:
93  MultiValue(const MultiValue&) { }
94  MultiValue& operator= (MultiValue&) { return *this; }
95 
96  public:
98 
99  MultiValue(Statement* stmt, OCIParam* paramp_, ub4 pos, unsigned n);
100  MultiValue(Statement* stmt, ub4 pos, unsigned n);
101  ~MultiValue();
102 
103  unsigned size() const { return _n; }
104 
105  bool isNull(unsigned n) const;
106  bool getBool(unsigned n) const;
107  short getShort(unsigned n) const;
108  int getInt(unsigned n) const;
109  long getLong(unsigned n) const;
110  unsigned short getUnsignedShort(unsigned n) const;
111  unsigned getUnsigned(unsigned n) const;
112  unsigned long getUnsignedLong(unsigned n) const;
113  int32_t getInt32(unsigned n) const;
114  uint32_t getUnsigned32(unsigned n) const;
115  int64_t getInt64(unsigned n) const;
116  uint64_t getUnsigned64(unsigned n) const;
117  Decimal getDecimal(unsigned n) const;
118  float getFloat(unsigned n) const;
119  double getDouble(unsigned n) const;
120  char getChar(unsigned n) const;
121  void getString(unsigned n, std::string& ret) const;
122  void getBlob(unsigned n, tntdb::Blob& ret) const;
123  Date getDate(unsigned n) const;
124  Time getTime(unsigned n) const;
125  tntdb::Datetime getDatetime(unsigned n) const;
126 
127  const std::string& getColumnName() const { return _colName; }
128  };
129  }
130 }
131 
132 #endif // TNTDB_ORACLE_MULTIVALUE_H
133