PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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, p1len, p2, p2len)   wchareq((p1), (p1len), (p2), (p2len))
 
#define NextChar(p, plen)    do { int __l = pg_mblen_with_len((p), (plen)); (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, p1len, p2, p2len)   (*(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, int p1len, const char *p2, int p2len)
 
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,
  p1len,
  p2,
  p2len 
)    wchareq((p1), (p1len), (p2), (p2len))

Definition at line 96 of file like.c.

◆ CHAREQ [2/2]

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

Definition at line 96 of file like.c.

◆ CopyAdvChar [1/2]

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

Definition at line 99 of file like.c.

100 { int __l = pg_mblen_with_len((src), (srclen)); \
101 (srclen) -= __l; \
102 while (__l-- > 0) \
103 *(dst)++ = *(src)++; \
104 } while (0)

◆ CopyAdvChar [2/2]

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

Definition at line 99 of file like.c.

100 { int __l = pg_mblen_with_len((src), (srclen)); \
101 (srclen) -= __l; \
102 while (__l-- > 0) \
103 *(dst)++ = *(src)++; \
104 } while (0)

◆ do_like_escape [1/2]

#define do_like_escape   MB_do_like_escape

Definition at line 107 of file like.c.

◆ do_like_escape [2/2]

#define do_like_escape   SB_do_like_escape

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

◆ MatchText [2/4]

#define MatchText   SB_MatchText

Definition at line 106 of file like.c.

◆ MatchText [3/4]

#define MatchText   C_IMatchText

Definition at line 106 of file like.c.

◆ MatchText [4/4]

#define MatchText   UTF8_MatchText

Definition at line 106 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_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)

Definition at line 97 of file like.c.

98 { int __l = pg_mblen_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)

◆ NextChar [2/4]

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

Definition at line 97 of file like.c.

98 { int __l = pg_mblen_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)

◆ NextChar [3/4]

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

Definition at line 97 of file like.c.

98 { int __l = pg_mblen_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)

◆ NextChar [4/4]

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

Definition at line 97 of file like.c.

98 { int __l = pg_mblen_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)

Function Documentation

◆ bytealike()

Datum bytealike ( PG_FUNCTION_ARGS  )

Definition at line 318 of file like.c.

319{
322 bool result;
323 char *s,
324 *p;
325 int slen,
326 plen;
327
328 s = VARDATA_ANY(str);
330 p = VARDATA_ANY(pat);
331 plen = VARSIZE_ANY_EXHDR(pat);
332
333 result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE);
334
336}
uint32 result
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
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:835
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

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

◆ byteanlike()

Datum byteanlike ( PG_FUNCTION_ARGS  )

Definition at line 339 of file like.c.

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

References fb(), LIKE_TRUE, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, result, 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;
171 pg_locale_t locale;
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 */
181 errmsg("could not determine which collation to use for ILIKE"),
182 errhint("Use the COLLATE clause to set the collation explicitly.")));
183 }
184
185 locale = pg_newlocale_from_collation(collation);
186
187 if (!locale->deterministic)
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
201 if (locale->ctype_is_c && pg_database_encoding_max_length() == 1)
202 {
203 p = VARDATA_ANY(pat);
204 plen = VARSIZE_ANY_EXHDR(pat);
205 s = VARDATA_ANY(str);
207 return C_IMatchText(s, slen, p, plen, locale);
208 }
209 else
210 {
213 p = VARDATA_ANY(pat);
214 plen = VARSIZE_ANY_EXHDR(pat);
217 s = VARDATA_ANY(str);
219
221 return UTF8_MatchText(s, slen, p, plen, 0);
222 else if (pg_database_encoding_max_length() > 1)
223 return MB_MatchText(s, slen, p, plen, 0);
224 else
225 return SB_MatchText(s, slen, p, plen, 0);
226 }
227}
#define OidIsValid(objectId)
Definition c.h:917
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
Definition fmgr.c:794
#define DatumGetTextPP(X)
Definition fmgr.h:293
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)
#define PG_UTF8
Definition mbprint.c:43
int GetDatabaseEncoding(void)
Definition mbutils.c:1389
int pg_database_encoding_max_length(void)
Definition mbutils.c:1673
static char * errmsg
Datum lower(PG_FUNCTION_ARGS)
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition pg_locale.c:1189
#define PointerGetDatum(X)
Definition postgres.h:354

References C_IMatchText(), pg_locale_struct::ctype_is_c, DatumGetTextPP, pg_locale_struct::deterministic, DirectFunctionCall1Coll(), ereport, errcode(), errhint(), errmsg, ERROR, fb(), GetDatabaseEncoding(), 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{
140 pg_locale_t locale;
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 */
150 errmsg("could not determine which collation to use for LIKE"),
151 errhint("Use the COLLATE clause to set the collation explicitly.")));
152 }
153
154 locale = pg_newlocale_from_collation(collation);
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, fb(), GetDatabaseEncoding(), 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 422 of file like.c.

423{
426 text *result;
427
430 else
432
434}
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
static text * MB_do_like_escape(text *pat, text *esc)
static text * SB_do_like_escape(text *pat, text *esc)

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

◆ like_escape_bytea()

Datum like_escape_bytea ( PG_FUNCTION_ARGS  )

Definition at line 441 of file like.c.

442{
446
448}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373

References fb(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, result, 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 364 of file like.c.

365{
368 bool result;
369 text *strtext;
370
372 NameGetDatum(str)));
374
376}
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:688
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#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:406
Definition c.h:889
Datum name_text(PG_FUNCTION_ARGS)
Definition varlena.c:2705

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

◆ nameicnlike()

◆ namelike()

Datum namelike ( PG_FUNCTION_ARGS  )

Definition at line 234 of file like.c.

235{
238 bool result;
239 char *s,
240 *p;
241 int slen,
242 plen;
243
244 s = NameStr(*str);
245 slen = strlen(s);
246 p = VARDATA_ANY(pat);
247 plen = VARSIZE_ANY_EXHDR(pat);
248
250
252}
#define NameStr(name)
Definition c.h:894
static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
Definition like.c:138

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

◆ namenlike()

Datum namenlike ( PG_FUNCTION_ARGS  )

Definition at line 255 of file like.c.

256{
259 bool result;
260 char *s,
261 *p;
262 int slen,
263 plen;
264
265 s = NameStr(*str);
266 slen = strlen(s);
267 p = VARDATA_ANY(pat);
268 plen = VARSIZE_ANY_EXHDR(pat);
269
271
273}

References fb(), GenericMatchText(), LIKE_TRUE, NameStr, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, result, 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  )

◆ texticnlike()

Datum texticnlike ( PG_FUNCTION_ARGS  )

◆ textlike()

Datum textlike ( PG_FUNCTION_ARGS  )

Definition at line 276 of file like.c.

277{
280 bool result;
281 char *s,
282 *p;
283 int slen,
284 plen;
285
286 s = VARDATA_ANY(str);
288 p = VARDATA_ANY(pat);
289 plen = VARSIZE_ANY_EXHDR(pat);
290
292
294}

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

◆ textnlike()

Datum textnlike ( PG_FUNCTION_ARGS  )

Definition at line 297 of file like.c.

298{
301 bool result;
302 char *s,
303 *p;
304 int slen,
305 plen;
306
307 s = VARDATA_ANY(str);
309 p = VARDATA_ANY(pat);
310 plen = VARSIZE_ANY_EXHDR(pat);
311
313
315}

References fb(), GenericMatchText(), LIKE_TRUE, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, result, 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,
int  p1len,
const char p2,
int  p2len 
)
inlinestatic

Definition at line 58 of file like.c.

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

References fb(), and pg_mblen_with_len().