Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * like.c
4 : : * like expression handling code.
5 : : *
6 : : * NOTES
7 : : * A big hack of the regexp.c code!! Contributed by
8 : : * Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
9 : : *
10 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
11 : : * Portions Copyright (c) 1994, Regents of the University of California
12 : : *
13 : : * IDENTIFICATION
14 : : * src/backend/utils/adt/like.c
15 : : *
16 : : *-------------------------------------------------------------------------
17 : : */
18 : : #include "postgres.h"
19 : :
20 : : #include <ctype.h>
21 : :
22 : : #include "catalog/pg_collation.h"
23 : : #include "mb/pg_wchar.h"
24 : : #include "miscadmin.h"
25 : : #include "utils/fmgrprotos.h"
26 : : #include "utils/pg_locale.h"
27 : : #include "varatt.h"
28 : :
29 : :
30 : : #define LIKE_TRUE 1
31 : : #define LIKE_FALSE 0
32 : : #define LIKE_ABORT (-1)
33 : :
34 : :
35 : : static int SB_MatchText(const char *t, int tlen, const char *p, int plen,
36 : : pg_locale_t locale);
37 : : static text *SB_do_like_escape(text *pat, text *esc);
38 : :
39 : : static int MB_MatchText(const char *t, int tlen, const char *p, int plen,
40 : : pg_locale_t locale);
41 : : static text *MB_do_like_escape(text *pat, text *esc);
42 : :
43 : : static int UTF8_MatchText(const char *t, int tlen, const char *p, int plen,
44 : : pg_locale_t locale);
45 : :
46 : : static int C_IMatchText(const char *t, int tlen, const char *p, int plen,
47 : : pg_locale_t locale);
48 : :
49 : : static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation);
50 : : static int Generic_Text_IC_like(text *str, text *pat, Oid collation);
51 : :
52 : : /*--------------------
53 : : * Support routine for MatchText. Compares given multibyte streams
54 : : * as wide characters. If they match, returns 1 otherwise returns 0.
55 : : *--------------------
56 : : */
57 : : static inline int
199 tmunro@postgresql.or 58 :CBC 910 : wchareq(const char *p1, int p1len, const char *p2, int p2len)
59 : : {
60 : : int p1clen;
61 : :
62 : : /* Optimization: quickly compare the first byte. */
7588 bruce@momjian.us 63 [ + + ]: 910 : if (*p1 != *p2)
7500 neilc@samurai.com 64 : 705 : return 0;
65 : :
199 tmunro@postgresql.or 66 : 205 : p1clen = pg_mblen_with_len(p1, p1len);
67 [ - + ]: 205 : if (pg_mblen_with_len(p2, p2len) != p1clen)
7500 neilc@samurai.com 68 :UBC 0 : return 0;
69 : :
70 : : /* They are the same length */
199 tmunro@postgresql.or 71 [ + + ]:CBC 410 : while (p1clen--)
72 : : {
9444 tgl@sss.pgh.pa.us 73 [ - + ]: 205 : if (*p1++ != *p2++)
7500 neilc@samurai.com 74 :UBC 0 : return 0;
75 : : }
7500 neilc@samurai.com 76 :CBC 205 : return 1;
77 : : }
78 : :
79 : : /*
80 : : * Formerly we had a routine iwchareq() here that tried to do case-insensitive
81 : : * comparison of multibyte characters. It did not work at all, however,
82 : : * because it relied on tolower() which has a single-byte API ... and
83 : : * towlower() wouldn't be much better since we have no suitably cheap way
84 : : * of getting a single character transformed to the system's wchar_t format.
85 : : * So now, we just downcase the strings using lower() and apply regular LIKE
86 : : * comparison. This should be revisited when we install better locale support.
87 : : *
88 : : * We do handle case-insensitive matching for the C locale using
89 : : * fold-on-the-fly processing, however.
90 : : */
91 : :
92 : :
93 : : #define NextByte(p, plen) ((p)++, (plen)--)
94 : :
95 : : /* Set up to compile like_match.c for multibyte characters */
96 : : #define CHAREQ(p1, p1len, p2, p2len) wchareq((p1), (p1len), (p2), (p2len))
97 : : #define NextChar(p, plen) \
98 : : do { int __l = pg_mblen_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)
99 : : #define CopyAdvChar(dst, src, srclen) \
100 : : do { int __l = pg_mblen_with_len((src), (srclen)); \
101 : : (srclen) -= __l; \
102 : : while (__l-- > 0) \
103 : : *(dst)++ = *(src)++; \
104 : : } while (0)
105 : :
106 : : #define MatchText MB_MatchText
107 : : #define do_like_escape MB_do_like_escape
108 : :
109 : : #include "like_match.c"
110 : :
111 : : /* Set up to compile like_match.c for single-byte characters */
112 : : #define CHAREQ(p1, p1len, p2, p2len) (*(p1) == *(p2))
113 : : #define NextChar(p, plen) NextByte((p), (plen))
114 : : #define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
115 : :
116 : : #define MatchText SB_MatchText
117 : : #define do_like_escape SB_do_like_escape
118 : :
119 : : #include "like_match.c"
120 : :
121 : : /* setup to compile like_match.c for case-insensitive matches in C locale */
122 : : #define MATCH_LOWER
123 : : #define NextChar(p, plen) NextByte((p), (plen))
124 : : #define MatchText C_IMatchText
125 : :
126 : : #include "like_match.c"
127 : :
128 : : /* setup to compile like_match.c for UTF8 encoding, using fast NextChar */
129 : :
130 : : #define NextChar(p, plen) \
131 : : do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )
132 : : #define MatchText UTF8_MatchText
133 : :
134 : : #include "like_match.c"
135 : :
136 : : /* Generic for all cases not requiring inline case-folding */
137 : : static inline int
2682 peter@eisentraut.org 138 : 721585 : GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
139 : : {
140 : : pg_locale_t locale;
141 : :
605 142 [ - + ]: 721585 : if (!OidIsValid(collation))
143 : : {
144 : : /*
145 : : * This typically means that the parser could not resolve a conflict
146 : : * of implicit collations, so report it that way.
147 : : */
605 peter@eisentraut.org 148 [ # # ]:UBC 0 : ereport(ERROR,
149 : : (errcode(ERRCODE_INDETERMINATE_COLLATION),
150 : : errmsg("could not determine which collation to use for LIKE"),
151 : : errhint("Use the COLLATE clause to set the collation explicitly.")));
152 : : }
153 : :
605 peter@eisentraut.org 154 :CBC 721585 : locale = pg_newlocale_from_collation(collation);
155 : :
6993 andrew@dunslane.net 156 [ + + ]: 721585 : if (pg_database_encoding_max_length() == 1)
605 peter@eisentraut.org 157 : 40453 : return SB_MatchText(s, slen, p, plen, locale);
6993 andrew@dunslane.net 158 [ + - ]: 681132 : else if (GetDatabaseEncoding() == PG_UTF8)
605 peter@eisentraut.org 159 : 681132 : return UTF8_MatchText(s, slen, p, plen, locale);
160 : : else
605 peter@eisentraut.org 161 :UBC 0 : return MB_MatchText(s, slen, p, plen, locale);
162 : : }
163 : :
164 : : static inline int
5646 peter_e@gmx.net 165 :CBC 46369 : Generic_Text_IC_like(text *str, text *pat, Oid collation)
166 : : {
167 : : char *s,
168 : : *p;
169 : : int slen,
170 : : plen;
171 : : pg_locale_t locale;
172 : :
1647 peter@eisentraut.org 173 [ - + ]: 46369 : if (!OidIsValid(collation))
174 : : {
175 : : /*
176 : : * This typically means that the parser could not resolve a conflict
177 : : * of implicit collations, so report it that way.
178 : : */
1647 peter@eisentraut.org 179 [ # # ]:UBC 0 : ereport(ERROR,
180 : : (errcode(ERRCODE_INDETERMINATE_COLLATION),
181 : : errmsg("could not determine which collation to use for ILIKE"),
182 : : errhint("Use the COLLATE clause to set the collation explicitly.")));
183 : : }
184 : :
719 jdavis@postgresql.or 185 :CBC 46369 : locale = pg_newlocale_from_collation(collation);
186 : :
681 187 [ + + ]: 46369 : if (!locale->deterministic)
1647 peter@eisentraut.org 188 [ + - ]: 8 : ereport(ERROR,
189 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
190 : : errmsg("nondeterministic collations are not supported for ILIKE")));
191 : :
192 : : /*
193 : : * For efficiency reasons, in the C locale we don't call lower() on the
194 : : * pattern and text, but instead lowercase each character lazily. This
195 : : * only works for single-byte encodings, otherwise "_" may incorrectly
196 : : * match an incomplete byte sequence.
197 : : *
198 : : * XXX: use casefolding instead?
199 : : */
200 : :
18 jdavis@postgresql.or 201 [ + + - + ]: 46361 : if (locale->ctype_is_c && pg_database_encoding_max_length() == 1)
202 : : {
389 jdavis@postgresql.or 203 :UBC 0 : p = VARDATA_ANY(pat);
204 : 0 : plen = VARSIZE_ANY_EXHDR(pat);
205 : 0 : s = VARDATA_ANY(str);
206 : 0 : slen = VARSIZE_ANY_EXHDR(str);
227 207 : 0 : return C_IMatchText(s, slen, p, plen, locale);
208 : : }
209 : : else
210 : : {
3422 noah@leadboat.com 211 :CBC 46361 : pat = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
212 : : PointerGetDatum(pat)));
213 : 46361 : p = VARDATA_ANY(pat);
214 : 46361 : plen = VARSIZE_ANY_EXHDR(pat);
215 : 46361 : str = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
216 : : PointerGetDatum(str)));
217 : 46361 : s = VARDATA_ANY(str);
218 : 46361 : slen = VARSIZE_ANY_EXHDR(str);
219 : :
6881 andrew@dunslane.net 220 [ + - ]: 46361 : if (GetDatabaseEncoding() == PG_UTF8)
680 peter@eisentraut.org 221 : 46361 : return UTF8_MatchText(s, slen, p, plen, 0);
227 jdavis@postgresql.or 222 [ # # ]:UBC 0 : else if (pg_database_encoding_max_length() > 1)
680 peter@eisentraut.org 223 : 0 : return MB_MatchText(s, slen, p, plen, 0);
224 : : else
227 jdavis@postgresql.or 225 : 0 : return SB_MatchText(s, slen, p, plen, 0);
226 : : }
227 : : }
228 : :
229 : : /*
230 : : * interface routines called by the function manager
231 : : */
232 : :
233 : : Datum
9444 tgl@sss.pgh.pa.us 234 :CBC 143154 : namelike(PG_FUNCTION_ARGS)
235 : : {
9481 lockhart@fourpalms.o 236 : 143154 : Name str = PG_GETARG_NAME(0);
6882 tgl@sss.pgh.pa.us 237 : 143154 : text *pat = PG_GETARG_TEXT_PP(1);
238 : : bool result;
239 : : char *s,
240 : : *p;
241 : : int slen,
242 : : plen;
243 : :
9481 lockhart@fourpalms.o 244 : 143154 : s = NameStr(*str);
245 : 143154 : slen = strlen(s);
6882 tgl@sss.pgh.pa.us 246 : 143154 : p = VARDATA_ANY(pat);
247 : 143154 : plen = VARSIZE_ANY_EXHDR(pat);
248 : :
2682 peter@eisentraut.org 249 : 143154 : result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
250 : :
9481 lockhart@fourpalms.o 251 : 143154 : PG_RETURN_BOOL(result);
252 : : }
253 : :
254 : : Datum
9444 tgl@sss.pgh.pa.us 255 : 3788 : namenlike(PG_FUNCTION_ARGS)
256 : : {
9481 lockhart@fourpalms.o 257 : 3788 : Name str = PG_GETARG_NAME(0);
6882 tgl@sss.pgh.pa.us 258 : 3788 : text *pat = PG_GETARG_TEXT_PP(1);
259 : : bool result;
260 : : char *s,
261 : : *p;
262 : : int slen,
263 : : plen;
264 : :
9481 lockhart@fourpalms.o 265 : 3788 : s = NameStr(*str);
266 : 3788 : slen = strlen(s);
6882 tgl@sss.pgh.pa.us 267 : 3788 : p = VARDATA_ANY(pat);
268 : 3788 : plen = VARSIZE_ANY_EXHDR(pat);
269 : :
2682 peter@eisentraut.org 270 : 3788 : result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
271 : :
9481 lockhart@fourpalms.o 272 : 3788 : PG_RETURN_BOOL(result);
273 : : }
274 : :
275 : : Datum
9484 276 : 362059 : textlike(PG_FUNCTION_ARGS)
277 : : {
6882 tgl@sss.pgh.pa.us 278 : 362059 : text *str = PG_GETARG_TEXT_PP(0);
279 : 362059 : text *pat = PG_GETARG_TEXT_PP(1);
280 : : bool result;
281 : : char *s,
282 : : *p;
283 : : int slen,
284 : : plen;
285 : :
286 : 362059 : s = VARDATA_ANY(str);
287 : 362059 : slen = VARSIZE_ANY_EXHDR(str);
288 : 362059 : p = VARDATA_ANY(pat);
289 : 362059 : plen = VARSIZE_ANY_EXHDR(pat);
290 : :
2682 peter@eisentraut.org 291 : 362059 : result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
292 : :
9481 lockhart@fourpalms.o 293 : 362055 : PG_RETURN_BOOL(result);
294 : : }
295 : :
296 : : Datum
9444 tgl@sss.pgh.pa.us 297 : 212584 : textnlike(PG_FUNCTION_ARGS)
298 : : {
6882 299 : 212584 : text *str = PG_GETARG_TEXT_PP(0);
300 : 212584 : text *pat = PG_GETARG_TEXT_PP(1);
301 : : bool result;
302 : : char *s,
303 : : *p;
304 : : int slen,
305 : : plen;
306 : :
307 : 212584 : s = VARDATA_ANY(str);
308 : 212584 : slen = VARSIZE_ANY_EXHDR(str);
309 : 212584 : p = VARDATA_ANY(pat);
310 : 212584 : plen = VARSIZE_ANY_EXHDR(pat);
311 : :
2682 peter@eisentraut.org 312 : 212584 : result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
313 : :
9481 lockhart@fourpalms.o 314 : 212584 : PG_RETURN_BOOL(result);
315 : : }
316 : :
317 : : Datum
9080 bruce@momjian.us 318 : 10 : bytealike(PG_FUNCTION_ARGS)
319 : : {
6882 tgl@sss.pgh.pa.us 320 : 10 : bytea *str = PG_GETARG_BYTEA_PP(0);
321 : 10 : bytea *pat = PG_GETARG_BYTEA_PP(1);
322 : : bool result;
323 : : char *s,
324 : : *p;
325 : : int slen,
326 : : plen;
327 : :
328 : 10 : s = VARDATA_ANY(str);
329 : 10 : slen = VARSIZE_ANY_EXHDR(str);
330 : 10 : p = VARDATA_ANY(pat);
331 : 10 : plen = VARSIZE_ANY_EXHDR(pat);
332 : :
680 peter@eisentraut.org 333 : 10 : result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE);
334 : :
9080 bruce@momjian.us 335 : 10 : PG_RETURN_BOOL(result);
336 : : }
337 : :
338 : : Datum
339 : 10 : byteanlike(PG_FUNCTION_ARGS)
340 : : {
6882 tgl@sss.pgh.pa.us 341 : 10 : bytea *str = PG_GETARG_BYTEA_PP(0);
342 : 10 : bytea *pat = PG_GETARG_BYTEA_PP(1);
343 : : bool result;
344 : : char *s,
345 : : *p;
346 : : int slen,
347 : : plen;
348 : :
349 : 10 : s = VARDATA_ANY(str);
350 : 10 : slen = VARSIZE_ANY_EXHDR(str);
351 : 10 : p = VARDATA_ANY(pat);
352 : 10 : plen = VARSIZE_ANY_EXHDR(pat);
353 : :
680 peter@eisentraut.org 354 : 10 : result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE);
355 : :
9080 bruce@momjian.us 356 : 10 : PG_RETURN_BOOL(result);
357 : : }
358 : :
359 : : /*
360 : : * Case-insensitive versions
361 : : */
362 : :
363 : : Datum
9444 tgl@sss.pgh.pa.us 364 : 12048 : nameiclike(PG_FUNCTION_ARGS)
365 : : {
9481 lockhart@fourpalms.o 366 : 12048 : Name str = PG_GETARG_NAME(0);
6882 tgl@sss.pgh.pa.us 367 : 12048 : text *pat = PG_GETARG_TEXT_PP(1);
368 : : bool result;
369 : : text *strtext;
370 : :
3422 noah@leadboat.com 371 : 12048 : strtext = DatumGetTextPP(DirectFunctionCall1(name_text,
372 : : NameGetDatum(str)));
5646 peter_e@gmx.net 373 : 12048 : result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) == LIKE_TRUE);
374 : :
9481 lockhart@fourpalms.o 375 : 12048 : PG_RETURN_BOOL(result);
376 : : }
377 : :
378 : : Datum
9444 tgl@sss.pgh.pa.us 379 : 5 : nameicnlike(PG_FUNCTION_ARGS)
380 : : {
9481 lockhart@fourpalms.o 381 : 5 : Name str = PG_GETARG_NAME(0);
6882 tgl@sss.pgh.pa.us 382 : 5 : text *pat = PG_GETARG_TEXT_PP(1);
383 : : bool result;
384 : : text *strtext;
385 : :
3422 noah@leadboat.com 386 : 5 : strtext = DatumGetTextPP(DirectFunctionCall1(name_text,
387 : : NameGetDatum(str)));
5646 peter_e@gmx.net 388 : 5 : result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) != LIKE_TRUE);
389 : :
9481 lockhart@fourpalms.o 390 : 5 : PG_RETURN_BOOL(result);
391 : : }
392 : :
393 : : Datum
9444 tgl@sss.pgh.pa.us 394 : 34280 : texticlike(PG_FUNCTION_ARGS)
395 : : {
6882 396 : 34280 : text *str = PG_GETARG_TEXT_PP(0);
397 : 34280 : text *pat = PG_GETARG_TEXT_PP(1);
398 : : bool result;
399 : :
5646 peter_e@gmx.net 400 : 34280 : result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) == LIKE_TRUE);
401 : :
9481 lockhart@fourpalms.o 402 : 34272 : PG_RETURN_BOOL(result);
403 : : }
404 : :
405 : : Datum
9444 tgl@sss.pgh.pa.us 406 : 36 : texticnlike(PG_FUNCTION_ARGS)
407 : : {
6882 408 : 36 : text *str = PG_GETARG_TEXT_PP(0);
409 : 36 : text *pat = PG_GETARG_TEXT_PP(1);
410 : : bool result;
411 : :
5646 peter_e@gmx.net 412 : 36 : result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) != LIKE_TRUE);
413 : :
9481 lockhart@fourpalms.o 414 : 36 : PG_RETURN_BOOL(result);
415 : : }
416 : :
417 : : /*
418 : : * like_escape() --- given a pattern and an ESCAPE string,
419 : : * convert the pattern to use Postgres' standard backslash escape convention.
420 : : */
421 : : Datum
9444 tgl@sss.pgh.pa.us 422 : 186 : like_escape(PG_FUNCTION_ARGS)
423 : : {
6882 424 : 186 : text *pat = PG_GETARG_TEXT_PP(0);
425 : 186 : text *esc = PG_GETARG_TEXT_PP(1);
426 : : text *result;
427 : :
9060 ishii@postgresql.org 428 [ - + ]: 186 : if (pg_database_encoding_max_length() == 1)
6993 andrew@dunslane.net 429 :UBC 0 : result = SB_do_like_escape(pat, esc);
430 : : else
9039 bruce@momjian.us 431 :CBC 186 : result = MB_do_like_escape(pat, esc);
432 : :
9444 tgl@sss.pgh.pa.us 433 : 186 : PG_RETURN_TEXT_P(result);
434 : : }
435 : :
436 : : /*
437 : : * like_escape_bytea() --- given a pattern and an ESCAPE string,
438 : : * convert the pattern to use Postgres' standard backslash escape convention.
439 : : */
440 : : Datum
9080 bruce@momjian.us 441 : 10 : like_escape_bytea(PG_FUNCTION_ARGS)
442 : : {
6882 tgl@sss.pgh.pa.us 443 : 10 : bytea *pat = PG_GETARG_BYTEA_PP(0);
444 : 10 : bytea *esc = PG_GETARG_BYTEA_PP(1);
6827 bruce@momjian.us 445 : 10 : bytea *result = SB_do_like_escape((text *) pat, (text *) esc);
446 : :
447 : 10 : PG_RETURN_BYTEA_P((bytea *) result);
448 : : }
|