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
//! Fixed or variable-length string
mod tosql;
use crate::{Result, oci::{self, *}};
use super::Ctx;
pub(crate) fn new(size: u32, env: &OCIEnv, err: &OCIError) -> Result<Ptr<OCIString>> {
let mut txt = Ptr::<OCIString>::null();
oci::string_resize(env, err, size, txt.as_mut_ptr())?;
Ok( txt )
}
pub(crate) fn free(txt: &mut Ptr<OCIString>, env: &OCIEnv, err: &OCIError) {
unsafe {
OCIStringResize(env, err, 0, txt.as_mut_ptr());
}
}
pub(crate) fn capacity(txt: *const OCIString, env: &OCIEnv, err: &OCIError) -> Result<usize> {
let mut size = 0u32;
oci::string_alloc_size(env, err, txt, &mut size)?;
Ok( size as usize )
}
pub(crate) fn to_string(txt: &OCIString, env: &OCIEnv) -> String {
let ptr = raw_ptr(txt, env);
let len = len(txt, env);
let txt = unsafe {
std::slice::from_raw_parts(ptr, len)
};
String::from_utf8_lossy(txt).to_string()
}
pub(crate) fn raw_ptr(txt: &OCIString, env: &OCIEnv) -> *const u8 {
unsafe {
OCIStringPtr(env, txt)
}
}
pub(crate) fn len(txt: &OCIString, env: &OCIEnv) -> usize {
unsafe {
OCIStringSize(env, txt) as usize
}
}
pub(crate) fn as_str<'a>(txt: &OCIString, env: &OCIEnv) -> &'a str {
unsafe {
std::str::from_utf8_unchecked(
std::slice::from_raw_parts(
raw_ptr(txt, env),
len(txt, env)
)
)
}
}
/// Represents Oracle character types - VARCHAR, LONG, etc.
pub struct Varchar<'a> {
txt: Ptr<OCIString>,
ctx: &'a dyn Ctx,
}
impl Drop for Varchar<'_> {
fn drop(&mut self) {
free(&mut self.txt, self.ctx.as_ref(), self.ctx.as_ref());
}
}
impl<'a> Varchar<'a> {
/**
Returns a new Varchar constructed from the specified string
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let txt = Varchar::from("Hello, World!", &env)?;
assert!(txt.capacity()? >= 13);
assert_eq!(txt.len(), 13);
assert_eq!(txt.as_str(), "Hello, World!");
# Ok::<(),oracle::Error>(())
```
*/
pub fn from(text: &str, ctx: &'a dyn Ctx) -> Result<Self> {
let mut txt = Ptr::<OCIString>::null();
oci::string_assign_text(ctx.as_ref(), ctx.as_ref(), text.as_ptr(), text.len() as u32, txt.as_mut_ptr())?;
Ok( Self { ctx, txt } )
}
/**
Returns a new Varchar constructed with the copy of the date from the `other` Varchar.
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let src = Varchar::from("Hello, World!", &env)?;
let txt = Varchar::from_varchar(&src)?;
assert_eq!(txt.len(), 13);
assert_eq!(txt.as_str(), "Hello, World!");
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_varchar(other: &'a Varchar) -> Result<Self> {
let ctx = other.ctx;
let mut txt = Ptr::<OCIString>::null();
oci::string_assign(ctx.as_ref(), ctx.as_ref(), &other.txt, txt.as_mut_ptr())?;
Ok( Self { ctx, txt } )
}
pub(crate) fn from_ocistring(oci_str: &OCIString, ctx: &'a dyn Ctx) -> Result<Self> {
let mut txt = Ptr::<OCIString>::null();
oci::string_assign(ctx.as_ref(), ctx.as_ref(), oci_str, txt.as_mut_ptr())?;
Ok( Self { ctx, txt } )
}
/**
Returns a new Varchar with the memory allocated for the txt data.
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let txt = Varchar::with_capacity(19, &env)?;
assert!(txt.capacity()? >= 19);
assert_eq!(txt.len(), 0);
# Ok::<(),oracle::Error>(())
```
*/
pub fn with_capacity(size: usize, ctx: &'a dyn Ctx) -> Result<Self> {
let txt = new(size as u32, ctx.as_ref(), ctx.as_ref())?;
Ok( Self { ctx, txt } )
}
/**
Sets the content of self to `text`
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let mut txt = Varchar::with_capacity(0, &env)?;
txt.set("Hello, World!")?;
assert_eq!(txt.len(), 13);
assert_eq!(txt.as_str(), "Hello, World!");
# Ok::<(),oracle::Error>(())
```
*/
pub fn set(&mut self, text: &str) -> Result<()> {
oci::string_assign_text(self.ctx.as_ref(), self.ctx.as_ref(), text.as_ptr(), text.len() as u32, self.txt.as_mut_ptr())
}
/**
Returns the size of the string in bytes.
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let mut txt = Varchar::from("🚲🛠📬🎓", &env)?;
assert_eq!(txt.len(), 16);
# Ok::<(),oracle::Error>(())
```
*/
pub fn len(&self) -> usize {
len(&self.txt, self.ctx.as_ref())
}
/**
Returns the allocated size of string memory in bytes
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let mut txt = Varchar::from("🚲🛠📬🎓", &env)?;
assert!(txt.capacity()? >= 16);
# Ok::<(),oracle::Error>(())
```
*/
pub fn capacity(&self) -> Result<usize> {
capacity(self.txt.get(), self.ctx.as_ref(), self.ctx.as_ref())
}
/**
Changes the size of the memory of a string in the object cache.
Content of the string is not preserved.
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let mut txt = Varchar::with_capacity(10, &env)?;
assert!(txt.capacity()? >= 10);
txt.resize(20)?;
assert!(txt.capacity()? >= 20);
txt.resize(0)?;
// Cannot not ask for capacity after resize to 0.
// Yes, it works for Raw, but not for Varchars
let res = txt.capacity();
assert!(res.is_err());
if let Err( sibyl::Error::Oracle(code, _message) ) = res {
assert_eq!(code, 21500);
} else {
panic!("cannot match the error");
}
txt.resize(16)?;
assert!(txt.capacity()? >= 16);
# Ok::<(),oracle::Error>(())
```
*/
pub fn resize(&mut self, new_size: usize) -> Result<()> {
oci::string_resize(self.ctx.as_ref(), self.ctx.as_ref(), new_size as u32, self.txt.as_mut_ptr())
}
/**
Extracts a string slice containing the entire content of the VARCHAR.
# Example
```
use sibyl::{ self as oracle, Varchar };
let env = oracle::env()?;
let txt = Varchar::from("Hello, World!", &env)?;
assert_eq!(txt.as_str(), "Hello, World!");
# Ok::<(),oracle::Error>(())
```
*/
pub fn as_str(&self) -> &str {
as_str(&self.txt, self.ctx.as_ref())
}
}
impl std::fmt::Debug for Varchar<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
const MAX_LEN : usize = 50;
let len = self.len();
if len == 0 {
f.write_str("VARCHAR ''")
} else if len <= MAX_LEN {
f.write_fmt(format_args!("VARCHAR '{}'", self.as_str()))
} else {
f.write_fmt(format_args!("VARCHAR '{}...'", &self.as_str()[..MAX_LEN]))
}
}
}
impl std::string::ToString for Varchar<'_> {
fn to_string(&self) -> String {
to_string(&self.txt, self.ctx.as_ref())
}
}