Reputation: 189
I am trying to run MySQL UDF written in C++. It compiles fine and generates the correct output, but it generates a lot of junk after the output. I would like to know the reason for this junk and how I can resolve this problem? I have attached my code and a screenshot of the output.
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "mysql-connector-c-6.1.9-linux-glibc2.5-x86_64/include/mysql.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/mysql_connection.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/sqlstring.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/datatype.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset_metadata.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/exception.h"
using namespace std;
extern "C" {
char *hello_world (UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long length,char *is_null, char *error);
my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message);
void hello_world_deinit (UDF_INIT *initid);
//template <typename T> T adder (UDF_INIT *initid, UDF_eARGS *args,string result, unsigned long length,string is_null, string error,T v);
}
char *hello_world (UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long length, char *is_null, char *error)
{
string res;
res = args->args[0];
res.append(" hello");
char *c = new char[res.size()];
strcpy(c, res.c_str());
return c;
}
my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return 0;
//cout<<"success"<<endl;
}
void hello_world_deinit (UDF_INIT *initid)
{
return;
}
Upvotes: 2
Views: 969
Reputation: 820
The signature of the hello_world
function is wrong. The fourth parameter should be
unsigned long *length
The value pointed to by that pointer has to be set to the length of the returned string.
char *c = new char[res.size() + 1];
strcpy(c, res.c_str());
*length = res.size();
return c;
Upvotes: 4