Skip to content

Commit

Permalink
Merge pull request #29 from Emulator000/diesel-new
Browse files Browse the repository at this point in the history
Update to Diesel 2.0.0-rc.0
  • Loading branch information
weiznich authored Jul 5, 2022
2 parents 886fe85 + c6f9261 commit 37224cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "diesel_full_text_search"
version = "1.1.0"
version = "2.0.0"
authors = ["Sean Griffin <[email protected]>"]
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 = "1.3.0", features = ["postgres"] }
diesel = { version = "2.0.0-rc.0", features = ["postgres"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# diesel_full_text_search

[![Crate](https://meritbadge.herokuapp.com/diesel_full_text_search)](https://crates.io/crates/diesel_full_text_search)
![crates.io](https://img.shields.io/crates/v/diesel_full_text_search.svg)
[![docs](https://docs.rs/diesel_full_text_search/badge.svg)](https://docs.rs/diesel_full_text_search)

Add support for [Postgres full text search](https://www.postgresql.org/docs/current/textsearch.html)
Expand Down
61 changes: 27 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ mod types {
use diesel::SqlType;

#[derive(Clone, Copy, SqlType)]
#[postgres(oid = "3615", array_oid = "3645")]
#[diesel(postgres_type(oid = 3615, array_oid = 3645))]
pub struct TsQuery;

#[derive(Clone, Copy, SqlType)]
#[postgres(oid = "3614", array_oid = "3643")]
#[diesel(postgres_type(oid = 3614, array_oid = 3643))]
pub struct TsVector;
pub type Tsvector = TsVector;

Expand All @@ -22,23 +22,21 @@ mod types {
impl TextOrNullableText for Nullable<Text> {}

#[derive(SqlType)]
#[postgres(type_name = "regconfig")]
pub struct Regconfig;
#[diesel(postgres_type(name = "regconfig"))]
pub struct RegConfig;
}

pub mod configuration {
use crate::Regconfig;
use crate::RegConfig;

use std::io::Write;

use diesel::backend::Backend;
use diesel::backend::RawValue;
use diesel::deserialize::{self, FromSql};
use diesel::serialize::{self, Output};
use diesel::pg::Pg;
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::Integer;
use diesel::types::ToSql;

#[derive(Debug, PartialEq, AsExpression)]
#[sql_type = "Regconfig"]
#[diesel(sql_type = RegConfig)]
pub struct TsConfiguration(pub u32);

impl TsConfiguration {
Expand All @@ -60,23 +58,21 @@ pub mod configuration {
pub const TURKISH: Self = Self(12852);
}

impl<DB> FromSql<Regconfig, DB> for TsConfiguration
impl FromSql<RegConfig, Pg> for TsConfiguration
where
DB: Backend,
i32: FromSql<Integer, DB>,
i32: FromSql<Integer, Pg>,
{
fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
<i32 as FromSql<Integer, DB>>::from_sql(bytes).map(|oid| TsConfiguration(oid as u32))
fn from_sql(bytes: RawValue<'_, Pg>) -> deserialize::Result<Self> {
i32::from_sql(bytes).map(|oid| TsConfiguration(oid as u32))
}
}

impl<DB> ToSql<Regconfig, DB> for TsConfiguration
impl ToSql<RegConfig, Pg> for TsConfiguration
where
DB: Backend,
i32: ToSql<Integer, DB>,
i32: ToSql<Integer, Pg>,
{
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
<i32 as ToSql<Integer, DB>>::to_sql(&(*&self.0 as i32), out)
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())
}
}
}
Expand All @@ -94,12 +90,12 @@ mod functions {
sql_function!(fn to_tsquery(x: Text) -> TsQuery);
sql_function! {
#[sql_name = "to_tsquery"]
fn to_tsquery_with_search_config(config: Regconfig, querytext: Text) -> TsQuery;
fn to_tsquery_with_search_config(config: RegConfig, querytext: Text) -> TsQuery;
}
sql_function!(fn to_tsvector<T: TextOrNullableText>(x: T) -> TsVector);
sql_function!(fn to_tsvector<T: TextOrNullableText + SingleValue>(x: T) -> TsVector);
sql_function! {
#[sql_name = "to_tsvector"]
fn to_tsvector_with_search_config<T: TextOrNullableText>(config: Regconfig, document_content: T) -> TsVector;
fn to_tsvector_with_search_config<T: TextOrNullableText + SingleValue>(config: RegConfig, document_content: T) -> TsVector;
}
sql_function!(fn ts_headline(x: Text, y: TsQuery) -> Text);
sql_function!(fn ts_rank(x: TsVector, y: TsQuery) -> Float);
Expand All @@ -113,33 +109,30 @@ mod functions {
}

mod dsl {
use diesel::expression::grouped::Grouped;
use diesel::expression::{AsExpression, Expression};
use types::*;

mod predicates {
use diesel::pg::Pg;
use types::*;

diesel_infix_operator!(Matches, " @@ ", backend: Pg);
diesel_infix_operator!(Concat, " || ", TsVector, backend: Pg);
diesel_infix_operator!(And, " && ", TsQuery, backend: Pg);
diesel_infix_operator!(Or, " || ", TsQuery, backend: Pg);
diesel_infix_operator!(Contains, " @> ", backend: Pg);
diesel_infix_operator!(ContainedBy, " <@ ", backend: Pg);
diesel::infix_operator!(Matches, " @@ ", backend: Pg);
diesel::infix_operator!(Concat, " || ", TsVector, backend: Pg);
diesel::infix_operator!(And, " && ", TsQuery, backend: Pg);
diesel::infix_operator!(Or, " || ", TsQuery, backend: Pg);
diesel::infix_operator!(Contains, " @> ", backend: Pg);
diesel::infix_operator!(ContainedBy, " <@ ", backend: Pg);
}

use self::predicates::*;

pub type Concat<T, U> = Grouped<predicates::Concat<T, U>>;

pub trait TsVectorExtensions: Expression<SqlType = TsVector> + Sized {
fn matches<T: AsExpression<TsQuery>>(self, other: T) -> Matches<Self, T::Expression> {
Matches::new(self, other.as_expression())
}

fn concat<T: AsExpression<TsVector>>(self, other: T) -> Concat<Self, T::Expression> {
Grouped(predicates::Concat::new(self, other.as_expression()))
Concat::new(self, other.as_expression())
}
}

Expand Down

0 comments on commit 37224cd

Please sign in to comment.