Skip to content

Commit

Permalink
Merge pull request #36 from mohe2015/setweight
Browse files Browse the repository at this point in the history
Add setweight and allow selecting a text search configuration by name
  • Loading branch information
weiznich committed May 26, 2023
2 parents fa59985 + df330c5 commit d031655
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "diesel_full_text_search"
version = "2.0.0"
version = "2.1.0"
description = "Adds support for PostgreSQL full text search to Diesel"
license = "MIT"
repository = "https://github.com/diesel-rs/diesel_full_text_search"

[dependencies]
diesel = { version = "2.0.0", features = ["postgres_backend"], default-features = false }
diesel = { version = "~2.1.0", features = ["postgres_backend"], default-features = false }

[features]
default = ["with-diesel-postgres"]
Expand Down
47 changes: 41 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ mod types {
pub mod configuration {
use crate::RegConfig;

use diesel::backend::RawValue;
use diesel::backend::Backend;
use diesel::deserialize::{self, FromSql};
use diesel::pg::Pg;
use diesel::expression::{is_aggregate, ValidGrouping};
use diesel::pg::{Pg, PgValue};
use diesel::query_builder::{AstPass, QueryFragment, QueryId};
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::Integer;
use diesel::{AppearsOnTable, Expression, QueryResult, SelectableExpression};

#[derive(Debug, PartialEq, diesel::expression::AsExpression)]
#[derive(Debug, PartialEq, Eq, diesel::expression::AsExpression)]
#[diesel(sql_type = RegConfig)]
pub struct TsConfiguration(pub u32);

Expand All @@ -59,7 +62,7 @@ pub mod configuration {
where
i32: FromSql<Integer, Pg>,
{
fn from_sql(bytes: RawValue<'_, Pg>) -> deserialize::Result<Self> {
fn from_sql(bytes: PgValue) -> deserialize::Result<Self> {
<i32 as FromSql<Integer, Pg>>::from_sql(bytes).map(|oid| TsConfiguration(oid as u32))
}
}
Expand All @@ -69,9 +72,40 @@ pub mod configuration {
i32: ToSql<Integer, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
<i32 as ToSql<Integer, Pg>>::to_sql(&(*&self.0 as i32), &mut out.reborrow())
<i32 as ToSql<Integer, Pg>>::to_sql(&(self.0 as i32), &mut out.reborrow())
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TsConfigurationByName(pub &'static str);

impl<DB> QueryFragment<DB> for TsConfigurationByName
where
DB: Backend,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
out.push_sql(&format!("'{}'", &self.0));
Ok(())
}
}

impl<GB> ValidGrouping<GB> for TsConfigurationByName {
type IsAggregate = is_aggregate::Never;
}

impl QueryId for TsConfigurationByName {
const HAS_STATIC_QUERY_ID: bool = false;

type QueryId = ();
}

impl<QS> SelectableExpression<QS> for TsConfigurationByName where Self: Expression {}

impl<QS> AppearsOnTable<QS> for TsConfigurationByName where Self: Expression {}

impl Expression for TsConfigurationByName {
type SqlType = RegConfig;
}
}

#[allow(deprecated)]
Expand All @@ -98,7 +132,7 @@ mod functions {
sql_function! {
#[sql_name = "ts_headline"]
fn ts_headline_with_search_config(config: RegConfig, x: Text, y: TsQuery) -> Text;
}
}
sql_function!(fn ts_rank(x: TsVector, y: TsQuery) -> Float);
sql_function!(fn ts_rank_cd(x: TsVector, y: TsQuery) -> Float);
sql_function! {
Expand All @@ -119,6 +153,7 @@ mod functions {
#[sql_name = "websearch_to_tsquery"]
fn websearch_to_tsquery_with_search_config(config: RegConfig, x: Text) -> TsQuery;
}
sql_function!(fn setweight(x: TsVector, w: CChar) -> TsVector);
}

mod dsl {
Expand Down

0 comments on commit d031655

Please sign in to comment.