Skip to content

Commit

Permalink
WIP: Fix Build Issues, Add Tests For TsVector #729,#2705
Browse files Browse the repository at this point in the history
  • Loading branch information
anshap1719 committed Feb 1, 2024
1 parent d4dc2c4 commit ffe29a8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
16 changes: 8 additions & 8 deletions sqlx-postgres/src/types/ts_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ impl TryFrom<u8> for Operators {
}

#[derive(Clone, Debug)]
struct Operator {
pub struct Operator {
operator: Operators,
distance: Option<u16>,
}

#[derive(Clone, Debug)]
struct Value {
pub struct Value {
weight: u8,
text: String,
prefix: u8,
Expand Down Expand Up @@ -139,15 +139,15 @@ impl TryFrom<&[u8]> for TsQuery {
}
}

impl<'bytes> TryInto<&'bytes [u8]> for &TsQuery {
impl TryInto<Vec<u8>> for &TsQuery {
type Error = BoxDynError;

fn try_into(self) -> Result<&'bytes [u8], Self::Error> {
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
let buf: &mut Vec<u8> = &mut vec![];

buf.write_u32::<BigEndian>(u32::try_from(self.entries.len())?)?;

for entry in self.entries {
for entry in &self.entries {
match entry {
Entry::Operator(operator) => {
buf.write_u8(EntryType::Operator.into())?;
Expand All @@ -171,7 +171,7 @@ impl<'bytes> TryInto<&'bytes [u8]> for &TsQuery {

buf.flush()?;

Ok(&buf)
Ok(buf.to_vec())
}
}

Expand All @@ -189,8 +189,8 @@ impl PgHasArrayType for TsQuery {

impl Encode<'_, Postgres> for TsQuery {
fn encode_by_ref(&self, buf: &mut <Postgres as HasArguments<'_>>::ArgumentBuffer) -> IsNull {
if let Ok(encoded_ts_vector) = self.try_into() {
buf.extend_from_slice(encoded_ts_vector);
if let Ok(encoded_ts_query) = <&TsQuery as TryInto<Vec<u8>>>::try_into(self) {
buf.extend_from_slice(encoded_ts_query.as_slice());

IsNull::No
} else {
Expand Down
14 changes: 7 additions & 7 deletions sqlx-postgres/src/types/ts_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ impl TryFrom<&[u8]> for TsVector {
}
}

impl<'bytes> TryInto<&'bytes [u8]> for &TsVector {
impl TryInto<Vec<u8>> for &TsVector {
type Error = BoxDynError;

fn try_into(self) -> Result<&'bytes [u8], Self::Error> {
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
let buf: &mut Vec<u8> = &mut vec![];

buf.write_u32::<BigEndian>(u32::try_from(self.words.len())?)?;
Expand All @@ -99,15 +99,15 @@ impl<'bytes> TryInto<&'bytes [u8]> for &TsVector {
buf.write_u16::<BigEndian>(u16::try_from(lexeme.positions.len())?)?;

if !lexeme.positions.is_empty() {
for position in lexeme.positions {
buf.write_u16::<BigEndian>(position)?;
for position in &lexeme.positions {
buf.write_u16::<BigEndian>(*position)?;
}
}
}

buf.flush()?;

Ok(&buf)
Ok(buf.to_vec())
}
}

Expand Down Expand Up @@ -150,8 +150,8 @@ impl PgHasArrayType for TsVector {

impl Encode<'_, Postgres> for TsVector {
fn encode_by_ref(&self, buf: &mut <Postgres as HasArguments<'_>>::ArgumentBuffer) -> IsNull {
if let Ok(encoded_ts_vector) = self.try_into() {
buf.extend_from_slice(encoded_ts_vector);
if let Ok(encoded_ts_vector) = <&TsVector as TryInto<Vec<u8>>>::try_into(self) {
buf.extend_from_slice(encoded_ts_vector.as_slice());

IsNull::No
} else {
Expand Down
33 changes: 33 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,39 @@ mod json {
}
}

#[cfg(feature = "full_text_search")]
mod full_text_search {
use super::*;
use sqlx::postgres::types::TsVector;
use sqlx::postgres::PgRow;
use sqlx::{Executor, Row};
use sqlx_test::new;

#[sqlx_macros::test]
async fn test_ts_vector() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

// unprepared, text API
let row: PgRow = conn
.fetch_one("SELECT to_tsvector('english', 'A quick brown fox')")
.await?;
let value: TsVector = row.try_get(0)?;

assert_eq!(value.to_string(), "'brown':3 'fox':4 'quick':2");

// prepared, binary API
let row: PgRow = conn
.fetch_one(sqlx::query("SELECT to_tsvector('A quick brown fox')"))
.await?;

let value: TsVector = row.try_get(0)?;

assert_eq!(value.to_string(), "'brown':3 'fox':4 'quick':2");

Ok(())
}
}

#[cfg(feature = "bigdecimal")]
test_type!(bigdecimal<sqlx::types::BigDecimal>(Postgres,

Expand Down

0 comments on commit ffe29a8

Please sign in to comment.