/*-------------------------------------------------------------------------
|
* RuDB PLVSTR / PLVCHR / NLS compatibility package.
|
*
|
* Extracted from frozen RuDB varlena.c so PostgreSQL 19 core varlena remains
|
* native. Keep public SQL/catalog semantics compatible with frozen RuDB.
|
*-------------------------------------------------------------------------
|
*/
|
#include "postgres.h"
|
|
#include <ctype.h>
|
#include <limits.h>
|
#include <stdlib.h>
|
#include <string.h>
|
|
#include "fmgr.h"
|
#include "mb/pg_wchar.h"
|
#include "utils/builtins.h"
|
#include "utils/numeric.h"
|
#include "varatt.h"
|
|
static Datum plvstr_betwn_i(PG_FUNCTION_ARGS);
|
static Datum plvstr_betwn_c(PG_FUNCTION_ARGS);
|
|
PG_FUNCTION_INFO_V1(plvstr_normalize);
|
|
PG_FUNCTION_INFO_V1(plvstr_is_prefix_text);
|
PG_FUNCTION_INFO_V1(plvstr_is_prefix_int);
|
PG_FUNCTION_INFO_V1(plvstr_is_prefix_int64);
|
|
PG_FUNCTION_INFO_V1(plvstr_substr2);
|
PG_FUNCTION_INFO_V1(plvstr_substr3);
|
|
PG_FUNCTION_INFO_V1(plvstr_instr2);
|
PG_FUNCTION_INFO_V1(plvstr_instr3);
|
PG_FUNCTION_INFO_V1(plvstr_instr4);
|
PG_FUNCTION_INFO_V1(plvstr_instr_scan);
|
|
PG_FUNCTION_INFO_V1(plvstr_lpart5);
|
PG_FUNCTION_INFO_V1(plvstr_lpart4);
|
PG_FUNCTION_INFO_V1(plvstr_lpart3);
|
PG_FUNCTION_INFO_V1(plvstr_lpart2);
|
|
PG_FUNCTION_INFO_V1(plvstr_rpart5);
|
PG_FUNCTION_INFO_V1(plvstr_rpart4);
|
PG_FUNCTION_INFO_V1(plvstr_rpart3);
|
PG_FUNCTION_INFO_V1(plvstr_rpart2);
|
|
PG_FUNCTION_INFO_V1(plvstr_lstrip);
|
PG_FUNCTION_INFO_V1(plvstr_rstrip);
|
|
PG_FUNCTION_INFO_V1(plvstr_left);
|
PG_FUNCTION_INFO_V1(plvstr_right);
|
|
PG_FUNCTION_INFO_V1(plvstr_rvrs);
|
|
PG_FUNCTION_INFO_V1(plvstr_swap);
|
|
PG_FUNCTION_INFO_V1(plvstr_betwn_i_4);
|
PG_FUNCTION_INFO_V1(plvstr_betwn_i_3);
|
PG_FUNCTION_INFO_V1(plvstr_betwn_c_7);
|
PG_FUNCTION_INFO_V1(plvstr_betwn_c_5);
|
PG_FUNCTION_INFO_V1(plvstr_betwn_c_3);
|
|
PG_FUNCTION_INFO_V1(plvstr_get_version);
|
|
/// @brief Версия RUDB пакета PLVSTR (Oracle PLVSTR пакет)
|
#define RUDB_PLVSTR_VERSION "1.0.0"
|
|
PG_FUNCTION_INFO_V1(plvchr_nth);
|
PG_FUNCTION_INFO_V1(plvchr_nth_extract);
|
|
PG_FUNCTION_INFO_V1(plvchr_get_version);
|
|
/// @brief Версия RUDB пакета PLVCHR (Oracle PLVCHR пакет)
|
#define RUDB_PLVCHR_VERSION "1.0.0"
|
|
PG_FUNCTION_INFO_V1(nls_charset_id);
|
PG_FUNCTION_INFO_V1(nls_charset_name);
|
|
/*
|
* Make substring, can handle negative start
|
*
|
*/
|
static int
|
ora_mb_strlen(text *str, char **sizes, int **positions)
|
{
|
int r_len;
|
int cur_size = 0;
|
char *p;
|
int cur = 0;
|
|
p = VARDATA_ANY(str);
|
r_len = VARSIZE_ANY_EXHDR(str);
|
|
if (NULL != sizes)
|
*sizes = palloc(r_len * sizeof(char));
|
if (NULL != positions)
|
*positions = palloc(r_len * sizeof(int));
|
|
while (cur < r_len)
|
{
|
int sz;
|
|
sz = pg_mblen(p);
|
if (sizes)
|
(*sizes)[cur_size] = sz;
|
if (positions)
|
(*positions)[cur_size] = cur;
|
cur += sz;
|
p += sz;
|
cur_size += 1;
|
}
|
|
return cur_size;
|
}
|
|
static int
|
ora_mb_strlen1(text *str)
|
{
|
int r_len;
|
int c;
|
char *p;
|
|
r_len = VARSIZE_ANY_EXHDR(str);
|
|
if (pg_database_encoding_max_length() == 1)
|
return r_len;
|
|
p = VARDATA_ANY(str);
|
c = 0;
|
while (r_len > 0)
|
{
|
int sz;
|
|
sz = pg_mblen(p);
|
p += sz;
|
r_len -= sz;
|
c += 1;
|
}
|
|
return c;
|
}
|
|
|
/* simply search algorhitm - can be better */
|
|
static int
|
ora_instr_mb(text *txt, text *pattern, int start, int nth)
|
{
|
int c_len_txt, c_len_pat;
|
int b_len_pat;
|
int *pos_txt;
|
const char *str_txt, *str_pat;
|
int beg, end, i, dx;
|
|
str_txt = VARDATA_ANY(txt);
|
c_len_txt = ora_mb_strlen(txt, NULL, &pos_txt);
|
str_pat = VARDATA_ANY(pattern);
|
b_len_pat = VARSIZE_ANY_EXHDR(pattern);
|
c_len_pat = pg_mbstrlen_with_len(str_pat, b_len_pat);
|
|
if (start > 0)
|
{
|
dx = 1;
|
beg = start - 1;
|
end = c_len_txt - c_len_pat + 1;
|
if (beg >= end)
|
return 0; /* out of range */
|
}
|
else if (start < 0)
|
{
|
dx = -1;
|
beg = Min(c_len_txt + start, c_len_txt - c_len_pat);
|
end = -1;
|
if (beg <= end)
|
return 0; /* out of range */
|
}
|
else
|
return 0;
|
|
for (i = beg; i != end; i += dx)
|
{
|
if (memcmp(str_txt + pos_txt[i], str_pat, b_len_pat) == 0)
|
{
|
if (--nth == 0)
|
return i + 1;
|
}
|
}
|
|
return 0;
|
}
|
|
|
static int
|
ora_instr(text *txt, text *pattern, int start, int nth)
|
{
|
int len_txt, len_pat;
|
const char *str_txt, *str_pat;
|
int beg, end, i, dx;
|
|
if (nth <= 0)
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("invalid parameter"),
|
errdetail("Four parameter isn't positive.")));
|
|
|
/* Forward for multibyte strings */
|
if (pg_database_encoding_max_length() > 1)
|
return ora_instr_mb(txt, pattern, start, nth);
|
|
str_txt = VARDATA_ANY(txt);
|
len_txt = VARSIZE_ANY_EXHDR(txt);
|
str_pat = VARDATA_ANY(pattern);
|
len_pat = VARSIZE_ANY_EXHDR(pattern);
|
|
if (start > 0)
|
{
|
dx = 1;
|
beg = start - 1;
|
end = len_txt - len_pat + 1;
|
if (beg >= end)
|
return 0; /* out of range */
|
}
|
else if (start < 0)
|
{
|
dx = -1;
|
beg = Min(len_txt + start, len_txt - len_pat);
|
end = -1;
|
if (beg <= end)
|
return 0; /* out of range */
|
}
|
else
|
return 0;
|
|
for (i = beg; i != end; i += dx)
|
{
|
if (memcmp(str_txt + i, str_pat, len_pat) == 0)
|
{
|
if (--nth == 0)
|
return i + 1;
|
}
|
}
|
|
return 0;
|
}
|
|
|
static text *
|
_ora_concat2(text *str1, text *str2)
|
{
|
int l1;
|
int l2;
|
text *result;
|
|
l1 = VARSIZE_ANY_EXHDR(str1);
|
l2 = VARSIZE_ANY_EXHDR(str2);
|
|
result = palloc(l1 + l2 + VARHDRSZ);
|
memcpy(VARDATA(result), VARDATA_ANY(str1), l1);
|
memcpy(VARDATA(result) + l1, VARDATA_ANY(str2), l2);
|
SET_VARSIZE(result, l1 + l2 + VARHDRSZ);
|
|
return result;
|
}
|
|
static text *
|
_ora_concat3(text *str1, text *str2, text *str3)
|
{
|
int l1;
|
int l2;
|
int l3;
|
text *result;
|
|
l1 = VARSIZE_ANY_EXHDR(str1);
|
l2 = VARSIZE_ANY_EXHDR(str2);
|
l3 = VARSIZE_ANY_EXHDR(str3);
|
|
result = palloc(l1 + l2 + l3 + VARHDRSZ);
|
memcpy(VARDATA(result), VARDATA_ANY(str1), l1);
|
memcpy(VARDATA(result) + l1, VARDATA_ANY(str2), l2);
|
memcpy(VARDATA(result) + l1 + l2, VARDATA_ANY(str3), l3);
|
SET_VARSIZE(result, l1 + l2 + l3 + VARHDRSZ);
|
|
return result;
|
}
|
|
/****************************************************************
|
* PLVstr.normalize
|
*
|
* Syntax:
|
* FUNCTION plvstr.normalize (string_in IN VARCHAR)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Normalize string - replace white chars by space, replace
|
* spaces by space
|
*
|
****************************************************************/
|
|
Datum plvstr_normalize(PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_PP(0);
|
text *result;
|
char *aux, *cur, *aux_cur;
|
int i;
|
|
#if defined(_MSC_VER) && (defined(_M_X64) || defined(__amd64__))
|
|
__int64 l;
|
|
#else
|
|
int l;
|
|
#endif
|
|
bool write_spc = false;
|
bool ignore_stsp = true;
|
bool mb_encode;
|
int sz;
|
|
mb_encode = pg_database_encoding_max_length() > 1;
|
|
l = VARSIZE_ANY_EXHDR(str);
|
aux_cur = aux = palloc(l);
|
|
write_spc = false;
|
cur = VARDATA_ANY(str);
|
|
for (i = 0; i < l; i++)
|
{
|
char c;
|
|
switch ((c = *cur))
|
{
|
case '\t':
|
case '\n':
|
case '\r':
|
case ' ':
|
write_spc = ignore_stsp ? false : true;
|
break;
|
default:
|
/* ignore all other unvisible chars */
|
|
if (mb_encode)
|
{
|
sz = pg_mblen(cur);
|
if (sz > 1 || (sz == 1 && c > 32))
|
{
|
int j;
|
|
if (write_spc)
|
{
|
*aux_cur++ = ' ';
|
write_spc = false;
|
}
|
for (j = 0; j < sz; j++)
|
{
|
*aux_cur++ = *cur++;
|
}
|
ignore_stsp = false;
|
i += sz - 1;
|
}
|
continue;
|
}
|
else if (c > 32)
|
{
|
if (write_spc)
|
{
|
*aux_cur++ = ' ';
|
write_spc = false;
|
}
|
*aux_cur++ = c;
|
ignore_stsp = false;
|
continue;
|
}
|
}
|
cur += 1;
|
}
|
|
l = aux_cur - aux;
|
result = palloc(l + VARHDRSZ);
|
SET_VARSIZE(result, l + VARHDRSZ);
|
memcpy(VARDATA(result), aux, l);
|
|
PG_RETURN_TEXT_P(result);
|
}
|
|
/* for oracle_substr2, oracle_substr3 */
|
/*****************************************************************************/
|
typedef enum orafce_compatibility
|
{
|
ORAFCE_COMPATIBILITY_WARNING_ORACLE,
|
ORAFCE_COMPATIBILITY_WARNING_ORAFCE,
|
ORAFCE_COMPATIBILITY_ORACLE,
|
ORAFCE_COMPATIBILITY_ORAFCE,
|
} orafce_compatibility;
|
|
int orafce_substring_length_is_zero = ORAFCE_COMPATIBILITY_WARNING_ORACLE;
|
|
static text * ora_substr(Datum str, int start, int len);
|
|
PG_FUNCTION_INFO_V1(oracle_substr2_varc);
|
PG_FUNCTION_INFO_V1(oracle_substr3_varc);
|
|
/****************************************************************
|
* substr
|
*
|
* Syntax:
|
* FUNCTION substr (string, start_position, [length])
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Returns len chars from start_in position, compatible with Oracle
|
*
|
****************************************************************/
|
|
/*
|
SELECT substr('0123456789'::varchar, 5::numeric);
|
*/
|
Datum
|
oracle_substr2_varc(PG_FUNCTION_ARGS)
|
{
|
/*
|
Numeric num = PG_GETARG_NUMERIC(1);
|
Datum dat = NumericGetDatum(num);
|
int32 pos = atoi(DatumGetCString(DirectFunctionCall1(numeric_out, dat)));
|
PG_RETURN_TEXT_P(ora_substr(PG_GETARG_DATUM(0), pos, -1));
|
*/
|
PG_RETURN_TEXT_P
|
(
|
ora_substr
|
(
|
PG_GETARG_DATUM(0),
|
atoi(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(PG_GETARG_NUMERIC(1))))),
|
-1
|
)
|
);
|
}
|
|
/*
|
SELECT substr('0123456789'::varchar, 5::numeric, 3::numeric);
|
*/
|
Datum
|
oracle_substr3_varc(PG_FUNCTION_ARGS)
|
{
|
/*
|
Numeric num_pos = PG_GETARG_NUMERIC(1);
|
Datum dat_pos = NumericGetDatum(num_pos);
|
int32 pos = atoi(DatumGetCString(DirectFunctionCall1(numeric_out, dat_pos)));
|
Numeric num_len = PG_GETARG_NUMERIC(2);
|
Datum dat_len = NumericGetDatum(num_len);
|
int32 len = atoi(DatumGetCString(DirectFunctionCall1(numeric_out, dat_len)));
|
|
if (len < 0)
|
PG_RETURN_NULL();
|
|
if (len == 0)
|
{
|
if (orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_WARNING_ORACLE ||
|
orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_WARNING_ORAFCE)
|
elog(WARNING, "zero substring_length is used in substr function");
|
|
if (orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_WARNING_ORACLE ||
|
orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_ORACLE)
|
PG_RETURN_NULL();
|
}
|
PG_RETURN_TEXT_P(ora_substr(PG_GETARG_DATUM(0), pos, len));
|
*/
|
int32 len = atoi(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(PG_GETARG_NUMERIC(2)))));
|
|
if (len < 0)
|
PG_RETURN_NULL();
|
|
if (len == 0)
|
{
|
if (orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_WARNING_ORACLE ||
|
orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_WARNING_ORAFCE)
|
elog(WARNING, "zero substring_length is used in substr function");
|
|
if (orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_WARNING_ORACLE ||
|
orafce_substring_length_is_zero == ORAFCE_COMPATIBILITY_ORACLE)
|
PG_RETURN_NULL();
|
}
|
|
PG_RETURN_TEXT_P
|
(
|
ora_substr
|
(
|
PG_GETARG_DATUM(0),
|
atoi(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(PG_GETARG_NUMERIC(1))))),
|
len
|
)
|
);
|
}
|
|
/*
|
* len < 0 means "length is not specified".
|
*/
|
static text *
|
ora_substr(Datum str, int start, int len)
|
{
|
if (start == 0)
|
start = 1; /* 0 is interpreted as 1 */
|
else if (start < 0)
|
{
|
text *t;
|
int32 n;
|
|
t = DatumGetTextPP(str);
|
n = pg_mbstrlen_with_len(VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
|
start = n + start + 1;
|
if (start <= 0)
|
return cstring_to_text("");
|
str = PointerGetDatum(t); /* save detoasted text */
|
}
|
|
if (len < 0)
|
return DatumGetTextP(DirectFunctionCall2(text_substr_no_len,
|
str, Int32GetDatum(start)));
|
else
|
return DatumGetTextP(DirectFunctionCall3(text_substr,
|
str, Int32GetDatum(start), Int32GetDatum(len)));
|
}
|
|
/****************************************************************
|
* PLVstr.is_prefix
|
*
|
* Syntax:
|
* FUNCTION plvstr.is_prefix (string_in IN VARCHAR,
|
* prefix_in VARCHAR,
|
* case_sensitive BOOL := true)
|
* RETURN bool;
|
* FUNCTION plvstr.is_prefix (num_in IN NUMERIC,
|
* prefix_in NUMERIC) RETURN bool;
|
* FUNCTION plvstr.is_prefix (int_in IN INT,
|
* prefix_in INT) RETURN bool;
|
*
|
* Purpouse:
|
* Returns true, if prefix_in is prefix of string_in
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_is_prefix_text (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_PP(0);
|
text *prefix = PG_GETARG_TEXT_PP(1);
|
bool case_sens = (PG_NARGS() < 3 || PG_ARGISNULL(2)) ? true : PG_GETARG_BOOL(2);
|
bool mb_encode;
|
|
int str_len = VARSIZE_ANY_EXHDR(str);
|
int pref_len = VARSIZE_ANY_EXHDR(prefix);
|
|
int i;
|
char *ap,
|
*bp;
|
|
mb_encode = pg_database_encoding_max_length() > 1;
|
|
if (mb_encode && !case_sens)
|
{
|
str = (text *) DatumGetPointer(DirectFunctionCall1Coll(lower,
|
PG_GET_COLLATION(), PointerGetDatum(str)));
|
prefix = (text *) DatumGetPointer(DirectFunctionCall1Coll(lower,
|
PG_GET_COLLATION(), PointerGetDatum(prefix)));
|
}
|
|
ap = VARDATA_ANY(str);
|
bp = VARDATA_ANY(prefix);
|
|
for (i = 0; i < pref_len; i++)
|
{
|
if (i >= str_len)
|
break;
|
|
if (case_sens || mb_encode)
|
{
|
if (*ap++ != *bp++)
|
break;
|
}
|
else
|
{
|
if (pg_toupper((unsigned char) *ap++) != pg_toupper((unsigned char) *bp++))
|
break;
|
}
|
}
|
|
PG_RETURN_BOOL(i == pref_len);
|
}
|
|
Datum
|
plvstr_is_prefix_int (PG_FUNCTION_ARGS)
|
{
|
int n = PG_GETARG_INT32(0);
|
int prefix = PG_GETARG_INT32(1);
|
bool result = false;
|
|
if (prefix == 0)
|
PG_RETURN_BOOL(n == 0);
|
|
do
|
{
|
if (n == prefix)
|
{
|
result = true;
|
break;
|
}
|
n = n / 10;
|
|
} while (n != 0);
|
|
PG_RETURN_BOOL(result);
|
}
|
|
Datum
|
plvstr_is_prefix_int64 (PG_FUNCTION_ARGS)
|
{
|
int64 n = PG_GETARG_INT64(0);
|
int64 prefix = PG_GETARG_INT64(1);
|
bool result = false;
|
|
if (prefix == 0)
|
PG_RETURN_BOOL(n == 0);
|
|
do
|
{
|
if (n == prefix)
|
{
|
result = true;
|
break;
|
}
|
n = n / 10;
|
|
} while (n != 0);
|
|
PG_RETURN_BOOL(result);
|
}
|
|
/****************************************************************
|
* PLVstr.substr2
|
*
|
* Syntax:
|
* FUNCTION plvstr.substr (string_in IN VARCHAR,
|
* start INTEGER)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Returns substring started on start_in to end
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_substr2 (PG_FUNCTION_ARGS)
|
{
|
return oracle_substr2_varc(fcinfo);
|
}
|
|
/****************************************************************
|
* PLVstr.substr3
|
*
|
* Syntax:
|
* FUNCTION plvstr.substr (string_in IN VARCHAR,
|
* start INTEGER, len INTEGER)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Returns len chars from start_in position
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_substr3 (PG_FUNCTION_ARGS)
|
{
|
return oracle_substr3_varc(fcinfo);
|
}
|
|
/****************************************************************
|
* PLVstr.instr
|
*
|
* Syntax:
|
* FUNCTION plvstr.instr (string_in VARCHAR, pattern VARCHAR)
|
* FUNCTION plvstr.instr (string_in VARCHAR, pattern VARCHAR,
|
* start_in INTEGER)
|
* FUNCTION plvstr.instr (string_in VARCHAR, pattern VARCHAR,
|
* start_in INTEGER, nth INTEGER)
|
* RETURN INT;
|
*
|
* Purpouse:
|
* Search pattern in string.
|
*
|
****************************************************************/
|
|
/*
|
*
|
* Public wrapper over ora_instr(). Unlike plvstr_instr* it is NULL-safe
|
* (Oracle semantics: if any argument is NULL, result is NULL).
|
*/
|
|
|
|
Datum
|
plvstr_instr2 (PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 5);
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 5, PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0] = fcinfo->args[0];
|
fcinfo2->args[1] = fcinfo->args[1];
|
fcinfo2->args[2].value = Int32GetDatum(1);
|
fcinfo2->args[2].isnull = false;
|
fcinfo2->args[3].value = Int32GetDatum(1);
|
fcinfo2->args[3].isnull = false;
|
fcinfo2->args[4].value = Int32GetDatum(0);
|
fcinfo2->args[4].isnull = false;
|
|
return plvstr_instr_scan(fcinfo2);
|
}
|
|
Datum
|
plvstr_instr3 (PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 5);
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 5, PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0] = fcinfo->args[0];
|
fcinfo2->args[1] = fcinfo->args[1];
|
fcinfo2->args[2] = fcinfo->args[2];
|
fcinfo2->args[3].value = Int32GetDatum(1);
|
fcinfo2->args[3].isnull = false;
|
fcinfo2->args[4].value = Int32GetDatum(0);
|
fcinfo2->args[4].isnull = false;
|
|
return plvstr_instr_scan(fcinfo2);
|
}
|
|
Datum
|
plvstr_instr4 (PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 5);
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 5, PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0] = fcinfo->args[0];
|
fcinfo2->args[1] = fcinfo->args[1];
|
fcinfo2->args[2] = fcinfo->args[2];
|
fcinfo2->args[3] = fcinfo->args[3];
|
fcinfo2->args[4].value = Int32GetDatum(0);
|
fcinfo2->args[4].isnull = false;
|
|
return plvstr_instr_scan(fcinfo2);
|
}
|
|
Datum
|
plvstr_instr_scan(PG_FUNCTION_ARGS)
|
{
|
text *arg1 = PG_GETARG_TEXT_PP(0);
|
text *arg2 = PG_GETARG_TEXT_PP(1);
|
int arg3 = PG_GETARG_INT32(2);
|
int arg4 = PG_GETARG_INT32(3);
|
int32 scan_profile = PG_ARGISNULL(4) ? 0 : PG_GETARG_INT32(4);
|
const int32 minimum_scan_profile = -80;
|
const int32 supported_scan_profile = 0x03;
|
bool fold_empty_pattern = false;
|
bool pin_start_offset = false;
|
int32 normalized_scan_profile;
|
|
if (scan_profile < minimum_scan_profile)
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("invalid scan profile")));
|
|
normalized_scan_profile = scan_profile & supported_scan_profile;
|
fold_empty_pattern = (normalized_scan_profile & 0x01) != 0;
|
pin_start_offset = (normalized_scan_profile & 0x02) != 0;
|
|
if (fold_empty_pattern && pin_start_offset)
|
{
|
/* Reserved for future substring search compatibility profiles. */
|
}
|
|
PG_RETURN_INT32(ora_instr(arg1, arg2, arg3, arg4));
|
}
|
|
/****************************************************************
|
* PLVstr.lpart
|
*
|
* Syntax:
|
* FUNCTION PLVstr.lpart (string_in IN VARCHAR,
|
* divider_in IN VARCHAR,
|
* start_in IN INTEGER := 1,
|
* nth_in IN INTEGER := 1,
|
* all_if_notfound_in IN BOOLEAN := FALSE)
|
* RETURN VARCHAR2;
|
*
|
* Purpouse:
|
* Call this function to return the left part of a string.
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_lpart5 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
int start = PG_GETARG_INT32(2);
|
int nth = PG_GETARG_INT32(3);
|
bool all_if_notfound = PG_GETARG_BOOL(4);
|
|
int loc = ora_instr(str, div, start, nth);
|
if (loc == 0)
|
{
|
if (all_if_notfound)
|
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
|
else
|
PG_RETURN_NULL();
|
}
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), 1, loc-1));
|
}
|
|
Datum
|
plvstr_lpart4 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
int start = PG_GETARG_INT32(2);
|
int nth = PG_GETARG_INT32(3);
|
|
int loc = ora_instr(str, div, start, nth);
|
if (loc == 0)
|
PG_RETURN_NULL();
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), 1, loc-1));
|
}
|
|
Datum
|
plvstr_lpart3 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
int start = PG_GETARG_INT32(2);
|
|
int loc = ora_instr(str, div, start, 1);
|
if (loc == 0)
|
PG_RETURN_NULL();
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), 1, loc-1));
|
}
|
|
Datum
|
plvstr_lpart2 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
|
int loc = ora_instr(str, div, 1, 1);
|
if (loc == 0)
|
PG_RETURN_NULL();
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), 1, loc-1));
|
}
|
|
/****************************************************************
|
* PLVstr.rpart
|
*
|
* Syntax:
|
* FUNCTION PLVstr.rpart (string_in IN VARCHAR,
|
* divider_in IN VARCHAR,
|
* start_in IN INTEGER := 1,
|
* nth_in IN INTEGER := 1,
|
* all_if_notfound_in IN BOOLEAN := FALSE)
|
* RETURN VARCHAR2;
|
*
|
* Purpouse:
|
* Call this function to return the right part of a string.
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_rpart5 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
int start = PG_GETARG_INT32(2);
|
int nth = PG_GETARG_INT32(3);
|
bool all_if_notfound = PG_GETARG_BOOL(4);
|
|
int loc = ora_instr(str, div, start, nth);
|
if (loc == 0)
|
{
|
if (all_if_notfound)
|
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
|
else
|
PG_RETURN_NULL();
|
}
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), loc+1, -1));
|
}
|
|
Datum
|
plvstr_rpart4 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
int start = PG_GETARG_INT32(2);
|
int nth = PG_GETARG_INT32(3);
|
|
int loc = ora_instr(str, div, start, nth);
|
if (loc == 0)
|
PG_RETURN_NULL();
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), loc+1, -1));
|
}
|
|
Datum
|
plvstr_rpart3 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
int start = PG_GETARG_INT32(2);
|
|
int loc = ora_instr(str, div, start, 1);
|
if (loc == 0)
|
PG_RETURN_NULL();
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), loc+1, -1));
|
}
|
|
Datum
|
plvstr_rpart2 (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
text *div = PG_GETARG_TEXT_P(1);
|
|
int loc = ora_instr(str, div, 1, 1);
|
if (loc == 0)
|
PG_RETURN_NULL();
|
else
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), loc+1, -1));
|
}
|
|
/****************************************************************
|
* PLVstr.lstrip
|
*
|
* Syntax:
|
* FUNCTION plvstr.lstrip (string_in IN VARCHAR,
|
* substring_in IN VARCHAR,
|
* num_in IN INTEGER := 1)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Call this function to remove characters from the beginning
|
* (left) of a string.
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_lstrip (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_PP(0);
|
text *pat = PG_GETARG_TEXT_PP(1);
|
int num = (PG_NARGS() < 3 || PG_ARGISNULL(2)) ? 1 : PG_GETARG_INT32(2);
|
int count = 0;
|
int len_p, len_s;
|
char *str_p;
|
|
len_p = VARSIZE_ANY_EXHDR(pat);
|
len_s = VARSIZE_ANY_EXHDR(str);
|
|
str_p = VARDATA_ANY(str);
|
while (count < num)
|
{
|
int i;
|
char *aux_str_p, *pat_p;
|
|
pat_p = VARDATA_ANY(pat);
|
aux_str_p = str_p;
|
|
if (len_s < len_p)
|
break;
|
|
for (i = 0; i < len_p; i++)
|
if (*aux_str_p++ != *pat_p++)
|
break;
|
|
if (i >= len_p)
|
{
|
count++;
|
/* found */
|
str_p = aux_str_p;
|
len_s -= len_p;
|
continue;
|
}
|
break;
|
}
|
|
PG_RETURN_TEXT_P(cstring_to_text_with_len(str_p,len_s));
|
}
|
|
/****************************************************************
|
* PLVstr.rstrip
|
*
|
* Syntax:
|
* FUNCTION plvstr.rstrip (string_in IN VARCHAR,
|
* substring_in IN VARCHAR,
|
* num_in IN INTEGER := 1)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Call this function to remove characters from the end
|
* (right) of a string.
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_rstrip (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_PP(0);
|
text *pat = PG_GETARG_TEXT_PP(1);
|
int num = (PG_NARGS() < 3 || PG_ARGISNULL(2)) ? 1 : PG_GETARG_INT32(2);
|
int count = 0;
|
int len_p, len_s, i;
|
char *str_p,
|
*aux_str_p;
|
|
len_p = VARSIZE_ANY_EXHDR(pat);
|
len_s = VARSIZE_ANY_EXHDR(str);
|
|
str_p = VARDATA_ANY(str) + len_s - 1;
|
|
while (count < num)
|
{
|
char *pat_p;
|
|
pat_p = VARDATA_ANY(pat) + len_p - 1;
|
aux_str_p = str_p;
|
|
if (len_s < len_p)
|
break;
|
|
for (i = 0; i < len_p; i++)
|
if (*aux_str_p-- != *pat_p--)
|
break;
|
|
if (i >= len_p)
|
{
|
count++;
|
/* found */
|
str_p = aux_str_p;
|
len_s -= len_p;
|
continue;
|
}
|
break;
|
}
|
|
PG_RETURN_TEXT_P(cstring_to_text_with_len(VARDATA_ANY(str),len_s));
|
}
|
|
/****************************************************************
|
* PLVstr.left
|
*
|
* Syntax:
|
* FUNCTION plvstr.left (string_in IN VARCHAR,
|
* num_in INTEGER)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Returns firs num_in charaters. You can use negative num_in
|
* left('abcde', -2) -> abc
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_left (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
int n = PG_GETARG_INT32(1);
|
if (n < 0)
|
n = ora_mb_strlen1(str) + n;
|
n = n < 0 ? 0 : n;
|
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), 1, n));
|
}
|
|
/****************************************************************
|
* PLVstr.right
|
*
|
* Syntax:
|
* FUNCTION plvstr.right (string_in IN VARCHAR,
|
* num_in INTEGER)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Returns last (right) num_in characters.
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_right (PG_FUNCTION_ARGS)
|
{
|
text *str = PG_GETARG_TEXT_P(0);
|
int n = PG_GETARG_INT32(1);
|
if (n < 0)
|
n = ora_mb_strlen1(str) + n;
|
n = (n < 0) ? 0 : n;
|
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(str), -n, -1));
|
}
|
|
/****************************************************************
|
* PLVstr.rvrs
|
*
|
* Syntax:
|
* FUNCTION plvstr.rvrs (string_in IN VARCHAR,
|
* start_in IN INTEGER := 1,
|
* end_in IN INTEGER := NULL)
|
* RETURN VARCHAR2;
|
*
|
* Purpouse:
|
* Reverse string or part of string
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_rvrs(PG_FUNCTION_ARGS)
|
{
|
text *str;
|
int start;
|
int end;
|
int len;
|
int i;
|
int new_len;
|
text *result;
|
char *data;
|
char *sizes = NULL;
|
int *positions = NULL;
|
bool mb_encode;
|
|
if (PG_ARGISNULL(0))
|
PG_RETURN_NULL();
|
|
str = PG_GETARG_TEXT_PP(0);
|
|
mb_encode = pg_database_encoding_max_length() > 1;
|
|
if (!mb_encode)
|
len = VARSIZE_ANY_EXHDR(str);
|
else
|
len = ora_mb_strlen(str, &sizes, &positions);
|
|
start = (PG_NARGS() < 2 || PG_ARGISNULL(1)) ? 1 : PG_GETARG_INT32(1);
|
end = (PG_NARGS() < 3 || PG_ARGISNULL(2)) ? (start < 0 ? -len : len) : PG_GETARG_INT32(2);
|
|
if ((start > end && start > 0) || (start < end && start < 0))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("invalid parameter"),
|
errdetail("Second parameter is bigger than third.")));
|
|
if (start < 0)
|
{
|
int new_start, new_end;
|
|
new_start = len + start + 1;
|
new_end = len + end + 1;
|
start = new_end;
|
end = new_start;
|
}
|
|
start = start != 0 ? start : 1;
|
end = end < len ? end : len;
|
|
new_len = end - start + 1;
|
new_len = new_len >= 0 ? new_len : 0;
|
|
if (mb_encode)
|
{
|
int max_size;
|
int cur_size;
|
char *p;
|
int j;
|
int fz_size;
|
|
fz_size = VARSIZE_ANY_EXHDR(str);
|
|
if ((max_size = (new_len*pg_database_encoding_max_length())) > fz_size)
|
result = palloc(fz_size + VARHDRSZ);
|
else
|
result = palloc(max_size + VARHDRSZ);
|
data = (char*) VARDATA(result);
|
|
cur_size = 0;
|
p = VARDATA_ANY(str);
|
for (i = end - 1; i>= start - 1; i--)
|
{
|
for (j=0; j<sizes[i]; j++)
|
*data++ = *(p+positions[i]+j);
|
cur_size += sizes[i];
|
}
|
SET_VARSIZE(result, cur_size + VARHDRSZ);
|
|
}
|
else
|
{
|
char *p = VARDATA_ANY(str);
|
result = palloc(new_len + VARHDRSZ);
|
data = (char*) VARDATA(result);
|
SET_VARSIZE(result, new_len + VARHDRSZ);
|
|
for (i = end - 1; i >= start - 1; i--)
|
*data++ = p[i];
|
}
|
|
PG_RETURN_TEXT_P(result);
|
}
|
|
/****************************************************************
|
* PLVchr.swap
|
*
|
* Синтаксис:
|
* FUNCTION swap
|
* (string_in IN VARCHAR2, -- исходная строка
|
* replace_in IN VARCHAR2, -- строка, которой будет заменён кусок исходной строки
|
* start_in IN INTEGER := 1, -- позиция, с которой начинается замена (по умолчанию 1)
|
* oldlen_in IN INTEGER := NULL) -- длина заменяемой части (по умолчанию равна длине строки replace_in)
|
* RETURN VARCHAR2
|
*
|
* Назначение:
|
* Функция заменяет подстроку в исходной строке на заданную строку.
|
*
|
* **Перегрузки:**
|
* Реализована одна C‑функция, которая регистрируется в системе в двух вариантах:
|
* - plvstr_swap(text, text)
|
* → При вызове с двумя аргументами используются значения по умолчанию:
|
* start_in = 1 и oldlen_in = длина строки replace_in.
|
* - plvstr_swap(text, text, integer, integer)
|
* → При вызове с четырьмя аргументами явно передаются start_in и oldlen_in.
|
*
|
****************************************************************/
|
|
Datum
|
plvstr_swap(PG_FUNCTION_ARGS)
|
{
|
text *string_in;
|
text *replace_in;
|
int start_in;
|
int oldlen_in;
|
int v_len;
|
|
/* Если хотя бы один из обязательных параметров равен NULL, возвращаем NULL */
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
PG_RETURN_NULL();
|
string_in = PG_GETARG_TEXT_P(0);
|
replace_in = PG_GETARG_TEXT_P(1);
|
|
/* Проверяем количество переданных аргументов (зависит от того, какая перегрузка вызвана) */
|
if (PG_NARGS() == 2)
|
{
|
/* Если переданы только два аргумента, то:
|
* - start_in по умолчанию = 1 (начинаем с первого символа)
|
* - oldlen_in по умолчанию = длина строки replace_in */
|
start_in = 1;
|
oldlen_in = ora_mb_strlen1(replace_in);
|
}
|
else
|
{
|
/* Если переданы четыре аргумента, то берем значения из параметров */
|
start_in = PG_GETARG_INT32(2);
|
if (PG_ARGISNULL(3))
|
oldlen_in = ora_mb_strlen1(replace_in);
|
else
|
oldlen_in = PG_GETARG_INT32(3);
|
}
|
|
v_len = ora_mb_strlen1(string_in);
|
start_in = start_in > 0 ? start_in : v_len + start_in + 1;
|
|
if (start_in == 0 || start_in > v_len)
|
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
|
else if (start_in == 1)
|
PG_RETURN_TEXT_P(_ora_concat2(
|
replace_in, ora_substr(PointerGetDatum(string_in), oldlen_in + 1, -1)));
|
else
|
PG_RETURN_TEXT_P(_ora_concat3(
|
ora_substr(PointerGetDatum(string_in), 1, start_in - 1),
|
replace_in,
|
ora_substr(PointerGetDatum(string_in), start_in + oldlen_in, -1)));
|
}
|
|
/****************************************************************
|
* PLVchr.betwn
|
*
|
* Find the Substring Between Start and End Locations
|
*
|
* Syntax:
|
* FUNCTION plvstr.betwn (string_in IN VARCHAR2,
|
* start_in IN INTEGER,
|
* end_in IN INTEGER,
|
* inclusive IN BOOLEAN := TRUE)
|
* RETURN VARCHAR2;
|
*
|
* FUNCTION plvstr.betwn (string_in IN VARCHAR2,
|
* start_in IN VARCHAR2,
|
* end_in IN VARCHAR2 := NULL,
|
* startnth_in IN INTEGER := 1,
|
* endnth_in IN INTEGER := 1,
|
* inclusive IN BOOLEAN := TRUE,
|
* gotoend IN BOOLEAN := FALSE)
|
* RETURN VARCHAR2;
|
*
|
* Purpouse:
|
* Call this function to extract a sub-string from a string. This
|
* function is overloaded. You can either provide the start and end
|
* locations or you can provide start and end substrings.
|
*
|
****************************************************************/
|
|
static Datum
|
plvstr_betwn_i(PG_FUNCTION_ARGS)
|
{
|
text *string_in = PG_GETARG_TEXT_P(0);
|
int start_in = PG_GETARG_INT32(1);
|
int end_in = PG_GETARG_INT32(2);
|
bool inclusive = PG_ARGISNULL(3) ? true : PG_GETARG_BOOL(3);
|
|
if ((start_in < 0 && end_in > 0) ||
|
(start_in > 0 && end_in < 0) ||
|
(start_in > end_in))
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("invalid parameter"),
|
errdetail("Wrong positions.")));
|
|
if (start_in < 0) {
|
int v_len = ora_mb_strlen1(string_in);
|
start_in = v_len + start_in + 1;
|
end_in = v_len + start_in + 1;
|
}
|
|
if (!inclusive)
|
{
|
start_in += 1;
|
end_in -= 1;
|
|
if (start_in > end_in)
|
PG_RETURN_TEXT_P(cstring_to_text(""));
|
}
|
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(string_in),
|
start_in,
|
end_in - start_in + 1));
|
}
|
|
Datum
|
plvstr_betwn_i_4(PG_FUNCTION_ARGS)
|
{
|
return plvstr_betwn_i(fcinfo);
|
}
|
|
Datum
|
plvstr_betwn_i_3(PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 4);
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 4,
|
PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0] = fcinfo->args[0];
|
fcinfo2->args[1] = fcinfo->args[1];
|
fcinfo2->args[2] = fcinfo->args[2];
|
fcinfo2->args[3].value = BoolGetDatum(true);
|
fcinfo2->args[3].isnull = false;
|
|
return plvstr_betwn_i(fcinfo2);
|
}
|
|
static Datum
|
plvstr_betwn_c(PG_FUNCTION_ARGS)
|
{
|
text *string_in;
|
text *start_in;
|
text *end_in;
|
int startnth_in;
|
int endnth_in;
|
bool inclusive;
|
bool gotoend;
|
|
int v_start;
|
int v_end;
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) ||
|
PG_ARGISNULL(3) || PG_ARGISNULL(4) ||
|
PG_ARGISNULL(5) || PG_ARGISNULL(6))
|
PG_RETURN_NULL();
|
|
|
string_in = PG_GETARG_TEXT_P(0);
|
start_in = PG_GETARG_TEXT_P(1);
|
end_in = PG_ARGISNULL(2) ? start_in : PG_GETARG_TEXT_P(2);
|
startnth_in = PG_GETARG_INT32(3);
|
endnth_in = PG_GETARG_INT32(4);
|
inclusive = PG_GETARG_BOOL(5);
|
gotoend = PG_GETARG_BOOL(6);
|
|
if (startnth_in == 0)
|
{
|
v_start = 1;
|
v_end = ora_instr(string_in, end_in, 1, endnth_in);
|
}
|
else
|
{
|
v_start = ora_instr(string_in, start_in, 1, startnth_in);
|
v_end = ora_instr(string_in, end_in, v_start + 1, endnth_in);
|
}
|
|
if (v_start == 0)
|
PG_RETURN_NULL();
|
|
if (!inclusive)
|
{
|
if (startnth_in > 0)
|
v_start += ora_mb_strlen1(start_in);
|
|
v_end -= 1;
|
}
|
else
|
v_end += (ora_mb_strlen1(end_in) - 1);
|
|
if (((v_start > v_end) && (v_end > 0)) ||
|
(v_end <= 0 && !gotoend))
|
PG_RETURN_NULL();
|
|
if (v_end <= 0)
|
v_end = ora_mb_strlen1(string_in);
|
|
PG_RETURN_TEXT_P(ora_substr(PointerGetDatum(string_in),
|
v_start,
|
v_end - v_start + 1));
|
}
|
|
Datum
|
plvstr_betwn_c_7(PG_FUNCTION_ARGS)
|
{
|
return plvstr_betwn_c(fcinfo);
|
}
|
|
Datum
|
plvstr_betwn_c_5(PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 7);
|
int i;
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 7,
|
PG_GET_COLLATION(), fcinfo->context, NULL);
|
for (i = 0; i < 5; i++)
|
fcinfo2->args[i] = fcinfo->args[i];
|
fcinfo2->args[5].value = BoolGetDatum(true);
|
fcinfo2->args[5].isnull = false;
|
fcinfo2->args[6].value = BoolGetDatum(false);
|
fcinfo2->args[6].isnull = false;
|
|
return plvstr_betwn_c(fcinfo2);
|
}
|
|
Datum
|
plvstr_betwn_c_3(PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 7);
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 7,
|
PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0] = fcinfo->args[0];
|
fcinfo2->args[1] = fcinfo->args[1];
|
fcinfo2->args[2] = fcinfo->args[2];
|
fcinfo2->args[3].value = Int32GetDatum(1);
|
fcinfo2->args[3].isnull = false;
|
fcinfo2->args[4].value = Int32GetDatum(1);
|
fcinfo2->args[4].isnull = false;
|
fcinfo2->args[5].value = BoolGetDatum(true);
|
fcinfo2->args[5].isnull = false;
|
fcinfo2->args[6].value = BoolGetDatum(false);
|
fcinfo2->args[6].isnull = false;
|
|
return plvstr_betwn_c(fcinfo2);
|
}
|
|
/// @brief Возвращает версию RUDB пакета PLVSTR (Oracle PLVSTR пакет)
|
Datum plvstr_get_version(PG_FUNCTION_ARGS) {
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_PLVSTR_VERSION));
|
}
|
|
/****************************************************************
|
* PLVchr.nth
|
*
|
* Syntax:
|
* FUNCTION plvchr.nth (string_in IN VARCHAR,
|
* nth_in IN INTEGER)
|
* RETURN VARCHAR;
|
*
|
* Purpouse:
|
* Call this function to return the Nth character in a string.
|
*
|
****************************************************************/
|
|
Datum
|
plvchr_nth(PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 3);
|
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 3, PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0] = fcinfo->args[0];
|
fcinfo2->args[1] = fcinfo->args[1];
|
fcinfo2->args[2].value = Int32GetDatum(0);
|
fcinfo2->args[2].isnull = false;
|
|
return plvchr_nth_extract(fcinfo2);
|
}
|
|
Datum
|
plvchr_nth_extract(PG_FUNCTION_ARGS)
|
{
|
int32 extract_profile = PG_ARGISNULL(2) ? 0 : PG_GETARG_INT32(2);
|
const int32 minimum_extract_profile = -112;
|
const int32 supported_extract_profile = 0x03;
|
bool preserve_multibyte_boundary = false;
|
bool normalize_zero_position = false;
|
int32 normalized_extract_profile;
|
|
if (extract_profile < minimum_extract_profile)
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("invalid extract profile")));
|
|
normalized_extract_profile = extract_profile & supported_extract_profile;
|
preserve_multibyte_boundary = (normalized_extract_profile & 0x01) != 0;
|
normalize_zero_position = (normalized_extract_profile & 0x02) != 0;
|
|
if (preserve_multibyte_boundary || normalize_zero_position)
|
{
|
/* Reserved for future character extraction rules. */
|
}
|
|
PG_RETURN_TEXT_P(ora_substr(PG_GETARG_DATUM(0), PG_GETARG_INT32(1), 1));
|
}
|
|
/// @brief Возвращает версию RUDB пакета PLVCHR (Oracle PLVCHR пакет)
|
Datum plvchr_get_version(PG_FUNCTION_ARGS) {
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_PLVCHR_VERSION));
|
}
|
|
// TODO: в будущем хотим расширить таблицу доступных кодировок и перенести её в БД,
|
// а пока так, на массивах
|
#define CHARSET_CNT 6
|
static const char *const charsets[] = {
|
"AL32UTF8",
|
"AL16UTF16",
|
"CL8MSWIN1251",
|
"EE8MSWIN1250",
|
"WE8MSWIN1252",
|
"UTF8",
|
NULL // Завершающий NULL
|
};
|
|
static const int charset_ids[CHARSET_CNT] = {873, //AL32UTF8
|
2000, //AL16UTF16
|
171, //CL8MSWIN1251
|
170, //EE8MSWIN1250
|
178, // WE8MSWIN1252
|
871 // UTF8
|
};
|
|
Datum
|
nls_charset_id(PG_FUNCTION_ARGS) {
|
text *charset_text = NULL;
|
size_t charset_len = 0;
|
char *charset = NULL;
|
int index = 0;
|
int i;
|
const char *str;
|
|
if(PG_ARGISNULL(0)){
|
PG_RETURN_NULL();
|
}
|
charset_text = PG_GETARG_TEXT_PP(0);
|
charset_len = VARSIZE_ANY_EXHDR(charset_text);
|
charset = palloc(charset_len+1);
|
|
memcpy(charset, VARDATA_ANY(charset_text), charset_len);
|
charset[charset_len] = 0;
|
|
// к верхнему регистру
|
for(i = 0; i < charset_len; i++)
|
charset[i] = toupper((unsigned char) charset[i]);
|
// поиск идентификатора
|
str = charsets[0];
|
while(str != NULL) {
|
if(strcmp(str, charset) == 0){
|
pfree(charset);
|
PG_RETURN_INT32(charset_ids[index]);
|
}
|
++index;
|
str = charsets[index];
|
}
|
|
pfree(charset);
|
|
PG_RETURN_NULL();
|
}
|
|
Datum
|
nls_charset_name(PG_FUNCTION_ARGS) {
|
int32 id = 0;
|
int index;
|
|
if(PG_ARGISNULL(0)) {
|
PG_RETURN_NULL();
|
}
|
id = PG_GETARG_INT32(0);
|
// поиск имени кодировки
|
for(index = 0; index < CHARSET_CNT; index++) {
|
if(charset_ids[index] == id) {
|
int32 name_len = strlen(charsets[index]);
|
int32 len = name_len + VARHDRSZ;
|
text *result = (text*)palloc(len);
|
SET_VARSIZE(result, len);
|
// svacer_review: -NONTERMINATED_STRING.STYLE // varlena not use null-terminated string data
|
memcpy(VARDATA(result), charsets[index], name_len);
|
|
PG_RETURN_TEXT_P(result);
|
}
|
}
|
|
PG_RETURN_NULL();
|
}
|