OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Interval.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 // ReSharper disable CppClangTidyHicppUseEqualsDefault
26 // ReSharper disable CppClangTidyModernizeUseEqualsDefault
27 
28 namespace ocilib
29 {
30 
32 {
33 }
34 
36 {
37  Acquire(core::Check(OCI_IntervalCreate(nullptr, type)), reinterpret_cast<HandleFreeFunc>(OCI_IntervalFree), nullptr, nullptr);
38 }
39 
40 inline Interval::Interval(IntervalType type, const ostring& data)
41 {
42  Acquire(core::Check(OCI_IntervalCreate(nullptr, type)), reinterpret_cast<HandleFreeFunc>(OCI_IntervalFree), nullptr, nullptr);
43 
44  FromString(data);
45 }
46 
47 inline Interval::Interval(OCI_Interval *pInterval, core::Handle *parent)
48 {
49  Acquire(pInterval, nullptr, nullptr, parent);
50 }
51 
52 inline Interval Interval::Clone() const
53 {
54  Interval result(GetType());
55 
56  core::Check(OCI_IntervalAssign(result, *this));
57 
58  return result;
59 }
60 
61 inline int Interval::Compare(const Interval& other) const
62 {
63  return core::Check(OCI_IntervalCompare(*this, other));
64 }
65 
67 {
68  return IntervalType(static_cast<IntervalType::Type>(core::Check(OCI_IntervalGetType(*this))));
69 }
70 
71 inline bool Interval::IsValid() const
72 {
73  return (core::Check(OCI_IntervalCheck(*this)) == 0);
74 }
75 
76 inline int Interval::GetYear() const
77 {
78  int year = 0, month = 0;
79 
80  GetYearMonth(year, month);
81 
82  return year;
83 }
84 
85 inline void Interval::SetYear(int value)
86 {
87  int year = 0, month = 0;
88 
89  GetYearMonth(year, month);
90  SetYearMonth(value, month);
91 }
92 
93 inline int Interval::GetMonth() const
94 {
95  int year = 0, month = 0;
96 
97  GetYearMonth(year, month);
98 
99  return month;
100 }
101 
102 inline void Interval::SetMonth(int value)
103 {
104  int year = 0, month = 0;
105 
106  GetYearMonth(year, month);
107  SetYearMonth(year, value);
108 }
109 
110 inline int Interval::GetDay() const
111 {
112  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
113 
114  GetDaySecond(day, hour, minutes, seconds, milliseconds);
115 
116  return day;
117 }
118 
119 inline void Interval::SetDay(int value)
120 {
121  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
122 
123  GetDaySecond(day, hour, minutes, seconds, milliseconds);
124  SetDaySecond(value, hour, minutes, seconds, milliseconds);
125 }
126 
127 inline int Interval::GetHours() const
128 {
129  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
130 
131  GetDaySecond(day, hour, minutes, seconds, milliseconds);
132 
133  return hour;
134 }
135 
136 inline void Interval::SetHours(int value)
137 {
138  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
139 
140  GetDaySecond(day, hour, minutes, seconds, milliseconds);
141  SetDaySecond(day, value, minutes, seconds, milliseconds);
142 }
143 
144 inline int Interval::GetMinutes() const
145 {
146  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
147 
148  GetDaySecond(day, hour, minutes, seconds, milliseconds);
149 
150  return minutes;
151 }
152 
153 inline void Interval::SetMinutes(int value)
154 {
155  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
156 
157  GetDaySecond(day, hour, minutes, seconds, milliseconds);
158  SetDaySecond(day, hour, value, seconds, milliseconds);
159 }
160 
161 inline int Interval::GetSeconds() const
162 {
163  int day, hour, minutes, seconds, milliseconds;
164 
165  GetDaySecond(day, hour, minutes, seconds, milliseconds);
166 
167  return seconds;
168 }
169 
170 inline void Interval::SetSeconds(int value)
171 {
172  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
173 
174  GetDaySecond(day, hour, minutes, seconds, milliseconds);
175  SetDaySecond(day, hour, minutes, value, milliseconds);
176 }
177 
178 inline int Interval::GetMilliSeconds() const
179 {
180  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
181 
182  GetDaySecond(day, hour, minutes, seconds, milliseconds);
183 
184  return milliseconds;
185 }
186 
187 inline void Interval::SetMilliSeconds(int value)
188 {
189  int day = 0, hour = 0, minutes = 0, seconds = 0, milliseconds = 0;
190 
191  GetDaySecond(day, hour, minutes, seconds, milliseconds);
192  SetDaySecond(day, hour, minutes, seconds, value);
193 }
194 
195 inline void Interval::GetDaySecond(int &day, int &hour, int &min, int &sec, int &fsec) const
196 {
197  core::Check(OCI_IntervalGetDaySecond(*this, &day, &hour, &min, &sec, &fsec));
198 }
199 
200 inline void Interval::SetDaySecond(int day, int hour, int min, int sec, int fsec)
201 {
202  core::Check(OCI_IntervalSetDaySecond(*this, day, hour, min, sec, fsec));
203 }
204 
205 inline void Interval::GetYearMonth(int &year, int &month) const
206 {
207  core::Check(OCI_IntervalGetYearMonth(*this, &year, &month));
208 }
209 inline void Interval::SetYearMonth(int year, int month)
210 {
211  core::Check(OCI_IntervalSetYearMonth(*this, year, month));
212 }
213 
214 inline void Interval::UpdateTimeZone(const ostring& timeZone)
215 {
216  core::Check(OCI_IntervalFromTimeZone(*this, timeZone.c_str()));
217 }
218 
219 inline void Interval::FromString(const ostring& data)
220 {
221  core::Check(OCI_IntervalFromText(*this, data.c_str()));
222 }
223 
224 inline ostring Interval::ToString(int leadingPrecision, int fractionPrecision) const
225 {
226  if (!IsNull())
227  {
228  const size_t size = OCI_SIZE_BUFFER;
229 
230  core::ManagedBuffer<otext> buffer(static_cast<size_t>(size + 1));
231 
232  core::Check(OCI_IntervalToText(*this, leadingPrecision, fractionPrecision, static_cast<int>(size), buffer));
233 
234  return core::MakeString(static_cast<const otext *>(buffer));
235  }
236 
237  return OCI_STRING_NULL;
238 }
239 
241 {
242  return ToString(OCI_STRING_DEFAULT_PREC, OCI_STRING_DEFAULT_PREC);
243 }
244 
245 inline Interval Interval::operator + (const Interval& other) const
246 {
247  Interval result = Clone();
248  return result += other;
249 }
250 
251 inline Interval Interval::operator - (const Interval& other) const
252 {
253  Interval result = Clone();
254  return result -= other;
255 }
256 
258 {
259  core::Check(OCI_IntervalAdd(*this, other));
260  return *this;
261 }
262 
264 {
265  core::Check(OCI_IntervalSubtract(*this, other));
266  return *this;
267 }
268 
269 inline bool Interval::operator == (const Interval& other) const
270 {
271  return Compare(other) == 0;
272 }
273 
274 inline bool Interval::operator != (const Interval& other) const
275 {
276  return (!(*this == other));
277 }
278 
279 inline bool Interval::operator > (const Interval& other) const
280 {
281  return (Compare(other) > 0);
282 }
283 
284 inline bool Interval::operator < (const Interval& other) const
285 {
286  return (Compare(other) < 0);
287 }
288 
289 inline bool Interval::operator >= (const Interval& other) const
290 {
291  const int res = Compare(other);
292 
293  return (res == 0 || res < 0);
294 }
295 
296 inline bool Interval::operator <= (const Interval& other) const
297 {
298  const int res = Compare(other);
299 
300  return (res == 0 || res > 0);
301 }
302 
303 }
Interval operator-(const Interval &other) const
Return a new Interval holding the difference of the current Interval value and the given Interval val...
Definition: Interval.hpp:251
void SetHours(int value)
Set the interval hours value.
Definition: Interval.hpp:136
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:312
bool operator!=(const Interval &other) const
Indicates if the current Interval value is not equal the given Interval value.
Definition: Interval.hpp:274
Interval()
Create an empty null Interval instance.
Definition: Interval.hpp:31
int GetSeconds() const
Return the interval seconds value.
Definition: Interval.hpp:161
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalGetYearMonth(OCI_Interval *itv, int *year, int *month)
Return the year / month portion of an interval handle.
OCILIB ++ Namespace.
ostring ToString() const override
Convert the interval value to a string using the default precisions of 10.
Definition: Interval.hpp:240
int GetMonth() const
Return the interval month value.
Definition: Interval.hpp:93
void SetDaySecond(int day, int hour, int min, int sec, int fsec)
Set the Day / Second parts.
Definition: Interval.hpp:200
int GetDay() const
Return the interval day value.
Definition: Interval.hpp:110
int GetHours() const
Return the interval hours value.
Definition: Interval.hpp:127
OCI_SYM_PUBLIC unsigned int OCI_API OCI_IntervalGetType(OCI_Interval *itv)
Return the type of the given Interval object.
core::Enum< IntervalTypeValues > IntervalType
Interval types.
Definition: types.hpp:3123
void SetYear(int value)
Set the interval year value.
Definition: Interval.hpp:85
struct OCI_Interval OCI_Interval
Oracle internal interval representation.
Definition: types.h:287
bool operator>(const Interval &other) const
Indicates if the current Interval value is superior to the given Interval value.
Definition: Interval.hpp:279
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
void SetMinutes(int value)
Set the interval minutes value.
Definition: Interval.hpp:153
int GetYear() const
Return the interval year value.
Definition: Interval.hpp:76
OCI_SYM_PUBLIC OCI_Interval *OCI_API OCI_IntervalCreate(OCI_Connection *con, unsigned int type)
Create a local interval object.
OCI_SYM_PUBLIC int OCI_API OCI_IntervalCheck(OCI_Interval *itv)
Check if the given interval is valid.
void FromString(const ostring &data)
Assign to the interval object the value provided by the input interval string.
Definition: Interval.hpp:219
bool operator<=(const Interval &other) const
Indicates if the current Interval value is inferior or equal to the given Interval value...
Definition: Interval.hpp:296
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalGetDaySecond(OCI_Interval *itv, int *day, int *hour, int *min, int *sec, int *fsec)
Return the day / time portion of an interval handle.
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
bool operator>=(const Interval &other) const
Indicates if the current Interval value is superior or equal to the given Interval value...
Definition: Interval.hpp:289
void SetMilliSeconds(int value)
Set the interval milliseconds value.
Definition: Interval.hpp:187
Interval operator+(const Interval &other) const
Return a new Interval holding the sum of the current Interval value and the given Interval value...
Definition: Interval.hpp:245
Interval & operator+=(const Interval &other)
Increment the current Value with the given Interval value.
Definition: Interval.hpp:257
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:117
Object identifying the SQL data type INTERVAL.
Definition: types.hpp:3091
int GetMilliSeconds() const
Return the interval seconds value.
Definition: Interval.hpp:178
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:196
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalAdd(OCI_Interval *itv, OCI_Interval *itv2)
Adds an interval handle value to another.
void UpdateTimeZone(const ostring &timeZone)
Update the interval value with the given time zone.
Definition: Interval.hpp:214
int GetMinutes() const
Return the interval minutes value.
Definition: Interval.hpp:144
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalFromText(OCI_Interval *itv, const otext *str)
Convert a string to an interval and store it in the given interval handle.
OCI_SYM_PUBLIC int OCI_API OCI_IntervalCompare(OCI_Interval *itv, OCI_Interval *itv2)
Compares two interval handles.
void GetYearMonth(int &year, int &month) const
Extract the year / month parts from the interval value.
Definition: Interval.hpp:205
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalFree(OCI_Interval *itv)
Free an OCI_Interval handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalSubtract(OCI_Interval *itv, OCI_Interval *itv2)
Subtract an interval handle value from another.
void GetDaySecond(int &day, int &hour, int &min, int &sec, int &fsec) const
Extract the date / second parts from the interval value.
Definition: Interval.hpp:195
IntervalType GetType() const
Return the type of the given interval object.
Definition: Interval.hpp:66
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalFromTimeZone(OCI_Interval *itv, const otext *str)
Correct an interval handle value with the given time zone.
Interval & operator-=(const Interval &other)
Decrement the current Value with the given Interval value.
Definition: Interval.hpp:263
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalToText(OCI_Interval *itv, int leading_prec, int fraction_prec, int size, otext *str)
Convert an interval value from the given interval handle to a string.
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalAssign(OCI_Interval *itv, OCI_Interval *itv_src)
Assign the value of a interval handle to another one.
bool operator<(const Interval &other) const
Indicates if the current Interval value is inferior to the given Interval value.
Definition: Interval.hpp:284
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalSetYearMonth(OCI_Interval *itv, int year, int month)
Set the year / month portion if the given Interval handle.
void SetDay(int value)
Set the interval day value.
Definition: Interval.hpp:119
OCI_SYM_PUBLIC boolean OCI_API OCI_IntervalSetDaySecond(OCI_Interval *itv, int day, int hour, int min, int sec, int fsec)
Set the day / time portion if the given interval handle.
Interval Clone() const
Clone the current instance to a new one performing deep copy.
Definition: Interval.hpp:52
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
bool IsValid() const
Check if the given interval is valid.
Definition: Interval.hpp:71
void SetSeconds(int value)
Set the interval seconds value.
Definition: Interval.hpp:170
void SetMonth(int value)
Set the interval month value.
Definition: Interval.hpp:102
void SetYearMonth(int year, int month)
Set the Year / Month parts.
Definition: Interval.hpp:209
bool operator==(const Interval &other) const
Indicates if the current Interval value is equal to the given Interval value.
Definition: Interval.hpp:269