📄 varlena.c
字号:
*/ appendStringInfoChar(str, '\\'); continue; } if (so != -1 && eo != -1) { /* * Copy the text that is back reference of regexp. Because so and * eo are counted in characters not bytes, it's easiest to use * text_substring to pull out the correct chunk of text. */ text *append_text; append_text = text_substring(PointerGetDatum(src_text), so + 1, (eo - so), false); appendStringInfoText(str, append_text); pfree(append_text); } }}#define REGEXP_REPLACE_BACKREF_CNT 10/* * replace_text_regexp * * replace text that matches to regexp in src_text to replace_text. * * Note: to avoid having to include regex.h in builtins.h, we declare * the regexp argument as void *, but really it's regex_t *. */text *replace_text_regexp(text *src_text, void *regexp, text *replace_text, bool glob){ text *ret_text; regex_t *re = (regex_t *) regexp; int src_text_len = VARSIZE(src_text) - VARHDRSZ; StringInfo str = makeStringInfo(); int regexec_result; regmatch_t pmatch[REGEXP_REPLACE_BACKREF_CNT]; pg_wchar *data; size_t data_len; int search_start; int data_pos; bool have_escape; /* Convert data string to wide characters. */ data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar)); data_len = pg_mb2wchar_with_len(VARDATA(src_text), data, src_text_len); /* Check whether replace_text has escape char. */ have_escape = check_replace_text_has_escape_char(replace_text); for (search_start = data_pos = 0; search_start <= data_len;) { regexec_result = pg_regexec(re, data, data_len, search_start, NULL, /* no details */ REGEXP_REPLACE_BACKREF_CNT, pmatch, 0); if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH) { char errMsg[100]; /* re failed??? */ pg_regerror(regexec_result, re, errMsg, sizeof(errMsg)); ereport(ERROR, (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION), errmsg("regular expression failed: %s", errMsg))); } if (regexec_result == REG_NOMATCH) break; /* * Copy the text to the left of the match position. Because we are * working with character not byte indexes, it's easiest to use * text_substring to pull out the needed data. */ if (pmatch[0].rm_so - data_pos > 0) { text *left_text; left_text = text_substring(PointerGetDatum(src_text), data_pos + 1, pmatch[0].rm_so - data_pos, false); appendStringInfoText(str, left_text); pfree(left_text); } /* * Copy the replace_text. Process back references when the * replace_text has escape characters. */ if (have_escape) appendStringInfoRegexpSubstr(str, replace_text, pmatch, src_text); else appendStringInfoText(str, replace_text); search_start = data_pos = pmatch[0].rm_eo; /* * When global option is off, replace the first instance only. */ if (!glob) break; /* * Search from next character when the matching text is zero width. */ if (pmatch[0].rm_so == pmatch[0].rm_eo) search_start++; } /* * Copy the text to the right of the last match. */ if (data_pos < data_len) { text *right_text; right_text = text_substring(PointerGetDatum(src_text), data_pos + 1, -1, true); appendStringInfoText(str, right_text); pfree(right_text); } ret_text = PG_STR_GET_TEXT(str->data); pfree(str->data); pfree(str); pfree(data); return ret_text;}/* * split_text * parse input string * return ord item (1 based) * based on provided field separator */Datumsplit_text(PG_FUNCTION_ARGS){ text *inputstring = PG_GETARG_TEXT_P(0); text *fldsep = PG_GETARG_TEXT_P(1); int fldnum = PG_GETARG_INT32(2); int inputstring_len = TEXTLEN(inputstring); int fldsep_len = TEXTLEN(fldsep); int start_posn; int end_posn; text *result_text; /* field number is 1 based */ if (fldnum < 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("field position must be greater than zero"))); /* return empty string for empty input string */ if (inputstring_len < 1) PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); /* empty field separator */ if (fldsep_len < 1) { /* if first field, return input string, else empty string */ if (fldnum == 1) PG_RETURN_TEXT_P(inputstring); else PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); } start_posn = text_position(inputstring, fldsep, fldnum - 1); end_posn = text_position(inputstring, fldsep, fldnum); if ((start_posn == 0) && (end_posn == 0)) /* fldsep not found */ { /* if first field, return input string, else empty string */ if (fldnum == 1) PG_RETURN_TEXT_P(inputstring); else PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); } else if (start_posn == 0) { /* first field requested */ result_text = LEFT(inputstring, fldsep); PG_RETURN_TEXT_P(result_text); } else if (end_posn == 0) { /* last field requested */ result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, -1, true); PG_RETURN_TEXT_P(result_text); } else { /* interior field requested */ result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, end_posn - start_posn - fldsep_len, false); PG_RETURN_TEXT_P(result_text); }}/* * text_to_array * parse input string * return text array of elements * based on provided field separator */Datumtext_to_array(PG_FUNCTION_ARGS){ text *inputstring = PG_GETARG_TEXT_P(0); text *fldsep = PG_GETARG_TEXT_P(1); int inputstring_len = TEXTLEN(inputstring); int fldsep_len = TEXTLEN(fldsep); int fldnum; int start_posn; int end_posn; text *result_text; ArrayBuildState *astate = NULL; /* return NULL for empty input string */ if (inputstring_len < 1) PG_RETURN_NULL(); /* * empty field separator return one element, 1D, array using the input * string */ if (fldsep_len < 1) PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID, CStringGetDatum(inputstring), 1)); /* start with end position holding the initial start position */ end_posn = 0; for (fldnum = 1;; fldnum++) /* field number is 1 based */ { Datum dvalue; bool disnull = false; start_posn = end_posn; end_posn = text_position(inputstring, fldsep, fldnum); if ((start_posn == 0) && (end_posn == 0)) /* fldsep not found */ { if (fldnum == 1) { /* * first element return one element, 1D, array using the input * string */ PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID, CStringGetDatum(inputstring), 1)); } else { /* otherwise create array and exit */ PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext)); } } else if (start_posn == 0) { /* first field requested */ result_text = LEFT(inputstring, fldsep); } else if (end_posn == 0) { /* last field requested */ result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, -1, true); } else { /* interior field requested */ result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, end_posn - start_posn - fldsep_len, false); } /* stash away current value */ dvalue = PointerGetDatum(result_text); astate = accumArrayResult(astate, dvalue, disnull, TEXTOID, CurrentMemoryContext); } /* never reached -- keep compiler quiet */ PG_RETURN_NULL();}/* * array_to_text * concatenate Cstring representation of input array elements * using provided field separator */Datumarray_to_text(PG_FUNCTION_ARGS){ ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); char *fldsep = PG_TEXTARG_GET_STR(1); int nitems, *dims, ndims; char *p; Oid element_type; int typlen; bool typbyval; char typalign; StringInfo result_str = makeStringInfo(); int i; ArrayMetaState *my_extra; p = ARR_DATA_PTR(v); ndims = ARR_NDIM(v); dims = ARR_DIMS(v); nitems = ArrayGetNItems(ndims, dims); /* if there are no elements, return an empty string */ if (nitems == 0) PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); element_type = ARR_ELEMTYPE(v); /* * We arrange to look up info about element type, including its output * conversion proc, only once per series of calls, assuming the element * type doesn't change underneath us. */ my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; if (my_extra == NULL) { fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(ArrayMetaState)); my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; my_extra->element_type = InvalidOid; } if (my_extra->element_type != element_type) { /* * Get info about element type, including its output conversion proc */ get_type_io_data(element_type, IOFunc_output, &my_extra->typlen, &my_extra->typbyval, &my_extra->typalign, &my_extra->typdelim, &my_extra->typioparam, &my_extra->typiofunc); fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc, fcinfo->flinfo->fn_mcxt); my_extra->element_type = element_type; } typlen = my_extra->typlen; typbyval = my_extra->typbyval; typalign = my_extra->typalign; for (i = 0; i < nitems; i++) { Datum itemvalue; char *value; itemvalue = fetch_att(p, typbyval, typlen); value = DatumGetCString(FunctionCall1(&my_extra->proc, itemvalue)); if (i > 0) appendStringInfo(result_str, "%s%s", fldsep, value); else appendStringInfoString(result_str, value); p = att_addlength(p, typlen, PointerGetDatum(p)); p = (char *) att_align(p, typalign); } PG_RETURN_TEXT_P(PG_STR_GET_TEXT(result_str->data));}#define HEXBASE 16/* * Convert a int32 to a string containing a base 16 (hex) representation of * the number. */Datumto_hex32(PG_FUNCTION_ARGS){ uint32 value = (uint32) PG_GETARG_INT32(0); text *result_text; char *ptr; const char *digits = "0123456789abcdef"; char buf[32]; /* bigger than needed, but reasonable */ ptr = buf + sizeof(buf) - 1; *ptr = '\0'; do { *--ptr = digits[value % HEXBASE]; value /= HEXBASE; } while (ptr > buf && value); result_text = PG_STR_GET_TEXT(ptr); PG_RETURN_TEXT_P(result_text);}/* * Convert a int64 to a string containing a base 16 (hex) representation of * the number. */Datumto_hex64(PG_FUNCTION_ARGS){ uint64 value = (uint64) PG_GETARG_INT64(0); text *result_text; char *ptr; const char *digits = "0123456789abcdef"; char buf[32]; /* bigger than needed, but reasonable */ ptr = buf + sizeof(buf) - 1; *ptr = '\0'; do { *--ptr = digits[value % HEXBASE]; value /= HEXBASE; } while (ptr > buf && value); result_text = PG_STR_GET_TEXT(ptr); PG_RETURN_TEXT_P(result_text);}/* * Create an md5 hash of a text string and return it as hex * * md5 produces a 16 byte (128 bit) hash; double it for hex */#define MD5_HASH_LEN 32Datummd5_text(PG_FUNCTION_ARGS){ text *in_text = PG_GETARG_TEXT_P(0); size_t len; char hexsum[MD5_HASH_LEN + 1]; text *result_text; /* Calculate the length of the buffer using varlena metadata */ len = VARSIZE(in_text) - VARHDRSZ; /* get the hash result */ if (pg_md5_hash(VARDATA(in_text), len, hexsum) == false) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); /* convert to text and return it */ result_text = PG_STR_GET_TEXT(hexsum); PG_RETURN_TEXT_P(result_text);}/* * Create an md5 hash of a bytea field and return it as a hex string: * 16-byte md5 digest is represented in 32 hex characters. */Datummd5_bytea(PG_FUNCTION_ARGS){ bytea *in = PG_GETARG_BYTEA_P(0); size_t len; char hexsum[MD5_HASH_LEN + 1]; text *result_text; len = VARSIZE(in) - VARHDRSZ; if (pg_md5_hash(VARDATA(in), len, hexsum) == false) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); result_text = PG_STR_GET_TEXT(hexsum); PG_RETURN_TEXT_P(result_text);}/* * Return the size of a datum, possibly compressed * * Works on any data type */Datumpg_column_size(PG_FUNCTION_ARGS){ Datum value = PG_GETARG_DATUM(0); int32 result; int typlen; /* On first call, get the input type's typlen, and save at *fn_extra */ if (fcinfo->flinfo->fn_extra == NULL) { /* Lookup the datatype of the supplied argument */ Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0); typlen = get_typlen(argtypeid); if (typlen == 0) /* should not happen */ elog(ERROR, "cache lookup failed for type %u", argtypeid); fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(int)); *((int *) fcinfo->flinfo->fn_extra) = typlen; } else typlen = *((int *) fcinfo->flinfo->fn_extra); if (typlen == -1) { /* varlena type, possibly toasted */ result = toast_datum_size(value); } else if (typlen == -2) { /* cstring */ result = strlen(DatumGetCString(value)) + 1; } else { /* ordinary fixed-width type */ result = typlen; } PG_RETURN_INT32(result);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -