/*------------------------------------------------------------------------- * * varchar2.c * Oracle-compatible VARCHAR2 and NVARCHAR2 types. * *------------------------------------------------------------------------- */ #include "postgres.h" #include "varatt.h" #include "catalog/pg_type.h" #include "fmgr.h" #include "libpq/pqformat.h" #include "mb/pg_wchar.h" #include "utils/builtins.h" #include "utils/varlena.h" PG_FUNCTION_INFO_V1(varchar2in); PG_FUNCTION_INFO_V1(varchar2out); PG_FUNCTION_INFO_V1(varchar2recv); PG_FUNCTION_INFO_V1(varchar2send); PG_FUNCTION_INFO_V1(nvarchar2in); PG_FUNCTION_INFO_V1(nvarchar2out); PG_FUNCTION_INFO_V1(nvarchar2recv); PG_FUNCTION_INFO_V1(nvarchar2send); PG_FUNCTION_INFO_V1(nvarchar2); PG_FUNCTION_INFO_V1(ora_varchar_transform); extern Datum varchar_support(PG_FUNCTION_ARGS); static VarChar * varchar2_input(const char *s, size_t len, int32 atttypmod) { VarChar *result; size_t maxlen; maxlen = atttypmod - VARHDRSZ; if (atttypmod >= (int32) VARHDRSZ && len > maxlen) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input value length is %zu; too long for type varchar2(%zu)", len, maxlen))); result = (VarChar *) cstring_to_text_with_len(s, (int) len); return result; } static VarChar * nvarchar2_input(const char *s, size_t len, int32 atttypmod) { VarChar *result; size_t maxlen; maxlen = atttypmod - VARHDRSZ; if (atttypmod >= (int32) VARHDRSZ && len > maxlen) { size_t charlen = pg_mbstrlen_with_len(s, len); if (charlen > maxlen) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input value length is %zu; too long for type nvarchar2(%zu)", charlen, maxlen))); } result = (VarChar *) cstring_to_text_with_len(s, (int) len); return result; } Datum varchar2in(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); int32 atttypmod = PG_GETARG_INT32(2); VarChar *result; result = varchar2_input(s, strlen(s), atttypmod); PG_RETURN_VARCHAR_P(result); } Datum varchar2out(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); PG_RETURN_CSTRING(TextDatumGetCString(txt)); } Datum varchar2recv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); int32 atttypmod = PG_GETARG_INT32(2); VarChar *result; char *str; int nbytes; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); result = varchar2_input(str, nbytes, atttypmod); pfree(str); PG_RETURN_VARCHAR_P(result); } Datum varchar2send(PG_FUNCTION_ARGS) { return textsend(fcinfo); } Datum nvarchar2in(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); int32 atttypmod = PG_GETARG_INT32(2); VarChar *result; result = nvarchar2_input(s, strlen(s), atttypmod); PG_RETURN_VARCHAR_P(result); } Datum nvarchar2out(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); PG_RETURN_CSTRING(TextDatumGetCString(txt)); } Datum nvarchar2recv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); int32 atttypmod = PG_GETARG_INT32(2); VarChar *result; char *str; int nbytes; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); result = nvarchar2_input(str, nbytes, atttypmod); pfree(str); PG_RETURN_VARCHAR_P(result); } Datum nvarchar2send(PG_FUNCTION_ARGS) { return textsend(fcinfo); } Datum nvarchar2(PG_FUNCTION_ARGS) { VarChar *source = PG_GETARG_VARCHAR_PP(0); int32 typmod = PG_GETARG_INT32(1); bool isExplicit = PG_GETARG_BOOL(2); int32 len; int32 maxlen; int maxmblen; char *s_data; len = VARSIZE_ANY_EXHDR(source); s_data = VARDATA_ANY(source); maxlen = typmod - VARHDRSZ; if (maxlen < 0 || len <= maxlen) PG_RETURN_VARCHAR_P(source); maxmblen = pg_mbcharcliplen(s_data, len, maxlen); if (!isExplicit && len > maxmblen) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input value too long for type nvarchar2(%d)", maxlen))); PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text_with_len(s_data, maxmblen)); } Datum ora_varchar_transform(PG_FUNCTION_ARGS) { return varchar_support(fcinfo); }