1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! The Oracle time interval data types: INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND
mod tosql;
use super::{Ctx, Number};
use crate::{Result, oci::{self, *}};
use libc::size_t;
use std::{mem, cmp::Ordering, ops::{Deref, DerefMut}};
pub(crate) fn to_string(int: &OCIInterval, lfprec: u8, fsprec: u8, ctx: &dyn Ctx) -> Result<String> {
let name = mem::MaybeUninit::<[u8;32]>::uninit();
let mut name = unsafe { name.assume_init() };
let mut size = mem::MaybeUninit::<size_t>::uninit();
oci::interval_to_text(ctx.as_context(), ctx.as_ref(), int, lfprec, fsprec, name.as_mut_ptr(), name.len(), size.as_mut_ptr())?;
let size = unsafe { size.assume_init() } as usize;
let txt = &name[0..size];
Ok( String::from_utf8_lossy(txt).to_string() )
}
pub(crate) fn to_number(int: &OCIInterval, ctx: &dyn Ctx) -> Result<OCINumber> {
let mut num = mem::MaybeUninit::<OCINumber>::uninit();
oci::interval_to_number(ctx.as_context(), ctx.as_ref(), int, num.as_mut_ptr())?;
Ok( unsafe { num.assume_init() } )
}
pub(crate) fn from_interval<'a,T>(int: &Descriptor<T>, ctx: &'a dyn Ctx) -> Result<Interval<'a,T>>
where T: DescriptorType<OCIType=OCIInterval>
{
let mut interval = Descriptor::<T>::new(&ctx)?;
oci::interval_assign(ctx.as_context(), ctx.as_ref(), int, &mut interval)?;
Ok( Interval { ctx, interval } )
}
/// Represents interval data types
pub struct Interval<'a, T> where T: DescriptorType<OCIType=OCIInterval> {
interval: Descriptor<T>,
ctx: &'a dyn Ctx,
}
impl<'a,T> AsRef<T::OCIType> for Interval<'a,T> where T: DescriptorType<OCIType=OCIInterval> {
fn as_ref(&self) -> &T::OCIType {
self.interval.as_ref()
}
}
impl<'a,T> AsMut<T::OCIType> for Interval<'a,T> where T: DescriptorType<OCIType=OCIInterval> {
fn as_mut(&mut self) -> &mut T::OCIType {
self.interval.as_mut()
}
}
impl<'a,T> Deref for Interval<'a,T> where T: DescriptorType<OCIType=OCIInterval> {
type Target = T::OCIType;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<'a,T> DerefMut for Interval<'a,T> where T: DescriptorType<OCIType=OCIInterval> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}
impl<'a, T> Interval<'a, T> where T: DescriptorType<OCIType=OCIInterval> {
pub(crate) fn from(interval: Descriptor<T>, ctx: &'a dyn Ctx) -> Self {
Self { interval, ctx }
}
/// Returns new interval. This new interval is zero.
pub fn new(ctx: &'a dyn Ctx) -> Result<Self> {
let interval = Descriptor::<T>::new(&ctx)?;
Ok( Self { ctx, interval } )
}
/**
When given an interval string, returns the interval represented by the string.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let int = IntervalDS::from_string("3 11:45:28.150000000", &env)?;
assert_eq!(int.duration()?, (3,11,45,28,150000000));
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_string(txt: &str, ctx: &'a dyn Ctx) -> Result<Self> {
let mut interval = Descriptor::<T>::new(&ctx)?;
oci::interval_from_text(ctx.as_context(), ctx.as_ref(), txt.as_ptr(), txt.len(), &mut interval)?;
Ok( Self { ctx, interval } )
}
/**
Converts an Oracle NUMBER to an interval.
`num` is in years for YEAR TO MONTH intervals and in days for DAY TO SECOND intervals
# Example
```
use sibyl::{ self as oracle, IntervalDS, Number };
let env = oracle::env()?;
let num = Number::from_real(5.5, &env)?;
let int = IntervalDS::from_number(&num)?;
assert_eq!(int.duration()?, (5,12,0,0,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_number(num: &'a Number) -> Result<Self> {
num.to_interval()
}
/**
Copies one interval to another.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let int = IntervalDS::from_string("3 11:45:28.150000000", &env)?;
let cpy = IntervalDS::from_interval(&int)?;
assert_eq!(cpy.duration()?, (3,11,45,28,150000000));
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_interval(other: &Self) -> Result<Self> {
from_interval(&other.interval, other.ctx)
}
/**
Returns number of years (for YEAR TO MONTH intervals) or days (for DAY TO SECOND intervals)
Fractional portions of the interval are included in the Oracle NUMBER produced.
Excess precision is truncated.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let int = IntervalDS::from_string("3 12:00:00.000000000", &env)?;
let num = int.to_number()?;
assert_eq!(num.to_real::<f64>()?, 3.5);
# Ok::<(),oracle::Error>(())
```
*/
pub fn to_number(&self) -> Result<Number<'_>> {
let mut num = Number::new(self.ctx);
oci::interval_to_number(self.ctx.as_context(), self.ctx.as_ref(), self, num.as_mut())?;
Ok( num )
}
/**
Returns a string representing the interval.
- `lfprec` is a leading field precision: the number of digits used to represent the leading field.
- `fsprec` is a fractional second precision of the interval: the number of digits used to represent the fractional seconds.
The interval literal is output as 'year' or 'year-month' for INTERVAL YEAR TO MONTH intervals
and as 'seconds' or 'minutes\[:seconds\]' or 'hours\[:minutes\[:seconds\]\]' or 'days\[ hours\[:minutes\[:seconds\]\]\]'
for INTERVAL DAY TO SECOND intervals (where optional fields are surrounded by brackets)
# Example
```
use sibyl::{ self as oracle, IntervalDS, Number };
let env = oracle::env()?;
let num = Number::from_real(3.1415927, &env)?;
let int = IntervalDS::from_number(&num)?;
assert_eq!(int.to_string(1, 3)?, "+3 03:23:53.609");
# Ok::<(),oracle::Error>(())
```
*/
pub fn to_string(&self, lfprec: u8, fsprec: u8) -> Result<String> {
to_string(self.as_ref(), lfprec, fsprec, self.ctx)
}
/**
Compares two intervals.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
use std::cmp::Ordering;
let env = oracle::env()?;
let i1 = IntervalDS::from_string("3 12:00:00.000000001", &env)?;
let i2 = IntervalDS::from_string("3 12:00:00.000000002", &env)?;
assert_eq!(i1.compare(&i2)?, Ordering::Less);
assert_eq!(i2.compare(&i1)?, Ordering::Greater);
let i3 = IntervalDS::from_interval(&i2)?;
assert_eq!(i2.compare(&i3)?, Ordering::Equal);
# Ok::<(),oracle::Error>(())
```
*/
pub fn compare(&self, other: &Self) -> Result<Ordering> {
let mut res = 0i32;
oci::interval_compare(self.ctx.as_context(), self.ctx.as_ref(), self.as_ref(), other.as_ref(), &mut res)?;
let ordering = if res < 0 { Ordering::Less } else if res == 0 { Ordering::Equal } else { Ordering::Greater };
Ok( ordering )
}
/**
Adds two intervals to produce a resulting interval.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let i1 = IntervalDS::from_string("+0 02:13:40.000000000", &env)?;
let i2 = IntervalDS::from_string("+0 00:46:20.000000000", &env)?;
let res = i1.add(&i2)?;
assert_eq!(res.duration()?, (0,3,0,0,0));
let i3 = oracle::IntervalDS::from_string("-0 00:13:40.000000000", &env)?;
let res = i1.add(&i3)?;
assert_eq!(res.duration()?, (0,2,0,0,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn add(&self, other: &Self) -> Result<Self> {
let ctx = self.ctx;
let mut interval = Descriptor::<T>::new(&ctx)?;
oci::interval_add(ctx.as_context(), ctx.as_ref(), self.as_ref(), other.as_ref(), interval.as_mut())?;
Ok( Self { ctx, interval } )
}
/**
Subtracts an interval from this interval and returns the difference as a new interval.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let i1 = IntervalDS::from_string("+0 02:13:40.000000000", &env)?;
let i2 = IntervalDS::from_string("+0 01:13:40.000000000", &env)?;
let res = i1.sub(&i2)?;
assert_eq!(res.duration()?, (0,1,0,0,0));
let i3 = IntervalDS::from_string("-0 01:46:20.000000000", &env)?;
let res = i1.sub(&i3)?;
assert_eq!(res.duration()?, (0,4,0,0,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn sub(&self, other: &Self) -> Result<Self> {
let ctx = self.ctx;
let mut interval = Descriptor::<T>::new(&ctx)?;
oci::interval_subtract(ctx.as_context(), ctx.as_ref(), self.as_ref(), other.as_ref(), interval.as_mut())?;
Ok( Self { ctx, interval } )
}
/**
Multiplies an interval by an Oracle NUMBER to produce an interval.
# Example
```
use sibyl::{ self as oracle, IntervalDS, Number };
let env = oracle::env()?;
let int = IntervalDS::from_string("+0 00:10:15.000000000", &env)?;
let num = Number::from_int(4, &env)?;
let res = int.mul(&num)?;
assert_eq!(res.duration()?, (0,0,41,0,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn mul(&self, num: &Number) -> Result<Self> {
let ctx = self.ctx;
let mut interval = Descriptor::<T>::new(&ctx)?;
oci::interval_multiply(ctx.as_context(), ctx.as_ref(), self.as_ref(), num.as_ref(), interval.as_mut())?;
Ok( Self { ctx, interval } )
}
/**
Divides an interval by an Oracle NUMBER to produce an interval.
# Example
```
use sibyl::{ self as oracle, IntervalDS, Number };
let env = oracle::env()?;
let int = IntervalDS::from_string("+0 00:50:15.000000000", &env)?;
let num = Number::from_int(5, &env)?;
let res = int.div(&num)?;
assert_eq!(res.duration()?, (0,0,10,3,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn div(&self, num: &Number) -> Result<Self> {
let ctx = self.ctx;
let mut interval = Descriptor::<T>::new(&ctx)?;
oci::interval_divide(ctx.as_context(), ctx.as_ref(), self.as_ref(), num.as_ref(), &mut interval)?;
Ok( Self { ctx, interval } )
}
}
impl<'a> Interval<'a, OCIIntervalDayToSecond> {
/**
Returns interval with the region ID set (if the region is specified
in the input string) and the current absolute offset, or an absolute
offset with the region ID set to 0
The input string must be of the form `\[+/-\]TZH:TZM` or `TZR \[TZD\]`
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let int = IntervalDS::from_tz("EST", &env)?;
assert_eq!(int.to_string(1, 1)?, "-0 05:00:00.0");
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_tz(txt: &str, ctx: &'a dyn Ctx) -> Result<Self> {
let mut interval = Descriptor::<OCIIntervalDayToSecond>::new(&ctx)?;
oci::interval_from_tz(ctx.as_context(), ctx.as_ref(), txt.as_ptr(), txt.len(), &mut interval)?;
Ok( Self { ctx, interval } )
}
/**
Returns new interval with a preset duration.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
// 3 days, 14 hours, 15 minutes, 26 seconds, 535897932 nanoseconds
let int = IntervalDS::with_duration(3, 14, 15, 26, 535_897_932, &env)?;
assert_eq!(int.duration()?, (3, 14, 15, 26, 535897932));
# Ok::<(),oracle::Error>(())
```
*/
pub fn with_duration(dd: i32, hh: i32, mi: i32, ss: i32, ns: i32, ctx: &'a dyn Ctx) -> Result<Self> {
let mut interval = Descriptor::<OCIIntervalDayToSecond>::new(&ctx)?;
oci::interval_set_day_second(ctx.as_context(), ctx.as_ref(), dd, hh, mi, ss, ns, &mut interval)?;
Ok( Self { ctx, interval } )
}
/**
Gets values of day, hour, minute, second, and nanoseconds from an interval.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let int = IntervalDS::from_tz("EST", &env)?;
assert_eq!(int.duration()?, (0, -5, 0, 0, 0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn duration(&self) -> Result<(i32,i32,i32,i32,i32)> {
let mut day = 0i32;
let mut hour = 0i32;
let mut min = 0i32;
let mut sec = 0i32;
let mut fsec = 0i32;
oci::interval_get_day_second(
self.ctx.as_context(), self.ctx.as_ref(),
&mut day, &mut hour, &mut min, &mut sec, &mut fsec,
self.as_ref()
)?;
Ok( (day, hour, min, sec, fsec) )
}
/**
Sets day, hour, minute, second, and nanosecond in an interval.
# Example
```
use sibyl::{ self as oracle, IntervalDS };
let env = oracle::env()?;
let mut int = IntervalDS::with_duration(3, 14, 15, 26, 535_897_932, &env)?;
assert_eq!(int.duration()?, (3, 14, 15, 26, 535897932));
int.set_duration(0, -5, 0, 0, 0)?;
assert_eq!(int.duration()?, (0, -5, 0, 0, 0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn set_duration(&mut self, dd: i32, hh: i32, mi: i32, ss: i32, ns: i32) -> Result<()> {
oci::interval_set_day_second(self.ctx.as_context(), self.ctx.as_ref(), dd, hh, mi, ss, ns, self.as_mut())
}
}
impl<'a> Interval<'a, OCIIntervalYearToMonth> {
/**
Returns new interval with a preset duration.
# Example
```
use sibyl::{ self as oracle, IntervalYM };
let env = oracle::env()?;
// 3 years, 1 month
let int = IntervalYM::with_duration(3, 1, &env)?;
assert_eq!(int.duration()?, (3, 1));
# Ok::<(),oracle::Error>(())
```
*/
pub fn with_duration(year: i32, month: i32, ctx: &'a dyn Ctx) -> Result<Self> {
let mut interval = Descriptor::<OCIIntervalYearToMonth>::new(&ctx)?;
oci::interval_set_year_month(ctx.as_context(), ctx.as_ref(), year, month, &mut interval)?;
Ok( Self { ctx, interval } )
}
/**
Gets values of year and month from an interval.
# Example
```
use sibyl::{ self as oracle, IntervalYM };
let env = oracle::env()?;
let int = IntervalYM::with_duration(3, 1, &env)?;
let (year, month) = int.duration()?;
assert_eq!((year, month), (3, 1));
# Ok::<(),oracle::Error>(())
```
*/
pub fn duration(&self) -> Result<(i32,i32)> {
let mut year = 0i32;
let mut month = 0i32;
oci::interval_get_year_month(self.ctx.as_context(), self.ctx.as_ref(), &mut year, &mut month, self.as_ref())?;
Ok( (year, month) )
}
/**
Sets year and month in an interval.
# Example
```
use sibyl::{ self as oracle, IntervalYM };
let env = oracle::env()?;
let mut int = IntervalYM::with_duration(3, 1, &env)?;
assert_eq!(int.duration()?, (3, 1));
int.set_duration(0, 17)?;
assert_eq!(int.duration()?, (0, 17));
# Ok::<(),oracle::Error>(())
```
*/
pub fn set_duration(&mut self, year: i32, month: i32) -> Result<()> {
oci::interval_set_year_month(self.ctx.as_context(), self.ctx.as_ref(), year, month, self.as_mut())
}
}
impl std::fmt::Debug for Interval<'_, OCIIntervalDayToSecond> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.duration() {
Ok(duration) => fmt.write_fmt(format_args!("IntervalDS {:?}", duration)),
Err(err) => fmt.write_fmt(format_args!("IntervalDS {:?}", err)),
}
}
}
impl std::fmt::Debug for Interval<'_, OCIIntervalYearToMonth> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.duration() {
Ok(duration) => fmt.write_fmt(format_args!("IntervalDS {:?}", duration)),
Err(err) => fmt.write_fmt(format_args!("IntervalDS {:?}", err)),
}
}
}