Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sqlx-sqlite/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,6 @@ mod text;
#[cfg(feature = "time")]
mod time;
mod uint;
mod url;
#[cfg(feature = "uuid")]
mod uuid;
35 changes: 35 additions & 0 deletions sqlx-sqlite/src/types/url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::arguments::SqliteArgumentsBuffer;
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::type_info::DataType;
use crate::types::Type;
use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
use std::sync::Arc;
use url::Url;

impl Type<Sqlite> for Url {
fn type_info() -> SqliteTypeInfo {
SqliteTypeInfo(DataType::Text)
}

fn compatible(ty: &SqliteTypeInfo) -> bool {
matches!(ty.0, DataType::Text)
}
}

impl Encode<'_, Sqlite> for Url {
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Text(Arc::new(
self.as_str().to_string(),
)));

Ok(IsNull::No)
}
}

impl Decode<'_, Sqlite> for Url {
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
Url::parse(value.text_borrowed()?).map_err(Into::into)
}
}
3 changes: 3 additions & 0 deletions tests/sqlite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::borrow::Cow;
use std::net::SocketAddr;
use std::rc::Rc;
use std::sync::Arc;
use url::Url;

test_type!(null<Option<i32>>(Sqlite,
"NULL" == None::<i32>
Expand Down Expand Up @@ -218,6 +219,8 @@ test_type!(test_cow_slice<Cow<'_, [u8]>>(Sqlite, "X'01020304'" == Cow::<'static,
test_type!(test_arc_slice<Arc<[u8]>>(Sqlite, "X'01020304'" == Arc::<[u8]>::from([1,2,3,4])));
test_type!(test_rc_slice<Rc<[u8]>>(Sqlite, "X'01020304'" == Rc::<[u8]>::from([1,2,3,4])));

test_type!(test_url<Url>(Sqlite, "'https://example.com/'" == Url::parse("https://example.com/").unwrap()));

#[sqlx_macros::test]
async fn test_text_adapter() -> anyhow::Result<()> {
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)]
Expand Down