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
/*
Portions Copyright 2019-2021 ZomboDB, LLC.
Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>

All rights reserved.

Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use proc_macro2::Ident;
use quote::quote;

pub fn eq(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_eq", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_operator(immutable, parallel_safe)]
        #[opname(=)]
        #[negator(<>)]
        #[restrict(eqsel)]
        #[join(eqjoinsel)]
        #[merges]
        #[hashes]
        fn #pg_name(left: #type_name, right: #type_name) -> bool {
            left == right
        }
    }
}

pub fn ne(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_ne", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_operator(immutable, parallel_safe)]
        #[opname(<>)]
        #[negator(=)]
        #[restrict(neqsel)]
        #[join(neqjoinsel)]
        fn #pg_name(left: #type_name, right: #type_name) -> bool {
            left != right
        }
    }
}

pub fn lt(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_lt", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_operator(immutable, parallel_safe)]
        #[opname(<)]
        #[negator(>=)]
        #[commutator(>)]
        #[restrict(scalarltsel)]
        #[join(scalarltjoinsel)]
        fn #pg_name(left: #type_name, right: #type_name) -> bool {
            left < right
        }

    }
}

pub fn gt(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_gt", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_operator(immutable, parallel_safe)]
        #[opname(>)]
        #[negator(<=)]
        #[commutator(<)]
        #[restrict(scalargtsel)]
        #[join(scalargtjoinsel)]
        fn #pg_name(left: #type_name, right: #type_name) -> bool {
            left > right
        }
    }
}

pub fn le(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_le", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_operator(immutable, parallel_safe)]
        #[opname(<=)]
        #[negator(>)]
        #[commutator(>=)]
        #[restrict(scalarlesel)]
        #[join(scalarlejoinsel)]
        fn #pg_name(left: #type_name, right: #type_name) -> bool {
            left <= right
        }
    }
}

pub fn ge(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_ge", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_operator(immutable, parallel_safe)]
        #[opname(>=)]
        #[negator(<)]
        #[commutator(<=)]
        #[restrict(scalargesel)]
        #[join(scalargejoinsel)]
        fn #pg_name(left: #type_name, right: #type_name) -> bool {
            left >= right
        }
    }
}

pub fn cmp(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_cmp", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_extern(immutable, parallel_safe)]
        fn #pg_name(left: #type_name, right: #type_name) -> i32 {
            left.cmp(&right) as i32
        }
    }
}

pub fn hash(type_name: &Ident) -> proc_macro2::TokenStream {
    let pg_name = Ident::new(
        &format!("{}_hash", type_name).to_lowercase(),
        type_name.span(),
    );
    quote! {
        #[allow(non_snake_case)]
        #[pg_extern(immutable, parallel_safe)]
        fn #pg_name(value: #type_name) -> i32 {
            pgx::misc::pgx_seahash(&value) as i32
        }
    }
}