PostgreSQL Source Code git master
like.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "catalog/pg_collation.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/fmgrprotos.h"
#include "utils/pg_locale.h"
#include "varatt.h"
#include "like_match.c"
Include dependency graph for like.c:

Go to the source code of this file.

Macros

#define LIKE_TRUE   1
 
#define LIKE_FALSE   0
 
#define LIKE_ABORT   (-1)
 
#define NextByte(p, plen)   ((p)++, (plen)--)
 
#define CHAREQ(p1, p2)   wchareq((p1), (p2))
 
#define NextChar(p, plen)    do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)
 
#define CopyAdvChar(dst, src, srclen)
 
#define MatchText   MB_MatchText
 
#define do_like_escape   MB_do_like_escape
 
#define CHAREQ(p1, p2)   (*(p1) == *(p2))
 
#define NextChar(p, plen)   NextByte((p), (plen))
 
#define CopyAdvChar(dst, src, srclen)   (*(dst)++ = *(src)++, (srclen)--)
 
#define MatchText   SB_MatchText
 
#define do_like_escape   SB_do_like_escape
 
#define MATCH_LOWER
 
#define NextChar(p, plen)   NextByte((p), (plen))
 
#define MatchText   C_IMatchText
 
#define NextChar(p, plen)    do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )
 
#define MatchText   UTF8_MatchText
 

Functions

static int SB_MatchText (const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
 
static textSB_do_like_escape (text *pat, text *esc)
 
static int MB_MatchText (const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
 
static textMB_do_like_escape (text *pat, text *esc)
 
static int UTF8_MatchText (const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
 
static int C_IMatchText (const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
 
static int GenericMatchText (const char *s, int slen, const char *p, int plen, Oid collation)
 
static int Generic_Text_IC_like (text *str, text *pat, Oid collation)
 
static int wchareq (const char *p1, const char *p2)
 
Datum namelike (PG_FUNCTION_ARGS)
 
Datum namenlike (PG_FUNCTION_ARGS)
 
Datum textlike (PG_FUNCTION_ARGS)
 
Datum textnlike (PG_FUNCTION_ARGS)
 
Datum bytealike (PG_FUNCTION_ARGS)
 
Datum byteanlike (PG_FUNCTION_ARGS)
 
Datum nameiclike (PG_FUNCTION_ARGS)
 
Datum nameicnlike (PG_FUNCTION_ARGS)
 
Datum texticlike (PG_FUNCTION_ARGS)
 
Datum texticnlike (PG_FUNCTION_ARGS)
 
Datum like_escape (PG_FUNCTION_ARGS)
 
Datum like_escape_bytea (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ CHAREQ [1/2]

#define CHAREQ (   p1,
  p2 
)    wchareq((p1), (p2))

Definition at line 112 of file like.c.

◆ CHAREQ [2/2]

#define CHAREQ (   p1,
  p2 
)    (*(p1) == *(p2))

Definition at line 112 of file like.c.

◆ CopyAdvChar [1/2]

#define CopyAdvChar (   dst,
  src,
  srclen 
)
Value:
do { int __l = pg_mblen(src); \
(srclen) -= __l; \
while (__l-- > 0) \
*(dst)++ = *(src)++; \
} while (0)
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1026

Definition at line 114 of file like.c.

◆ CopyAdvChar [2/2]

#define CopyAdvChar (   dst,
  src,
  srclen 
)    (*(dst)++ = *(src)++, (srclen)--)

Definition at line 114 of file like.c.

◆ do_like_escape [1/2]

#define do_like_escape   MB_do_like_escape

Definition at line 117 of file like.c.

◆ do_like_escape [2/2]

#define do_like_escape   SB_do_like_escape

Definition at line 117 of file like.c.

◆ LIKE_ABORT

#define LIKE_ABORT   (-1)

Definition at line 32 of file like.c.

◆ LIKE_FALSE

#define LIKE_FALSE   0

Definition at line 31 of file like.c.

◆ LIKE_TRUE

#define LIKE_TRUE   1

Definition at line 30 of file like.c.

◆ MATCH_LOWER

#define MATCH_LOWER

Definition at line 122 of file like.c.

◆ MatchText [1/4]

#define MatchText   MB_MatchText

Definition at line 132 of file like.c.

◆ MatchText [2/4]

#define MatchText   SB_MatchText

Definition at line 132 of file like.c.

◆ MatchText [3/4]

#define MatchText   C_IMatchText

Definition at line 132 of file like.c.

◆ MatchText [4/4]

#define MatchText   UTF8_MatchText

Definition at line 132 of file like.c.

◆ NextByte

#define NextByte (   p,
  plen 
)    ((p)++, (plen)--)

Definition at line 93 of file like.c.

◆ NextChar [1/4]

#define NextChar (   p,
  plen 
)     do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)

Definition at line 130 of file like.c.

◆ NextChar [2/4]

#define NextChar (   p,
  plen 
)    NextByte((p), (plen))

Definition at line 130 of file like.c.

◆ NextChar [3/4]

#define NextChar (   p,
  plen 
)    NextByte((p), (plen))

Definition at line 130 of file like.c.

◆ NextChar [4/4]

#define NextChar (   p,
  plen 
)     do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )

Definition at line 130 of file like.c.

Function Documentation

◆ bytealike()

Datum bytealike ( PG_FUNCTION_ARGS  )

Definition at line 316 of file like.c.

317{
319 bytea *pat = PG_GETARG_BYTEA_PP(1);
320 bool result;
321 char *s,
322 *p;
323 int slen,
324 plen;
325
326 s = VARDATA_ANY(str);
327 slen = VARSIZE_ANY_EXHDR(str);
328 p = VARDATA_ANY(pat);
329 plen = VARSIZE_ANY_EXHDR(pat);
330
331 result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE);
332
333 PG_RETURN_BOOL(result);
334}
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
const char * str
#define LIKE_TRUE
Definition: like.c:30
static int SB_MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
Definition: c.h:706
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition: varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition: varatt.h:486

References LIKE_TRUE, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, SB_MatchText(), str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ byteanlike()

Datum byteanlike ( PG_FUNCTION_ARGS  )

Definition at line 337 of file like.c.

338{
340 bytea *pat = PG_GETARG_BYTEA_PP(1);
341 bool result;
342 char *s,
343 *p;
344 int slen,
345 plen;
346
347 s = VARDATA_ANY(str);
348 slen = VARSIZE_ANY_EXHDR(str);
349 p = VARDATA_ANY(pat);
350 plen = VARSIZE_ANY_EXHDR(pat);
351
352 result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE);
353
354 PG_RETURN_BOOL(result);
355}

References LIKE_TRUE, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, SB_MatchText(), str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ C_IMatchText()

static int C_IMatchText ( const char *  t,
int  tlen,
const char *  p,
int  plen,
pg_locale_t  locale 
)
static

Referenced by Generic_Text_IC_like().

◆ Generic_Text_IC_like()

static int Generic_Text_IC_like ( text str,
text pat,
Oid  collation 
)
inlinestatic

Definition at line 165 of file like.c.

166{
167 char *s,
168 *p;
169 int slen,
170 plen;
172
173 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 */
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
186
187 if (!locale->deterministic)
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.
195 *
196 * XXX: use casefolding instead?
197 */
198
199 if (locale->ctype_is_c)
200 {
201 p = VARDATA_ANY(pat);
202 plen = VARSIZE_ANY_EXHDR(pat);
203 s = VARDATA_ANY(str);
204 slen = VARSIZE_ANY_EXHDR(str);
205 return C_IMatchText(s, slen, p, plen, locale);
206 }
207 else
208 {
210 PointerGetDatum(pat)));
211 p = VARDATA_ANY(pat);
212 plen = VARSIZE_ANY_EXHDR(pat);
215 s = VARDATA_ANY(str);
216 slen = VARSIZE_ANY_EXHDR(str);
217
219 return UTF8_MatchText(s, slen, p, plen, 0);
220 else if (pg_database_encoding_max_length() > 1)
221 return MB_MatchText(s, slen, p, plen, 0);
222 else
223 return SB_MatchText(s, slen, p, plen, 0);
224 }
225}
#define OidIsValid(objectId)
Definition: c.h:788
int errhint(const char *fmt,...)
Definition: elog.c:1330
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
Definition: fmgr.c:793
#define DatumGetTextPP(X)
Definition: fmgr.h:292
static char * locale
Definition: initdb.c:140
static int UTF8_MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
static int MB_MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
static int C_IMatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
int GetDatabaseEncoding(void)
Definition: mbutils.c:1264
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1549
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1186
@ PG_UTF8
Definition: pg_wchar.h:232
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332

References C_IMatchText(), DatumGetTextPP, DirectFunctionCall1Coll(), ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), locale, lower(), MB_MatchText(), OidIsValid, pg_database_encoding_max_length(), pg_newlocale_from_collation(), PG_UTF8, PointerGetDatum(), SB_MatchText(), str, UTF8_MatchText(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by nameiclike(), nameicnlike(), texticlike(), and texticnlike().

◆ GenericMatchText()

static int GenericMatchText ( const char *  s,
int  slen,
const char *  p,
int  plen,
Oid  collation 
)
inlinestatic

Definition at line 138 of file like.c.

139{
141
142 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 */
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
155
157 return SB_MatchText(s, slen, p, plen, locale);
158 else if (GetDatabaseEncoding() == PG_UTF8)
159 return UTF8_MatchText(s, slen, p, plen, locale);
160 else
161 return MB_MatchText(s, slen, p, plen, locale);
162}

References ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), locale, MB_MatchText(), OidIsValid, pg_database_encoding_max_length(), pg_newlocale_from_collation(), PG_UTF8, SB_MatchText(), and UTF8_MatchText().

Referenced by namelike(), namenlike(), textlike(), and textnlike().

◆ like_escape()

Datum like_escape ( PG_FUNCTION_ARGS  )

Definition at line 420 of file like.c.

421{
422 text *pat = PG_GETARG_TEXT_PP(0);
423 text *esc = PG_GETARG_TEXT_PP(1);
424 text *result;
425
427 result = SB_do_like_escape(pat, esc);
428 else
429 result = MB_do_like_escape(pat, esc);
430
431 PG_RETURN_TEXT_P(result);
432}
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static text * MB_do_like_escape(text *pat, text *esc)
static text * SB_do_like_escape(text *pat, text *esc)

References MB_do_like_escape(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and SB_do_like_escape().

◆ like_escape_bytea()

Datum like_escape_bytea ( PG_FUNCTION_ARGS  )

Definition at line 439 of file like.c.

440{
441 bytea *pat = PG_GETARG_BYTEA_PP(0);
442 bytea *esc = PG_GETARG_BYTEA_PP(1);
443 bytea *result = SB_do_like_escape((text *) pat, (text *) esc);
444
445 PG_RETURN_BYTEA_P((bytea *) result);
446}
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371

References PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, and SB_do_like_escape().

◆ MB_do_like_escape()

static text * MB_do_like_escape ( text pat,
text esc 
)
static

Referenced by like_escape().

◆ MB_MatchText()

static int MB_MatchText ( const char *  t,
int  tlen,
const char *  p,
int  plen,
pg_locale_t  locale 
)
static

◆ nameiclike()

Datum nameiclike ( PG_FUNCTION_ARGS  )

Definition at line 362 of file like.c.

363{
365 text *pat = PG_GETARG_TEXT_PP(1);
366 bool result;
367 text *strtext;
368
370 NameGetDatum(str)));
371 result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) == LIKE_TRUE);
372
373 PG_RETURN_BOOL(result);
374}
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_GET_COLLATION()
Definition: fmgr.h:198
static int Generic_Text_IC_like(text *str, text *pat, Oid collation)
Definition: like.c:165
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:383
Definition: c.h:760
Datum name_text(PG_FUNCTION_ARGS)
Definition: varlena.c:2680

References DatumGetTextPP, DirectFunctionCall1, Generic_Text_IC_like(), LIKE_TRUE, name_text(), NameGetDatum(), PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and str.

◆ nameicnlike()

Datum nameicnlike ( PG_FUNCTION_ARGS  )

Definition at line 377 of file like.c.

378{
380 text *pat = PG_GETARG_TEXT_PP(1);
381 bool result;
382 text *strtext;
383
385 NameGetDatum(str)));
386 result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) != LIKE_TRUE);
387
388 PG_RETURN_BOOL(result);
389}

References DatumGetTextPP, DirectFunctionCall1, Generic_Text_IC_like(), LIKE_TRUE, name_text(), NameGetDatum(), PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and str.

◆ namelike()

Datum namelike ( PG_FUNCTION_ARGS  )

Definition at line 232 of file like.c.

233{
235 text *pat = PG_GETARG_TEXT_PP(1);
236 bool result;
237 char *s,
238 *p;
239 int slen,
240 plen;
241
242 s = NameStr(*str);
243 slen = strlen(s);
244 p = VARDATA_ANY(pat);
245 plen = VARSIZE_ANY_EXHDR(pat);
246
247 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
248
249 PG_RETURN_BOOL(result);
250}
#define NameStr(name)
Definition: c.h:765
static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
Definition: like.c:138

References GenericMatchText(), LIKE_TRUE, NameStr, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ namenlike()

Datum namenlike ( PG_FUNCTION_ARGS  )

Definition at line 253 of file like.c.

254{
256 text *pat = PG_GETARG_TEXT_PP(1);
257 bool result;
258 char *s,
259 *p;
260 int slen,
261 plen;
262
263 s = NameStr(*str);
264 slen = strlen(s);
265 p = VARDATA_ANY(pat);
266 plen = VARSIZE_ANY_EXHDR(pat);
267
268 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
269
270 PG_RETURN_BOOL(result);
271}

References GenericMatchText(), LIKE_TRUE, NameStr, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ SB_do_like_escape()

static text * SB_do_like_escape ( text pat,
text esc 
)
static

Referenced by like_escape(), and like_escape_bytea().

◆ SB_MatchText()

static int SB_MatchText ( const char *  t,
int  tlen,
const char *  p,
int  plen,
pg_locale_t  locale 
)
static

◆ texticlike()

Datum texticlike ( PG_FUNCTION_ARGS  )

Definition at line 392 of file like.c.

393{
395 text *pat = PG_GETARG_TEXT_PP(1);
396 bool result;
397
398 result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) == LIKE_TRUE);
399
400 PG_RETURN_BOOL(result);
401}

References Generic_Text_IC_like(), LIKE_TRUE, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and str.

◆ texticnlike()

Datum texticnlike ( PG_FUNCTION_ARGS  )

Definition at line 404 of file like.c.

405{
407 text *pat = PG_GETARG_TEXT_PP(1);
408 bool result;
409
410 result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) != LIKE_TRUE);
411
412 PG_RETURN_BOOL(result);
413}

References Generic_Text_IC_like(), LIKE_TRUE, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and str.

◆ textlike()

Datum textlike ( PG_FUNCTION_ARGS  )

Definition at line 274 of file like.c.

275{
277 text *pat = PG_GETARG_TEXT_PP(1);
278 bool result;
279 char *s,
280 *p;
281 int slen,
282 plen;
283
284 s = VARDATA_ANY(str);
285 slen = VARSIZE_ANY_EXHDR(str);
286 p = VARDATA_ANY(pat);
287 plen = VARSIZE_ANY_EXHDR(pat);
288
289 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
290
291 PG_RETURN_BOOL(result);
292}

References GenericMatchText(), LIKE_TRUE, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ textnlike()

Datum textnlike ( PG_FUNCTION_ARGS  )

Definition at line 295 of file like.c.

296{
298 text *pat = PG_GETARG_TEXT_PP(1);
299 bool result;
300 char *s,
301 *p;
302 int slen,
303 plen;
304
305 s = VARDATA_ANY(str);
306 slen = VARSIZE_ANY_EXHDR(str);
307 p = VARDATA_ANY(pat);
308 plen = VARSIZE_ANY_EXHDR(pat);
309
310 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
311
312 PG_RETURN_BOOL(result);
313}

References GenericMatchText(), LIKE_TRUE, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ UTF8_MatchText()

static int UTF8_MatchText ( const char *  t,
int  tlen,
const char *  p,
int  plen,
pg_locale_t  locale 
)
static

◆ wchareq()

static int wchareq ( const char *  p1,
const char *  p2 
)
inlinestatic

Definition at line 58 of file like.c.

59{
60 int p1_len;
61
62 /* Optimization: quickly compare the first byte. */
63 if (*p1 != *p2)
64 return 0;
65
66 p1_len = pg_mblen(p1);
67 if (pg_mblen(p2) != p1_len)
68 return 0;
69
70 /* They are the same length */
71 while (p1_len--)
72 {
73 if (*p1++ != *p2++)
74 return 0;
75 }
76 return 1;
77}

References pg_mblen().