File: ip4r.sql.in

package info (click to toggle)
ip4r 1.03-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 140 kB
  • ctags: 232
  • sloc: ansic: 1,411; makefile: 51
file content (893 lines) | stat: -rw-r--r-- 19,216 bytes parent folder | download
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
-- Create the user-defined types for IP4 and IP4 intervals (ip4 ip4r)
-- 

-- Adjust this setting to control where the objects get created.
SET search_path = public;

BEGIN;
-- type creation is needlessly chatty
SET LOCAL client_min_messages = warning;

CREATE OR REPLACE FUNCTION ip4_in(cstring)
RETURNS ip4
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_out(ip4)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_recv(internal)
RETURNS ip4
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_send(ip4)
RETURNS bytea
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE TYPE ip4 (
	INTERNALLENGTH = 4,
	INPUT = ip4_in,
	OUTPUT = ip4_out,
	RECEIVE = ip4_recv,
	SEND = ip4_send,
	ALIGNMENT = int4,
	PASSEDBYVALUE
);

COMMENT ON TYPE ip4 IS
'IPv4 address ''#.#.#.#''';

COMMIT;

CREATE OR REPLACE FUNCTION ip4(inet)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_cast_from_inet'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION cidr(ip4)
RETURNS cidr
AS 'MODULE_PATHNAME', 'ip4_cast_to_cidr'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4(text)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_cast_from_text'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION text(ip4)
RETURNS text
AS 'MODULE_PATHNAME', 'ip4_cast_to_text'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION to_bigint(ip4)
RETURNS bigint
AS 'MODULE_PATHNAME', 'ip4_cast_to_bigint'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4(bigint)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_cast_from_bigint'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION to_double(ip4)
RETURNS double precision
AS 'MODULE_PATHNAME', 'ip4_cast_to_double'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4(double precision)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_cast_from_double'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4hash(ip4)
RETURNS integer
AS 'MODULE_PATHNAME', 'ip4hash'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE CAST (text as ip4) WITH FUNCTION ip4(text);
CREATE CAST (inet as ip4) WITH FUNCTION ip4(inet) AS ASSIGNMENT;
CREATE CAST (ip4 as cidr) WITH FUNCTION cidr(ip4) AS ASSIGNMENT;
CREATE CAST (ip4 as text) WITH FUNCTION text(ip4);

CREATE CAST (ip4 as bigint) WITH FUNCTION to_bigint(ip4);
CREATE CAST (bigint as ip4) WITH FUNCTION ip4(bigint);

CREATE CAST (ip4 as double precision) WITH FUNCTION to_double(ip4);
CREATE CAST (double precision as ip4) WITH FUNCTION ip4(double precision);

--

BEGIN;
-- type creation is needlessly chatty
SET LOCAL client_min_messages = warning;

CREATE OR REPLACE FUNCTION ip4r_in(cstring)
RETURNS ip4r
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_out(ip4r)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_recv(internal)
RETURNS ip4r
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_send(ip4r)
RETURNS bytea
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE TYPE ip4r (
	INTERNALLENGTH = 8,
	INPUT = ip4r_in,
	OUTPUT = ip4r_out,
	RECEIVE = ip4r_recv,
	SEND = ip4r_send,
	ALIGNMENT = int4,
        ELEMENT = ip4
);

COMMENT ON TYPE ip4r IS
'IPv4 range ''#.#.#.#-#.#.#.#'' or ''#.#.#.#/#'' or ''#.#.#.#''';

COMMIT;

CREATE OR REPLACE FUNCTION ip4r(cidr)
RETURNS ip4r
AS 'MODULE_PATHNAME', 'ip4r_cast_from_cidr'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r(text)
RETURNS ip4r
AS 'MODULE_PATHNAME', 'ip4r_cast_from_text'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r(ip4)
RETURNS ip4r
AS 'MODULE_PATHNAME', 'ip4r_cast_from_ip4'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION text(ip4r)
RETURNS text
AS 'MODULE_PATHNAME', 'ip4r_cast_to_text'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION cidr(ip4r)
RETURNS cidr
AS 'MODULE_PATHNAME', 'ip4r_cast_to_cidr'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r(ip4,ip4)
RETURNS ip4r
AS 'MODULE_PATHNAME', 'ip4r_from_ip4s'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_net_prefix(ip4,integer)
RETURNS ip4r
AS 'MODULE_PATHNAME', 'ip4r_net_prefix'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_net_mask(ip4,ip4)
RETURNS ip4r
AS 'MODULE_PATHNAME', 'ip4r_net_mask'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4rhash(ip4r)
RETURNS integer
AS 'MODULE_PATHNAME', 'ip4rhash'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE CAST (text as ip4r) WITH FUNCTION ip4r(text);
CREATE CAST (cidr as ip4r) WITH FUNCTION ip4r(cidr) AS ASSIGNMENT;
CREATE CAST (ip4r as cidr) WITH FUNCTION cidr(ip4r);
CREATE CAST (ip4 as ip4r) WITH FUNCTION ip4r(ip4) AS IMPLICIT;
CREATE CAST (ip4r as text) WITH FUNCTION text(ip4r);

CREATE OR REPLACE FUNCTION is_cidr(ip4r)
RETURNS boolean
AS 'MODULE_PATHNAME', 'ip4r_is_cidr'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION lower(ip4r)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4r_lower'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION upper(ip4r)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4r_upper'
LANGUAGE 'C' IMMUTABLE STRICT;

-- miscellaneous functions for ip4

CREATE OR REPLACE FUNCTION ip4_netmask(integer)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_netmask'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_net_lower(ip4,integer)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_net_lower'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_net_upper(ip4,integer)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_net_upper'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_plus_int(ip4,integer)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_plus_int'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_plus_bigint(ip4,bigint)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_plus_bigint'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_minus_int(ip4,integer)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_minus_int'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_minus_bigint(ip4,bigint)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_minus_bigint'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_minus_ip4(ip4,ip4)
RETURNS bigint
AS 'MODULE_PATHNAME', 'ip4_minus_ip4'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_and(ip4,ip4)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_and'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_or(ip4,ip4)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_or'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_xor(ip4,ip4)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_xor'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_not(ip4)
RETURNS ip4
AS 'MODULE_PATHNAME', 'ip4_not'
LANGUAGE 'C' IMMUTABLE STRICT;

-- Comparison methods for ip4

CREATE OR REPLACE FUNCTION ip4_cmp(ip4, ip4)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_lt(ip4, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_gt(ip4, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_eq(ip4, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_neq(ip4, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_le(ip4, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_ge(ip4, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

-- Comparison methods for ip4r

CREATE OR REPLACE FUNCTION ip4r_cmp(ip4r, ip4r)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_lt(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_gt(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_eq(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_neq(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_le(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_ge(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_overlaps(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_contains(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_contains_strict(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_contained_by(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_contained_by_strict(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

-- overlap relations (mainly for rtree)

CREATE OR REPLACE FUNCTION ip4r_left_of(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_left_overlap(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_right_of(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_right_overlap(ip4r, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

-- mixed ip4/ip4r

CREATE OR REPLACE FUNCTION ip4_contains(ip4r, ip4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4_contained_by(ip4, ip4r)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

-- misc funcs

CREATE OR REPLACE FUNCTION ip4r_union(ip4r, ip4r)
RETURNS ip4r
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_inter(ip4r, ip4r)
RETURNS ip4r
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION ip4r_size(ip4r)
RETURNS double precision
AS 'MODULE_PATHNAME'
LANGUAGE 'C' IMMUTABLE STRICT;

--
-- OPERATORS for ip4 only
--

CREATE OPERATOR < (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_lt,
	COMMUTATOR = '>',
	NEGATOR = '>=',
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);

CREATE OPERATOR <= (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_le,
	COMMUTATOR = '>=',
	NEGATOR = '>',
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);

CREATE OPERATOR > (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_gt,
	COMMUTATOR = '<',
	NEGATOR = '<=',
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);

CREATE OPERATOR >= (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_ge,
	COMMUTATOR = '<=',
	NEGATOR = '<',
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);

CREATE OPERATOR = (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_eq,
	COMMUTATOR = '=',
	NEGATOR = '<>',
	RESTRICT = eqsel,
	JOIN = eqjoinsel,
	SORT1 = '<',
	SORT2 = '<',
	HASHES
);

CREATE OPERATOR <> (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_neq,
	COMMUTATOR = '<>',
	NEGATOR = '=',
	RESTRICT = neqsel,
	JOIN = neqjoinsel
);


CREATE OPERATOR + (
	LEFTARG = ip4,
	RIGHTARG = integer,
	PROCEDURE = ip4_plus_int
);

CREATE OPERATOR + (
	LEFTARG = ip4,
	RIGHTARG = bigint,
	PROCEDURE = ip4_plus_bigint
);

CREATE OPERATOR - (
	LEFTARG = ip4,
	RIGHTARG = integer,
	PROCEDURE = ip4_minus_int
);

CREATE OPERATOR - (
	LEFTARG = ip4,
	RIGHTARG = bigint,
	PROCEDURE = ip4_minus_bigint
);

CREATE OPERATOR - (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_minus_ip4
);

CREATE OPERATOR & (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_and
);

CREATE OPERATOR | (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_or
);

CREATE OPERATOR # (
	LEFTARG = ip4,
	RIGHTARG = ip4,
	PROCEDURE = ip4_xor
);

CREATE OPERATOR ~ (
	RIGHTARG = ip4,
	PROCEDURE = ip4_not
);


--
-- OPERATORS for ip4r only
--

CREATE OPERATOR < (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_lt,
	COMMUTATOR = '>',
	NEGATOR = '>=',
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);

CREATE OPERATOR <= (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_le,
	COMMUTATOR = '>=',
	NEGATOR = '>',
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);

CREATE OPERATOR > (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_gt,
	COMMUTATOR = '<',
	NEGATOR = '<=',
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);

CREATE OPERATOR >= (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_ge,
	COMMUTATOR = '<=',
	NEGATOR = '<',
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);

CREATE OPERATOR << (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_contained_by_strict,
	COMMUTATOR = '>>',
	RESTRICT = contsel,
	JOIN = contjoinsel
);

CREATE OPERATOR <<= (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_contained_by,
	COMMUTATOR = '>>=',
	RESTRICT = contsel,
	JOIN = contjoinsel
);

CREATE OPERATOR && (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_overlaps,
	COMMUTATOR = '&&',
	RESTRICT = areasel,
	JOIN = areajoinsel
);

CREATE OPERATOR >> (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_contains_strict,
	COMMUTATOR = '<<',
	RESTRICT = contsel,
	JOIN = contjoinsel
);

CREATE OPERATOR >>= (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_contains,
	COMMUTATOR = '<<=',
	RESTRICT = contsel,
	JOIN = contjoinsel
);

CREATE OPERATOR = (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_eq,
	COMMUTATOR = '=',
	NEGATOR = '<>',
	RESTRICT = eqsel,
	JOIN = eqjoinsel,
	SORT1 = '<',
	SORT2 = '<',
	HASHES
);

CREATE OPERATOR <> (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_neq,
	COMMUTATOR = '<>',
	NEGATOR = '=',
	RESTRICT = neqsel,
	JOIN = neqjoinsel
);

-- an attempt at compatibility with ipr only (not recommended for new code)

CREATE OPERATOR @ (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_contains,
	COMMUTATOR = '~',
	RESTRICT = contsel,
	JOIN = contjoinsel
);

CREATE OPERATOR ~ (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_contained_by,
	COMMUTATOR = '@',
	RESTRICT = contsel,
	JOIN = contjoinsel
);

-- relationals for rtree

CREATE OPERATOR <<< (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_left_of,
	COMMUTATOR = '>>>',
	RESTRICT = positionsel,
	JOIN = positionjoinsel
);

CREATE OPERATOR >>> (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_right_of,
	COMMUTATOR = '<<<',
	RESTRICT = positionsel,
	JOIN = positionjoinsel
);

-- These next two look like they should be commutators, but they
-- are _NOT_.

CREATE OPERATOR &<< (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_left_overlap,
	RESTRICT = positionsel,
	JOIN = positionjoinsel
);

CREATE OPERATOR &>> (
	LEFTARG = ip4r,
	RIGHTARG = ip4r,
	PROCEDURE = ip4r_right_overlap,
	RESTRICT = positionsel,
	JOIN = positionjoinsel
);

--
-- OPERATORS for ip4 and ip4r together
--
-- these seem to do more harm than good.
--CREATE OPERATOR <<= (
--	LEFTARG = ip4,
--	RIGHTARG = ip4r,
--	PROCEDURE = ip4_contained_by,
--	COMMUTATOR = '>>=',
--	RESTRICT = contsel,
--	JOIN = contjoinsel
--);
--
--CREATE OPERATOR >>= (
--	LEFTARG = ip4r,
--	RIGHTARG = ip4,
--	PROCEDURE = ip4_contains,
--	COMMUTATOR = '<<=',
--	RESTRICT = contsel,
--	JOIN = contjoinsel
--);
--


-- define the GiST support methods

CREATE OR REPLACE FUNCTION gip4r_consistent(internal,ip4r,int4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION gip4r_compress(internal)
RETURNS internal 
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION gip4r_decompress(internal)
RETURNS internal 
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION gip4r_penalty(internal,internal,internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C' STRICT;

CREATE OR REPLACE FUNCTION gip4r_picksplit(internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION gip4r_union(internal, internal)
RETURNS ip4r
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION gip4r_same(ip4r, ip4r, internal)
RETURNS internal 
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

-- Create the operator classes for indexing

CREATE OPERATOR CLASS btree_ip4_ops
DEFAULT FOR TYPE ip4 USING btree
AS
	OPERATOR	1	< ,
	OPERATOR	2	<= ,
	OPERATOR	3	= ,
	OPERATOR	4	>= ,
	OPERATOR	5	> ,
	FUNCTION	1	ip4_cmp(ip4, ip4);

CREATE OPERATOR CLASS btree_ip4r_ops
DEFAULT FOR TYPE ip4r USING btree
AS
	OPERATOR	1	< ,
	OPERATOR	2	<= ,
	OPERATOR	3	= ,
	OPERATOR	4	>= ,
	OPERATOR	5	> ,
	FUNCTION	1	ip4r_cmp(ip4r, ip4r);

-- we don't really expect people to use hash indexes, but hash-join
-- and hash-aggregate use this too.

CREATE OPERATOR CLASS hash_ip4_ops
DEFAULT FOR TYPE ip4 USING hash
AS
	OPERATOR	1	= ,
	FUNCTION	1	ip4hash(ip4);

CREATE OPERATOR CLASS hash_ip4r_ops
DEFAULT FOR TYPE ip4r USING hash
AS
	OPERATOR	1	= ,
	FUNCTION	1	ip4rhash(ip4r);

-- gist

CREATE OPERATOR CLASS gist_ip4r_ops
DEFAULT FOR TYPE ip4r USING gist 
AS
	OPERATOR	1	>>= ,
	OPERATOR	2	<<= ,
	OPERATOR	3	>> ,
	OPERATOR	4	<< ,
	OPERATOR	5	&& ,
	OPERATOR	6	= ,
	FUNCTION	1	gip4r_consistent (internal, ip4r, int4),
	FUNCTION	2	gip4r_union (internal, internal),
	FUNCTION	3	gip4r_compress (internal),
	FUNCTION	4	gip4r_decompress (internal),
	FUNCTION	5	gip4r_penalty (internal, internal, internal),
	FUNCTION	6	gip4r_picksplit (internal, internal),
	FUNCTION	7	gip4r_same (ip4r, ip4r, internal);

-- Rtree
--
-- rtree is gone as of 8.2, but we want to keep it here for the
-- benefit of 7.x and 8.0 where it beats GiST in some cases.
-- various gory hacks to suppress the error that otherwise occurs.
-- Moved this to the end of the file so that even if it chokes, we
-- still installed everything else.

BEGIN;

-- rtree support methods

-- union and inter actually take ip4r parameters, but we must
-- declare them as taking "internal" to maintain the security of
-- the internal type. (functions returning internal must take at
-- least one parameter of internal type). union could be declared
-- to return an ip4r, but inter can return a NULL pointer due to
-- some very dubious calling conventions in the backend.

CREATE OR REPLACE FUNCTION rt_ip4r_union(internal,internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION rt_ip4r_inter(internal,internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

CREATE OR REPLACE FUNCTION rt_ip4r_size(ip4r,internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';

SELECT case s when 'panic' then 'ignore following ROLLBACK'
              else 'creating rtree opclass'
       end
  FROM set_config('client_min_messages',
                  case when substring(version()
                                      from 'PostgreSQL ([0-9]+[.][0-9]+)')
                              ~ '^(7[.][34]|8[.][01])$'
                       then 'notice'
                       else 'panic' end,
                  true) as s;

CREATE OPERATOR CLASS rtree_ip4r_ops
DEFAULT FOR TYPE ip4r USING rtree
AS
	OPERATOR	1	<<< ,
	OPERATOR	2	&<< ,
	OPERATOR	3	&& ,
	OPERATOR	4	&>> ,
	OPERATOR	5	>>> ,
	OPERATOR	6	= ,
	OPERATOR	7	>>= ,
	OPERATOR	8	<<= ,
	FUNCTION	1	rt_ip4r_union(internal, internal),
	FUNCTION	2	rt_ip4r_inter(internal, internal),
	FUNCTION	3	rt_ip4r_size(ip4r, internal);

COMMIT;

-- end