Skip to content

Commit 89130fc

Browse files
committed
refactor: Move Encode and Decode implementations to types module
This commit refactors the ODBC module by moving the Encode and Decode implementations for various data types (i32, i64, f32, f64, String, etc.) to a new types module. This change simplifies the structure of the ODBC module and enhances maintainability. The previous implementations in the arguments and value files have been removed, and the necessary imports have been adjusted accordingly.
1 parent 4bc1bcd commit 89130fc

File tree

15 files changed

+951
-967
lines changed

15 files changed

+951
-967
lines changed

sqlx-core/src/odbc/arguments.rs

Lines changed: 1 addition & 364 deletions
Original file line numberDiff line numberDiff line change
@@ -34,370 +34,7 @@ impl<'q> Arguments<'q> for OdbcArguments<'q> {
3434
}
3535
}
3636

37-
impl<'q> Encode<'q, Odbc> for i32 {
38-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
39-
buf.push(OdbcArgumentValue::Int(self as i64));
40-
crate::encode::IsNull::No
41-
}
42-
43-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
44-
buf.push(OdbcArgumentValue::Int(*self as i64));
45-
crate::encode::IsNull::No
46-
}
47-
}
48-
49-
impl<'q> Encode<'q, Odbc> for i64 {
50-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
51-
buf.push(OdbcArgumentValue::Int(self));
52-
crate::encode::IsNull::No
53-
}
54-
55-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
56-
buf.push(OdbcArgumentValue::Int(*self));
57-
crate::encode::IsNull::No
58-
}
59-
}
60-
61-
impl<'q> Encode<'q, Odbc> for f32 {
62-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
63-
buf.push(OdbcArgumentValue::Float(self as f64));
64-
crate::encode::IsNull::No
65-
}
66-
67-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
68-
buf.push(OdbcArgumentValue::Float(*self as f64));
69-
crate::encode::IsNull::No
70-
}
71-
}
72-
73-
impl<'q> Encode<'q, Odbc> for f64 {
74-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
75-
buf.push(OdbcArgumentValue::Float(self));
76-
crate::encode::IsNull::No
77-
}
78-
79-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
80-
buf.push(OdbcArgumentValue::Float(*self));
81-
crate::encode::IsNull::No
82-
}
83-
}
84-
85-
impl<'q> Encode<'q, Odbc> for String {
86-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
87-
buf.push(OdbcArgumentValue::Text(self));
88-
crate::encode::IsNull::No
89-
}
90-
91-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
92-
buf.push(OdbcArgumentValue::Text(self.clone()));
93-
crate::encode::IsNull::No
94-
}
95-
}
96-
97-
impl<'q> Encode<'q, Odbc> for &'q str {
98-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
99-
buf.push(OdbcArgumentValue::Text(self.to_owned()));
100-
crate::encode::IsNull::No
101-
}
102-
103-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
104-
buf.push(OdbcArgumentValue::Text((*self).to_owned()));
105-
crate::encode::IsNull::No
106-
}
107-
}
108-
109-
impl<'q> Encode<'q, Odbc> for Vec<u8> {
110-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
111-
buf.push(OdbcArgumentValue::Bytes(self));
112-
crate::encode::IsNull::No
113-
}
114-
115-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
116-
buf.push(OdbcArgumentValue::Bytes(self.clone()));
117-
crate::encode::IsNull::No
118-
}
119-
}
120-
121-
impl<'q> Encode<'q, Odbc> for &'q [u8] {
122-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
123-
buf.push(OdbcArgumentValue::Bytes(self.to_vec()));
124-
crate::encode::IsNull::No
125-
}
126-
127-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
128-
buf.push(OdbcArgumentValue::Bytes(self.to_vec()));
129-
crate::encode::IsNull::No
130-
}
131-
}
132-
133-
impl<'q> Encode<'q, Odbc> for i16 {
134-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
135-
buf.push(OdbcArgumentValue::Int(self as i64));
136-
crate::encode::IsNull::No
137-
}
138-
139-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
140-
buf.push(OdbcArgumentValue::Int(*self as i64));
141-
crate::encode::IsNull::No
142-
}
143-
}
144-
145-
impl<'q> Encode<'q, Odbc> for i8 {
146-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
147-
buf.push(OdbcArgumentValue::Int(self as i64));
148-
crate::encode::IsNull::No
149-
}
150-
151-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
152-
buf.push(OdbcArgumentValue::Int(*self as i64));
153-
crate::encode::IsNull::No
154-
}
155-
}
156-
157-
impl<'q> Encode<'q, Odbc> for u8 {
158-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
159-
buf.push(OdbcArgumentValue::Int(self as i64));
160-
crate::encode::IsNull::No
161-
}
162-
163-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
164-
buf.push(OdbcArgumentValue::Int(*self as i64));
165-
crate::encode::IsNull::No
166-
}
167-
}
168-
169-
impl<'q> Encode<'q, Odbc> for u16 {
170-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
171-
buf.push(OdbcArgumentValue::Int(self as i64));
172-
crate::encode::IsNull::No
173-
}
174-
175-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
176-
buf.push(OdbcArgumentValue::Int(*self as i64));
177-
crate::encode::IsNull::No
178-
}
179-
}
180-
181-
impl<'q> Encode<'q, Odbc> for u32 {
182-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
183-
buf.push(OdbcArgumentValue::Int(self as i64));
184-
crate::encode::IsNull::No
185-
}
186-
187-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
188-
buf.push(OdbcArgumentValue::Int(*self as i64));
189-
crate::encode::IsNull::No
190-
}
191-
}
192-
193-
impl<'q> Encode<'q, Odbc> for u64 {
194-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
195-
match i64::try_from(self) {
196-
Ok(value) => {
197-
buf.push(OdbcArgumentValue::Int(value));
198-
crate::encode::IsNull::No
199-
}
200-
Err(_) => {
201-
log::warn!("u64 value {} too large for ODBC, encoding as NULL", self);
202-
buf.push(OdbcArgumentValue::Null);
203-
crate::encode::IsNull::Yes
204-
}
205-
}
206-
}
207-
208-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
209-
match i64::try_from(*self) {
210-
Ok(value) => {
211-
buf.push(OdbcArgumentValue::Int(value));
212-
crate::encode::IsNull::No
213-
}
214-
Err(_) => {
215-
log::warn!("u64 value {} too large for ODBC, encoding as NULL", self);
216-
buf.push(OdbcArgumentValue::Null);
217-
crate::encode::IsNull::Yes
218-
}
219-
}
220-
}
221-
}
222-
223-
impl<'q> Encode<'q, Odbc> for bool {
224-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
225-
buf.push(OdbcArgumentValue::Int(if self { 1 } else { 0 }));
226-
crate::encode::IsNull::No
227-
}
228-
229-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
230-
buf.push(OdbcArgumentValue::Int(if *self { 1 } else { 0 }));
231-
crate::encode::IsNull::No
232-
}
233-
}
234-
235-
// Feature-gated Encode implementations
236-
#[cfg(feature = "chrono")]
237-
mod chrono_encode {
238-
use super::*;
239-
use chrono::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc};
240-
241-
impl<'q> Encode<'q, Odbc> for NaiveDate {
242-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
243-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d").to_string()));
244-
crate::encode::IsNull::No
245-
}
246-
247-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
248-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d").to_string()));
249-
crate::encode::IsNull::No
250-
}
251-
}
252-
253-
impl<'q> Encode<'q, Odbc> for NaiveTime {
254-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
255-
buf.push(OdbcArgumentValue::Text(self.format("%H:%M:%S").to_string()));
256-
crate::encode::IsNull::No
257-
}
258-
259-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
260-
buf.push(OdbcArgumentValue::Text(self.format("%H:%M:%S").to_string()));
261-
crate::encode::IsNull::No
262-
}
263-
}
264-
265-
impl<'q> Encode<'q, Odbc> for NaiveDateTime {
266-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
267-
buf.push(OdbcArgumentValue::Text(
268-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
269-
));
270-
crate::encode::IsNull::No
271-
}
272-
273-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
274-
buf.push(OdbcArgumentValue::Text(
275-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
276-
));
277-
crate::encode::IsNull::No
278-
}
279-
}
280-
281-
impl<'q> Encode<'q, Odbc> for DateTime<Utc> {
282-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
283-
buf.push(OdbcArgumentValue::Text(
284-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
285-
));
286-
crate::encode::IsNull::No
287-
}
288-
289-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
290-
buf.push(OdbcArgumentValue::Text(
291-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
292-
));
293-
crate::encode::IsNull::No
294-
}
295-
}
296-
297-
impl<'q> Encode<'q, Odbc> for DateTime<FixedOffset> {
298-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
299-
buf.push(OdbcArgumentValue::Text(
300-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
301-
));
302-
crate::encode::IsNull::No
303-
}
304-
305-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
306-
buf.push(OdbcArgumentValue::Text(
307-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
308-
));
309-
crate::encode::IsNull::No
310-
}
311-
}
312-
313-
impl<'q> Encode<'q, Odbc> for DateTime<Local> {
314-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
315-
buf.push(OdbcArgumentValue::Text(
316-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
317-
));
318-
crate::encode::IsNull::No
319-
}
320-
321-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
322-
buf.push(OdbcArgumentValue::Text(
323-
self.format("%Y-%m-%d %H:%M:%S").to_string(),
324-
));
325-
crate::encode::IsNull::No
326-
}
327-
}
328-
}
329-
330-
#[cfg(feature = "json")]
331-
mod json_encode {
332-
use super::*;
333-
use serde_json::Value;
334-
335-
impl<'q> Encode<'q, Odbc> for Value {
336-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
337-
buf.push(OdbcArgumentValue::Text(self.to_string()));
338-
crate::encode::IsNull::No
339-
}
340-
341-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
342-
buf.push(OdbcArgumentValue::Text(self.to_string()));
343-
crate::encode::IsNull::No
344-
}
345-
}
346-
}
347-
348-
#[cfg(feature = "bigdecimal")]
349-
mod bigdecimal_encode {
350-
use super::*;
351-
use bigdecimal::BigDecimal;
352-
353-
impl<'q> Encode<'q, Odbc> for BigDecimal {
354-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
355-
buf.push(OdbcArgumentValue::Text(self.to_string()));
356-
crate::encode::IsNull::No
357-
}
358-
359-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
360-
buf.push(OdbcArgumentValue::Text(self.to_string()));
361-
crate::encode::IsNull::No
362-
}
363-
}
364-
}
365-
366-
#[cfg(feature = "decimal")]
367-
mod decimal_encode {
368-
use super::*;
369-
use rust_decimal::Decimal;
370-
371-
impl<'q> Encode<'q, Odbc> for Decimal {
372-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
373-
buf.push(OdbcArgumentValue::Text(self.to_string()));
374-
crate::encode::IsNull::No
375-
}
376-
377-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
378-
buf.push(OdbcArgumentValue::Text(self.to_string()));
379-
crate::encode::IsNull::No
380-
}
381-
}
382-
}
383-
384-
#[cfg(feature = "uuid")]
385-
mod uuid_encode {
386-
use super::*;
387-
use uuid::Uuid;
388-
389-
impl<'q> Encode<'q, Odbc> for Uuid {
390-
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
391-
buf.push(OdbcArgumentValue::Text(self.to_string()));
392-
crate::encode::IsNull::No
393-
}
394-
395-
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
396-
buf.push(OdbcArgumentValue::Text(self.to_string()));
397-
crate::encode::IsNull::No
398-
}
399-
}
400-
}
37+
// Encode implementations are now in the types module
40138

40239
impl<'q, T> Encode<'q, Odbc> for Option<T>
40340
where

sqlx-core/src/odbc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ mod query_result;
1212
mod row;
1313
mod statement;
1414
mod transaction;
15-
mod r#type;
1615
mod type_info;
16+
pub mod types;
1717
mod value;
1818

1919
pub use arguments::{OdbcArgumentValue, OdbcArguments};

0 commit comments

Comments
 (0)