OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Number.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2021 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 #include "ocilibcpp/types.hpp"
24 
25 namespace ocilib
26 {
27 
28 inline Number::Number(bool create)
29 {
30  if (create)
31  {
32  Allocate();
33  }
34 }
35 
36 inline Number::Number(OCI_Number *pNumber, core::Handle *parent)
37 {
38  Acquire(pNumber, nullptr, nullptr, parent);
39 }
40 
41 inline Number::Number(const ostring& str, const ostring& format)
42 {
43  Allocate();
44 
45  FromString(str, format);
46 }
47 
48 inline Number::Number(const otext* str, const otext* format)
49 {
50  Allocate();
51 
52  FromString(str, format);
53 }
54 
55 inline void Number::Allocate()
56 {
57  Acquire(core::Check(OCI_NumberCreate(nullptr)), reinterpret_cast<HandleFreeFunc>(OCI_NumberFree), nullptr, nullptr);
58 }
59 
60 inline void Number::FromString(const ostring& str, const ostring& format) const
61 {
62  core::Check(OCI_NumberFromText(*this, str.c_str(), format.empty() ? Environment::GetFormat(FormatNumeric).c_str() : format.c_str()));
63 }
64 
65 inline ostring Number::ToString(const ostring& format) const
66 {
67  if (!IsNull())
68  {
69  const size_t size = OCI_SIZE_BUFFER;
70 
71  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
72 
73  core::Check(OCI_NumberToText(*this, format.c_str(), static_cast<int>(size), buffer));
74 
75  return core::MakeString(static_cast<const otext *>(buffer));
76  }
77 
78  return OCI_STRING_NULL;
79 }
80 
81 inline ostring Number::ToString() const
82 {
84 }
85 
86 inline Number Number::Clone() const
87 {
88  Number result;
89 
90  result.Allocate();
91 
92  core::Check(OCI_NumberAssign(result, *this));
93 
94  return result;
95 }
96 
97 template<class T>
98 AnyPointer Number::GetNativeValue(const T& value)
99 {
100  return reinterpret_cast<AnyPointer>(const_cast<T*>(&value));
101 }
102 
103 template<>
104 inline AnyPointer Number::GetNativeValue(const Number& value)
105 {
106  return (reinterpret_cast<OCI_Number*>(core::Check(OCI_NumberGetContent(value))));
107 }
108 
109 inline int Number::Compare(const Number& other) const
110 {
111  return core::Check(OCI_NumberCompare(*this, other));
112 }
113 
114 template<class T>
115 T Number::GetValue() const
116 {
117  T value;
118 
120 
121  return value;
122 }
123 
124 template<class T>
125 Number& Number::SetValue(const T &value)
126 {
127  if (IsNull())
128  {
129  Allocate();
130  }
131 
133 
134  return *this;
135 }
136 
137 template<class T>
138 void Number::Add(const T &value)
139 {
140  core::Check(OCI_NumberAdd(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
141 }
142 
143 template<class T>
144 void Number::Sub(const T &value)
145 {
146  core::Check(OCI_NumberSub(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
147 }
148 
149 template<class T>
150 void Number::Multiply(const T &value)
151 {
153 }
154 
155 template<class T>
156 void Number::Divide(const T &value)
157 {
159 }
160 
161 inline Number& Number::operator = (OCI_Number * &lhs)
162 {
163  Acquire(lhs, reinterpret_cast<HandleFreeFunc>(OCI_NumberFree), nullptr, nullptr);
164  return *this;
165 }
166 
167 template<class T, typename core::SupportedNumeric<T>::Type::type*>
168 Number& Number::operator = (const T &lhs)
169 {
170  SetValue<T>(lhs);
171  return *this;
172 }
173 
174 template<class T, typename core::SupportedNumeric<T>::Type::type*>
175 Number::operator T() const
176 {
177  return GetValue<T>();
178 }
179 
180 template<class T, typename core::SupportedNumeric<T>::Type::type*>
181 Number Number::operator + (const T &value)
182 {
183  Number result = Clone();
184  result.Add(value);
185  return result;
186 }
187 
188 template<class T, typename core::SupportedNumeric<T>::Type::type*>
189 Number Number::operator - (const T &value)
190 {
191  Number result = Clone();
192  result.Sub(value);
193  return result;
194 }
195 
196 template<class T, typename core::SupportedNumeric<T>::Type::type*>
197 Number Number::operator * (const T &value)
198 {
199  Number result = Clone();
200  result.Multiply(value);
201  return result;
202 }
203 
204 template<class T, typename core::SupportedNumeric<T>::Type::type*>
205 Number Number::operator / (const T &value)
206 {
207  Number result = Clone();
208  result.Divide(value);
209  return result;
210 }
211 
212 template<class T, typename core::SupportedNumeric<T>::Type::type*>
213 Number& Number::operator += (const T &value)
214 {
215  Add<T>(value);
216  return *this;
217 }
218 
219 template<class T, typename core::SupportedNumeric<T>::Type::type*>
220 Number& Number::operator -= (const T &value)
221 {
222  Sub<T>(value);
223  return *this;
224 }
225 
226 template<class T, typename core::SupportedNumeric<T>::Type::type*>
227 Number& Number::operator *= (const T &value)
228 {
229  Multiply<T>(value);
230  return *this;
231 }
232 
233 template<class T, typename core::SupportedNumeric<T>::Type::type*>
234 Number& Number::operator /= (const T &value)
235 {
236  Divide<T>(value);
237  return *this;
238 }
239 
240 inline Number& Number::operator ++ ()
241 {
242  return *this += 1;
243 }
244 
245 inline Number& Number::operator -- ()
246 {
247  return *this += 1;
248 }
249 
250 inline Number Number::operator ++ (int)
251 {
252  return *this + 1;
253 }
254 
255 inline Number Number::operator -- (int)
256 {
257  return *this - 1;
258 }
259 
260 inline bool Number::operator == (const Number& other) const
261 {
262  return Compare(other) == 0;
263 }
264 
265 inline bool Number::operator != (const Number& other) const
266 {
267  return !(*this == other);
268 }
269 
270 inline bool Number::operator > (const Number& other) const
271 {
272  return Compare(other) > 0;
273 }
274 
275 inline bool Number::operator < (const Number& other) const
276 {
277  return Compare(other) < 0;
278 }
279 
280 inline bool Number::operator >= (const Number& other) const
281 {
282  const int res = Compare(other);
283 
284  return res == 0 || res < 0;
285 }
286 
287 inline bool Number::operator <= (const Number& other) const
288 {
289  const int res = Compare(other);
290 
291  return res == 0 || res > 0;
292 }
293 
294 }
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:312
void FromString(const ostring &str, const ostring &format=OTEXT("")) const
Assign to the number object the value provided by the input number time string.
Definition: Number.hpp:60
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSub(OCI_Number *number, unsigned int type, void *value)
Subtract the value of a native C numeric type to the given number.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberAdd(OCI_Number *number, unsigned int type, void *value)
Add the value of a native C numeric type to the given number.
OCILIB ++ Namespace.
static ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
OCI_SYM_PUBLIC int OCI_API OCI_NumberAssign(OCI_Number *number, OCI_Number *number_src)
Assign the value of a number handle to another one.
Allow resolving a the C API numeric enumerated type from a C++ type.
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so, it raises a C++ exception using the retrieved error handle.
Definition: Utils.hpp:53
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberGetValue(OCI_Number *number, unsigned int type, void *value)
Assign the number value to a native C numeric type.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSetValue(OCI_Number *number, unsigned int type, void *value)
Assign the number value with the value of a native C numeric type.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberToText(OCI_Number *number, const otext *fmt, int size, otext *str)
Convert a number value from the given number handle to a string.
Number Clone() const
Clone the current instance to a new one performing deep copy.
Definition: Number.hpp:86
OCI_SYM_PUBLIC unsigned char *OCI_API OCI_NumberGetContent(OCI_Number *number)
Return the number value content.
ostring MakeString(const otext *result, int size=-1)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Definition: Utils.hpp:65
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberDivide(OCI_Number *number, unsigned int type, void *value)
Divide the given number with the value of a native C numeric.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFree(OCI_Number *number)
Free a number object.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFromText(OCI_Number *number, const otext *str, const otext *fmt)
Convert a string to a number and store it in the given number handle.
OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_NumberCreate(OCI_Connection *con)
Create a local number object.
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:196
ostring ToString() const override
Convert the number value to a string using default format OCI_STRING_FORMAT_NUMERIC.
Definition: Number.hpp:81
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberMultiply(OCI_Number *number, unsigned int type, void *value)
Multiply the given number with the value of a native C numeric.
OCI_SYM_PUBLIC int OCI_API OCI_NumberCompare(OCI_Number *number1, OCI_Number *number2)
Compares two number handles.
struct OCI_Number OCI_Number
Oracle NUMBER representation.
Definition: types.h:257
Object identifying the SQL data type NUMBER.
Definition: types.hpp:2477
Number(bool create=false)
Create an empty null number object.
Definition: Number.hpp:28
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets ) ...
Definition: config.hpp:120