sibyl 0.7.0

An OCI-based (synchronous or asynchronous) interface between Rust applications and Oracle databases
Documentation
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
use super::{cursor::Cursor, cols::{ColumnBuffer, Column}, rows::Row};
use crate::{
    Error,
    IntervalDS, IntervalYM, Result, RowID, Timestamp, TimestampLTZ, TimestampTZ,
    oci::*,
    types::{
        date, interval, number, raw, timestamp, varchar,
        Date, Varchar, rowid
    },
    lob::{ self, LOB }, 
    Raw,
};

/// A trait for types which values can be created from the returned Oracle data.
pub trait FromSql<'a> : Sized {
    /**
        Converts, if possible, data stored in the column buffer into the requested
        type and returns the created value.
        
        Returns error if the conversion fails or conversion from the type of the
        column buffer into a requested type is not defined.
    */
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self>;
}

fn assert_not_null(row: &Row, col: &Column) -> Result<()> {
    if col.is_null() {
        let col_name = col.name(row.as_ref())?;
        Err(Error::msg(format!("Column {} is null", col_name)))
    } else {
        Ok(())
    }
}

impl<'a> FromSql<'a> for String {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Text( oci_str_ptr )   => Ok( varchar::to_string(oci_str_ptr, row.as_ref()) ),
            ColumnBuffer::Number( oci_num_box ) => number::to_string("TM", oci_num_box.as_ref(), row.as_ref()),
            ColumnBuffer::Date( oci_date )      => date::to_string("YYYY-MM-DD HH24::MI:SS", oci_date, row.as_ref()),
            ColumnBuffer::Timestamp( ts )       => timestamp::to_string("YYYY-MM-DD HH24:MI:SSXFF", 3, ts.as_ref(), row),
            ColumnBuffer::TimestampTZ( ts )     => timestamp::to_string("YYYY-MM-DD HH24:MI:SSXFF TZH:TZM", 3, ts.as_ref(), row),
            ColumnBuffer::TimestampLTZ( ts )    => timestamp::to_string("YYYY-MM-DD HH24:MI:SSXFF TZH:TZM", 3, ts.as_ref(), row),
            ColumnBuffer::IntervalYM( int )     => interval::to_string(int.as_ref(), 4, 3, row),
            ColumnBuffer::IntervalDS( int )     => interval::to_string(int.as_ref(), 9, 5, row),
            ColumnBuffer::Float( val )          => Ok( val.to_string() ),
            ColumnBuffer::Double( val )         => Ok( val.to_string() ),
            ColumnBuffer::Rowid( rowid )        => rowid::to_string(rowid, row.as_ref()),
            _                                   => Err( Error::new("cannot return as a String") )
        }
    }
}

impl<'a> FromSql<'a> for Varchar<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        if let ColumnBuffer::Text( oci_str_ptr ) = col.data() {
            Varchar::from_ocistring(oci_str_ptr, row)
        } else {
            let text : String = FromSql::value(row, col)?;
            Varchar::from(&text, row)
        }
    }
}

impl<'a> FromSql<'a> for &'a str {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Text( oci_str_ptr ) => Ok( varchar::as_str(&oci_str_ptr, row.as_ref()) ),
            _ => Err( Error::new("cannot borrow as &str") )
        }
    }
}

impl<'a> FromSql<'a> for &'a [u8] {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Binary( oci_raw_ptr ) => Ok( {
                // inlined Raw::as_bytes to deal with the buffer lifetime issue
                let ptr = raw::as_ptr(&oci_raw_ptr, row.as_ref());
                let len = raw::len(&oci_raw_ptr, row.as_ref());
                unsafe {
                    std::slice::from_raw_parts(ptr, len)
                }
            }),
            _ => Err( Error::new("cannot borrow as &[u8]") )
        }
    }
}

impl<'a, T: number::Integer> FromSql<'a> for T {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Number( oci_num_box ) => <T>::from_number(oci_num_box, row.as_ref()),
            _ => Err( Error::new("cannot return as an integer") )
        }
    }
}

impl<'a> FromSql<'a> for f32 {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Number( oci_num_box ) => number::to_real(oci_num_box, row.as_ref()),
            ColumnBuffer::Float( val )          => Ok( *val ),
            ColumnBuffer::Double( val )         => Ok( *val as f32 ),
            ColumnBuffer::IntervalYM( int )     => {
                let num = interval::to_number(int, row)?;
                number::to_real(&num, row.as_ref())
            }
            ColumnBuffer::IntervalDS( int )     => {
                let num = interval::to_number(int, row)?;
                number::to_real(&num, row.as_ref())
            }
            _ => Err( Error::new("cannot return as f32") )
        }
    }
}

impl<'a> FromSql<'a> for f64 {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Number( oci_num_box ) => number::to_real(oci_num_box, row.as_ref()),
            ColumnBuffer::Float( val )          => Ok( *val as f64 ),
            ColumnBuffer::Double( val )         => Ok( *val ),
            ColumnBuffer::IntervalYM( int )     => {
                let num = interval::to_number(int, row)?;
                number::to_real(&num, row.as_ref())
            }
            ColumnBuffer::IntervalDS( int )     => {
                let num = interval::to_number(int, row)?;
                number::to_real(&num, row.as_ref())
            }
            _ => Err( Error::new("cannot return as f64") )
        }
    }
}

impl<'a> FromSql<'a> for number::Number<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Number( oci_num_box ) => number::Number::from(oci_num_box, row),
            ColumnBuffer::Float( val )          => number::Number::from_real(*val, row),
            ColumnBuffer::Double( val )         => number::Number::from_real(*val, row),
            ColumnBuffer::IntervalYM( int )     => {
                let num = interval::to_number(int, row)?;
                Ok( number::Number::make(num, row) )
            }
            ColumnBuffer::IntervalDS( int )     => {
                let num = interval::to_number(int, row)?;
                Ok( number::Number::make(num, row) )
            }
            _ => Err( Error::new("cannot return as a Number") )
        }
    }
}

impl<'a> FromSql<'a> for Date<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data()  {
            ColumnBuffer::Date( oci_date ) => date::from_date(oci_date, row.as_ref()),
            _ => Err( Error::new("cannot return as a Date") )
        }
    }
}

impl<'a> FromSql<'a> for Timestamp<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Timestamp( ts )    => timestamp::from_timestamp(ts, row),
            ColumnBuffer::TimestampTZ( ts )  => timestamp::convert_into(ts, row),
            ColumnBuffer::TimestampLTZ( ts ) => timestamp::convert_into(ts, row),
            _ => Err( Error::new("cannot return as a Timestamp") )
        }
    }
}

impl<'a> FromSql<'a> for TimestampTZ<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Timestamp( ts )    => timestamp::convert_into(ts, row),
            ColumnBuffer::TimestampTZ( ts )  => timestamp::from_timestamp(ts, row),
            ColumnBuffer::TimestampLTZ( ts ) => timestamp::convert_into(ts, row),
            _ => Err( Error::new("cannot return as a Timestamp with time zone") )
        }
    }
}

impl<'a> FromSql<'a> for TimestampLTZ<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Timestamp( ts )    => timestamp::convert_into(ts, row),
            ColumnBuffer::TimestampTZ( ts )  => timestamp::convert_into(ts, row),
            ColumnBuffer::TimestampLTZ( ts ) => timestamp::from_timestamp(ts, row),
            _ => Err( Error::new("cannot return as a Timestamp with local time zone") )
        }
    }
}

impl<'a> FromSql<'a> for IntervalYM<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::IntervalYM( int )  => interval::from_interval(int, row),
            _ => Err( Error::new("cannot return as Inteval year to month") )
        }
    }
}

impl<'a> FromSql<'a> for IntervalDS<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::IntervalDS( int )  => interval::from_interval(int, row),
            _ => Err( Error::new("cannot return as Interval day to second") )
        }
    }
}

impl<'a> FromSql<'a> for Cursor<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Cursor( handle ) => {
                let mut ref_cursor : Handle<OCIStmt> = Handle::new(row)?;
                ref_cursor.swap(handle);
                Ok( Cursor::explicit(ref_cursor, row) )
            }
            _ => Err( Error::new("cannot return as Cursor") )
        }
    }
}

macro_rules! impl_from_lob {
    ($var:path => $t:ident ) => {
        impl<'a> FromSql<'a> for LOB<'a,$t> {
            fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
                assert_not_null(row, col)?;
                match col.data() {
                    $var ( row_loc ) => {
                        if lob::is_initialized(&row_loc.get_ptr(), row.as_ref(), row.as_ref())? {
                            Ok( LOB::<$t>::at_column(row_loc, row.session()) )
                        } else {
                            Err(Error::new("already consumed"))
                        }
                    },
                    _ => Err( Error::new("cannot return as a LOB locator") )
                }
            }
        }
    };
}

impl_from_lob!{ ColumnBuffer::CLOB  => OCICLobLocator  }
impl_from_lob!{ ColumnBuffer::BLOB  => OCIBLobLocator  }
impl_from_lob!{ ColumnBuffer::BFile => OCIBFileLocator }

impl<'a> FromSql<'a> for RowID {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Rowid( rowid )  => {
                if rowid::is_initialized(rowid) {
                    let mut res = Descriptor::<OCIRowid>::new(row)?;
                    res.swap(rowid);
                    Ok(RowID::from(res))
                } else {
                    Err(Error::new("already consumed"))
                }
            },
            _ => Err( Error::new("cannot return as row id") )
        }
    }
}

impl<'a> FromSql<'a> for Raw<'a> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        assert_not_null(row, col)?;
        match col.data() {
            ColumnBuffer::Binary( oci_raw_ptr ) => {
                // inlined Raw::as_bytes to deal with the buffer lifetime issue
                let ptr = raw::as_ptr(&oci_raw_ptr, row.as_ref());
                let len = raw::len(&oci_raw_ptr, row.as_ref());
                let raw = unsafe {
                    std::slice::from_raw_parts(ptr, len)
                };
                Raw::from_bytes(raw, row.session())
            },
            _ => Err( Error::new("cannot return as Raw") )
        }
    }
}

impl<'a, T: FromSql<'a>> FromSql<'a> for Option<T> {
    fn value(row: &'a Row<'a>, col: &mut Column) -> Result<Self> {
        if col.is_null() {
            Ok(None)
        } else {
            let val : T = FromSql::value(row, col)?;
            Ok(Some(val))
        }
    }
}

#[cfg(all(test,feature="blocking"))]
mod tests {
    use crate::*;

    #[test]
    fn from_lob() -> Result<()> {
        let session = crate::test_env::get_session()?;        

        let (feature_release, _, _, _, _) = client_version();
        if feature_release <= 19 {
            // 19 barfs a two-task error when one of the BFileName arguments is a constant and another is a parameter
            // see below the BFileName call in the 23ai INSERT.
            let stmt = session.prepare("
                INSERT INTO test_large_object_data (fbin) 
                     VALUES (BFileName(:DIR_NAME,:FILE_NAME))
                  RETURNING fbin
                       INTO :NEW_LOB
            ")?;
            let mut lob = BFile::new(&session)?;
            let count = stmt.execute(((":DIR_NAME", "MEDIA_DIR"), (":FILE_NAME", "hello_world.txt"), (":NEW_LOB", &mut lob)))?;
            assert_eq!(count, 1);
            assert!(lob.file_exists()?);
            let (dir, name) = lob.file_name()?;
            assert_eq!((dir.as_str(), name.as_str()), ("MEDIA_DIR","hello_world.txt"));
            assert_eq!(lob.len()?, 30);

            let count = stmt.execute(((":DIR_NAME", "MEDIA_DIR"), (":FILE_NAME", "hello_supplemental.txt"), (":NEW_LOB", &mut lob)))?;
            assert_eq!(count, 1);
            assert!(lob.file_exists()?);
            let (dir, name) = lob.file_name()?;
            assert_eq!((dir.as_str(), name.as_str()), ("MEDIA_DIR","hello_supplemental.txt"));
            assert_eq!(lob.len()?, 20);
        } else {
            // 23 (maybe also 21) throws "ORA-03120: two-task conversion routine" when `fbin` is RETURNING from INSERT
            // If there is problem with bidning BFile to a parameter placeholder, I do not see it. Plus 19 has no issues with it.
            // On the other hand, 23 client was connecting to the 19c database. Maybe it freaks out in this case.
            let stmt = session.prepare("
                INSERT INTO test_large_object_data (fbin)
                     VALUES (BFileName('MEDIA_DIR',:FILE_NAME))
                  RETURNING ROWID
                       INTO :ROW_ID
            ")?;
            let mut rowid1 = RowID::new(&session)?;
            let count = stmt.execute(("hello_world.txt", &mut rowid1, ()))?;
            assert_eq!(count, 1);

            let mut rowid2 = RowID::new(&session)?;
            let count = stmt.execute(("hello_supplemental.txt", &mut rowid2, ()))?;
            assert_eq!(count, 1);

            // Not locking the row as BFiles are read-only
            let stmt = session.prepare("
                SELECT fbin FROM test_large_object_data WHERE ROWID = :ROW_ID
            ")?;
            let rows = stmt.query(&rowid1)?;
            let row = rows.next()?.expect("first row");
            let lob: BFile = row.get(0)?;

            assert!(lob.file_exists()?);
            let (dir, name) = lob.file_name()?;
            assert_eq!((dir.as_str(), name.as_str()), ("MEDIA_DIR","hello_world.txt"));
            assert_eq!(lob.len()?, 30);

            let rows = stmt.query(&rowid2)?;
            let row = rows.next()?.expect("first row");
            let lob: BFile = row.get(0)?;

            assert!(lob.file_exists()?);
            let (dir, name) = lob.file_name()?;
            assert_eq!((dir.as_str(), name.as_str()), ("MEDIA_DIR","hello_supplemental.txt"));
            assert_eq!(lob.len()?, 20);
        }
        Ok(())
    }

    #[test]
    fn from_rowid() -> Result<()> {
        let session = crate::test_env::get_session()?;
        let stmt = session.prepare("
            SELECT ROWID, manager_id
              FROM hr.employees
             WHERE employee_id = :ID
        ")?;
        if let Some(row) = stmt.query_single((":ID", 107))? {
            let strid : String = row.get(0)?;
            let rowid : RowID = row.get(0)?;
            assert_eq!(rowid.to_string(&session)?, strid);
            let manager_id: u32 = row.get(1)?;
            assert_eq!(manager_id, 103, "employee ID of Alexander Hunold");

            match get_rowid(&row) {
                Ok(_) => panic!("unexpected duplicate ROWID descriptor"),
                Err(Error::Interface(msg)) => assert_eq!(msg, "already consumed"),
                Err(err) => panic!("unexpected error: {:?}", err)
            }
        }

        Ok(())
    }

    fn get_rowid<'a>(row: &'a Row<'a>) -> Result<RowID> {
        let rowid : RowID = row.get(0)?;
        Ok(rowid)
    }
}