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(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
Self: Sized,
fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
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) -> IsNull
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull
sourceยงfn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
Self: Sized,
fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
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, Sqlite> for Text<T>where
T: Display,
impl<'q, T> Encode<'q, Sqlite> for Text<T>where
T: Display,
sourceยงfn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull
sourceยงfn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
Self: Sized,
fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
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,
1.21.0 ยท sourceยงfn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
sourceยงimpl<T> PartialEq for Text<T>where
T: PartialEq,
impl<T> PartialEq for Text<T>where
T: PartialEq,
sourceยงimpl<T> PartialOrd for Text<T>where
T: PartialOrd,
impl<T> PartialOrd for Text<T>where
T: PartialOrd,
1.0.0 ยท sourceยงfn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceยง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
sourceยงimpl<T> Type<Sqlite> for Text<T>
impl<T> Type<Sqlite> for Text<T>
sourceยงfn type_info() -> SqliteTypeInfo
fn type_info() -> SqliteTypeInfo
sourceยงfn compatible(ty: &SqliteTypeInfo) -> bool
fn compatible(ty: &SqliteTypeInfo) -> 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<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.