OCILIB (C and C++ Driver for Oracle)  4.7.6
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-2023 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  AcquireTransient(pNumber, 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  AcquireAllocated
58  (
59  core::Check(OCI_NumberCreate(nullptr)),
60  Environment::GetEnvironmentHandle()
61  );
62 }
63 
64 inline void Number::FromString(const ostring& str, const ostring& format) const
65 {
66  core::Check(OCI_NumberFromText(*this, str.c_str(), format.empty() ? Environment::GetFormat(FormatNumeric).c_str() : format.c_str()));
67 }
68 
69 inline ostring Number::ToString(const ostring& format) const
70 {
71  if (!IsNull())
72  {
73  const size_t size = OCI_SIZE_BUFFER;
74 
75  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
76 
77  core::Check(OCI_NumberToText(*this, format.c_str(), static_cast<int>(size), buffer));
78 
79  return core::MakeString(static_cast<const otext *>(buffer));
80  }
81 
82  return OCI_STRING_NULL;
83 }
84 
85 inline ostring Number::ToString() const
86 {
88 }
89 
90 inline Number Number::Clone() const
91 {
92  Number result;
93 
94  result.Allocate();
95 
96  core::Check(OCI_NumberAssign(result, *this));
97 
98  return result;
99 }
100 
101 template<class T>
102 AnyPointer Number::GetNativeValue(const T& value)
103 {
104  return reinterpret_cast<AnyPointer>(const_cast<T*>(&value));
105 }
106 
107 template<>
108 inline AnyPointer Number::GetNativeValue(const Number& value)
109 {
110  return (reinterpret_cast<OCI_Number*>(core::Check(OCI_NumberGetContent(value))));
111 }
112 
113 inline int Number::Compare(const Number& other) const
114 {
115  return core::Check(OCI_NumberCompare(*this, other));
116 }
117 
118 template<class T>
119 T Number::GetValue() const
120 {
121  T value;
122 
123  core::Check(OCI_NumberGetValue(*this, support::NumericTypeResolver<T>::Value, &value));
124 
125  return value;
126 }
127 
128 template<class T>
129 Number& Number::SetValue(const T &value)
130 {
131  if (IsNull())
132  {
133  Allocate();
134  }
135 
136  core::Check(OCI_NumberSetValue(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
137 
138  return *this;
139 }
140 
141 template<class T>
142 void Number::Add(const T &value)
143 {
144  core::Check(OCI_NumberAdd(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
145 }
146 
147 template<class T>
148 void Number::Sub(const T &value)
149 {
150  core::Check(OCI_NumberSub(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
151 }
152 
153 template<class T>
154 void Number::Multiply(const T &value)
155 {
156  core::Check(OCI_NumberMultiply(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
157 }
158 
159 template<class T>
160 void Number::Divide(const T &value)
161 {
162  core::Check(OCI_NumberDivide(*this, support::NumericTypeResolver<T>::Value, GetNativeValue(value)));
163 }
164 
165 inline Number& Number::operator = (OCI_Number * &lhs)
166 {
167  AcquireAllocated
168  (
169  lhs,
170  Environment::GetEnvironmentHandle()
171  );
172 
173  return *this;
174 }
175 
176 template<class T, typename core::SupportedNumeric<T>::Type::type*>
177 Number& Number::operator = (const T &lhs)
178 {
179  SetValue<T>(lhs);
180  return *this;
181 }
182 
183 template<class T, typename core::SupportedNumeric<T>::Type::type*>
184 Number::operator T() const
185 {
186  return GetValue<T>();
187 }
188 
189 template<class T, typename core::SupportedNumeric<T>::Type::type*>
190 Number Number::operator + (const T &value)
191 {
192  Number result = Clone();
193  result.Add(value);
194  return result;
195 }
196 
197 template<class T, typename core::SupportedNumeric<T>::Type::type*>
198 Number Number::operator - (const T &value)
199 {
200  Number result = Clone();
201  result.Sub(value);
202  return result;
203 }
204 
205 template<class T, typename core::SupportedNumeric<T>::Type::type*>
206 Number Number::operator * (const T &value)
207 {
208  Number result = Clone();
209  result.Multiply(value);
210  return result;
211 }
212 
213 template<class T, typename core::SupportedNumeric<T>::Type::type*>
214 Number Number::operator / (const T &value)
215 {
216  Number result = Clone();
217  result.Divide(value);
218  return result;
219 }
220 
221 template<class T, typename core::SupportedNumeric<T>::Type::type*>
222 Number& Number::operator += (const T &value)
223 {
224  Add<T>(value);
225  return *this;
226 }
227 
228 template<class T, typename core::SupportedNumeric<T>::Type::type*>
229 Number& Number::operator -= (const T &value)
230 {
231  Sub<T>(value);
232  return *this;
233 }
234 
235 template<class T, typename core::SupportedNumeric<T>::Type::type*>
236 Number& Number::operator *= (const T &value)
237 {
238  Multiply<T>(value);
239  return *this;
240 }
241 
242 template<class T, typename core::SupportedNumeric<T>::Type::type*>
243 Number& Number::operator /= (const T &value)
244 {
245  Divide<T>(value);
246  return *this;
247 }
248 
249 inline Number& Number::operator ++ ()
250 {
251  return *this += 1;
252 }
253 
254 inline Number& Number::operator -- ()
255 {
256  return *this += 1;
257 }
258 
259 inline Number Number::operator ++ (int)
260 {
261  return *this + 1;
262 }
263 
264 inline Number Number::operator -- (int)
265 {
266  return *this - 1;
267 }
268 
269 inline bool Number::operator == (const Number& other) const
270 {
271  return Compare(other) == 0;
272 }
273 
274 inline bool Number::operator != (const Number& other) const
275 {
276  return !(*this == other);
277 }
278 
279 inline bool Number::operator > (const Number& other) const
280 {
281  return Compare(other) > 0;
282 }
283 
284 inline bool Number::operator < (const Number& other) const
285 {
286  return Compare(other) < 0;
287 }
288 
289 inline bool Number::operator >= (const Number& other) const
290 {
291  const int res = Compare(other);
292 
293  return res == 0 || res < 0;
294 }
295 
296 inline bool Number::operator <= (const Number& other) const
297 {
298  const int res = Compare(other);
299 
300  return res == 0 || res > 0;
301 }
302 
303 }
static ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
Object identifying the SQL data type NUMBER.
Definition: types.hpp:2500
Number Clone() const
Clone the current instance to a new one performing deep copy.
Definition: Number.hpp:90
ostring ToString() const override
Convert the number value to a string using default format OCI_STRING_FORMAT_NUMERIC.
Definition: Number.hpp:85
Number(bool create=false)
Create an empty null number object.
Definition: Number.hpp:28
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:64
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:325
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:197
struct OCI_Number OCI_Number
Oracle NUMBER representation.
Definition: types.h:269
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_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 int OCI_API OCI_NumberCompare(OCI_Number *number1, OCI_Number *number2)
Compares two number handles.
OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_NumberCreate(OCI_Connection *con)
Create a local number object.
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 unsigned char *OCI_API OCI_NumberGetContent(OCI_Number *number)
Return the number value content.
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_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 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.
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.
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 int OCI_API OCI_NumberAssign(OCI_Number *number, OCI_Number *number_src)
Assign the value of a number handle to another one.
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so,...
Definition: Utils.hpp:53
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
OCILIB ++ Namespace.
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
@ FormatNumeric
Definition: types.hpp:333
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129