Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

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(t, locale)   SB_lower_char((unsigned char) (t), locale)
 
#define NextChar(p, plen)   NextByte((p), (plen))
 
#define MatchText   SB_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 SB_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)
 
static char SB_lower_char (unsigned char c, pg_locale_t locale)
 
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 124 of file like.c.

◆ CHAREQ [2/2]

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

Definition at line 124 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:1024

Definition at line 126 of file like.c.

◆ CopyAdvChar [2/2]

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

Definition at line 126 of file like.c.

◆ do_like_escape [1/2]

#define do_like_escape   MB_do_like_escape

Definition at line 129 of file like.c.

◆ do_like_escape [2/2]

#define do_like_escape   SB_do_like_escape

Definition at line 129 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 (   t,
  locale 
)    SB_lower_char((unsigned char) (t), locale)

Definition at line 134 of file like.c.

◆ MatchText [1/4]

#define MatchText   MB_MatchText

Definition at line 144 of file like.c.

◆ MatchText [2/4]

#define MatchText   SB_MatchText

Definition at line 144 of file like.c.

◆ MatchText [3/4]

#define MatchText   SB_IMatchText

Definition at line 144 of file like.c.

◆ MatchText [4/4]

#define MatchText   UTF8_MatchText

Definition at line 144 of file like.c.

◆ NextByte

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

Definition at line 105 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 142 of file like.c.

◆ NextChar [2/4]

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

Definition at line 142 of file like.c.

◆ NextChar [3/4]

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

Definition at line 142 of file like.c.

◆ NextChar [4/4]

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

Definition at line 142 of file like.c.

Function Documentation

◆ bytealike()

Datum bytealike ( PG_FUNCTION_ARGS  )

Definition at line 328 of file like.c.

329{
331 bytea *pat = PG_GETARG_BYTEA_PP(1);
332 bool result;
333 char *s,
334 *p;
335 int slen,
336 plen;
337
338 s = VARDATA_ANY(str);
339 slen = VARSIZE_ANY_EXHDR(str);
340 p = VARDATA_ANY(pat);
341 plen = VARSIZE_ANY_EXHDR(pat);
342
343 result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE);
344
345 PG_RETURN_BOOL(result);
346}
#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:693
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 349 of file like.c.

350{
352 bytea *pat = PG_GETARG_BYTEA_PP(1);
353 bool result;
354 char *s,
355 *p;
356 int slen,
357 plen;
358
359 s = VARDATA_ANY(str);
360 slen = VARSIZE_ANY_EXHDR(str);
361 p = VARDATA_ANY(pat);
362 plen = VARSIZE_ANY_EXHDR(pat);
363
364 result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE);
365
366 PG_RETURN_BOOL(result);
367}

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

◆ Generic_Text_IC_like()

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

Definition at line 177 of file like.c.

178{
179 char *s,
180 *p;
181 int slen,
182 plen;
184
185 if (!OidIsValid(collation))
186 {
187 /*
188 * This typically means that the parser could not resolve a conflict
189 * of implicit collations, so report it that way.
190 */
192 (errcode(ERRCODE_INDETERMINATE_COLLATION),
193 errmsg("could not determine which collation to use for ILIKE"),
194 errhint("Use the COLLATE clause to set the collation explicitly.")));
195 }
196
198
199 if (!locale->deterministic)
201 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
202 errmsg("nondeterministic collations are not supported for ILIKE")));
203
204 /*
205 * For efficiency reasons, in the single byte case we don't call lower()
206 * on the pattern and text, but instead call SB_lower_char on each
207 * character. In the multi-byte case we don't have much choice :-(. Also,
208 * ICU does not support single-character case folding, so we go the long
209 * way.
210 */
211
212 if (locale->ctype_is_c ||
215 {
216 p = VARDATA_ANY(pat);
217 plen = VARSIZE_ANY_EXHDR(pat);
218 s = VARDATA_ANY(str);
219 slen = VARSIZE_ANY_EXHDR(str);
220 return SB_IMatchText(s, slen, p, plen, locale);
221 }
222 else
223 {
225 PointerGetDatum(pat)));
226 p = VARDATA_ANY(pat);
227 plen = VARSIZE_ANY_EXHDR(pat);
230 s = VARDATA_ANY(str);
231 slen = VARSIZE_ANY_EXHDR(str);
233 return UTF8_MatchText(s, slen, p, plen, 0);
234 else
235 return MB_MatchText(s, slen, p, plen, 0);
236 }
237}
#define OidIsValid(objectId)
Definition: c.h:775
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
Definition: fmgr.c:792
#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 SB_IMatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
int GetDatabaseEncoding(void)
Definition: mbutils.c:1262
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1547
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1166
bool char_tolower_enabled(pg_locale_t locale)
Definition: pg_locale.c:1417
@ PG_UTF8
Definition: pg_wchar.h:232
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332

References char_tolower_enabled(), 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_IMatchText(), 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 150 of file like.c.

151{
153
154 if (!OidIsValid(collation))
155 {
156 /*
157 * This typically means that the parser could not resolve a conflict
158 * of implicit collations, so report it that way.
159 */
161 (errcode(ERRCODE_INDETERMINATE_COLLATION),
162 errmsg("could not determine which collation to use for LIKE"),
163 errhint("Use the COLLATE clause to set the collation explicitly.")));
164 }
165
167
169 return SB_MatchText(s, slen, p, plen, locale);
170 else if (GetDatabaseEncoding() == PG_UTF8)
171 return UTF8_MatchText(s, slen, p, plen, locale);
172 else
173 return MB_MatchText(s, slen, p, plen, locale);
174}

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 432 of file like.c.

433{
434 text *pat = PG_GETARG_TEXT_PP(0);
435 text *esc = PG_GETARG_TEXT_PP(1);
436 text *result;
437
439 result = SB_do_like_escape(pat, esc);
440 else
441 result = MB_do_like_escape(pat, esc);
442
443 PG_RETURN_TEXT_P(result);
444}
#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 451 of file like.c.

452{
453 bytea *pat = PG_GETARG_BYTEA_PP(0);
454 bytea *esc = PG_GETARG_BYTEA_PP(1);
455 bytea *result = SB_do_like_escape((text *) pat, (text *) esc);
456
457 PG_RETURN_BYTEA_P((bytea *) result);
458}
#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 374 of file like.c.

375{
377 text *pat = PG_GETARG_TEXT_PP(1);
378 bool result;
379 text *strtext;
380
382 NameGetDatum(str)));
383 result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) == LIKE_TRUE);
384
385 PG_RETURN_BOOL(result);
386}
#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:177
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:383
Definition: c.h:747
Datum name_text(PG_FUNCTION_ARGS)
Definition: varlena.c:2669

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 389 of file like.c.

390{
392 text *pat = PG_GETARG_TEXT_PP(1);
393 bool result;
394 text *strtext;
395
397 NameGetDatum(str)));
398 result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) != LIKE_TRUE);
399
400 PG_RETURN_BOOL(result);
401}

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 244 of file like.c.

245{
247 text *pat = PG_GETARG_TEXT_PP(1);
248 bool result;
249 char *s,
250 *p;
251 int slen,
252 plen;
253
254 s = NameStr(*str);
255 slen = strlen(s);
256 p = VARDATA_ANY(pat);
257 plen = VARSIZE_ANY_EXHDR(pat);
258
259 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
260
261 PG_RETURN_BOOL(result);
262}
#define NameStr(name)
Definition: c.h:752
static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
Definition: like.c:150

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 265 of file like.c.

266{
268 text *pat = PG_GETARG_TEXT_PP(1);
269 bool result;
270 char *s,
271 *p;
272 int slen,
273 plen;
274
275 s = NameStr(*str);
276 slen = strlen(s);
277 p = VARDATA_ANY(pat);
278 plen = VARSIZE_ANY_EXHDR(pat);
279
280 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
281
282 PG_RETURN_BOOL(result);
283}

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_IMatchText()

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

Referenced by Generic_Text_IC_like().

◆ SB_lower_char()

static char SB_lower_char ( unsigned char  c,
pg_locale_t  locale 
)
static

Definition at line 94 of file like.c.

95{
96 if (locale->ctype_is_c)
97 return pg_ascii_tolower(c);
98 else if (locale->is_default)
99 return pg_tolower(c);
100 else
101 return char_tolower(c, locale);
102}
char char_tolower(unsigned char ch, pg_locale_t locale)
Definition: pg_locale.c:1428
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
char * c

References char_tolower(), locale, pg_ascii_tolower(), and pg_tolower().

◆ 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 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.

◆ texticnlike()

Datum texticnlike ( PG_FUNCTION_ARGS  )

Definition at line 416 of file like.c.

417{
419 text *pat = PG_GETARG_TEXT_PP(1);
420 bool result;
421
422 result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) != LIKE_TRUE);
423
424 PG_RETURN_BOOL(result);
425}

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 286 of file like.c.

287{
289 text *pat = PG_GETARG_TEXT_PP(1);
290 bool result;
291 char *s,
292 *p;
293 int slen,
294 plen;
295
296 s = VARDATA_ANY(str);
297 slen = VARSIZE_ANY_EXHDR(str);
298 p = VARDATA_ANY(pat);
299 plen = VARSIZE_ANY_EXHDR(pat);
300
301 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
302
303 PG_RETURN_BOOL(result);
304}

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 307 of file like.c.

308{
310 text *pat = PG_GETARG_TEXT_PP(1);
311 bool result;
312 char *s,
313 *p;
314 int slen,
315 plen;
316
317 s = VARDATA_ANY(str);
318 slen = VARSIZE_ANY_EXHDR(str);
319 p = VARDATA_ANY(pat);
320 plen = VARSIZE_ANY_EXHDR(pat);
321
322 result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
323
324 PG_RETURN_BOOL(result);
325}

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().