1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
mod types {
    use diesel::sql_types::*;

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

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

    pub trait TextOrNullableText {}

    impl TextOrNullableText for Text {}
    impl TextOrNullableText for Nullable<Text> {}

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

pub mod configuration {
    use crate::RegConfig;

    use diesel::backend::Backend;
    use diesel::deserialize::{self, FromSql, FromSqlRow};
    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, Eq, diesel::expression::AsExpression, FromSqlRow)]
    #[diesel(sql_type = RegConfig)]
    pub struct TsConfiguration(pub u32);

    impl TsConfiguration {
        pub const SIMPLE: Self = Self(3748);
        pub const DANISH: Self = Self(12824);
        pub const DUTCH: Self = Self(12826);
        pub const ENGLISH: Self = Self(12828);
        pub const FINNISH: Self = Self(12830);
        pub const FRENCH: Self = Self(12832);
        pub const GERMAN: Self = Self(12834);
        pub const HUNGARIAN: Self = Self(12836);
        pub const ITALIAN: Self = Self(12838);
        pub const NORWEGIAN: Self = Self(12840);
        pub const PORTUGUESE: Self = Self(12842);
        pub const ROMANIAN: Self = Self(12844);
        pub const RUSSIAN: Self = Self(12846);
        pub const SPANISH: Self = Self(12848);
        pub const SWEDISH: Self = Self(12850);
        pub const TURKISH: Self = Self(12852);
    }

    impl FromSql<RegConfig, Pg> for TsConfiguration
    where
        i32: FromSql<Integer, Pg>,
    {
        fn from_sql(bytes: PgValue) -> deserialize::Result<Self> {
            <i32 as FromSql<Integer, Pg>>::from_sql(bytes).map(|oid| TsConfiguration(oid as u32))
        }
    }

    impl ToSql<RegConfig, Pg> for TsConfiguration
    where
        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())
        }
    }

    #[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)]
mod functions {
    use crate::types::*;
    use diesel::define_sql_function;
    use diesel::sql_types::*;

    define_sql_function!(fn length(x: TsVector) -> Integer);
    define_sql_function!(fn numnode(x: TsQuery) -> Integer);
    define_sql_function!(fn plainto_tsquery(x: Text) -> TsQuery);
    define_sql_function! {
        #[sql_name = "plainto_tsquery"]
        fn plainto_tsquery_with_search_config(config: RegConfig, querytext: Text) -> TsQuery;
    }
    define_sql_function!(fn querytree(x: TsQuery) -> Text);
    define_sql_function!(fn strip(x: TsVector) -> TsVector);
    define_sql_function!(fn to_tsquery(x: Text) -> TsQuery);
    define_sql_function! {
        #[sql_name = "to_tsquery"]
        fn to_tsquery_with_search_config(config: RegConfig, querytext: Text) -> TsQuery;
    }
    define_sql_function!(fn to_tsvector<T: TextOrNullableText + SingleValue>(x: T) -> TsVector);
    define_sql_function! {
        #[sql_name = "to_tsvector"]
        fn to_tsvector_with_search_config<T: TextOrNullableText + SingleValue>(config: RegConfig, document_content: T) -> TsVector;
    }
    define_sql_function!(fn ts_headline(x: Text, y: TsQuery) -> Text);
    define_sql_function! {
        #[sql_name = "ts_headline"]
        fn ts_headline_with_search_config(config: RegConfig, x: Text, y: TsQuery) -> Text;
    }
    define_sql_function!(fn ts_rank(x: TsVector, y: TsQuery) -> Float);
    define_sql_function!(fn ts_rank_cd(x: TsVector, y: TsQuery) -> Float);
    define_sql_function! {
        #[sql_name = "ts_rank_cd"]
        fn ts_rank_cd_weighted(w: Array<Float>, x: TsVector, y: TsQuery) -> Float;
    }
    define_sql_function! {
        #[sql_name = "ts_rank_cd"]
        fn ts_rank_cd_normalized(x: TsVector, y: TsQuery, n: Integer) -> Float;
    }
    define_sql_function! {
        #[sql_name = "ts_rank_cd"]
        fn ts_rank_cd_weighted_normalized(w: Array<Float>, x: TsVector, y: TsQuery, n: Integer) -> Float;
    }
    define_sql_function!(fn phraseto_tsquery(x: Text) -> TsQuery);
    define_sql_function!(fn websearch_to_tsquery(x: Text) -> TsQuery);
    define_sql_function! {
        #[sql_name = "websearch_to_tsquery"]
        fn websearch_to_tsquery_with_search_config(config: RegConfig, x: Text) -> TsQuery;
    }
    define_sql_function!(fn setweight(x: TsVector, w: CChar) -> TsVector);
}

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

    mod predicates {
        use crate::types::*;
        use diesel::pg::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 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> {
            Concat::new(self, other.as_expression())
        }
    }

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

        fn and<T: AsExpression<TsQuery>>(self, other: T) -> And<Self, T::Expression> {
            And::new(self, other.as_expression())
        }

        fn or<T: AsExpression<TsQuery>>(self, other: T) -> Or<Self, T::Expression> {
            Or::new(self, other.as_expression())
        }

        fn contains<T: AsExpression<TsQuery>>(self, other: T) -> Contains<Self, T::Expression> {
            Contains::new(self, other.as_expression())
        }

        fn contained_by<T: AsExpression<TsQuery>>(
            self,
            other: T,
        ) -> ContainedBy<Self, T::Expression> {
            ContainedBy::new(self, other.as_expression())
        }
    }

    impl<T: Expression<SqlType = TsVector>> TsVectorExtensions for T {}

    impl<T: Expression<SqlType = TsQuery>> TsQueryExtensions for T {}
}

pub use self::dsl::*;
pub use self::functions::*;
pub use self::types::*;