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