UDF を作成するのは初めてなので、UDF に渡される同じ引数を返す単純な UDF を作成しようとしました。
以下のようなコード:
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <mysql.h>
#include <ctype.h>
#include <my_global.h>
#include <my_sys.h>
using namespace std;
extern "C" my_bool get_arg_init(UDF_INIT *initid, UDF_ARGS *args,
char *message)
{
if ( ( args->arg_count != 1 ) || ( args->arg_type[0] != STRING_RESULT ) )
{
strcpy( message, "Wrong argument type." );
return 1;
}
return 0;
}
extern "C" void get_arg_deinit(UDF_INIT *initid)
{
//nothing to free here
}
extern "C" char *get_arg(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error)
{
std::string str = args->args[0]; // get the first argument passed
memcpy(result, str.c_str(), str.size()); // copy argument value into result buffer
*length = str.size(); // set length
return result;//return the same argument
}
私のテーブルは次のようにデータを持っています;
SELECT c_name FROM tbl;
これにより、データが次のように返されます。
# c_name
amogh bharat shah
viraj
UDF を使用してクエリを実行すると:
SELECT get_arg(c_name) FROM tbl;
これは以下を返します:
# get_arg(c_name)
amogh bharat shah
viraj bharat shah
2行目の最初の5文字が実際の行データに置き換えられている間、文字列の他の部分は最初の行からのゴミです。
なぜこれが起こるのですか?文字列の重複を避けるために、関数で何を変更する必要がありますか?