更改 PostgreSQL C 语言函数中的字符编码

Change the character encode in PostgreSQL C language function

我在 windows 服务器上使用 PostgreSQL 9.5 64 位版本。 数据库的字符编码设置为UTF8.

我想创建一个处理多字节字符串的函数。 (例如清洁、更换等)

我从其他系统复制了C语言操作字符的逻辑, 逻辑假设字符编码为sjis。

我不想改变C语言逻辑,所以想在Postgresql的C语言函数中把UTF8转成sjis。 就像 convert_to 函数一样。 (不过,由于convert_to函数returnsbytea类型,所以想用TEXT类型获取。)

请告诉我如何用 C 语言将 UTF 8 转换为 sjis。

创建函数脚本:

CREATE FUNCTION CLEANSING_STRING(character varying)
RETURNS character varying AS
'$libdir/MyFunc/CLEANSING_STRING.dll', 'CLEANSING_STRING'
LANGUAGE c VOLATILE STRICT;

C 来源:

#include <stdio.h>
#include <string.h>
#include <postgres.h>
#include <port.h>
#include <fmgr.h>
#include <stdlib.h>
#include <builtins.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

extern PGDLLEXPORT Datum CLEANSING_STRING(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(CLEANSING_STRING);
Datum CLEANSING_STRING(PG_FUNCTION_ARGS)
{

    // Get Arg
    text *arg1 = (text *)PG_GETARG_TEXT_P(0);

    // Text to Char[]
    char *arg;
    arg = text_to_cstring(arg1);

    // UTF8 to Sjis
    //Char *sjisChar[] = foo(arg);  // something like that..

    // Copied from other system.(Assumes that the character code is sjis.)
    cleansingString(sjisChar);
    replaceStrimg(sjisChar);

    // Sjis to UTF8
    //arg = bar(sjisChar);  // something like that..

    //Char[] to Text and Return
    PG_RETURN_TEXT_P(cstring_to_text(arg));
}

按照问题评论教我的方式成功了。

#include <mb/pg_wchar.h> //Add to include.

...

Datum CLEANSING_STRING(PG_FUNCTION_ARGS)
{

    // Get Arg
    text *arg1 = (text *)PG_GETARG_TEXT_P(0);

    // Text to Char[]
    char *arg;
    arg = text_to_cstring(arg1);

    // UTF8 to Sjis
    Char *sjisChar[] = pg_server_to_any(arg, strlen(arg), PG_SJIS);

    // Copied from other system.(Assumes that the character code is sjis.)
    cleansingString(sjisChar);
    replaceStrimg(sjisChar);

    // Sjis to UTF8
    arg =  pg_any_to_server(sjisChar, strlen(sjisChar), PG_SJIS); //It converts from SJIS to server (UTF 8), the third argument sets the encoding of the conversion source.

    //Char[] to Text and Return
    PG_RETURN_TEXT_P(cstring_to_text(arg));
}