pub struct Text<T>(pub T);
Expand description
Map a SQL text value to/from a Rust type using Display
and FromStr
.
This can be useful for types that do not have a direct SQL equivalent, or are simply not supported by SQLx for one reason or another.
For strongly typed databases like Postgres, this will report the value’s type as TEXT
.
Explicit conversion may be necessary on the SQL side depending on the desired type.
§Panics
You should only use this adapter with Display
implementations that are infallible,
otherwise you may encounter panics when attempting to bind a value.
This is because the design of the Encode
trait assumes encoding is infallible, so there is no
way to bubble up the error.
Fortunately, most Display
implementations are infallible by convention anyway
(the standard ToString
trait also assumes this), but you may still want to audit
the source code for any types you intend to use with this adapter, just to be safe.
§Example: SocketAddr
MySQL and SQLite do not have a native SQL equivalent for SocketAddr
, so if you want to
store and retrieve instances of it, it makes sense to map it to TEXT
:
use std::net::SocketAddr;
use sqlx::Connection;
use sqlx::mysql::MySqlConnection;
use sqlx::types::Text;
use uuid::Uuid;
use time::OffsetDateTime;
#[derive(sqlx::FromRow, Debug)]
struct Login {
user_id: Uuid,
socket_addr: Text<SocketAddr>,
login_at: OffsetDateTime
}
let mut conn: MySqlConnection = MySqlConnection::connect("<DATABASE URL>").await?;
let user_id: Uuid = "e9a72cdc-d907-48d6-a488-c64a91fd063c".parse().unwrap();
let socket_addr: SocketAddr = "198.51.100.47:31790".parse().unwrap();
// CREATE TABLE user_login(user_id VARCHAR(36), socket_addr TEXT, login_at TIMESTAMP);
sqlx::query("INSERT INTO user_login(user_id, socket_addr, login_at) VALUES (?, ?, NOW())")
.bind(user_id)
.bind(Text(socket_addr))
.execute(&mut conn)
.await?;
let logins: Vec<Login> = sqlx::query_as("SELECT * FROM user_login")
.fetch_all(&mut conn)
.await?;
println!("Logins for user ID {user_id}: {logins:?}");
Tuple Fields§
§0: T
Implementations§
source§impl<T> Text<T>
impl<T> Text<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Extract the inner value.
Trait Implementations§
source§impl<'q, T> Encode<'q, MySql> for Text<T>where
T: Display,
impl<'q, T> Encode<'q, MySql> for Text<T>where
T: Display,
source§fn encode_by_ref(
&self,
buf: &mut Vec<u8>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn encode_by_ref( &self, buf: &mut Vec<u8>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
source§fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
self
into buf
in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
source§impl<'q, T> Encode<'q, Postgres> for Text<T>where
T: Display,
impl<'q, T> Encode<'q, Postgres> for Text<T>where
T: Display,
source§fn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
source§fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
self
into buf
in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
source§impl<T> Ord for Text<T>where
T: Ord,
impl<T> Ord for Text<T>where
T: Ord,
source§impl<T> PartialOrd for Text<T>where
T: PartialOrd,
impl<T> PartialOrd for Text<T>where
T: PartialOrd,
source§impl<T> PgHasArrayType for Text<T>
impl<T> PgHasArrayType for Text<T>
fn array_type_info() -> PgTypeInfo
fn array_compatible(ty: &PgTypeInfo) -> bool
source§impl<T> Type<MySql> for Text<T>
impl<T> Type<MySql> for Text<T>
source§fn type_info() -> MySqlTypeInfo
fn type_info() -> MySqlTypeInfo
source§fn compatible(ty: &MySqlTypeInfo) -> bool
fn compatible(ty: &MySqlTypeInfo) -> bool
source§impl<T> Type<Postgres> for Text<T>
impl<T> Type<Postgres> for Text<T>
source§fn type_info() -> PgTypeInfo
fn type_info() -> PgTypeInfo
source§fn compatible(ty: &PgTypeInfo) -> bool
fn compatible(ty: &PgTypeInfo) -> bool
impl<T> Copy for Text<T>where
T: Copy,
impl<T> Eq for Text<T>where
T: Eq,
impl<T> StructuralPartialEq for Text<T>
Auto Trait Implementations§
impl<T> Freeze for Text<T>where
T: Freeze,
impl<T> RefUnwindSafe for Text<T>where
T: RefUnwindSafe,
impl<T> Send for Text<T>where
T: Send,
impl<T> Sync for Text<T>where
T: Sync,
impl<T> Unpin for Text<T>where
T: Unpin,
impl<T> UnwindSafe for Text<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more