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: TImplementations§
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<'r, T> Decode<'r, Postgres> for Text<T>where
T: FromStr,
Box<dyn Error + Sync + Send + 'static, Global>: From<<T as FromStr>::Err>,
impl<'r, T> Decode<'r, Postgres> for Text<T>where T: FromStr, Box<dyn Error + Sync + Send + 'static, Global>: From<<T as FromStr>::Err>,
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<T> Ord for Text<T>where
T: Ord,
impl<T> Ord for Text<T>where T: Ord,
source§impl<T> PartialEq<Text<T>> for Text<T>where
T: PartialEq<T>,
impl<T> PartialEq<Text<T>> for Text<T>where T: PartialEq<T>,
source§impl<T> PartialOrd<Text<T>> for Text<T>where
T: PartialOrd<T>,
impl<T> PartialOrd<Text<T>> for Text<T>where T: PartialOrd<T>,
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<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> StructuralEq for Text<T>
impl<T> StructuralPartialEq for Text<T>
Auto Trait Implementations§
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> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.