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
use super::buffer::{buf_ptr, mut_buf_ptr};
use std::{borrow::Cow, mem::size_of};
#[cfg(feature = "narrow")]
use std::{ffi::CStr, string::FromUtf8Error};
#[cfg(not(feature = "narrow"))]
use std::{
char::{decode_utf16, DecodeUtf16Error},
marker::PhantomData,
};
#[cfg(not(feature = "narrow"))]
use widestring::{U16CStr, U16String};
#[cfg(feature = "narrow")]
pub type SqlChar = u8;
#[cfg(not(feature = "narrow"))]
pub type SqlChar = u16;
#[cfg(feature = "narrow")]
pub type DecodingError = FromUtf8Error;
#[cfg(not(feature = "narrow"))]
pub type DecodingError = DecodeUtf16Error;
#[cfg(feature = "narrow")]
pub fn slice_to_utf8(text: &[u8]) -> Result<String, FromUtf8Error> {
String::from_utf8(text.to_owned())
}
#[cfg(not(feature = "narrow"))]
pub fn slice_to_utf8(text: &[u16]) -> Result<String, DecodeUtf16Error> {
decode_utf16(text.iter().copied()).collect()
}
#[cfg(feature = "narrow")]
pub fn slice_to_cow_utf8(text: &[u8]) -> Cow<str> {
String::from_utf8_lossy(text)
}
#[cfg(not(feature = "narrow"))]
pub fn slice_to_cow_utf8(text: &[u16]) -> Cow<str> {
let text: Result<String, _> = decode_utf16(text.iter().copied()).collect();
text.unwrap().into()
}
#[cfg(not(feature = "narrow"))]
fn sz_to_utf8(buffer: &[u16]) -> String {
let c_str = U16CStr::from_slice_truncate(buffer).unwrap();
c_str.to_string_lossy()
}
#[cfg(feature = "narrow")]
fn sz_to_utf8(buffer: &[u8]) -> String {
let end = buffer
.iter()
.enumerate()
.find(|(_index, &character)| character == b'\0')
.expect("Buffer must contain terminating zero.")
.0;
let c_str = unsafe { CStr::from_bytes_with_nul_unchecked(&buffer[..=end]) };
c_str.to_string_lossy().into_owned()
}
pub fn binary_length(buffer: &[SqlChar]) -> usize {
buffer.len() * size_of::<SqlChar>()
}
pub fn is_truncated_bin(buffer: &[SqlChar], actual_length_bin: usize) -> bool {
buffer.len() * size_of::<SqlChar>() <= actual_length_bin
}
pub fn resize_to_fit_with_tz(buffer: &mut Vec<SqlChar>, required_binary_length: usize) {
buffer.resize((required_binary_length / size_of::<SqlChar>()) + 1, 0);
}
pub fn resize_to_fit_without_tz(buffer: &mut Vec<SqlChar>, required_binary_length: usize) {
buffer.resize(required_binary_length / size_of::<SqlChar>(), 0);
}
pub struct SqlText<'a> {
#[cfg(not(feature = "narrow"))]
text: U16String,
#[cfg(not(feature = "narrow"))]
_ref: PhantomData<&'a str>,
#[cfg(feature = "narrow")]
text: &'a str,
}
impl<'a> SqlText<'a> {
#[cfg(not(feature = "narrow"))]
pub fn new(text: &'a str) -> Self {
Self {
text: U16String::from_str(text),
_ref: PhantomData,
}
}
#[cfg(feature = "narrow")]
pub fn new(text: &'a str) -> Self {
Self { text }
}
#[cfg(not(feature = "narrow"))]
pub fn ptr(&self) -> *const u16 {
buf_ptr(self.text.as_slice())
}
#[cfg(feature = "narrow")]
pub fn ptr(&self) -> *const u8 {
buf_ptr(self.text.as_bytes())
}
pub fn len_char(&self) -> usize {
self.text.len()
}
}
pub struct SzBuffer {
buffer: Vec<SqlChar>,
}
impl SzBuffer {
pub fn with_capacity(capacity: usize) -> Self {
Self {
buffer: vec![0; capacity + 1],
}
}
pub fn mut_buf(&mut self) -> &mut [SqlChar] {
self.buffer.resize(self.buffer.capacity(), 0);
&mut self.buffer
}
pub fn to_utf8(&self) -> String {
sz_to_utf8(&self.buffer)
}
}
pub struct OutputStringBuffer {
buffer: Vec<SqlChar>,
actual_length: i16,
}
impl OutputStringBuffer {
pub fn empty() -> Self {
Self::with_buffer_size(0)
}
pub fn with_buffer_size(max_str_len: usize) -> Self {
Self {
buffer: vec![0; max_str_len],
actual_length: 0,
}
}
pub fn mut_buf_ptr(&mut self) -> *mut SqlChar {
mut_buf_ptr(&mut self.buffer)
}
pub fn buf_len(&self) -> i16 {
self.buffer.len().try_into().unwrap()
}
pub fn mut_actual_len_ptr(&mut self) -> *mut i16 {
&mut self.actual_length as *mut i16
}
pub fn to_utf8(&self) -> String {
if self.buffer.is_empty() {
return String::new();
}
if self.is_truncated() {
slice_to_utf8(&self.buffer[0..(self.buffer.len() - 1)]).unwrap()
} else {
let actual_length: usize = self.actual_length.try_into().unwrap();
slice_to_utf8(&self.buffer[0..actual_length]).unwrap()
}
}
pub fn is_truncated(&self) -> bool {
self.actual_length >= self.buffer.len().try_into().unwrap()
}
}