|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.128 2008/05/04 16:42:41 tgl Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.129 2008/05/27 00:13:09 tgl Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
@@ -886,3 +886,112 @@ hashbpchar(PG_FUNCTION_ARGS)
|
886 | 886 |
|
887 | 887 | return result;
|
888 | 888 | }
|
| 889 | + |
| 890 | + |
| 891 | +/* |
| 892 | + * The following operators support character-by-character comparison |
| 893 | + * of bpchar datums, to allow building indexes suitable for LIKE clauses. |
| 894 | + * Note that the regular bpchareq/bpcharne comparison operators are assumed |
| 895 | + * to be compatible with these! |
| 896 | + */ |
| 897 | + |
| 898 | +static int |
| 899 | +internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2) |
| 900 | +{ |
| 901 | + int result; |
| 902 | + int len1, |
| 903 | + len2; |
| 904 | + |
| 905 | + len1 = bcTruelen(arg1); |
| 906 | + len2 = bcTruelen(arg2); |
| 907 | + |
| 908 | + result = strncmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2)); |
| 909 | + if (result != 0) |
| 910 | + return result; |
| 911 | + else if (len1 < len2) |
| 912 | + return -1; |
| 913 | + else if (len1 > len2) |
| 914 | + return 1; |
| 915 | + else |
| 916 | + return 0; |
| 917 | +} |
| 918 | + |
| 919 | + |
| 920 | +Datum |
| 921 | +bpchar_pattern_lt(PG_FUNCTION_ARGS) |
| 922 | +{ |
| 923 | + BpChar *arg1 = PG_GETARG_BPCHAR_PP(0); |
| 924 | + BpChar *arg2 = PG_GETARG_BPCHAR_PP(1); |
| 925 | + int result; |
| 926 | + |
| 927 | + result = internal_bpchar_pattern_compare(arg1, arg2); |
| 928 | + |
| 929 | + PG_FREE_IF_COPY(arg1, 0); |
| 930 | + PG_FREE_IF_COPY(arg2, 1); |
| 931 | + |
| 932 | + PG_RETURN_BOOL(result < 0); |
| 933 | +} |
| 934 | + |
| 935 | + |
| 936 | +Datum |
| 937 | +bpchar_pattern_le(PG_FUNCTION_ARGS) |
| 938 | +{ |
| 939 | + BpChar *arg1 = PG_GETARG_BPCHAR_PP(0); |
| 940 | + BpChar *arg2 = PG_GETARG_BPCHAR_PP(1); |
| 941 | + int result; |
| 942 | + |
| 943 | + result = internal_bpchar_pattern_compare(arg1, arg2); |
| 944 | + |
| 945 | + PG_FREE_IF_COPY(arg1, 0); |
| 946 | + PG_FREE_IF_COPY(arg2, 1); |
| 947 | + |
| 948 | + PG_RETURN_BOOL(result <= 0); |
| 949 | +} |
| 950 | + |
| 951 | + |
| 952 | +Datum |
| 953 | +bpchar_pattern_ge(PG_FUNCTION_ARGS) |
| 954 | +{ |
| 955 | + BpChar *arg1 = PG_GETARG_BPCHAR_PP(0); |
| 956 | + BpChar *arg2 = PG_GETARG_BPCHAR_PP(1); |
| 957 | + int result; |
| 958 | + |
| 959 | + result = internal_bpchar_pattern_compare(arg1, arg2); |
| 960 | + |
| 961 | + PG_FREE_IF_COPY(arg1, 0); |
| 962 | + PG_FREE_IF_COPY(arg2, 1); |
| 963 | + |
| 964 | + PG_RETURN_BOOL(result >= 0); |
| 965 | +} |
| 966 | + |
| 967 | + |
| 968 | +Datum |
| 969 | +bpchar_pattern_gt(PG_FUNCTION_ARGS) |
| 970 | +{ |
| 971 | + BpChar *arg1 = PG_GETARG_BPCHAR_PP(0); |
| 972 | + BpChar *arg2 = PG_GETARG_BPCHAR_PP(1); |
| 973 | + int result; |
| 974 | + |
| 975 | + result = internal_bpchar_pattern_compare(arg1, arg2); |
| 976 | + |
| 977 | + PG_FREE_IF_COPY(arg1, 0); |
| 978 | + PG_FREE_IF_COPY(arg2, 1); |
| 979 | + |
| 980 | + PG_RETURN_BOOL(result > 0); |
| 981 | +} |
| 982 | + |
| 983 | + |
| 984 | +Datum |
| 985 | +btbpchar_pattern_cmp(PG_FUNCTION_ARGS) |
| 986 | +{ |
| 987 | + BpChar *arg1 = PG_GETARG_BPCHAR_PP(0); |
| 988 | + BpChar *arg2 = PG_GETARG_BPCHAR_PP(1); |
| 989 | + int result; |
| 990 | + |
| 991 | + result = internal_bpchar_pattern_compare(arg1, arg2); |
| 992 | + |
| 993 | + PG_FREE_IF_COPY(arg1, 0); |
| 994 | + PG_FREE_IF_COPY(arg2, 1); |
| 995 | + |
| 996 | + PG_RETURN_INT32(result); |
| 997 | +} |
0 commit comments