mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 16:44:07 +00:00
feat: Text adapter (#2894)
This commit is contained in:
@@ -124,12 +124,14 @@
|
||||
//! over a floating-point type in the first place.
|
||||
//!
|
||||
//! Instead, you should only use a type affinity that SQLite will not attempt to convert implicitly,
|
||||
//! such as `TEXT` or `BLOB`, and map values to/from SQLite as strings.
|
||||
//! such as `TEXT` or `BLOB`, and map values to/from SQLite as strings. You can do this easily
|
||||
//! using [the `Text` adapter].
|
||||
//!
|
||||
//!
|
||||
//! [`decimal.c`]: https://www.sqlite.org/floatingpoint.html#the_decimal_c_extension
|
||||
//! [amalgamation]: https://www.sqlite.org/amalgamation.html
|
||||
//! [type-affinity]: https://www.sqlite.org/datatype3.html#type_affinity
|
||||
//!
|
||||
//! [the `Text` adapter]: Text
|
||||
|
||||
pub(crate) use sqlx_core::types::*;
|
||||
|
||||
@@ -142,6 +144,7 @@ mod int;
|
||||
#[cfg(feature = "json")]
|
||||
mod json;
|
||||
mod str;
|
||||
mod text;
|
||||
#[cfg(feature = "time")]
|
||||
mod time;
|
||||
mod uint;
|
||||
|
||||
37
sqlx-sqlite/src/types/text.rs
Normal file
37
sqlx-sqlite/src/types/text.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
|
||||
use sqlx_core::decode::Decode;
|
||||
use sqlx_core::encode::{Encode, IsNull};
|
||||
use sqlx_core::error::BoxDynError;
|
||||
use sqlx_core::types::{Text, Type};
|
||||
use std::fmt::Display;
|
||||
use std::str::FromStr;
|
||||
|
||||
impl<T> Type<Sqlite> for Text<T> {
|
||||
fn type_info() -> SqliteTypeInfo {
|
||||
<String as Type<Sqlite>>::type_info()
|
||||
}
|
||||
|
||||
fn compatible(ty: &SqliteTypeInfo) -> bool {
|
||||
<String as Type<Sqlite>>::compatible(ty)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'q, T> Encode<'q, Sqlite> for Text<T>
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
|
||||
Encode::<Sqlite>::encode(self.0.to_string(), buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r, T> Decode<'r, Sqlite> for Text<T>
|
||||
where
|
||||
T: FromStr,
|
||||
BoxDynError: From<<T as FromStr>::Err>,
|
||||
{
|
||||
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
|
||||
let s: &str = Decode::<Sqlite>::decode(value)?;
|
||||
Ok(Self(s.parse()?))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user